main
1
2import 'dart:async';
3
4import 'package:bloc_test/bloc_test.dart';
5import 'package:blood_pressure_app/features/bluetooth/logic/bluetooth_cubit.dart';
6import 'package:blood_pressure_app/features/bluetooth/ui/closed_bluetooth_input.dart';
7import 'package:flutter/material.dart';
8import 'package:blood_pressure_app/l10n/app_localizations.dart';
9import 'package:flutter_test/flutter_test.dart';
10
11import '../../../util.dart';
12
13class MockBluetoothCubit extends MockCubit<BluetoothState>
14 implements BluetoothCubit {
15 @override
16 Future<bool> enableBluetooth() async => true;
17 @override
18 Future<void> forceRefresh() async {}
19}
20
21void main() {
22 testWidgets('should show states correctly', (WidgetTester tester) async {
23 final states = StreamController<BluetoothState>.broadcast();
24
25 final cubit = MockBluetoothCubit();
26 whenListen(cubit, states.stream, initialState: BluetoothStateInitial());
27
28 int startCount = 0;
29 await tester.pumpWidget(materialApp(ClosedBluetoothInput(
30 bluetoothCubit: cubit,
31 onStarted: () {
32 startCount++;
33 }
34 )));
35 await tester.pumpAndSettle();
36
37 expect(find.byType(SizedBox), findsOneWidget);
38 expect(find.byType(ListTile), findsNothing);
39
40 states.sink.add(BluetoothStateUnfeasible());
41 await tester.pump();
42 expect(find.byType(SizedBox), findsOneWidget);
43 expect(find.byType(ListTile), findsNothing);
44
45 states.sink.add(BluetoothStateUnauthorized());
46 await tester.pump();
47 final localizations = await AppLocalizations.delegate.load(const Locale('en'));
48 expect(find.text(localizations.errBleNoPerms), findsOneWidget);
49
50 await tester.tap(find.byType(ClosedBluetoothInput));
51 expect(startCount, 0);
52
53 states.sink.add(BluetoothStateDisabled());
54 await tester.pump();
55 expect(find.text(localizations.bluetoothDisabled), findsOneWidget);
56
57 await tester.tap(find.byType(ClosedBluetoothInput));
58 expect(startCount, 0);
59
60 states.sink.add(BluetoothStateReady());
61 await tester.pump();
62 expect(find.text(localizations.bluetoothInput), findsOneWidget);
63
64 await tester.tap(find.byType(ClosedBluetoothInput));
65 expect(startCount, 1);
66 });
67}