import 'dart:math'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:material_ui/material_ui.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:wake_alarm_sync/screens/ring_screen.dart'; import 'package:wake_alarm_sync/services/alarm_scheduler.dart'; import 'package:wake_alarm_sync/services/challenge.dart'; import 'package:wake_alarm_sync/services/dismiss_gate.dart'; import 'package:wake_alarm_sync/services/room_proof_settings.dart'; import 'package:wake_alarm_sync/ui/cues.dart'; const String _tag1 = '04a2b3c4'; const String _tag2 = '04ffee11'; /// The ring's status sentence, now spoken only (StatusCue.semanticLabel). String statusOf(WidgetTester tester) => tester.widget(find.byType(StatusCue)).semanticLabel; void main() { TestWidgetsFlutterBinding.ensureInitialized(); late List calls; var steps = 0; var hasNfc = true; var elapsed = Duration.zero; /// Installs the platform mock. Call after changing [steps] / [hasNfc]. void mockChannel() { TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .setMockMethodCallHandler( const MethodChannel(AlarmScheduler.channelName), (call) async { calls.add(call.method); if (call.method == 'stepCount') return steps; if (call.method == 'hasNfc') return hasNfc; return null; }, ); } /// Lets the periodic step poll run once. Future pollSteps(WidgetTester tester) async { await tester.pump(kStepPollInterval); await tester.pump(); } setUp(() { calls = []; steps = 0; hasNfc = true; elapsed = Duration.zero; SharedPreferences.setMockInitialValues({}); mockChannel(); }); /// A ring screen with both tags registered and a controllable clock. Future pumpRing(WidgetTester tester) async { await RoomProofSettings.saveNfcTagAt(0, _tag1); await RoomProofSettings.saveNfcTagAt(1, _tag2); await tester.pumpWidget( MaterialApp( home: RingScreen( generator: ChallengeGenerator(random: Random(1)), clock: () => elapsed, ), ), ); await tester.pump(); return ChallengeGenerator(random: Random(1)); } testWidgets('it is not offered before the timer elapses', (tester) async { await pumpRing(tester); await tester.pump(DismissGate.bypassAfter - const Duration(seconds: 1)); expect(find.byKey(const Key('ring-bypass')), findsNothing); expect(find.byKey(const ValueKey('ring-row-tag1')), findsOneWidget); }); testWidgets('after the timer it skips the tags but still needs the walk', ( tester, ) async { await pumpRing(tester); await tester.pump(DismissGate.bypassAfter); await tester.pump(); expect(find.byKey(const Key('ring-bypass')), findsOneWidget); // A hardware bypass must not borrow the away wording. expect(find.byKey(const Key('ring-bypass')), findsOneWidget); expect(find.byKey(const ValueKey('ring-row-tag1')), findsNothing); expect( find.byKey(const ValueKey('ring-row-steps')), findsOneWidget, ); expect(find.byKey(const Key('ring-submit')), findsNothing); steps = 500; elapsed = const Duration(minutes: 6); await pollSteps(tester); expect(find.text('1/6'), findsOneWidget); }); testWidgets('under bypass the walk still needs elapsed time', (tester) async { // The anti-shake floor lives here now and only here: with no tag to // prove the walk, 25 "steps" in five seconds is a phone shaken in bed. // Only an immediate bypass can hit it -- the five-minute one has already // outlasted the floor by the time it fires. hasNfc = false; mockChannel(); await pumpRing(tester); steps = 500; elapsed = const Duration(seconds: 5); await pollSteps(tester); expect(statusOf(tester), contains('Keep moving')); expect(find.text('Submit'), findsNothing); }); testWidgets('an unregistered second tag bypasses immediately', ( tester, ) async { await RoomProofSettings.saveNfcTagAt(0, _tag1); await tester.pumpWidget( MaterialApp(home: RingScreen(clock: () => elapsed)), ); await tester.pump(); expect(find.byKey(const Key('ring-bypass')), findsOneWidget); expect(find.byIcon(Icons.block), findsOneWidget); }); testWidgets('no NFC hardware bypasses immediately', (tester) async { hasNfc = false; mockChannel(); await pumpRing(tester); expect(statusOf(tester), contains('NFC is off')); expect(find.text('Tap tag 1'), findsNothing); }); } /// Local helper so the test need not import dart:async for one call. void unawaited(Future future) {}