Files
logic-puzzle/test/widget_test.dart
T
2026-11-02 06:14:48 +01:00

744 lines
25 KiB
Dart
Raw 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 'package:flutter/material.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/puzzle.dart';
import 'package:lumen/main.dart';
import 'package:lumen/services/progress.dart';
import 'package:lumen/services/ads.dart';
import 'package:lumen/ui/board.dart';
import 'package:lumen/ui/discovery.dart';
import 'package:lumen/ui/paper.dart';
class _RewardAds extends Ads {
_RewardAds({this.autoReward = false}) : super(enabled: false);
final bool autoReward;
bool available = true;
int requests = 0;
VoidCallback? reward;
@override
Future<bool> hint(VoidCallback earned) async {
requests++;
if (!available) return false;
reward = earned;
if (autoReward) earned();
return true;
}
}
void main() {
testWidgets('atmosphere controls fit large text and remember mute', (
tester,
) async {
tester.view.physicalSize = const Size(360, 640);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final book = (await tester.runAsync(Book.load))!;
final g = Game(book, Progress.memory());
g.showStory(1);
await tester.pumpWidget(
MaterialApp(
home: MediaQuery(
data: const MediaQueryData(
size: Size(360, 640),
textScaler: TextScaler.linear(1.5),
),
child: Notebook(game: g, adsEnabled: false),
),
),
);
await tester.pumpAndSettle();
final settings = find.text('Sound & atmosphere');
await tester.ensureVisible(settings);
await tester.pumpAndSettle();
await tester.tap(settings);
await tester.pumpAndSettle();
expect(
tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value,
true,
);
await tester.tap(find.byType(SwitchListTile));
await tester.pumpAndSettle();
expect(g.progress.flag('ambience_enabled'), false);
expect(tester.widget<Slider>(find.byType(Slider)).onChanged, isNull);
await tester.tap(find.text('Keep going'));
await tester.pumpAndSettle();
await tester.tap(settings);
await tester.pumpAndSettle();
expect(
tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value,
false,
);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
});
testWidgets(
'discovery blends the solution into art and keeps a replayable symbol',
(tester) async {
const artKey = ValueKey('test-artwork');
final picture = List.generate(49, (i) => i % 3 == 0 ? 1 : 0);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Discovery(
picture: picture,
label: 'The lantern',
height: 190,
artwork: const SizedBox(key: artKey, width: 190, height: 190),
),
),
),
);
double opacity() => tester
.widget<Opacity>(
find.ancestor(
of: find.byKey(artKey),
matching: find.byType(Opacity),
),
)
.opacity;
expect(opacity(), 0);
await tester.pump(const Duration(milliseconds: 1600));
expect(opacity(), inExclusiveRange(0, 1));
await tester.pumpAndSettle();
expect(opacity(), 1);
final stamp = tester
.widgetList<PatternStamp>(find.byType(PatternStamp))
.singleWhere((s) => s.height == 56);
expect(stamp.picture, picture);
await tester.tap(find.text('See the transformation again'));
await tester.pump();
expect(opacity(), 0);
await tester.pumpAndSettle();
await tester.pumpWidget(const SizedBox());
},
);
testWidgets(
'back from a discovery opens its completed grid and forward revisits the memory',
(tester) async {
final book = (await tester.runAsync(Book.load))!;
final g = Game(
book,
Progress.memory({
'story_access_level': 11,
'done_story_v1_4': true,
'solved': 1,
}),
);
g.reveal(4);
await tester.pumpWidget(LumenApp(game: g, adsEnabled: false));
await tester.pumpAndSettle();
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(g.screen, Screen.game);
expect(find.byType(Board), findsOneWidget);
expect(g.puzzle!.solved(g.cells), true);
final forward = find.text('Revisit the memory →');
await tester.ensureVisible(forward);
await tester.pumpAndSettle();
await tester.tap(forward);
await tester.pumpAndSettle();
expect(g.screen, Screen.reveal);
expect(g.campaignIndex, 4);
expect(g.solved, 1);
await tester.pumpWidget(const SizedBox());
},
);
testWidgets('every story hint needs a separate earned ad reward', (
tester,
) async {
final book = (await tester.runAsync(Book.load))!;
final g = Game(book, Progress.memory({'story_access_level': 11}));
final ads = _RewardAds();
g.startCampaign(8);
await tester.pumpWidget(
MaterialApp(
home: Notebook(game: g, adsEnabled: false, ads: ads),
),
);
await tester.pumpAndSettle();
for (var i = 0; i < 2; i++) {
final before = List<int>.of(g.cells);
final hint = find.text('Hint · rewarded ad');
await tester.ensureVisible(hint);
await tester.pumpAndSettle();
await tester.tap(hint);
await tester.pumpAndSettle();
expect(g.cells, before);
await tester.tap(find.text('Watch ad'));
await tester.pumpAndSettle();
expect(ads.requests, i + 1);
expect(g.cells, before, reason: 'Showing an ad alone earns no hint');
ads.reward!();
await tester.pumpAndSettle();
expect(
List.generate(
g.cells.length,
(c) => c,
).where((c) => g.cells[c] != before[c]).length,
1,
);
final after = List<int>.of(g.cells);
ads.reward!();
await tester.pumpAndSettle();
expect(g.cells, after, reason: 'One reward can grant only one hint');
await tester.tap(find.text('Keep going'));
await tester.pumpAndSettle();
}
expect(g.solved, 0);
await tester.pumpWidget(const SizedBox());
});
testWidgets(
'declining, unavailable or unfinished ads leave the puzzle untouched',
(tester) async {
final book = (await tester.runAsync(Book.load))!;
final g = Game(book, Progress.memory({'story_access_level': 11}));
final ads = _RewardAds();
g.startCampaign(8);
await tester.pumpWidget(
MaterialApp(
home: Notebook(game: g, adsEnabled: false, ads: ads),
),
);
await tester.pumpAndSettle();
Future<void> request() async {
final hint = find.text('Hint · rewarded ad');
await tester.ensureVisible(hint);
await tester.pumpAndSettle();
await tester.tap(hint);
await tester.pumpAndSettle();
}
final before = List<int>.of(g.cells);
await request();
await tester.tap(find.text('Keep thinking'));
await tester.pumpAndSettle();
expect(ads.requests, 0);
expect(g.cells, before);
ads.available = false;
await request();
await tester.tap(find.text('Watch ad'));
await tester.pumpAndSettle();
expect(
find.text('No ad available. Please try again later.'),
findsOneWidget,
);
expect(g.cells, before);
ads.available = true;
await request();
await tester.tap(find.text('Watch ad'));
await tester.pumpAndSettle();
expect(g.cells, before);
g.startCampaign(9);
await tester.pumpAndSettle();
final next = List<int>.of(g.cells);
ads.reward!();
await tester.pumpAndSettle();
expect(
g.cells,
next,
reason: 'A late reward cannot alter another puzzle',
);
await tester.pumpWidget(const SizedBox());
},
);
for (final mode in [Mode.daily, Mode.classic]) {
testWidgets('the first $mode hint also requires an ad', (tester) async {
final book = (await tester.runAsync(Book.load))!;
final g = Game(book, Progress.memory());
if (mode == Mode.daily) {
g.startDaily(DateTime(2026, 11, 2));
} else {
await tester.runAsync(() => g.startPractice());
}
final ads = _RewardAds();
await tester.pumpWidget(
MaterialApp(
home: Notebook(game: g, adsEnabled: false, ads: ads),
),
);
await tester.pumpAndSettle();
final before = List<int>.of(g.cells);
final hint = find.text('Hint · rewarded ad');
await tester.ensureVisible(hint);
await tester.pumpAndSettle();
await tester.tap(hint);
await tester.pumpAndSettle();
expect(find.text('Watch ad'), findsOneWidget);
expect(g.cells, before);
await tester.tap(find.text('Keep thinking'));
await tester.pumpAndSettle();
expect(ads.requests, 0);
await tester.pumpWidget(const SizedBox());
});
}
testWidgets(
'object illustrations remain hidden until their own pattern is solved',
(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({'story_access_level': 11}));
game.showStory(0);
await tester.pumpWidget(LumenApp(game: game, adsEnabled: false));
await tester.pumpAndSettle();
void expectHidden() {
for (final level in book.levels) {
expect(
find.image(AssetImage(level.illustrationAsset!)),
findsNothing,
);
}
expect(find.byType(PixelPicture), findsNothing);
}
for (var i = 0; i < 12; i++) {
game.reveal(i, readAhead: true);
await tester.pumpAndSettle();
expectHidden();
expect(game.solved, 0);
}
for (final index in [5, 6, 9, 12, 15]) {
game.showStory(index);
await tester.pumpAndSettle();
expectHidden();
}
game.showStory(book.pageFor(1, StoryPageType.puzzle));
final solution = List<int>.of(game.puzzle!.solution);
for (var c = 0; c < solution.length; c++) {
while (game.cells[c] != (solution[c] == 1 ? 1 : 2)) {
game.mark(c);
}
}
await tester.pumpAndSettle();
expect(
find.image(AssetImage(book.levels[1].illustrationAsset!)),
findsOneWidget,
);
game.showStory(5);
await tester.pumpAndSettle();
expectHidden();
game.reveal(2, readAhead: true);
await tester.pumpAndSettle();
expectHidden();
expect(game.solved, 1);
final restarted = Game(book, Progress.memory(game.progress.snapshot));
await restarted.restore();
expect(restarted.recovered(1), true);
expect(restarted.recovered(2), false);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
},
);
testWidgets('completed saves never put discovery art on earlier leaves', (
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({
for (var i = 0; i < 12; i++) 'done_story_v1_$i': true,
'notebook_handle_seen': true,
}),
);
game.showStory(0);
await tester.pumpWidget(LumenApp(game: game, adsEnabled: false));
await tester.pumpAndSettle();
for (var i = 0; i < book.pages.length; i++) {
game.showStory(i);
await tester.pumpAndSettle();
final page = book.pages[i];
for (var object = 0; object < 12; object++) {
expect(
find.image(AssetImage(book.levels[object].illustrationAsset!)),
page.type == StoryPageType.memory && page.campaignIndex == object
? findsOneWidget
: findsNothing,
reason: '${page.id} must not reveal object $object before its puzzle',
);
}
expect(find.byType(PixelPicture), findsNothing);
expect(tester.takeException(), isNull);
}
await tester.pumpWidget(const SizedBox());
});
testWidgets('all cottage leaves turn, offer hints and fit larger text', (
tester,
) async {
addTearDown(tester.view.reset);
await tester.runAsync(() async {
final font = FontLoader('MaraHand')
..addFont(rootBundle.load('assets/fonts/DancingScript.ttf'));
await font.load();
});
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());
g.showStory(0);
await tester.pumpWidget(
MaterialApp(
home: MediaQuery(
data: MediaQueryData(
size: size,
textScaler: const TextScaler.linear(1.5),
),
child: Notebook(
game: g,
adsEnabled: false,
ads: _RewardAds(autoReward: true),
),
),
),
);
await tester.pumpAndSettle();
for (var i = 0; i < book.pages.length; i++) {
expect(g.storyIndex, i);
final page = book.pages[i];
if (page.type == StoryPageType.puzzle) {
final before = List<int>.of(g.cells);
final hint = find.text('Hint · rewarded ad');
await tester.ensureVisible(hint);
await tester.pumpAndSettle();
await tester.tap(hint);
await tester.pumpAndSettle();
await tester.tap(find.text('Watch ad'));
await tester.pumpAndSettle();
expect(g.cells, isNot(before), reason: page.id);
await tester.tap(find.text('Keep going'));
await tester.pumpAndSettle();
}
final next = find.text(
i == book.pages.length - 1 ? 'Across the tide →' : 'Next leaf →',
);
expect(next.hitTestable(), findsOneWidget, reason: page.id);
await tester.tap(next);
await tester.pumpAndSettle();
if (page.type == StoryPageType.puzzle) {
await tester.tap(find.text('Read ahead'));
await tester.pumpAndSettle();
}
expect(g.solved, 0);
expect(tester.takeException(), isNull, reason: page.id);
}
expect(g.screen, Screen.briefing);
expect(g.campaignIndex, 4);
g.contents(0);
await tester.pumpAndSettle();
expect(find.text('I · The keeper’s cottage'), findsOneWidget);
expect(find.text('2 · Something to carry the flame'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
await tester.pumpAndSettle();
}
});
testWidgets('campaign discoveries use bundled art with a pixel fallback', (
tester,
) async {
addTearDown(tester.view.reset);
final book = (await tester.runAsync(Book.load))!;
final g = Game(
book,
Progress.memory({
'story_access_level': 11,
for (var i = 0; i < 12; i++) 'done_story_v1_$i': true,
}),
);
expect(book.levels.every((l) => l.illustrationAsset != null), true);
for (final size in [const Size(360, 640), const Size(960, 600)]) {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
for (var index = 0; index < book.levels.length; index++) {
g.reveal(index, readAhead: true);
await tester.pumpWidget(
MaterialApp(
home: MediaQuery(
data: MediaQueryData(
size: size,
textScaler: const TextScaler.linear(1.5),
),
child: Notebook(game: g, adsEnabled: false),
),
),
);
await tester.pumpAndSettle();
final asset = book.levels[index].illustrationAsset;
if (asset != null) {
final art = find.image(AssetImage(asset));
expect(art, findsOneWidget);
expect(find.byType(PixelPicture), findsNothing);
await tester.ensureVisible(art);
await tester.pumpAndSettle();
expect(tester.getSize(art).height, index < 4 ? 155 : 190);
} else {
expect(find.byType(PixelPicture), findsOneWidget);
}
final next = find.text(
index < 4
? 'Next leaf →'
: index == 11
? 'Return to the notebook'
: 'Turn the page →',
);
await tester.ensureVisible(next);
await tester.pumpAndSettle();
expect(tester.getRect(next).bottom, lessThan(size.height));
expect(g.solved, 0);
expect(tester.takeException(), isNull);
}
g.showStory(3);
await tester.pumpAndSettle();
final match = find.image(
AssetImage(book.levels.first.illustrationAsset!),
);
expect(match, findsOneWidget);
expect(tester.getSize(match).height, 155);
final nextLeaf = find.text('Next leaf →');
await tester.ensureVisible(nextLeaf);
await tester.pumpAndSettle();
expect(nextLeaf.hitTestable(), findsOneWidget);
expect(tester.takeException(), isNull);
}
await tester.pumpWidget(const SizedBox());
});
testWidgets('a discovery without artwork keeps its pixel picture', (
tester,
) async {
final book = (await tester.runAsync(Book.load))!;
final original = book.levels.first;
book.levels[0] = CampaignLevel({
'chapter': original.chapter,
'title': original.title,
'object': original.object,
'before': original.before,
'after': original.after,
'picture': original.picture.join(),
'clues': original.clues,
'areas': original.areas,
});
final game = Game(book, Progress.memory({'done_story_v1_0': true}));
game.reveal(0, readAhead: true);
await tester.pumpWidget(LumenApp(game: game, adsEnabled: false));
await tester.pumpAndSettle();
expect(find.byType(PixelPicture), findsOneWidget);
expect(game.solved, 0);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
});
testWidgets('page margins turn leaves; grid drags never turn', (
tester,
) async {
tester.view.reset();
tester.view.physicalSize = const Size(600, 960);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
var turns = 0, marks = 0;
final p = Puzzle(7, List.filled(49, 0));
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Paper(
onTurn: (d) => turns += d,
child: Board(
puzzle: p,
cells: List.filled(49, 0),
onMark: (_) => marks++,
),
),
),
),
);
await tester.pumpAndSettle();
await tester.dragFrom(const Offset(585, 400), const Offset(-180, 0));
await tester.pumpAndSettle();
expect(turns, 1);
await tester.drag(
find.byKey(const ValueKey('cell-20')),
const Offset(70, 0),
);
await tester.pumpAndSettle();
expect(turns, 1);
await tester.tap(find.byKey(const ValueKey('cell-20')));
expect(marks, 1);
expect(tester.takeException(), isNull);
});
testWidgets(
'reading swipes turn both ways; controls, scrolling and cancelled drags do not',
(tester) async {
tester.view.physicalSize = const Size(600, 960);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
var turns = 0, plays = 0;
final scroll = ScrollController();
addTearDown(scroll.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Paper(
onTurn: (direction) => turns += direction,
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
controller: scroll,
child: Column(
children: [
SizedBox(
key: const ValueKey('reading-zone'),
height: 130,
width: double.infinity,
child: writing(
'My dear, take your time with these pages.',
),
),
SizedBox(
key: const ValueKey('blank-zone'),
height: 100,
width: double.infinity,
),
for (var i = 0; i < 12; i++)
SizedBox(
height: 80,
child: writing('A remembered passage $i.'),
),
],
),
),
),
InkButton('Play this reading', () => plays++),
],
),
),
),
),
);
await tester.pumpAndSettle();
await tester.drag(
find.byKey(const ValueKey('reading-zone')),
const Offset(-150, 0),
);
await tester.pumpAndSettle();
expect(turns, 1);
await tester.dragFrom(
tester.getCenter(find.byKey(const ValueKey('blank-zone'))),
const Offset(150, 0),
);
await tester.pumpAndSettle();
expect(turns, 0);
await tester.drag(find.text('Play this reading'), const Offset(-120, 0));
await tester.pumpAndSettle();
expect(turns, 0);
expect(plays, 0);
await tester.tap(find.text('Play this reading'));
expect(plays, 1);
await tester.drag(
find.byKey(const ValueKey('reading-zone')),
const Offset(-30, 0),
);
await tester.pumpAndSettle();
expect(turns, 0);
final drag = await tester.startGesture(
tester.getCenter(find.byKey(const ValueKey('reading-zone'))),
);
await drag.moveBy(const Offset(-140, 0));
await drag.cancel();
await tester.pumpAndSettle();
expect(turns, 0);
await tester.drag(
find.byKey(const ValueKey('reading-zone')),
const Offset(0, -200),
);
await tester.pumpAndSettle();
expect(scroll.offset, greaterThan(0));
expect(turns, 0);
expect(tester.takeException(), isNull);
},
);
testWidgets('paper supports small and landscape viewports and larger text', (
tester,
) async {
addTearDown(tester.view.reset);
for (final size in [const Size(360, 640), const Size(960, 600)]) {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
await tester.pumpWidget(
MaterialApp(
home: MediaQuery(
data: MediaQueryData(
size: size,
textScaler: const TextScaler.linear(1.5),
),
child: Scaffold(
body: Leaf(
body: Column(
children: [
writing('For you, when you are ready', size: 31),
writing('A remembered passage. ' * 40),
],
),
footer: InkButton('Next leaf', () {}),
),
),
),
),
);
await tester.pumpAndSettle();
expect(find.text('Next leaf'), findsOneWidget);
expect(tester.takeException(), isNull);
}
});
testWidgets('landscape pages keep the whole grid beside its notes', (
tester,
) async {
const width = 960.0, height = 600.0;
tester.view.physicalSize = const Size(width, height);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final book = (await tester.runAsync(Book.load))!;
final game = Game(book, Progress.memory());
game.startCampaign(0);
await tester.pumpWidget(LumenApp(game: game, adsEnabled: false));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
// The campaign leaf gives the board its own column, with the heading and
// controls beside it rather than above the fold.
final lastCell = tester.getRect(find.byKey(const ValueKey('cell-48')));
expect(lastCell.bottom, lessThan(height * .94));
expect(
tester.getTopLeft(find.text('A spark in the drawer')).dx,
greaterThan(lastCell.right),
);
expect(find.text('Hint · rewarded ad'), findsOneWidget);
expect(find.text('Next leaf →'), findsOneWidget);
expect(tester.takeException(), isNull);
// The opening notebook's puzzle leaf uses the same two columns, with the
// grid on the page and its controls beside it.
game.showStory(2);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
final storyLast = tester.getRect(find.byKey(const ValueKey('cell-48')));
expect(storyLast.bottom, lessThan(height * .94));
expect(
tester.getTopLeft(find.text('Hint · rewarded ad')).dx,
greaterThan(storyLast.right),
);
expect(find.text('Next leaf →'), findsOneWidget);
expect(tester.takeException(), isNull);
});
}