From edde7899ddab032317e0a6643f78f0a195af87b5 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Tue, 3 May 2022 13:47:13 -0500 Subject: [PATCH] test: localization mocking --- .../displays/initials_input_display.dart | 51 +++++++++------ lib/game/pinball_game.dart | 6 ++ lib/game/view/pinball_game_page.dart | 13 +++- .../game/components/backbox/backbox_test.dart | 17 +++++ .../displays/initials_input_display_test.dart | 64 +++++++++++++++++++ test/helpers/test_games.dart | 10 +++ 6 files changed, 139 insertions(+), 22 deletions(-) create mode 100644 test/game/components/backbox/displays/initials_input_display_test.dart diff --git a/lib/game/components/backbox/displays/initials_input_display.dart b/lib/game/components/backbox/displays/initials_input_display.dart index 8983524e..fd286d62 100644 --- a/lib/game/components/backbox/displays/initials_input_display.dart +++ b/lib/game/components/backbox/displays/initials_input_display.dart @@ -4,7 +4,7 @@ import 'dart:math'; import 'package:flame/components.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:pinball/l10n/l10n.dart'; +import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_ui/pinball_ui.dart'; @@ -57,7 +57,7 @@ class InitialsInputDisplay extends Component with HasGameRef { Future onLoad() async { for (var i = 0; i < 3; i++) { await add( - _InitialsLetterPrompt( + 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<_InitialsLetterPrompt>() + .whereType() .map((prompt) => prompt.char) .join(); @@ -90,7 +90,7 @@ class InitialsInputDisplay extends Component with HasGameRef { } bool _movePrompt(bool left) { - final prompts = children.whereType<_InitialsLetterPrompt>().toList(); + final prompts = children.whereType().toList(); final current = prompts.firstWhere((prompt) => prompt.hasFocus) ..hasFocus = false; @@ -103,7 +103,8 @@ class InitialsInputDisplay extends Component with HasGameRef { } } -class _ScoreLabelTextComponent extends TextComponent with HasGameRef { +class _ScoreLabelTextComponent extends TextComponent + with HasGameRef { _ScoreLabelTextComponent() : super( anchor: Anchor.centerLeft, @@ -118,7 +119,7 @@ class _ScoreLabelTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = AppLocalizations.of(gameRef.buildContext!).score; + text = gameRef.l10n.score; } } @@ -132,7 +133,8 @@ class _ScoreTextComponent extends TextComponent { ); } -class _NameLabelTextComponent extends TextComponent with HasGameRef { +class _NameLabelTextComponent extends TextComponent + with HasGameRef { _NameLabelTextComponent() : super( anchor: Anchor.center, @@ -147,7 +149,7 @@ class _NameLabelTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = AppLocalizations.of(gameRef.buildContext!).name; + text = gameRef.l10n.name; } } @@ -170,8 +172,13 @@ class _CharacterIconSpriteComponent extends SpriteComponent with HasGameRef { } } -class _InitialsLetterPrompt extends PositionComponent { - _InitialsLetterPrompt({ +/// {@template initials_input_display} +/// Display that handles the user input on the game over view. +/// {@endtemplate} +@visibleForTesting +class InitialsLetterPrompt extends PositionComponent { + /// {@macro initials_input_display} + InitialsLetterPrompt({ required Vector2 position, bool hasFocus = false, }) : _hasFocus = hasFocus, @@ -292,7 +299,8 @@ class _InstructionsComponent extends PositionComponent with HasGameRef { ); } -class _EnterInitialsTextComponent extends TextComponent with HasGameRef { +class _EnterInitialsTextComponent extends TextComponent + with HasGameRef { _EnterInitialsTextComponent() : super( anchor: Anchor.center, @@ -303,11 +311,11 @@ class _EnterInitialsTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = AppLocalizations.of(gameRef.buildContext!).enterInitials; + text = gameRef.l10n.enterInitials; } } -class _ArrowsTextComponent extends TextComponent with HasGameRef { +class _ArrowsTextComponent extends TextComponent with HasGameRef { _ArrowsTextComponent() : super( anchor: Anchor.center, @@ -322,11 +330,12 @@ class _ArrowsTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = AppLocalizations.of(gameRef.buildContext!).arrows; + text = gameRef.l10n.arrows; } } -class _AndPressTextComponent extends TextComponent with HasGameRef { +class _AndPressTextComponent extends TextComponent + with HasGameRef { _AndPressTextComponent() : super( anchor: Anchor.center, @@ -337,11 +346,12 @@ class _AndPressTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = AppLocalizations.of(gameRef.buildContext!).andPress; + text = gameRef.l10n.andPress; } } -class _EnterReturnTextComponent extends TextComponent with HasGameRef { +class _EnterReturnTextComponent extends TextComponent + with HasGameRef { _EnterReturnTextComponent() : super( anchor: Anchor.center, @@ -356,11 +366,12 @@ class _EnterReturnTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = AppLocalizations.of(gameRef.buildContext!).enterReturn; + text = gameRef.l10n.enterReturn; } } -class _ToSubmitTextComponent extends TextComponent with HasGameRef { +class _ToSubmitTextComponent extends TextComponent + with HasGameRef { _ToSubmitTextComponent() : super( anchor: Anchor.center, @@ -371,6 +382,6 @@ class _ToSubmitTextComponent extends TextComponent with HasGameRef { @override Future onLoad() async { await super.onLoad(); - text = AppLocalizations.of(gameRef.buildContext!).toSubmit; + text = gameRef.l10n.toSubmit; } } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index f4e1e9ee..bad2573d 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -9,6 +9,7 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball/l10n/l10n.dart'; import 'package:pinball_audio/pinball_audio.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; @@ -23,6 +24,7 @@ class PinballGame extends Forge2DGame PinballGame({ required this.characterTheme, required this.audio, + required this.l10n, }) { images.prefix = ''; controller = _GameBallsController(this); @@ -38,6 +40,8 @@ class PinballGame extends Forge2DGame final PinballAudio audio; + final AppLocalizations l10n; + late final GameFlowController gameFlowController; @override @@ -175,9 +179,11 @@ class DebugPinballGame extends PinballGame with FPSCounter { DebugPinballGame({ required CharacterTheme characterTheme, required PinballAudio audio, + required AppLocalizations l10n, }) : super( characterTheme: characterTheme, audio: audio, + l10n: l10n, ) { controller = _DebugGameBallsController(this); } diff --git a/lib/game/view/pinball_game_page.dart b/lib/game/view/pinball_game_page.dart index be11a15c..1b186790 100644 --- a/lib/game/view/pinball_game_page.dart +++ b/lib/game/view/pinball_game_page.dart @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball/l10n/l10n.dart'; import 'package:pinball/select_character/select_character.dart'; import 'package:pinball/start_game/start_game.dart'; import 'package:pinball_audio/pinball_audio.dart'; @@ -37,8 +38,16 @@ class PinballGamePage extends StatelessWidget { final pinballAudio = context.read(); final game = isDebugMode - ? DebugPinballGame(characterTheme: characterTheme, audio: audio) - : PinballGame(characterTheme: characterTheme, audio: audio); + ? DebugPinballGame( + characterTheme: characterTheme, + audio: audio, + l10n: context.l10n, + ) + : PinballGame( + characterTheme: characterTheme, + audio: audio, + l10n: context.l10n, + ); final loadables = [ ...game.preLoadAssets(), diff --git a/test/game/components/backbox/backbox_test.dart b/test/game/components/backbox/backbox_test.dart index b9ae714a..d760577b 100644 --- a/test/game/components/backbox/backbox_test.dart +++ b/test/game/components/backbox/backbox_test.dart @@ -3,6 +3,7 @@ import 'package:flame/components.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:pinball/game/components/backbox/displays/initials_input_display.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_theme/pinball_theme.dart' as theme; @@ -48,5 +49,21 @@ void main() { }, ); }); + + tester.test( + 'initialsInput adds InitialsInputDisplay', + (game) async { + final backbox = Backbox(); + await game.ensureAdd(backbox); + await backbox.initialsInput( + score: 0, + characterIconPath: characterIconPath, + onSubmit: (_) {}, + ); + await game.ready(); + + expect(backbox.firstChild(), isNotNull); + }, + ); }); } diff --git a/test/game/components/backbox/displays/initials_input_display_test.dart b/test/game/components/backbox/displays/initials_input_display_test.dart new file mode 100644 index 00000000..c4596376 --- /dev/null +++ b/test/game/components/backbox/displays/initials_input_display_test.dart @@ -0,0 +1,64 @@ +// ignore_for_file: cascade_invocations + +import 'package:flame_test/flame_test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:pinball/game/components/backbox/displays/initials_input_display.dart'; +import 'package:pinball/l10n/l10n.dart'; +import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_theme/pinball_theme.dart' as theme; + +import '../../../../helpers/helpers.dart'; + +class _MockAppLocalizations extends Mock implements AppLocalizations { + @override + String get score => ''; + + @override + String get name => ''; + + @override + String get enterInitials => ''; + + @override + String get arrows => ''; + + @override + String get andPress => ''; + + @override + String get enterReturn => ''; + + @override + String get toSubmit => ''; +} + +void main() { + group('InitialsInputDisplay', () { + final characterIconPath = theme.Assets.images.dash.leaderboardIcon.keyName; + final assets = [ + characterIconPath, + Assets.images.backbox.displayDivider.keyName, + ]; + final tester = FlameTester( + () => EmptyPinballTestGame( + assets: assets, + l10n: _MockAppLocalizations(), + ), + ); + + tester.test( + 'loads correctly', + (game) async { + final initialsInputDisplay = InitialsInputDisplay( + score: 0, + characterIconPath: characterIconPath, + onSubmit: (_) {}, + ); + await game.ensureAdd(initialsInputDisplay); + + expect(game.children, contains(initialsInputDisplay)); + }, + ); + }); +} diff --git a/test/helpers/test_games.dart b/test/helpers/test_games.dart index baa466b8..79193b1d 100644 --- a/test/helpers/test_games.dart +++ b/test/helpers/test_games.dart @@ -4,12 +4,16 @@ import 'dart:async'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball/l10n/l10n.dart'; import 'package:pinball_audio/pinball_audio.dart'; import 'package:pinball_theme/pinball_theme.dart'; import 'helpers.dart'; +class _MockAppLocalizations extends Mock implements AppLocalizations {} + class TestGame extends Forge2DGame with FlameBloc { TestGame() { images.prefix = ''; @@ -21,10 +25,12 @@ class PinballTestGame extends PinballGame { List? assets, PinballAudio? audio, CharacterTheme? theme, + AppLocalizations? l10n, }) : _assets = assets, super( audio: audio ?? MockPinballAudio(), characterTheme: theme ?? const DashTheme(), + l10n: l10n ?? _MockAppLocalizations(), ); final List? _assets; @@ -42,10 +48,12 @@ class DebugPinballTestGame extends DebugPinballGame { List? assets, PinballAudio? audio, CharacterTheme? theme, + AppLocalizations? l10n, }) : _assets = assets, super( audio: audio ?? MockPinballAudio(), characterTheme: theme ?? const DashTheme(), + l10n: l10n ?? _MockAppLocalizations(), ); final List? _assets; @@ -64,10 +72,12 @@ class EmptyPinballTestGame extends PinballTestGame { List? assets, PinballAudio? audio, CharacterTheme? theme, + AppLocalizations? l10n, }) : super( assets: assets, audio: audio, theme: theme, + l10n: l10n ?? _MockAppLocalizations(), ); @override