// Temporary layout harness: renders the bookshelf at phone/tablet device // metrics and writes PNGs to artifacts/library/. Not part of the normal suite. import 'dart:io'; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:flutter/rendering.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'; const _screen = ValueKey('screenshot-surface'); const _output = 'artifacts/library'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); Future capture( WidgetTester tester, { required String name, required Size logical, required double dpr, double textScale = 1, bool captureEnd = false, }) async { addTearDown(tester.view.reset); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); 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))!; 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 assets = { 'assets/artwork/mara-single-page-v2.png', for (final story in library.stories) ?story.coverAsset, }; tester.view.devicePixelRatio = dpr; tester.view.physicalSize = Size(logical.width * dpr, logical.height * dpr); tester.platformDispatcher.textScaleFactorTestValue = textScale; final book = (await tester.runAsync(() => library.open('mara-island')))!; final g = Game(book, Progress.memory({'language': 'en'}), library: library) ..showLibrary(); await tester.pumpWidget( RepaintBoundary( key: _screen, child: LumenApp(game: g, adsEnabled: false), ), ); for (var i = 0; i < 3; i++) { await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 20)), ); await tester.pump(); } await tester.runAsync(() async { final context = tester.element(find.byKey(_screen)); for (final asset in assets) { await precacheImage(AssetImage(asset), context); } }); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); expect(g.screen, Screen.library, reason: name); expect(find.text('Choose a book'), findsOneWidget, reason: name); for (final story in library.stories) { expect( find.byKey(ValueKey('library-book-${story.id}')), findsOneWidget, reason: name, ); } expect(tester.takeException(), isNull, reason: name); Future write(String file) async { await tester.runAsync(() async { final boundary = tester.renderObject( find.byKey(_screen), ); final image = await boundary.toImage(pixelRatio: dpr); final data = await image.toByteData(format: ui.ImageByteFormat.png); final target = File('$_output/$file.png'); await target.parent.create(recursive: true); await target.writeAsBytes(data!.buffer.asUint8List()); image.dispose(); }); } await write(name); // Every book and Settings must remain reachable by scrolling, including on // the smallest phone and at 1.5x text. await tester.ensureVisible(find.text('Settings')); await tester.pumpAndSettle(); expect(find.text('Settings').hitTestable(), findsOneWidget, reason: name); expect(tester.takeException(), isNull, reason: name); if (captureEnd) await write('$name-end'); await tester.pumpWidget(const SizedBox()); } testWidgets('phone 360x640', (tester) async { await capture( tester, name: 'shelf-phone-360x640', logical: const Size(360, 640), dpr: 2, captureEnd: true, ); }); testWidgets('phone 390x844', (tester) async { await capture( tester, name: 'shelf-phone-390x844', logical: const Size(390, 844), dpr: 3, ); }); testWidgets('phone 360x640 at 1.5x text', (tester) async { await capture( tester, name: 'shelf-phone-360x640-text-1.5x', logical: const Size(360, 640), dpr: 2, textScale: 1.5, ); }); testWidgets('tablet portrait 800x1280', (tester) async { await capture( tester, name: 'shelf-tablet-800x1280', logical: const Size(800, 1280), dpr: 1.5, ); }); testWidgets('tablet landscape 1280x800', (tester) async { await capture( tester, name: 'shelf-tablet-1280x800', logical: const Size(1280, 800), dpr: 2, ); }); }