Files
logic-puzzle/test/widget_test.dart
T
2026-10-31 18:25:02 +01:00

301 lines
10 KiB
Dart

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/puzzle.dart';
import 'package:lumen/main.dart';
import 'package:lumen/services/progress.dart';
import 'package:lumen/ui/board.dart';
import 'package:lumen/ui/paper.dart';
void main() {
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}));
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, 190);
} else {
expect(find.byType(PixelPicture), findsOneWidget);
}
final next = find.text(
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());
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 · free'), findsOneWidget);
expect(find.text('Back to notebook'), 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 · free')).dx,
greaterThan(storyLast.right),
);
expect(find.text('Next leaf →'), findsOneWidget);
expect(tester.takeException(), isNull);
});
}