import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; import 'package:lumen/domain/book.dart'; import 'package:lumen/domain/game.dart'; import 'package:lumen/domain/library.dart'; import 'package:lumen/main.dart'; import 'package:lumen/services/narrator.dart'; import 'package:lumen/services/progress.dart'; void main() { final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets( 'complete Halloween story, twelve discoveries and all native recordings', (tester) async { expect(const bool.fromEnvironment('QA_TEST'), true); Future orient(DeviceOrientation orientation) async { await SystemChrome.setPreferredOrientations([orientation]); final portrait = orientation == DeviceOrientation.portraitUp; for (var i = 0; i < 30; i++) { await tester.pump(const Duration(milliseconds: 100)); final size = tester.view.physicalSize; if ((size.height > size.width) == portrait) { await tester.pumpAndSettle(); return; } } fail('Device did not enter $orientation'); } await orient(DeviceOrientation.portraitUp); final library = await StoryLibrary.load(); final mara = await library.open('mara-island'); final store = Progress.memory({'language': 'en'}); final g = Game(mara, store, library: library); final voice = Narrator(mara.tracks); await tester.pumpWidget( MaterialApp( debugShowCheckedModeBanner: false, theme: ThemeData(fontFamily: 'MaraHand'), home: Notebook(game: g, adsEnabled: false, narrator: voice), ), ); await tester.pumpAndSettle(); await binding.convertFlutterSurfaceToImage(); Future tap(String text) async { final f = find.text(text); await tester.ensureVisible(f); await tester.pumpAndSettle(); await tester.tap(f); await tester.pumpAndSettle(); } Future shot(String name) async { await tester.pumpAndSettle(); await binding.takeScreenshot(name); } await tap('Story library'); await tap('The Midnight Invitation'); expect(g.book.id, 'midnight-invitation'); await shot('halloween-00-cover'); await tap('Open The Midnight Invitation · 0 / 12'); expect(g.storyPage.id, 'midnight.opening'); await voice.stop(); await tester.pumpAndSettle(); await shot('halloween-01-opening'); // The actual on-page controls must play, pause and resume a native WAV. await tester.ensureVisible(find.text('Play this reading')); await tester.tap(find.text('Play this reading')); await Future.delayed(const Duration(seconds: 2)); await tester.pumpAndSettle(); expect(voice.error, isEmpty); expect(voice.playing, true); await tester.ensureVisible(find.text('Pause recording')); await tester.tap(find.text('Pause recording')); await tester.pumpAndSettle(); final paused = voice.position; expect(paused.inMilliseconds, greaterThan(500)); await tester.tap(find.text('Resume recording')); await Future.delayed(const Duration(seconds: 1)); await voice.pause(); expect(voice.position, greaterThan(paused)); await voice.stop(); // Decode every new bundled recording on the tablet, not just its metadata. for (final id in g.book.tracks.keys) { await voice.toggle(id, 1); await Future.delayed(const Duration(milliseconds: 350)); await voice.pause(); expect(voice.error, isEmpty, reason: id); expect(voice.position.inMilliseconds, greaterThan(100), reason: id); await voice.stop(); } final book = g.book; for (var index = 0; index < book.levels.length; index++) { g.showStory(book.pageFor(index, StoryPageType.entry)); await tester.pumpAndSettle(); await tap('Next leaf →'); expect(g.storyPage.type, StoryPageType.puzzle); final p = g.puzzle!; expect(find.text('Undo a mark'), findsNothing); expect(find.image(AssetImage(book.paperAsset!)), findsOneWidget); if (index == 2) { await tester.ensureVisible(find.byKey(const ValueKey('area-1'))); await tester.tap(find.byKey(const ValueKey('area-1'))); await tester.pumpAndSettle(); expect(g.selectedArea, 1); await shot('halloween-region-selected-landscape'); await orient(DeviceOrientation.portraitUp); await tester.ensureVisible( find.byKey(const ValueKey('selected-region-outline')), ); await shot('halloween-region-selected-portrait'); await orient(DeviceOrientation.landscapeLeft); } if (index == 0) { final cell = p.given.indexOf(false); await tester.ensureVisible(find.byKey(ValueKey('cell-$cell'))); await tester.tap(find.byKey(ValueKey('cell-$cell'))); await tester.pumpAndSettle(); final marks = List.of(g.cells); await tap('Next leaf →'); await tap('Read ahead'); expect(g.recoveredCount, 0); expect( find.image(AssetImage(book.levels[0].illustrationAsset!)), findsNothing, ); await tap('← Previous leaf'); expect(g.cells, marks); await orient(DeviceOrientation.landscapeLeft); await shot('halloween-02-puzzle-landscape'); } for (var c = 0; c < g.cells.length; c++) { while (g.cells[c] != (p.solution[c] == 1 ? 1 : 2)) { g.mark(c); } } await tester.pumpAndSettle(); expect(g.storyPage.type, StoryPageType.memory); expect(g.recoveredCount, index + 1); expect(g.solved, index + 1); expect( find.image(AssetImage(book.levels[index].illustrationAsset!)), findsOneWidget, ); if ([0, 5, 8, 11].contains(index)) { await shot('halloween-discovery-$index'); } expect(tester.takeException(), isNull); } await tap('Next leaf →'); expect(g.storyPage.id, 'midnight.ending'); await shot('halloween-03-ending'); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(g.screen, Screen.home); g.openNotebook(); await tester.pumpAndSettle(); final restarted = Game(book, Progress.memory(store.snapshot)); await restarted.restore(); expect(restarted.storyPage.id, 'midnight.ending'); expect(restarted.recoveredCount, 12); expect(restarted.progress.text('notebook_last_page'), 'midnight.ending'); await tap('Back to contents'); expect(g.screen, Screen.contents); await shot('halloween-04-completed-contents'); expect(g.recoveredCount, 12); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); await SystemChrome.setPreferredOrientations([]); }, ); }