Files
logic-puzzle/test/postcards_widget_test.dart
2026-11-15 16:20:17 +01:00

367 lines
14 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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/book.dart';
import 'package:lumen/domain/game.dart';
import 'package:lumen/domain/library.dart';
import 'package:lumen/domain/postcards.dart';
import 'package:lumen/main.dart';
import 'package:lumen/services/ads.dart';
import 'package:lumen/services/progress.dart';
import 'package:lumen/ui/postcards.dart';
import 'package:lumen/ui/licenses.dart';
import 'postcards_test.dart' show solve;
class NoFillAds extends Ads {
NoFillAds() : super(enabled: false);
int opportunities = 0;
@override
Future<void> between(VoidCallback next) async {
opportunities++;
next();
}
}
Future<void> capture(WidgetTester tester, String name) async {
if (!const bool.fromEnvironment('CAPTURE_POSTCARDS')) return;
final boundary = tester.renderObject<RenderRepaintBoundary>(
find.byKey(const ValueKey('capture')),
);
await tester.runAsync(() async {
final image = await boundary.toImage();
final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
final dir = Directory('artifacts/postcard-review')
..createSync(recursive: true);
File('${dir.path}/$name.png').writeAsBytesSync(bytes!.buffer.asUint8List());
image.dispose();
});
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async {
for (final family in ['sans-serif', 'serif', 'Roboto']) {
await (FontLoader(
family,
)..addFont(rootBundle.load('assets/fonts/NotoSans-Regular.ttf'))).load();
}
await (FontLoader(
'MaraHand',
)..addFont(rootBundle.load('assets/fonts/DancingScript.ttf'))).load();
});
testWidgets('postcard reveal fades from a blurred memory to clear colour', (
tester,
) async {
await tester.pumpWidget(
MaterialApp(home: PostcardRevealArt(card: islandPostcards.first)),
);
final blur = find.byType(ImageFiltered);
final color = find.byKey(const ValueKey('postcard-reveal-color'));
final initial = tester.widget<ColorFiltered>(color).colorFilter;
expect(tester.widget<ImageFiltered>(blur).enabled, isTrue);
await tester.pump(const Duration(milliseconds: 900));
expect(tester.widget<ColorFiltered>(color).colorFilter, isNot(initial));
expect(tester.widget<ImageFiltered>(blur).enabled, isTrue);
await tester.pump(const Duration(milliseconds: 900));
expect(tester.widget<ImageFiltered>(blur).enabled, isFalse);
});
testWidgets('later postcard puzzles show both lantern markers and the rule', (
tester,
) async {
tester.view.physicalSize = const Size(600, 960);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final book = (await tester.runAsync(Book.load))!;
final game = Game(book, Progress.memory())..startPostcard(2, 2);
await tester.pumpWidget(LumenApp(game: game, adsEnabled: false));
await tester.pumpAndSettle();
final pair = game.puzzle!.lanternPairs.single;
expect(find.byKey(ValueKey('lantern-${pair.first}')), findsOneWidget);
expect(find.byKey(ValueKey('lantern-${pair.second}')), findsOneWidget);
expect(
find.text('Matching lanterns: exactly one of the two cells is lit.'),
findsOneWidget,
);
expect(tester.takeException(), isNull);
});
testWidgets(
'three patterns collect a readable postcard before a single no-fill break',
(tester) async {
tester.view.physicalSize = const Size(600, 960);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final book = (await tester.runAsync(Book.load))!;
final g = Game(book, Progress.memory())..showPostcards();
final ads = NoFillAds();
await tester.pumpWidget(
RepaintBoundary(
key: const ValueKey('capture'),
child: MaterialApp(
theme: ThemeData(
fontFamily: 'MaraHand',
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xff355751),
surface: const Color(0xfff6f1e7),
),
),
home: Notebook(game: g, adsEnabled: false, ads: ads),
),
),
);
await tester.pumpAndSettle();
await tester.runAsync(() async {
await precacheImage(
const AssetImage('assets/postcards/illustrations/washing-line.png'),
tester.element(find.byKey(const ValueKey('postcard-collection'))),
);
});
await tester.pump();
final lockedFilter = tester
.widget<ColorFiltered>(find.byKey(const ValueKey('postcard-art-0')))
.colorFilter;
expect(
tester
.widget<ImageFiltered>(
find.byKey(const ValueKey('postcard-blur-0')),
)
.enabled,
isTrue,
);
await capture(tester, 'collection');
await tester.tap(find.byKey(const ValueKey('postcard-0')));
await tester.pumpAndSettle();
expect(g.postcardSlot, 0);
expect(find.byKey(const ValueKey('area-0')), findsOneWidget);
expect(find.byKey(const ValueKey('area-1')), findsOneWidget);
expect(find.byKey(const ValueKey('area-2')), findsOneWidget);
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);
expect(
find.byKey(const ValueKey('selected-region-outline')),
findsOneWidget,
);
await capture(tester, 'puzzle');
for (var slot = 0; slot < 3; slot++) {
solve(g);
await tester.pumpAndSettle();
final next = find.text(
slot == 2 ? 'Read your postcard' : 'Next pattern →',
);
await tester.ensureVisible(next);
await tester.tap(next);
await tester.pumpAndSettle();
expect(ads.opportunities, 0);
}
expect(find.text('Keep this postcard'), findsOneWidget);
final message = tester.widget<SelectableText>(
find.byKey(const ValueKey('postcard-message')),
);
expect(message.style?.fontFamily, 'MaraHand');
await capture(tester, 'collected');
await tester.tap(find.text('Keep this postcard'));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('postcard-reveal')), findsOneWidget);
final revealedImage = tester.widget<Image>(
find.descendant(
of: find.byKey(const ValueKey('postcard-reveal')),
matching: find.byType(Image),
),
);
expect(
(revealedImage.image as AssetImage).assetName,
'assets/postcards/illustrations/washing-line.png',
);
expect(ads.opportunities, 0);
await capture(tester, 'reveal');
await tester.tap(find.text('Back to collection'));
await tester.pumpAndSettle();
expect(g.screen, Screen.postcards);
expect(g.postcardsCollected, 1);
expect(ads.opportunities, 1);
expect(
tester
.widget<ImageFiltered>(
find.byKey(const ValueKey('postcard-blur-0')),
)
.enabled,
isFalse,
);
expect(
tester
.widget<ImageFiltered>(
find.byKey(const ValueKey('postcard-blur-1')),
)
.enabled,
isTrue,
);
expect(
tester
.widget<ColorFiltered>(find.byKey(const ValueKey('postcard-art-0')))
.colorFilter,
isNot(lockedFilter),
);
expect(
tester
.widget<ColorFiltered>(find.byKey(const ValueKey('postcard-art-1')))
.colorFilter,
lockedFilter,
);
await tester.ensureVisible(find.byKey(const ValueKey('postcard-0')));
await tester.tap(find.byKey(const ValueKey('postcard-0')));
await tester.pumpAndSettle();
await tester.tap(find.text('Keep this postcard'));
await tester.pumpAndSettle();
await tester.tap(find.text('Back to collection'));
await tester.pumpAndSettle();
expect(ads.opportunities, 1);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
},
);
testWidgets(
'postcard collection, puzzle and letter fit large text on small and wide screens',
(tester) async {
addTearDown(tester.view.reset);
tester.platformDispatcher.textScaleFactorTestValue = 1.5;
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
final book = (await tester.runAsync(Book.load))!;
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())..showPostcards();
await tester.pumpWidget(LumenApp(game: g, adsEnabled: false));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
g.startPostcard(0);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
for (var i = 0; i < 49; i++) {
final bounds = tester.getRect(find.byKey(ValueKey('cell-$i')));
expect(bounds.top, greaterThanOrEqualTo(0));
expect(bounds.bottom, lessThanOrEqualTo(size.height));
}
for (var slot = 0; slot < 3; slot++) {
g.startPostcard(0, slot);
solve(g);
}
await tester.pumpAndSettle();
await tester.ensureVisible(find.text('Read your postcard'));
await tester.tap(find.text('Read your postcard'));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.text('Keep this postcard').hitTestable(), findsOneWidget);
await tester.tap(find.text('Keep this postcard'));
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('postcard-reveal')), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.tap(find.text('Back to collection'));
await tester.pumpAndSettle();
await tester.pumpWidget(const SizedBox());
}
},
);
testWidgets('shelf activity buttons remain large on a small screen', (
tester,
) async {
tester.view.physicalSize = const Size(320, 700);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final book = (await tester.runAsync(Book.load))!;
final game = Game(book, Progress.memory());
await tester.pumpWidget(
MaterialApp(
home: MediaQuery(
data: const MediaQueryData(
size: Size(320, 700),
textScaler: TextScaler.linear(1.5),
),
child: Scaffold(
body: SingleChildScrollView(
child: ShelfActivities(game: game, onPostcards: () {}),
),
),
),
),
);
await tester.pumpAndSettle();
for (final key in ['shelf-daily', 'shelf-postcards']) {
final size = tester.getSize(find.byKey(ValueKey(key)));
expect(size.width, greaterThan(250));
expect(size.height, greaterThanOrEqualTo(64));
}
expect(tester.takeException(), isNull);
});
testWidgets('daily play and postcards are on the shelf, not the book page', (
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) {
await library.open(story.id);
}
});
final book = (await tester.runAsync(() => library.open('mara-island')))!;
final store = Progress.memory({'language': 'en'});
final g = Game(book, store, library: library)
..showStory(2)
..showLibrary();
await tester.pumpWidget(
RepaintBoundary(
key: const ValueKey('capture'),
child: LumenApp(game: g, adsEnabled: false),
),
);
// Cached asset futures were loaded in the real event loop above.
for (var i = 0; i < 3; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump();
}
await tester.pumpAndSettle();
await capture(tester, 'shelf');
expect(find.text('Open-source licenses'), findsNothing);
await tester.tap(find.text('Settings'));
await tester.pumpAndSettle();
await tester.tap(find.text('Open-source licenses'));
await tester.pumpAndSettle();
expect(find.byType(OpenSourceLicenses), findsOneWidget);
await tester.pageBack();
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('shelf-activities')), findsOneWidget);
expect(find.byKey(const ValueKey('shelf-daily')), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('shelf-postcards')));
await tester.pumpAndSettle();
expect(g.screen, Screen.postcards);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('shelf-daily')));
await tester.pumpAndSettle();
expect(g.mode, Mode.daily);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(g.screen, Screen.library);
g.home();
await tester.pumpAndSettle();
expect(find.text('Play today’s puzzle →'), findsNothing);
expect(find.text('Island postcards'), findsNothing);
expect(find.textContaining('Practice ·'), findsNothing);
expect(find.textContaining('Difficulty ·'), findsNothing);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
});
}