import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_blue_plus_platform_interface/flutter_blue_plus_platform_interface.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:mebarista/main.dart'; import 'package:mebarista/services/bluetooth_service.dart'; import '../support/fake_bluetooth_platform.dart'; void main() { final platform = FakeBluetoothPlatform(); setUpAll(() { FlutterBluePlusPlatform.instance = platform; }); setUp(platform.reset); for (final temperature in [39.42, 130.0]) { testWidgets('Settings opens with brew temperature $temperature', ( tester, ) async { SharedPreferences.setMockInitialValues({'brewTemperature': temperature}); final container = (await tester.runAsync(() async { final container = ProviderContainer(); await container .read(bluetoothServiceProvider.notifier) .connectToDevice(platform.newDevice()) .timeout(const Duration(seconds: 10)); return container; }))!; addTearDown(container.dispose); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const MyApp()), ); await tester.pumpAndSettle(); await tester.tap(find.byIcon(Icons.settings)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(find.text('Machine Settings'), findsOneWidget); final slider = tester.widget(find.byType(Slider).first); expect(slider.value, temperature.clamp(50.0, 125.0)); expect( container.read(bluetoothServiceProvider).brewTemperature, temperature, ); expect(find.text('${temperature.toStringAsFixed(1)}°C'), findsOneWidget); final prefs = await SharedPreferences.getInstance(); expect(prefs.getDouble('brewTemperature'), temperature); await tester.runAsync(() async { slider.onChangeEnd!(101.1); await Future.delayed(Duration.zero); }); await tester.pumpAndSettle(); expect(container.read(bluetoothServiceProvider).brewTemperature, 101.1); expect(container.read(bluetoothServiceProvider).targetTemperature, 101.1); expect(prefs.getDouble('brewTemperature'), 101.1); expect(tester.takeException(), isNull); }); } testWidgets( 'disconnected setting edits show an error and keep the old value', (tester) async { SharedPreferences.setMockInitialValues({}); final container = ProviderContainer(); addTearDown(container.dispose); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const MyApp()), ); await tester.pumpAndSettle(); await tester.tap(find.byIcon(Icons.settings)); await tester.pumpAndSettle(); tester.widget(find.byType(Slider).first).onChangeEnd!(95); await tester.pumpAndSettle(); expect( find.text('Settings not sent: connect to meCoffee first.'), findsOneWidget, ); expect(container.read(bluetoothServiceProvider).brewTemperature, 101); }, ); for (final temperature in [100.0, 150.0]) { testWidgets('Settings opens with steam temperature $temperature', ( tester, ) async { SharedPreferences.setMockInitialValues({'steamTemperature': temperature}); final container = ProviderContainer(); addTearDown(container.dispose); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const MyApp()), ); await tester.pumpAndSettle(); await tester.tap(find.byIcon(Icons.settings)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); final slider = tester.widget(find.byType(Slider).at(1)); expect(slider.value, temperature.clamp(110.0, 150.0)); expect( container.read(bluetoothServiceProvider).steamTemperature, temperature, ); expect(find.text('${temperature.toStringAsFixed(1)}°C'), findsOneWidget); }); } }