test: localization mocking

pull/319/head
Allison Ryan 3 years ago
parent fabba96a39
commit edde7899dd

@ -4,7 +4,7 @@ import 'dart:math';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.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_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
import 'package:pinball_ui/pinball_ui.dart'; import 'package:pinball_ui/pinball_ui.dart';
@ -57,7 +57,7 @@ class InitialsInputDisplay extends Component with HasGameRef {
Future<void> onLoad() async { Future<void> onLoad() async {
for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) {
await add( await add(
_InitialsLetterPrompt( InitialsLetterPrompt(
position: Vector2( position: Vector2(
11.4 + (2.3 * i), 11.4 + (2.3 * i),
-20, -20,
@ -80,7 +80,7 @@ class InitialsInputDisplay extends Component with HasGameRef {
/// Returns the current inputed initials /// Returns the current inputed initials
String get initials => children String get initials => children
.whereType<_InitialsLetterPrompt>() .whereType<InitialsLetterPrompt>()
.map((prompt) => prompt.char) .map((prompt) => prompt.char)
.join(); .join();
@ -90,7 +90,7 @@ class InitialsInputDisplay extends Component with HasGameRef {
} }
bool _movePrompt(bool left) { bool _movePrompt(bool left) {
final prompts = children.whereType<_InitialsLetterPrompt>().toList(); final prompts = children.whereType<InitialsLetterPrompt>().toList();
final current = prompts.firstWhere((prompt) => prompt.hasFocus) final current = prompts.firstWhere((prompt) => prompt.hasFocus)
..hasFocus = false; ..hasFocus = false;
@ -103,7 +103,8 @@ class InitialsInputDisplay extends Component with HasGameRef {
} }
} }
class _ScoreLabelTextComponent extends TextComponent with HasGameRef { class _ScoreLabelTextComponent extends TextComponent
with HasGameRef<PinballGame> {
_ScoreLabelTextComponent() _ScoreLabelTextComponent()
: super( : super(
anchor: Anchor.centerLeft, anchor: Anchor.centerLeft,
@ -118,7 +119,7 @@ class _ScoreLabelTextComponent extends TextComponent with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); 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<PinballGame> {
_NameLabelTextComponent() _NameLabelTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -147,7 +149,7 @@ class _NameLabelTextComponent extends TextComponent with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); 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 { /// {@template initials_input_display}
_InitialsLetterPrompt({ /// 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, required Vector2 position,
bool hasFocus = false, bool hasFocus = false,
}) : _hasFocus = hasFocus, }) : _hasFocus = hasFocus,
@ -292,7 +299,8 @@ class _InstructionsComponent extends PositionComponent with HasGameRef {
); );
} }
class _EnterInitialsTextComponent extends TextComponent with HasGameRef { class _EnterInitialsTextComponent extends TextComponent
with HasGameRef<PinballGame> {
_EnterInitialsTextComponent() _EnterInitialsTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -303,11 +311,11 @@ class _EnterInitialsTextComponent extends TextComponent with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); 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<PinballGame> {
_ArrowsTextComponent() _ArrowsTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -322,11 +330,12 @@ class _ArrowsTextComponent extends TextComponent with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); 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<PinballGame> {
_AndPressTextComponent() _AndPressTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -337,11 +346,12 @@ class _AndPressTextComponent extends TextComponent with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); 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<PinballGame> {
_EnterReturnTextComponent() _EnterReturnTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -356,11 +366,12 @@ class _EnterReturnTextComponent extends TextComponent with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); 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<PinballGame> {
_ToSubmitTextComponent() _ToSubmitTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -371,6 +382,6 @@ class _ToSubmitTextComponent extends TextComponent with HasGameRef {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
text = AppLocalizations.of(gameRef.buildContext!).toSubmit; text = gameRef.l10n.toSubmit;
} }
} }

@ -9,6 +9,7 @@ import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart';
import 'package:pinball_audio/pinball_audio.dart'; import 'package:pinball_audio/pinball_audio.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart'; import 'package:pinball_flame/pinball_flame.dart';
@ -23,6 +24,7 @@ class PinballGame extends Forge2DGame
PinballGame({ PinballGame({
required this.characterTheme, required this.characterTheme,
required this.audio, required this.audio,
required this.l10n,
}) { }) {
images.prefix = ''; images.prefix = '';
controller = _GameBallsController(this); controller = _GameBallsController(this);
@ -38,6 +40,8 @@ class PinballGame extends Forge2DGame
final PinballAudio audio; final PinballAudio audio;
final AppLocalizations l10n;
late final GameFlowController gameFlowController; late final GameFlowController gameFlowController;
@override @override
@ -175,9 +179,11 @@ class DebugPinballGame extends PinballGame with FPSCounter {
DebugPinballGame({ DebugPinballGame({
required CharacterTheme characterTheme, required CharacterTheme characterTheme,
required PinballAudio audio, required PinballAudio audio,
required AppLocalizations l10n,
}) : super( }) : super(
characterTheme: characterTheme, characterTheme: characterTheme,
audio: audio, audio: audio,
l10n: l10n,
) { ) {
controller = _DebugGameBallsController(this); controller = _DebugGameBallsController(this);
} }

@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart';
import 'package:pinball/select_character/select_character.dart'; import 'package:pinball/select_character/select_character.dart';
import 'package:pinball/start_game/start_game.dart'; import 'package:pinball/start_game/start_game.dart';
import 'package:pinball_audio/pinball_audio.dart'; import 'package:pinball_audio/pinball_audio.dart';
@ -37,8 +38,16 @@ class PinballGamePage extends StatelessWidget {
final pinballAudio = context.read<PinballAudio>(); final pinballAudio = context.read<PinballAudio>();
final game = isDebugMode final game = isDebugMode
? DebugPinballGame(characterTheme: characterTheme, audio: audio) ? DebugPinballGame(
: PinballGame(characterTheme: characterTheme, audio: audio); characterTheme: characterTheme,
audio: audio,
l10n: context.l10n,
)
: PinballGame(
characterTheme: characterTheme,
audio: audio,
l10n: context.l10n,
);
final loadables = [ final loadables = [
...game.preLoadAssets(), ...game.preLoadAssets(),

@ -3,6 +3,7 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_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/game/game.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_theme/pinball_theme.dart' as theme; 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<InitialsInputDisplay>(), isNotNull);
},
);
}); });
} }

@ -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));
},
);
});
}

