You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pinball/test/game/components/game_flow_controller_test.dart

124 lines
3.7 KiB

// ignore_for_file: type_annotate_public_apis, prefer_const_constructors
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_audio/pinball_audio.dart';
import 'package:pinball_theme/pinball_theme.dart';
class _MockPinballGame extends Mock implements PinballGame {}
class _MockBackbox extends Mock implements Backbox {}
class _MockCameraController extends Mock implements CameraController {}
class _MockActiveOverlaysNotifier extends Mock
implements ActiveOverlaysNotifier {}
class _MockPinballAudio extends Mock implements PinballAudio {}
void main() {
group('GameFlowController', () {
group('listenWhen', () {
test('is true when the game over state has changed', () {
final state = GameState(
totalScore: 0,
roundScore: 10,
feat: game bloc multiplier (#213) * feat: added events for multiplier * feat: added events for increment, apply and reset multiplier * feat: added multiplier to game bloc state * test: test for multiplier at game bloc * test: added multiplier to game state * refactor: multiplier always increased by 1 * refactor: add multiplier state on BallLost * refactor: added round to game state and changed gameover and ball lost logic * test: fixed tests for game bloc * refactor: multiplier max value 6 at game bloc * test: fixed tests with new game over logic * chore: properties renamed and removed unused * Update lib/game/bloc/game_event.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * fix: pubspec from main * refactor: pubspec from main * chore: removed unused import * feat: ball added event to game bloc * test: fixed test for ball added * feat: added BallAdded event on ball mounted * test: fixing tests for BallAdded * test: flamebloctester for ball added * test: refactored tests for pinballgame * refactor: BallAdded event on ball mounted * chore: removed unnecessary imports * test: refactor tests for pinball_game * refactor: use rounds instead of balls * refactor: changed BallLost event with RoundLost, and moved part of the logic to controlled_ball * test: tests for RoundLost * fix: fixed wrong tests for pinball_game * test: remove deleted balls property from GameState * chore: doc Co-authored-by: Alejandro Santiago <dev@alestiago.com> Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>
2 years ago
multiplier: 1,
rounds: 0,
bonusHistory: const [],
);
final previous = GameState.initial();
expect(
GameFlowController(_MockPinballGame()).listenWhen(previous, state),
isTrue,
);
});
});
group('onNewState', () {
late PinballGame game;
late Backbox backbox;
late CameraController cameraController;
late GameFlowController gameFlowController;
late PinballAudio pinballAudio;
late ActiveOverlaysNotifier overlays;
setUp(() {
game = _MockPinballGame();
backbox = _MockBackbox();
cameraController = _MockCameraController();
gameFlowController = GameFlowController(game);
overlays = _MockActiveOverlaysNotifier();
pinballAudio = _MockPinballAudio();
when(
() => backbox.initialsInput(
score: any(named: 'score'),
characterIconPath: any(named: 'characterIconPath'),
onSubmit: any(named: 'onSubmit'),
),
).thenAnswer((_) async {});
when(cameraController.focusOnWaitingBackbox).thenAnswer((_) async {});
when(cameraController.focusOnGame).thenAnswer((_) async {});
when(() => overlays.remove(any())).thenAnswer((_) => true);
when(() => game.descendants().whereType<Backbox>())
.thenReturn([backbox]);
when(game.firstChild<CameraController>).thenReturn(cameraController);
when(() => game.overlays).thenReturn(overlays);
when(() => game.characterTheme).thenReturn(DashTheme());
when(() => game.audio).thenReturn(pinballAudio);
});
test(
'changes the backbox display and camera correctly '
'when the game is over',
() {
gameFlowController.onNewState(
GameState(
totalScore: 0,
roundScore: 10,
feat: game bloc multiplier (#213) * feat: added events for multiplier * feat: added events for increment, apply and reset multiplier * feat: added multiplier to game bloc state * test: test for multiplier at game bloc * test: added multiplier to game state * refactor: multiplier always increased by 1 * refactor: add multiplier state on BallLost * refactor: added round to game state and changed gameover and ball lost logic * test: fixed tests for game bloc * refactor: multiplier max value 6 at game bloc * test: fixed tests with new game over logic * chore: properties renamed and removed unused * Update lib/game/bloc/game_event.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * fix: pubspec from main * refactor: pubspec from main * chore: removed unused import * feat: ball added event to game bloc * test: fixed test for ball added * feat: added BallAdded event on ball mounted * test: fixing tests for BallAdded * test: flamebloctester for ball added * test: refactored tests for pinballgame * refactor: BallAdded event on ball mounted * chore: removed unnecessary imports * test: refactor tests for pinball_game * refactor: use rounds instead of balls * refactor: changed BallLost event with RoundLost, and moved part of the logic to controlled_ball * test: tests for RoundLost * fix: fixed wrong tests for pinball_game * test: remove deleted balls property from GameState * chore: doc Co-authored-by: Alejandro Santiago <dev@alestiago.com> Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>
2 years ago
multiplier: 1,
rounds: 0,
bonusHistory: const [],
),
);
verify(
() => backbox.initialsInput(
score: 0,
characterIconPath: any(named: 'characterIconPath'),
onSubmit: any(named: 'onSubmit'),
),
).called(1);
verify(cameraController.focusOnGameOverBackbox).called(1);
},
);
test(
'changes the backbox and camera correctly when it is not a game over',
() {
gameFlowController.onNewState(GameState.initial());
verify(cameraController.focusOnGame).called(1);
verify(() => overlays.remove(PinballGame.playButtonOverlay))
.called(1);
},
);
test(
'plays the background music on start',
() {
gameFlowController.onNewState(GameState.initial());
verify(pinballAudio.backgroundMusic).called(1);
},
);
});
});
}