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/domain/library.dart'; import 'package:lumen/main.dart'; import 'package:lumen/services/progress.dart'; import 'package:lumen/ui/board.dart'; void main() { testWidgets( 'selected region follows its exact cells and never blocks marking', (tester) async { addTearDown(tester.view.reset); final library = (await tester.runAsync(StoryLibrary.load))!; final book = (await tester.runAsync( () => library.open('midnight-invitation'), ))!; for (final size in [const Size(360, 640), const Size(960, 600)]) { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; final game = Game(book, Progress.memory(), library: library); game.showStory(book.pageFor(2, StoryPageType.puzzle)); await tester.pumpWidget(LumenApp(game: game, adsEnabled: false)); await tester.pumpAndSettle(); expect(find.text('Undo a mark'), findsNothing); for (var index = 0; index < game.puzzle!.regions.length; index++) { final button = find.byKey(ValueKey('area-$index')); await tester.ensureVisible(button); await tester.pumpAndSettle(); await tester.tap(button); await tester.pumpAndSettle(); expect(game.selectedArea, index); expect(tester.widget(button).selected, true); expect( find.descendant(of: button, matching: find.byIcon(Icons.check)), findsNothing, ); final region = game.puzzle!.regions[index]; final n = game.puzzle!.size; final first = find.byKey( ValueKey('cell-${region.top * n + region.left}'), ); final last = find.byKey( ValueKey( 'cell-${(region.top + region.height - 1) * n + region.left + region.width - 1}', ), ); final outline = tester.getRect( find.byKey(const ValueKey('selected-region-outline')), ); final cells = Rect.fromPoints( tester.getTopLeft(first), tester.getBottomRight(last), ); expect(outline.left, closeTo(cells.left, .01)); expect(outline.top, closeTo(cells.top, .01)); expect(outline.width, closeTo(cells.width, .01)); expect(outline.height, closeTo(cells.height, .01)); final cell = List.generate( n * n, (i) => i, ).firstWhere((i) => region.contains(i, n) && !game.puzzle!.given[i]); final target = find.byKey(ValueKey('cell-$cell')); final original = game.cells[cell]; await tester.ensureVisible(target); await tester.pumpAndSettle(); for (var count = 0; count < 3; count++) { await tester.tap(target); await tester.pumpAndSettle(); expect(game.cells[cell], (original + count + 1) % 3); } } await tester.pumpWidget(const SizedBox()); } }, ); testWidgets( 'Halloween paper follows reading and contents and switches back with the book', (tester) async { final library = (await tester.runAsync(StoryLibrary.load))!; final book = (await tester.runAsync( () => library.open('midnight-invitation'), ))!; final game = Game(book, Progress.memory(), library: library); await tester.pumpWidget(LumenApp(game: game, adsEnabled: false)); game.openNotebook(); await tester.pumpAndSettle(); expect(find.image(AssetImage(book.paperAsset!)), findsOneWidget); expect( find.image(const AssetImage('assets/artwork/mara-single-page-v2.png')), findsNothing, ); game.contents(); await tester.pumpAndSettle(); expect(find.image(AssetImage(book.paperAsset!)), findsOneWidget); await tester.runAsync(() => game.selectStory('mara-island')); game.openNotebook(); await tester.pumpAndSettle(); expect( find.image(const AssetImage('assets/artwork/mara-single-page-v2.png')), findsOneWidget, ); expect(find.image(AssetImage(book.paperAsset!)), findsNothing); await tester.pumpWidget(const SizedBox()); }, ); }