Skip to content

Commit

Permalink
feat: Added size parameter for testGolden() (#1780)
Browse files Browse the repository at this point in the history
Added size parameter to testGolden() function, allowing us to reduce the size of the game canvas used. The default size is 2400x1800, as given by the Flutter framework, seems like too much for most tests.
  • Loading branch information
st-pasha authored Jul 3, 2022
1 parent 5857114 commit 8e41d83
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/flame_test/lib/src/test_golden.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flame/game.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meta/meta.dart';

Expand All @@ -23,29 +24,47 @@ import 'package:meta/meta.dart';
/// necessary game components, and possibly advancing the game clock. As a
/// convenience, we will run `await game.ready()` before rendering, to ensure
/// that all components that might be pending are properly mounted.
///
/// The [size] parameter controls the size of the "device" on which the game
/// widget is rendered, if omitted it defaults to 2400x1800. This size will be
/// equal to the canvas size of the game.
@isTest
void testGolden(
String testName,
PrepareGameFunction testBody, {
required String goldenFile,
Vector2? size,
FlameGame? game,
bool skip = false,
}) {
testWidgets(
testName,
(tester) async {
final gameInstance = game ?? FlameGame();
const myKey = ValueKey('game-instance');

await tester.runAsync(() async {
await tester.pumpWidget(GameWidget(game: gameInstance));
Widget widget = GameWidget(key: myKey, game: gameInstance);
if (size != null) {
widget = Center(
child: SizedBox(
width: size.x,
height: size.y,
child: RepaintBoundary(
child: widget,
),
),
);
}
await tester.pumpWidget(widget);
await tester.pump();
await testBody(gameInstance);
await gameInstance.ready();
await tester.pump();
});

await expectLater(
find.byWidgetPredicate((widget) => widget is GameWidget),
find.byKey(myKey),
matchesGoldenFile(goldenFile),
);
},
Expand Down
12 changes: 12 additions & 0 deletions packages/flame_test/test/golden_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ void main() {
goldenFile: 'golden_test.png',
);

testGolden(
'Same test, but with smaller size',
(game) async {
final paint = Paint()..color = Colors.white;
game.add(
CircleComponent(radius: 10, position: Vector2.all(100), paint: paint),
);
},
size: Vector2(200, 200),
goldenFile: 'golden_test_small.png',
);

testGolden(
'skipped test',
(game) async {},
Expand Down
Binary file added packages/flame_test/test/golden_test_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8e41d83

Please sign in to comment.