From aa86ef302b24efbaab4155a75b0b38cfcf39d839 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Tue, 19 Apr 2022 16:44:19 -0500 Subject: [PATCH] refactor: load assets in test games --- .../test/helpers/test_game.dart | 12 +++++- test/helpers/builders.dart | 9 ++++- test/helpers/test_games.dart | 39 ++++++++++++++++--- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/packages/pinball_components/test/helpers/test_game.dart b/packages/pinball_components/test/helpers/test_game.dart index 5bd4b30d..07744d12 100644 --- a/packages/pinball_components/test/helpers/test_game.dart +++ b/packages/pinball_components/test/helpers/test_game.dart @@ -2,9 +2,19 @@ import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; class TestGame extends Forge2DGame { - TestGame() { + TestGame([List? assets]) : _assets = assets { images.prefix = ''; } + + final List? _assets; + + @override + Future onLoad() async { + if (_assets != null) { + await Future.wait(_assets!.map(images.load)); + } + await super.onLoad(); + } } class KeyboardTestGame extends TestGame with HasKeyboardHandlerComponents {} diff --git a/test/helpers/builders.dart b/test/helpers/builders.dart index d0eea644..2c23e3fe 100644 --- a/test/helpers/builders.dart +++ b/test/helpers/builders.dart @@ -1,16 +1,23 @@ -import 'package:flame/src/game/flame_game.dart'; +import 'package:flame/game.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_test/flutter_test.dart'; class FlameBlocTester> extends FlameTester { FlameBlocTester({ required GameCreateFunction gameBuilder, required B Function() blocBuilder, + // TODO(allisonryan0002): find alternative for testGameWidget. Loading + // assets in onLoad fails because the game loads after + List? assets, List Function()? repositories, }) : super( gameBuilder, pumpWidget: (gameWidget, tester) async { + if (assets != null) { + await Future.wait(assets.map(gameWidget.game.images.load)); + } await tester.pumpWidget( BlocProvider.value( value: blocBuilder(), diff --git a/test/helpers/test_games.dart b/test/helpers/test_games.dart index 3747a231..d3f78484 100644 --- a/test/helpers/test_games.dart +++ b/test/helpers/test_games.dart @@ -1,5 +1,7 @@ // ignore_for_file: must_call_super +import 'dart:async'; + import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/game.dart'; @@ -14,26 +16,53 @@ class TestGame extends Forge2DGame with FlameBloc { } class PinballTestGame extends PinballGame { - PinballTestGame() - : super( + PinballTestGame([List? assets]) + : _assets = assets, + super( audio: MockPinballAudio(), theme: const PinballTheme( characterTheme: DashTheme(), ), ); + final List? _assets; + + @override + Future onLoad() async { + if (_assets != null) { + await Future.wait(_assets!.map(images.load)); + } + await super.onLoad(); + } } class DebugPinballTestGame extends DebugPinballGame { - DebugPinballTestGame() - : super( + DebugPinballTestGame([List? assets]) + : _assets = assets, + super( audio: MockPinballAudio(), theme: const PinballTheme( characterTheme: DashTheme(), ), ); + + final List? _assets; + + @override + Future onLoad() async { + if (_assets != null) { + await Future.wait(_assets!.map(images.load)); + } + await super.onLoad(); + } } class EmptyPinballTestGame extends PinballTestGame { + EmptyPinballTestGame([List? assets]) : super(assets); + @override - Future onLoad() async {} + Future onLoad() async { + if (_assets != null) { + await Future.wait(_assets!.map(images.load)); + } + } }