diff --git a/lib/game/components/backboard/displays/initials_input_display.dart b/lib/game/components/backboard/displays/initials_input_display.dart index 89b31150..c25606d4 100644 --- a/lib/game/components/backboard/displays/initials_input_display.dart +++ b/lib/game/components/backboard/displays/initials_input_display.dart @@ -57,7 +57,7 @@ class InitialsInputDisplay extends Component with HasGameRef { Future onLoad() async { for (var i = 0; i < 3; i++) { await add( - _BackboardLetterPrompt( + _InitialsLetterPrompt( position: Vector2( 11.4 + (2.3 * i), -20, @@ -80,7 +80,7 @@ class InitialsInputDisplay extends Component with HasGameRef { /// Returns the current inputed initials String get initials => children - .whereType<_BackboardLetterPrompt>() + .whereType<_InitialsLetterPrompt>() .map((prompt) => prompt.char) .join(); @@ -90,7 +90,7 @@ class InitialsInputDisplay extends Component with HasGameRef { } bool _movePrompt(bool left) { - final prompts = children.whereType<_BackboardLetterPrompt>().toList(); + final prompts = children.whereType<_InitialsLetterPrompt>().toList(); final current = prompts.firstWhere((prompt) => prompt.hasFocus) ..hasFocus = false; @@ -118,7 +118,7 @@ class _ScoreLabelTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = gameRef.buildContext!.l10n.score; + text = AppLocalizations.of(gameRef.buildContext!).score; } } @@ -147,7 +147,7 @@ class _NameLabelTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = gameRef.buildContext!.l10n.name; + text = AppLocalizations.of(gameRef.buildContext!).name; } } @@ -170,8 +170,8 @@ class _CharacterIconSpriteComponent extends SpriteComponent with HasGameRef { } } -class _BackboardLetterPrompt extends PositionComponent { - _BackboardLetterPrompt({ +class _InitialsLetterPrompt extends PositionComponent { + _InitialsLetterPrompt({ required Vector2 position, bool hasFocus = false, }) : _hasFocus = hasFocus, @@ -303,7 +303,7 @@ class _EnterInitialsTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = gameRef.buildContext!.l10n.enterInitials; + text = AppLocalizations.of(gameRef.buildContext!).enterInitials; } } @@ -322,7 +322,7 @@ class _ArrowsTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = gameRef.buildContext!.l10n.arrows; + text = AppLocalizations.of(gameRef.buildContext!).arrows; } } @@ -337,7 +337,7 @@ class _AndPressTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = gameRef.buildContext!.l10n.andPress; + text = AppLocalizations.of(gameRef.buildContext!).andPress; } } @@ -356,7 +356,7 @@ class _EnterReturnTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = gameRef.buildContext!.l10n.enterReturn; + text = AppLocalizations.of(gameRef.buildContext!).enterReturn; } } @@ -371,6 +371,6 @@ class _ToSubmitTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = gameRef.buildContext!.l10n.toSubmit; + text = AppLocalizations.of(gameRef.buildContext!).toSubmit; } } diff --git a/lib/game/components/game_flow_controller.dart b/lib/game/components/game_flow_controller.dart index f99715cf..185b1135 100644 --- a/lib/game/components/game_flow_controller.dart +++ b/lib/game/components/game_flow_controller.dart @@ -1,7 +1,6 @@ import 'package:flame/components.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:pinball/game/game.dart'; -import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; /// {@template game_flow_controller} diff --git a/packages/pinball_components/test/src/components/golden/backboard/waiting.png b/packages/pinball_components/test/src/components/golden/backboard/waiting.png deleted file mode 100644 index 00164289..00000000 Binary files a/packages/pinball_components/test/src/components/golden/backboard/waiting.png and /dev/null differ diff --git a/test/game/components/backboard/backboard_test.dart b/test/game/components/backboard/backboard_test.dart new file mode 100644 index 00000000..e6b9ace4 --- /dev/null +++ b/test/game/components/backboard/backboard_test.dart @@ -0,0 +1,76 @@ +// ignore_for_file: cascade_invocations + +import 'package:flame/components.dart'; +import 'package:flame_test/flame_test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_theme/pinball_theme.dart' as theme; + +import '../../../helpers/helpers.dart'; + +void main() { + group('Backboard', () { + final characterIconPath = theme.Assets.images.dash.leaderboardIcon.keyName; + final assets = [ + characterIconPath, + Assets.images.backboard.marquee.keyName, + Assets.images.backboard.displayDivider.keyName, + ]; + final tester = FlameTester(() => EmptyPinballTestGame(assets: assets)); + + tester.test( + 'loads correctly', + (game) async { + final backboard = Backboard(); + await game.ensureAdd(backboard); + + expect(game.children, contains(backboard)); + }, + ); + + group('renders correctly', () { + tester.testGameWidget( + 'empty', + setUp: (game, tester) async { + await game.images.loadAll(assets); + game.camera.zoom = 6; + game.camera.followVector2(Vector2(0, -130)); + await game.ensureAdd(Backboard()); + await game.ready(); + await tester.pump(); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('../golden/backboard/empty.png'), + ); + }, + ); + + tester.testGameWidget( + 'on initialsInput', + setUp: (game, tester) async { + await game.mounted; + await game.images.loadAll(assets); + game.camera.zoom = 2; + game.camera.followVector2(Vector2.zero()); + final backboard = Backboard(); + await game.ensureAdd(backboard); + await backboard.initialsInput( + score: 1000, + characterIconPath: characterIconPath, + onSubmit: (_) {}, + ); + await tester.pump(); + }, + verify: (game, tester) async { + await expectLater( + find.byGame(), + matchesGoldenFile('../golden/backboard/initials-input.png'), + ); + }, + ); + }); + }); +} diff --git a/test/game/components/backboard/displays/initials_input_display_test.dart b/test/game/components/backboard/displays/initials_input_display_test.dart new file mode 100644 index 00000000..b7e07f35 --- /dev/null +++ b/test/game/components/backboard/displays/initials_input_display_test.dart @@ -0,0 +1,139 @@ +// ignore_for_file: cascade_invocations + +import 'package:flame_test/flame_test.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pinball/game/components/backboard/displays/displays.dart'; +import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_theme/pinball_theme.dart' as theme; + +import '../../../../helpers/helpers.dart'; + +void main() { + group('InitialsInputDisplay', () { + final characterIconPath = theme.Assets.images.dash.leaderboardIcon.keyName; + final assets = [ + characterIconPath, + Assets.images.backboard.displayDivider.keyName, + ]; + final tester = FlameTester(() => EmptyPinballTestGame(assets: assets)); + + tester.testGameWidget( + 'changes the initials with arrow keys', + setUp: (game, tester) async { + final initialsInputDisplay = InitialsInputDisplay( + score: 1000, + characterIconPath: characterIconPath, + onSubmit: (_) {}, + ); + await game.ensureAdd(initialsInputDisplay); + + // Focus is already on the first letter + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.pump(); + + // Move to the next an press up again + await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight); + await tester.pump(); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.pump(); + + // One more time + await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight); + await tester.pump(); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.pump(); + + // Back to the previous and increase one more + await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft); + await tester.pump(); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.pump(); + }, + verify: (game, tester) async { + final initialsInputDisplay = + game.descendants().whereType().first; + + expect(initialsInputDisplay.initials, equals('BCB')); + }, + ); + + String? submitedInitials; + tester.testGameWidget( + 'enter submits the initials', + setUp: (game, tester) async { + final initialsInputDisplay = InitialsInputDisplay( + score: 1000, + characterIconPath: characterIconPath, + onSubmit: (value) { + submitedInitials = value; + }, + ); + await game.ensureAdd(initialsInputDisplay); + + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.pump(); + }, + verify: (game, tester) async { + expect(submitedInitials, equals('AAA')); + }, + ); + }); +} + + // group('BackboardLetterPrompt', () { + // final tester = FlameTester(KeyboardTestGame.new); + + // tester.testGameWidget( + // 'cycles the char up and down when it has focus', + // setUp: (game, tester) async { + // await game.ensureAdd( + // BackboardLetterPrompt(hasFocus: true, position: Vector2.zero()), + // ); + // await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + // await tester.pump(); + // await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + // await tester.pump(); + // await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + // await tester.pump(); + // await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + // await tester.pump(); + // }, + // verify: (game, tester) async { + // final prompt = game.firstChild(); + // expect(prompt?.char, equals('C')); + // }, + // ); + + // tester.testGameWidget( + // "does nothing when it doesn't have focus", + // setUp: (game, tester) async { + // await game.ensureAdd( + // BackboardLetterPrompt(position: Vector2.zero()), + // ); + // await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + // await tester.pump(); + // }, + // verify: (game, tester) async { + // final prompt = game.firstChild(); + // expect(prompt?.char, equals('A')); + // }, + // ); + + // tester.testGameWidget( + // 'blinks the prompt when it has the focus', + // setUp: (game, tester) async { + // await game.ensureAdd( + // BackboardLetterPrompt(position: Vector2.zero(), hasFocus: true), + // ); + // }, + // verify: (game, tester) async { + // final underscore = game.descendants().whereType().first; + // expect(underscore.paint.color, Colors.white); + + // game.update(2); + // expect(underscore.paint.color, Colors.transparent); + // }, + // ); + // }); + diff --git a/test/game/components/camera_controller_test.dart b/test/game/components/camera_controller_test.dart index 6af3f594..b2a51cda 100644 --- a/test/game/components/camera_controller_test.dart +++ b/test/game/components/camera_controller_test.dart @@ -24,15 +24,18 @@ void main() { test('correctly calculates the zooms', () async { expect(controller.gameFocus.zoom.toInt(), equals(12)); - expect(controller.backboardFocus.zoom.toInt(), equals(11)); + expect(controller.waitingBackboardFocus.zoom.toInt(), equals(11)); }); test('correctly sets the initial zoom and position', () async { - expect(game.camera.zoom, equals(controller.backboardFocus.zoom)); - expect(game.camera.follow, equals(controller.backboardFocus.position)); + expect(game.camera.zoom, equals(controller.waitingBackboardFocus.zoom)); + expect( + game.camera.follow, + equals(controller.waitingBackboardFocus.position), + ); }); - group('focusOnBoard', () { + group('focusOnGame', () { test('changes the zoom', () async { controller.focusOnGame(); @@ -57,18 +60,43 @@ void main() { }); }); - group('focusOnBackboard', () { + group('focusOnWaitingBackboard', () { test('changes the zoom', () async { - controller.focusOnBackboard(); + controller.focusOnWaitingBackboard(); await game.ready(); final zoom = game.firstChild(); expect(zoom, isNotNull); - expect(zoom?.value, equals(controller.backboardFocus.zoom)); + expect(zoom?.value, equals(controller.waitingBackboardFocus.zoom)); }); test('moves the camera after the zoom is completed', () async { - controller.focusOnBackboard(); + controller.focusOnWaitingBackboard(); + await game.ready(); + final cameraZoom = game.firstChild()!; + final future = cameraZoom.completed; + + game.update(10); + game.update(0); // Ensure that the component was removed + + await future; + + expect(game.camera.position, Vector2(-4.5, -109.8)); + }); + }); + + group('focusOnGameOverBackboard', () { + test('changes the zoom', () async { + controller.focusOnGameOverBackboard(); + + await game.ready(); + final zoom = game.firstChild(); + expect(zoom, isNotNull); + expect(zoom?.value, equals(controller.gameOverBackboardFocus.zoom)); + }); + + test('moves the camera after the zoom is completed', () async { + controller.focusOnGameOverBackboard(); await game.ready(); final cameraZoom = game.firstChild()!; final future = cameraZoom.completed; diff --git a/test/game/components/game_flow_controller_test.dart b/test/game/components/game_flow_controller_test.dart index ef93892c..a7a403d2 100644 --- a/test/game/components/game_flow_controller_test.dart +++ b/test/game/components/game_flow_controller_test.dart @@ -4,7 +4,6 @@ import 'package:flame/game.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; -import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_theme/pinball_theme.dart'; import '../../helpers/helpers.dart'; @@ -43,14 +42,13 @@ void main() { overlays = MockActiveOverlaysNotifier(); when( - () => backboard.gameOverMode( + () => backboard.initialsInput( score: any(named: 'score'), characterIconPath: any(named: 'characterIconPath'), onSubmit: any(named: 'onSubmit'), ), ).thenAnswer((_) async {}); - when(backboard.waitingMode).thenAnswer((_) async {}); - when(cameraController.focusOnBackboard).thenAnswer((_) async {}); + when(cameraController.focusOnWaitingBackboard).thenAnswer((_) async {}); when(cameraController.focusOnGame).thenAnswer((_) async {}); when(() => overlays.remove(any())).thenAnswer((_) => true); @@ -62,7 +60,7 @@ void main() { }); test( - 'changes the backboard and camera correctly when it is a game over', + 'changes the backboard and camera correctly when the game is over', () { gameFlowController.onNewState( GameState( @@ -74,13 +72,13 @@ void main() { ); verify( - () => backboard.gameOverMode( + () => backboard.initialsInput( score: 0, characterIconPath: any(named: 'characterIconPath'), onSubmit: any(named: 'onSubmit'), ), ).called(1); - verify(cameraController.focusOnBackboard).called(1); + verify(cameraController.focusOnWaitingBackboard).called(1); }, ); @@ -89,7 +87,6 @@ void main() { () { gameFlowController.onNewState(GameState.initial()); - verify(backboard.waitingMode).called(1); verify(cameraController.focusOnGame).called(1); verify(() => overlays.remove(PinballGame.playButtonOverlay)) .called(1); diff --git a/test/game/components/golden/backboard/empty.png b/test/game/components/golden/backboard/empty.png new file mode 100644 index 00000000..612c8ab6 Binary files /dev/null and b/test/game/components/golden/backboard/empty.png differ diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index b395020e..572fa91a 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -21,9 +21,8 @@ void main() { Assets.images.android.bumper.b.dimmed.keyName, Assets.images.android.bumper.cow.lit.keyName, Assets.images.android.bumper.cow.dimmed.keyName, - Assets.images.backboard.backboardScores.keyName, - Assets.images.backboard.backboardGameOver.keyName, - Assets.images.backboard.display.keyName, + Assets.images.backboard.marquee.keyName, + Assets.images.backboard.displayDivider.keyName, Assets.images.boardBackground.keyName, Assets.images.ball.ball.keyName, Assets.images.ball.flameEffect.keyName,