@ -4,12 +4,16 @@ import 'dart:async';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart';
import 'package:pinball_audio/pinball_audio.dart'; import 'package:pinball_audio/pinball_audio.dart';
import 'package:pinball_theme/pinball_theme.dart'; import 'package:pinball_theme/pinball_theme.dart';
import 'helpers.dart'; import 'helpers.dart';
class _MockAppLocalizations extends Mock implements AppLocalizations {}
class TestGame extends Forge2DGame with FlameBloc { class TestGame extends Forge2DGame with FlameBloc {
TestGame() { TestGame() {
images.prefix = ''; images.prefix = '';
@ -21,10 +25,12 @@ class PinballTestGame extends PinballGame {
List<String>? assets, List<String>? assets,
PinballAudio? audio, PinballAudio? audio,
CharacterTheme? theme, CharacterTheme? theme,
AppLocalizations? l10n,
}) : _assets = assets, }) : _assets = assets,
super( super(
audio: audio ?? MockPinballAudio(), audio: audio ?? MockPinballAudio(),
characterTheme: theme ?? const DashTheme(), characterTheme: theme ?? const DashTheme(),
l10n: l10n ?? _MockAppLocalizations(),
); );
final List<String>? _assets; final List<String>? _assets;
@ -42,10 +48,12 @@ class DebugPinballTestGame extends DebugPinballGame {
List<String>? assets, List<String>? assets,
PinballAudio? audio, PinballAudio? audio,
CharacterTheme? theme, CharacterTheme? theme,
AppLocalizations? l10n,
}) : _assets = assets, }) : _assets = assets,
super( super(
audio: audio ?? MockPinballAudio(), audio: audio ?? MockPinballAudio(),
characterTheme: theme ?? const DashTheme(), characterTheme: theme ?? const DashTheme(),
l10n: l10n ?? _MockAppLocalizations(),
); );
final List<String>? _assets; final List<String>? _assets;
@ -64,10 +72,12 @@ class EmptyPinballTestGame extends PinballTestGame {
List<String>? assets, List<String>? assets,
PinballAudio? audio, PinballAudio? audio,
CharacterTheme? theme, CharacterTheme? theme,
AppLocalizations? l10n,
}) : super( }) : super(
assets: assets, assets: assets,
audio: audio, audio: audio,
theme: theme, theme: theme,
l10n: l10n ?? _MockAppLocalizations(),
); );
@override @override

Loading…
Cancel
Save