import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.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/services/translation.dart'; import 'package:lumen/ui/translation.dart'; import 'support/story_fixtures.dart'; void main() { testWidgets( 'library and language controls open a translated story and preserve live marks', (tester) async { tester.view.physicalSize = const Size(600, 960); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.runAsync(() async { final font = FontLoader('MaraHand') ..addFont(rootBundle.load('assets/fonts/DancingScript.ttf')); await font.load(); }); final library = (await tester.runAsync(StoryLibrary.load))!; for (final story in library.stories) { for (final language in story.languages.keys) { await tester.runAsync( () => library.open(story.id, language: language), ); } } final book = (await tester.runAsync(() => library.open('mara-island')))!; final g = Game(book, Progress.memory(), library: library); await tester.pumpWidget(LumenApp(game: g, adsEnabled: false)); await tester.pumpAndSettle(); Future tap(String text) async { await tester.ensureVisible(find.text(text)); await tester.pumpAndSettle(); await tester.tap(find.text(text)); await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 20)), ); await tester.pumpAndSettle(); } await tap('Story library'); await tap('The paper boat'); expect(g.book.id, 'paper-boat'); expect(find.text('The paper boat').hitTestable(), findsOneWidget); await tap('Open The paper boat · 0 / 1'); expect(g.storyPage.id, 'boat.entry'); await tap('Next leaf →'); await tester.ensureVisible(find.byKey(const ValueKey('cell-0'))); await tester.tap(find.byKey(const ValueKey('cell-0'))); await tester.pumpAndSettle(); final marks = List.of(g.cells); expect(find.text('Contents'), findsNothing); expect(find.text('Bookmark'), findsNothing); expect(find.text('Language'), findsNothing); expect(find.text('Close book'), findsNothing); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(g.screen, Screen.home); await tap('Settings'); await tap('Language'); await tap('Deutsch'); expect(g.book.language, 'de'); expect(g.screen, Screen.home); g.openNotebook(); await tester.pumpAndSettle(); expect(g.storyPage.id, 'boat.puzzle'); expect(g.cells, marks); expect(g.canUndo, true); expect(find.text('Eine kleine Überfahrt'), findsOneWidget); expect(find.text('Nächste Seite →'), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(g.screen, Screen.home); expect(find.text('Einstellungen'), findsOneWidget); await tap('Geschichtenbibliothek'); expect(g.screen, Screen.library); expect(find.text('Wähle ein Buch'), findsOneWidget); expect(find.text('Das Papierboot'), findsOneWidget); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); }, ); testWidgets('the first window is the bookshelf and opens a chosen book', ( tester, ) async { tester.view.physicalSize = const Size(600, 960); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final library = (await tester.runAsync(StoryLibrary.load))!; await tester.runAsync(() async { for (final story in library.stories) { for (final language in story.languages.keys) { await library.open(story.id, language: language); } } }); final book = (await tester.runAsync(() => library.open('mara-island')))!; final g = Game(book, Progress.memory({'language': 'en'}), library: library) ..showLibrary(); await tester.pumpWidget(LumenApp(game: g, adsEnabled: false)); // The shelf loads books through futures created outside the fake async // zone, so give the real event loop a turn before checking the cards. for (var i = 0; i < 3; i++) { await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 20)), ); await tester.pump(); } expect(g.screen, Screen.library); expect(find.byKey(const ValueKey('library')), findsOneWidget); for (final story in library.stories) { expect(find.text(story.title), findsOneWidget); } expect(find.text('Open this book ›'), findsNWidgets(3)); final boat = find.text('The paper boat'); await tester.ensureVisible(boat); await tester.pumpAndSettle(); await tester.tap(boat); await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 20)), ); await tester.pumpAndSettle(); expect(g.book.id, 'paper-boat'); expect(g.screen, Screen.home); expect(find.text('Open The paper boat · 0 / 1'), findsOneWidget); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); }); testWidgets( 'German story pages fit portrait and landscape with larger text', (tester) async { addTearDown(tester.view.reset); tester.platformDispatcher.textScaleFactorTestValue = 1.5; addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); final library = (await tester.runAsync(StoryLibrary.load))!; final book = (await tester.runAsync( () => library.open('paper-boat', language: 'de'), ))!; for (final size in [const Size(360, 640), const Size(960, 600)]) { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; final g = Game( book, Progress.memory({'language': 'de'}), library: library, ); await tester.pumpWidget(LumenApp(game: g, adsEnabled: false)); await tester.pumpAndSettle(); for (var i = 0; i < book.pages.length; i++) { g.showStory(i); await tester.pumpAndSettle(); expect(find.text('Sprache'), findsNothing); expect( find .text(i == 2 ? 'Zum Inhaltsverzeichnis' : 'Nächste Seite →') .hitTestable(), findsOneWidget, ); expect(tester.takeException(), isNull); } g.contents(); await tester.pumpAndSettle(); expect(find.text('Eine kleine Überfahrt'), findsOneWidget); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); } }, ); testWidgets('a right-to-left story uses RTL layout and a system font', ( tester, ) async { final data = fixture(); data['fixture/ar.json'] = { 'title': 'قصة قصيرة', 'pages': { 'entry-0': {'title': 'البداية', 'text': 'تبدأ الرحلة هنا.'}, }, }; final original = definition('arabic'); original.languages['ar'] = {'label': 'العربية', 'asset': 'fixture/ar.json'}; final library = StoryLibrary( [original], bundle: FixtureBundle(data), copy: const TranslationCatalog( {'en': 'English', 'ar': 'العربية'}, { 'ar': {'Language': 'اللغة'}, }, ), ); final book = (await tester.runAsync( () => library.open('arabic', language: 'ar'), ))!; final g = Game(book, Progress.memory({'language': 'ar'}), library: library) ..showStory(0); await tester.pumpWidget(LumenApp(game: g, adsEnabled: false)); await tester.pumpAndSettle(); final heading = find.text('البداية'); expect(Directionality.of(tester.element(heading)), TextDirection.rtl); expect(tester.widget(heading).style?.fontFamily, 'sans-serif'); expect(find.text('اللغة'), findsNothing); g.startCampaign(0); await tester.pumpAndSettle(); expect( tester.getCenter(find.byKey(const ValueKey('cell-0'))).dx, lessThan(tester.getCenter(find.byKey(const ValueKey('cell-4'))).dx), ); expect(tester.takeException(), isNull); expect(tester.takeException(), isNull); expect(localeFromTag('zh-Hant-TW').scriptCode, 'Hant'); expect(localeFromTag('zh-Hant-TW').countryCode, 'TW'); await tester.pumpWidget(const SizedBox()); }); }