import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:lumen/domain/book.dart'; import 'package:lumen/domain/game.dart'; import 'package:lumen/main.dart'; import 'package:lumen/services/narrator.dart'; import 'package:lumen/services/progress.dart'; class _Voice extends Narrator { _Voice(super.tracks); final List<(String, double)> starts = []; bool active = false; @override bool get playing => active; @override Future toggle(String id, double speed) async { cancelAutoplay(); page = id; active = !active; starts.add((id, speed)); notifyListeners(); } @override Future pause() async { active = false; await super.pause(); } @override Future stop() async { active = false; await super.stop(); } } void main() { testWidgets('autoplay waits exactly three seconds and starts only once', ( tester, ) async { final voice = _Voice({'first': {}, 'second': {}}); voice.scheduleAutoplay('first', 1.15); await tester.pump(const Duration(milliseconds: 2999)); expect(voice.starts, isEmpty); await tester.pump(const Duration(milliseconds: 1)); expect(voice.starts, [('first', 1.15)]); await tester.pump(const Duration(seconds: 10)); expect(voice.starts.length, 1); await voice.stop(); voice.scheduleAutoplay('first', 1); await tester.pump(const Duration(seconds: 2)); voice.scheduleAutoplay('second', .85); await tester.pump(const Duration(seconds: 2)); expect(voice.starts.length, 1); await tester.pump(const Duration(seconds: 1)); expect(voice.starts.last, ('second', .85)); voice.dispose(); }); testWidgets( 'manual playback, pause, stop and disposal cancel pending autoplay', (tester) async { final voice = _Voice({'page': {}}); voice.scheduleAutoplay('page', 1); await tester.pump(const Duration(seconds: 1)); await voice.toggle('page', 1); await tester.pump(const Duration(seconds: 4)); expect(voice.starts.length, 1); await voice.stop(); voice.scheduleAutoplay('page', 1); await voice.pause(); await tester.pump(const Duration(seconds: 4)); expect(voice.starts.length, 1); voice.scheduleAutoplay('page', 1); await voice.stop(); await tester.pump(const Duration(seconds: 4)); expect(voice.starts.length, 1); voice.scheduleAutoplay('page', 1); voice.dispose(); await tester.pump(const Duration(seconds: 4)); expect(voice.starts.length, 1); }, ); testWidgets( 'page changes cancel old readings and folded notes wait until opened', (tester) async { final book = (await tester.runAsync(Book.load))!; final voice = _Voice(book.tracks); final g = Game(book, Progress.memory({'story_access_level': 11})); g.showStory(0); await tester.pumpWidget( MaterialApp( home: Notebook(game: g, adsEnabled: false, narrator: voice), ), ); await tester.pump(const Duration(seconds: 2)); g.showStory(1); await tester.pump(const Duration(seconds: 2)); expect(voice.starts, isEmpty); await tester.pump(const Duration(seconds: 1)); expect(voice.starts.single.$1, 'cottage.window'); g.showStory(2); await tester.pump(const Duration(seconds: 4)); expect(voice.starts.length, 1); g.showStory(4); await tester.pump(const Duration(seconds: 4)); expect(voice.starts.length, 1); await tester.ensureVisible(find.text('Unfold Gran’s note')); await tester.tap(find.text('Unfold Gran’s note')); await tester.pump(const Duration(seconds: 3)); expect(voice.starts.last.$1, 'cottage.folded.note'); g.briefing(4); await tester.pump(const Duration(seconds: 3)); expect(voice.starts.last.$1, 'campaign.4.before'); g.reveal(4, readAhead: true); await tester.pump(const Duration(seconds: 3)); expect(voice.starts.last.$1, 'campaign.4.after'); final count = voice.starts.length; g.home(); await tester.pump(const Duration(seconds: 4)); expect(voice.starts.length, count); await tester.pumpWidget(const SizedBox()); }, ); testWidgets('backgrounding cancels a delayed reading', (tester) async { final book = (await tester.runAsync(Book.load))!; final voice = _Voice(book.tracks); final g = Game(book, Progress.memory())..showStory(0); await tester.pumpWidget( MaterialApp( home: Notebook(game: g, adsEnabled: false, narrator: voice), ), ); await tester.pump(const Duration(seconds: 2)); tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); await tester.pump(const Duration(seconds: 4)); expect(voice.starts, isEmpty); tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); await tester.pumpWidget(const SizedBox()); }); }