widget_test.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:flutter_test/flutter_test.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'package:mebarista/main.dart';
  6. import 'package:mebarista/models/machine_state.dart';
  7. import 'package:mebarista/services/bluetooth_service.dart';
  8. class DashboardService extends MeCoffeeService {
  9. void showState(MachineState value) {
  10. state = value;
  11. }
  12. }
  13. void main() {
  14. setUp(() {
  15. SharedPreferences.setMockInitialValues({'brewTemperature': 99.5});
  16. });
  17. testWidgets(
  18. 'dashboard displays the saved brew target and disconnected state',
  19. (tester) async {
  20. await tester.pumpWidget(const ProviderScope(child: MyApp()));
  21. await tester.pumpAndSettle();
  22. expect(find.text('meBarista Dashboard'), findsOneWidget);
  23. expect(find.text('Boiler Temperature'), findsOneWidget);
  24. expect(find.text('BREW TARGET'), findsOneWidget);
  25. expect(find.text('99.5°C'), findsOneWidget);
  26. expect(find.byIcon(Icons.bluetooth_searching), findsOneWidget);
  27. expect(find.text('No data yet'), findsOneWidget);
  28. expect(tester.takeException(), isNull);
  29. },
  30. );
  31. testWidgets('dashboard settings button opens Machine Settings', (
  32. tester,
  33. ) async {
  34. await tester.pumpWidget(const ProviderScope(child: MyApp()));
  35. await tester.pumpAndSettle();
  36. await tester.tap(find.byIcon(Icons.settings));
  37. await tester.pumpAndSettle();
  38. expect(find.text('Machine Settings'), findsOneWidget);
  39. expect(find.text('Brew Temperature'), findsOneWidget);
  40. expect(find.text('99.5°C'), findsOneWidget);
  41. expect(tester.takeException(), isNull);
  42. });
  43. testWidgets('dashboard distinguishes stale readings from live temperature', (
  44. tester,
  45. ) async {
  46. final service = DashboardService();
  47. await tester.pump();
  48. service.showState(
  49. MachineState(
  50. connectionStatus: 'Connected',
  51. temperature: 98.5,
  52. lastTelemetryAt: DateTime(2026, 10, 6),
  53. telemetryStale: true,
  54. heatingPower: 1,
  55. isHeating: true,
  56. isBrewing: true,
  57. shotTimer: 12,
  58. ),
  59. );
  60. await tester.pumpWidget(
  61. ProviderScope(
  62. overrides: [bluetoothServiceProvider.overrideWith((ref) => service)],
  63. child: const MyApp(),
  64. ),
  65. );
  66. await tester.pumpAndSettle();
  67. expect(find.textContaining('Telemetry stale'), findsOneWidget);
  68. expect(find.text('98.5°C'), findsOneWidget);
  69. expect(find.text('Unavailable'), findsOneWidget);
  70. expect(
  71. tester
  72. .widget<LinearProgressIndicator>(find.byType(LinearProgressIndicator))
  73. .value,
  74. 0,
  75. );
  76. service.showState(service.state.copyWith(telemetryStale: false));
  77. await tester.pumpAndSettle();
  78. expect(find.textContaining('Telemetry stale'), findsNothing);
  79. await tester.pumpWidget(const SizedBox());
  80. });
  81. testWidgets('dashboard diagnostics button opens local logs', (tester) async {
  82. await tester.pumpWidget(const ProviderScope(child: MyApp()));
  83. await tester.pumpAndSettle();
  84. await tester.tap(find.byTooltip('Diagnostics'));
  85. await tester.pumpAndSettle();
  86. expect(find.text('Diagnostics'), findsOneWidget);
  87. expect(find.text('No diagnostic entries yet'), findsOneWidget);
  88. });
  89. testWidgets('dashboard connection control clears the bottom navigation bar', (
  90. tester,
  91. ) async {
  92. tester.view.physicalSize = const Size(800, 900);
  93. tester.view.devicePixelRatio = 1;
  94. tester.view.padding = const FakeViewPadding(bottom: 48);
  95. addTearDown(tester.view.resetPhysicalSize);
  96. addTearDown(tester.view.resetDevicePixelRatio);
  97. addTearDown(tester.view.resetPadding);
  98. await tester.pumpWidget(const ProviderScope(child: MyApp()));
  99. await tester.pumpAndSettle();
  100. await tester.drag(
  101. find.byType(SingleChildScrollView),
  102. const Offset(0, -2000),
  103. );
  104. await tester.pumpAndSettle();
  105. final button = find.ancestor(
  106. of: find.text('Connect to meCoffee'),
  107. matching: find.byWidgetPredicate((widget) => widget is ElevatedButton),
  108. );
  109. final safeBottom =
  110. tester.view.physicalSize.height - tester.view.padding.bottom;
  111. expect(tester.getRect(button).bottom, lessThanOrEqualTo(safeBottom - 16));
  112. expect(button.hitTestable(), findsOneWidget);
  113. expect(tester.takeException(), isNull);
  114. });
  115. }