widget_test.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 shows Steam instead of a PID percentage', (
  82. tester,
  83. ) async {
  84. final service = DashboardService();
  85. await tester.pump();
  86. service.showState(
  87. MachineState(
  88. connectionStatus: 'Connected',
  89. temperature: 120,
  90. lastTelemetryAt: DateTime(2026, 10, 6),
  91. telemetryStale: false,
  92. isHeating: true,
  93. isSteaming: true,
  94. ),
  95. );
  96. await tester.pumpWidget(
  97. ProviderScope(
  98. overrides: [bluetoothServiceProvider.overrideWith((ref) => service)],
  99. child: const MyApp(),
  100. ),
  101. );
  102. await tester.pump(const Duration(milliseconds: 100));
  103. expect(find.text('Steam'), findsOneWidget);
  104. expect(find.text('0%'), findsNothing);
  105. expect(
  106. tester
  107. .widget<LinearProgressIndicator>(find.byType(LinearProgressIndicator))
  108. .value,
  109. isNull,
  110. );
  111. await tester.pumpWidget(const SizedBox());
  112. });
  113. testWidgets('dashboard diagnostics button opens local logs', (tester) async {
  114. await tester.pumpWidget(const ProviderScope(child: MyApp()));
  115. await tester.pumpAndSettle();
  116. await tester.tap(find.byTooltip('Diagnostics'));
  117. await tester.pumpAndSettle();
  118. expect(find.text('Diagnostics'), findsOneWidget);
  119. expect(find.text('No diagnostic entries yet'), findsOneWidget);
  120. });
  121. testWidgets('dashboard connection control clears the bottom navigation bar', (
  122. tester,
  123. ) async {
  124. tester.view.physicalSize = const Size(800, 900);
  125. tester.view.devicePixelRatio = 1;
  126. tester.view.padding = const FakeViewPadding(bottom: 48);
  127. addTearDown(tester.view.resetPhysicalSize);
  128. addTearDown(tester.view.resetDevicePixelRatio);
  129. addTearDown(tester.view.resetPadding);
  130. await tester.pumpWidget(const ProviderScope(child: MyApp()));
  131. await tester.pumpAndSettle();
  132. await tester.drag(
  133. find.byType(SingleChildScrollView),
  134. const Offset(0, -2000),
  135. );
  136. await tester.pumpAndSettle();
  137. final button = find.ancestor(
  138. of: find.text('Connect to meCoffee'),
  139. matching: find.byWidgetPredicate((widget) => widget is ElevatedButton),
  140. );
  141. final safeBottom =
  142. tester.view.physicalSize.height - tester.view.padding.bottom;
  143. expect(tester.getRect(button).bottom, lessThanOrEqualTo(safeBottom - 16));
  144. expect(button.hitTestable(), findsOneWidget);
  145. expect(tester.takeException(), isNull);
  146. });
  147. }