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: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<void> 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<InitialsLetterPrompt>()
.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<InitialsLetterPrompt>().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<PinballGame> {
_ScoreLabelTextComponent()
: super(
anchor: Anchor.centerLeft,
@ -118,7 +119,7 @@ class _ScoreLabelTextComponent extends TextComponent with HasGameRef {
@override
Future<void> 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<PinballGame> {
_NameLabelTextComponent()
: super(
anchor: Anchor.center,
@ -147,7 +149,7 @@ class _NameLabelTextComponent extends TextComponent with HasGameRef {
@override
Future<void> 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<PinballGame> {
_EnterInitialsTextComponent()
: super(
anchor: Anchor.center,
@ -303,11 +311,11 @@ class _EnterInitialsTextComponent extends TextComponent with HasGameRef {
@override
Future<void> 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<PinballGame> {
_ArrowsTextComponent()
: super(
anchor: Anchor.center,
@ -322,11 +330,12 @@ class _ArrowsTextComponent extends TextComponent with HasGameRef {
@override
Future<void> 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<PinballGame> {
_AndPressTextComponent()
: super(
anchor: Anchor.center,
@ -337,11 +346,12 @@ class _AndPressTextComponent extends TextComponent with HasGameRef {
@override
Future<void> 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<PinballGame> {
_EnterReturnTextComponent()
: super(
anchor: Anchor.center,
@ -356,11 +366,12 @@ class _EnterReturnTextComponent extends TextComponent with HasGameRef {
@override
Future<void> 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<PinballGame> {
_ToSubmitTextComponent()
: super(
anchor: Anchor.center,
@ -371,6 +382,6 @@ class _ToSubmitTextComponent extends TextComponent with HasGameRef {
@override
Future<void> onLoad() async {
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/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);
}

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

@ -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<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_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<String>? assets,
PinballAudio? audio,
CharacterTheme? theme,
AppLocalizations? l10n,
}) : _assets = assets,
super(
audio: audio ?? MockPinballAudio(),
characterTheme: theme ?? const DashTheme(),
l10n: l10n ?? _MockAppLocalizations(),
);
final List<String>? _assets;
@ -42,10 +48,12 @@ class DebugPinballTestGame extends DebugPinballGame {
List<String>? assets,
PinballAudio? audio,
CharacterTheme? theme,
AppLocalizations? l10n,
}) : _assets = assets,
super(
audio: audio ?? MockPinballAudio(),
characterTheme: theme ?? const DashTheme(),
l10n: l10n ?? _MockAppLocalizations(),
);
final List<String>? _assets;
@ -64,10 +72,12 @@ class EmptyPinballTestGame extends PinballTestGame {
List<String>? assets,
PinballAudio? audio,
CharacterTheme? theme,
AppLocalizations? l10n,
}) : super(
assets: assets,
audio: audio,
theme: theme,
l10n: l10n ?? _MockAppLocalizations(),
);
@override

Loading…
Cancel
Save