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/controlled_flipper_test.dart

195 lines
5.8 KiB

import 'dart:collection';
import 'package:bloc_test/bloc_test.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/services.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 '../../helpers/helpers.dart';
class _MockGameBloc extends Mock implements GameBloc {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final assets = [
Assets.images.flipper.left.keyName,
Assets.images.flipper.right.keyName,
];
feat: migration to new `ContactCallbacks` (#234) * feat: migrated to new ContactCallbacks * refactor: moved renderBody to super call * feat: defined AlienBumper behaviours * feat: included ParentIsA mixin * refactor: modified ContactCallbacksGroup * feat: resolved missing ContactCallbacks with Behaviors * refactor: removed unused ParentIsA * refactor: moved tests * fix: invalid export * refactor: renamed behaviours * test: tested pinball_components * refactor: removed "Behaviour" typo * docs: included TODO comment for generics * docs: included flame_bloc TODO comments * refactor: renamed ContacCallbacksGroup variable * docs: included doc comments where possible * docs: rephrased ContactBehaviour doc comment * test: included ContactBehavior tests * feat: implemented FlutterForestBonusBehavior * refactor: fixed analyser warnings * test: tested DashNestBumper * refactor: moved children to last arguement * test: included closing test * refactor: used barrel files as imports * docs: included flutter_bloc TODO * test: correctly tested GoogleWordBonusBehavior * refactor: moved flutter_forest_test.dart * test: fixed AlienZone typo * feat: removed FlutterForestCubit * test: closed streams * refactor: removed optional bloc parameter * refactor: added flame_bloc TODO comment * docs: included .test constructor docs * feat: included GoogleLetter.test * test: made blink test pass * fix: renamed theme to CharacterTheme * refactor: moved timer.stop(); * refactor: renamed hasBonus to achievedBonus * refactor: ignore public_member_api_docs for cubits * test: removed beginContact group * refactor: typos correction * docs: used correct AlienBumper reference * docs: removed TODO comment from ContactBehavior subclasses * docs: includes ScoringBehavior doc * feat: adjusted FlutterForest priorities Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>
3 years ago
final flameTester = FlameTester(
() => EmptyPinballTestGame(assets: assets),
);
final flameBlocTester = FlameBlocTester<EmptyPinballTestGame, GameBloc>(
gameBuilder: EmptyPinballTestGame.new,
blocBuilder: () {
final bloc = _MockGameBloc();
const state = GameState(
totalScore: 0,
roundScore: 0,
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>
3 years ago
multiplier: 1,
rounds: 0,
bonusHistory: [],
);
whenListen(bloc, Stream.value(state), initialState: state);
return bloc;
},
assets: assets,
);
group('FlipperController', () {
group('onKeyEvent', () {
final leftKeys = UnmodifiableListView([
LogicalKeyboardKey.arrowLeft,
LogicalKeyboardKey.keyA,
]);
final rightKeys = UnmodifiableListView([
LogicalKeyboardKey.arrowRight,
LogicalKeyboardKey.keyD,
]);
group('and Flipper is left', () {
late Flipper flipper;
late FlipperController controller;
setUp(() {
flipper = Flipper(side: BoardSide.left);
controller = FlipperController(flipper);
flipper.add(controller);
});
testRawKeyDownEvents(leftKeys, (event) {
flameTester.test(
'moves upwards '
'when ${event.logicalKey.keyLabel} is pressed',
(game) async {
await game.ready();
await game.add(flipper);
controller.onKeyEvent(event, {});
expect(flipper.body.linearVelocity.y, isNegative);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
testRawKeyDownEvents(leftKeys, (event) {
flameBlocTester.testGameWidget(
'does nothing when is game over',
setUp: (game, tester) async {
await game.ensureAdd(flipper);
controller.onKeyEvent(event, {});
},
verify: (game, tester) async {
expect(flipper.body.linearVelocity.y, isZero);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
testRawKeyUpEvents(leftKeys, (event) {
flameTester.test(
'moves downwards '
'when ${event.logicalKey.keyLabel} is released',
(game) async {
await game.ready();
await game.add(flipper);
controller.onKeyEvent(event, {});
expect(flipper.body.linearVelocity.y, isPositive);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
testRawKeyUpEvents(rightKeys, (event) {
flameTester.test(
'does nothing '
'when ${event.logicalKey.keyLabel} is released',
(game) async {
await game.ready();
await game.add(flipper);
controller.onKeyEvent(event, {});
expect(flipper.body.linearVelocity.y, isZero);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
});
group('and Flipper is right', () {
late Flipper flipper;
late FlipperController controller;
setUp(() {
flipper = Flipper(side: BoardSide.right);
controller = FlipperController(flipper);
flipper.add(controller);
});
testRawKeyDownEvents(rightKeys, (event) {
flameTester.test(
'moves upwards '
'when ${event.logicalKey.keyLabel} is pressed',
(game) async {
await game.ready();
await game.add(flipper);
controller.onKeyEvent(event, {});
expect(flipper.body.linearVelocity.y, isNegative);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
testRawKeyUpEvents(rightKeys, (event) {
flameTester.test(
'moves downwards '
'when ${event.logicalKey.keyLabel} is released',
(game) async {
await game.ready();
await game.add(flipper);
controller.onKeyEvent(event, {});
expect(flipper.body.linearVelocity.y, isPositive);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
testRawKeyDownEvents(rightKeys, (event) {
flameBlocTester.testGameWidget(
'does nothing when is game over',
setUp: (game, tester) async {
await game.ensureAdd(flipper);
controller.onKeyEvent(event, {});
},
verify: (game, tester) async {
expect(flipper.body.linearVelocity.y, isZero);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
testRawKeyUpEvents(leftKeys, (event) {
flameTester.test(
'does nothing '
'when ${event.logicalKey.keyLabel} is released',
(game) async {
await game.ready();
await game.add(flipper);
controller.onKeyEvent(event, {});
expect(flipper.body.linearVelocity.y, isZero);
expect(flipper.body.linearVelocity.x, isZero);
},
);
});
});
});
});
}