mirror of https://github.com/flutter/pinball.git
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.
132 lines
3.9 KiB
132 lines
3.9 KiB
3 years ago
|
// ignore_for_file: cascade_invocations
|
||
|
|
||
|
import 'package:bloc_test/bloc_test.dart';
|
||
|
import 'package:flame/components.dart';
|
||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||
|
import 'package:flame_test/flame_test.dart';
|
||
|
import 'package:flutter_test/flutter_test.dart';
|
||
|
import 'package:mocktail/mocktail.dart';
|
||
|
import 'package:pinball/game/behaviors/behaviors.dart';
|
||
|
import 'package:pinball/select_character/select_character.dart';
|
||
|
import 'package:pinball_components/pinball_components.dart';
|
||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||
|
import 'package:pinball_theme/pinball_theme.dart' as theme;
|
||
|
|
||
|
class _TestGame extends Forge2DGame {
|
||
|
@override
|
||
|
Future<void> onLoad() async {
|
||
|
images.prefix = '';
|
||
|
await images.loadAll([
|
||
|
theme.Assets.images.dash.ball.keyName,
|
||
|
theme.Assets.images.dino.ball.keyName,
|
||
3 years ago
|
theme.Assets.images.dash.background.keyName,
|
||
|
theme.Assets.images.dino.background.keyName,
|
||
3 years ago
|
]);
|
||
|
}
|
||
|
|
||
|
Future<void> pump(
|
||
|
List<Component> children, {
|
||
|
CharacterThemeCubit? characterThemeBloc,
|
||
|
}) async {
|
||
|
await ensureAdd(
|
||
|
FlameBlocProvider<CharacterThemeCubit, CharacterThemeState>.value(
|
||
|
value: characterThemeBloc ?? CharacterThemeCubit(),
|
||
|
children: children,
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class _MockBallCubit extends Mock implements BallCubit {}
|
||
|
|
||
3 years ago
|
class _MockArcadeBackgroundCubit extends Mock implements ArcadeBackgroundCubit {
|
||
|
}
|
||
|
|
||
3 years ago
|
void main() {
|
||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||
|
|
||
|
group(
|
||
3 years ago
|
'CharacterSelectionBehavior',
|
||
3 years ago
|
() {
|
||
|
final flameTester = FlameTester(_TestGame.new);
|
||
|
|
||
|
test('can be instantiated', () {
|
||
|
expect(
|
||
3 years ago
|
CharacterSelectionBehavior(),
|
||
|
isA<CharacterSelectionBehavior>(),
|
||
3 years ago
|
);
|
||
|
});
|
||
|
|
||
|
flameTester.test(
|
||
|
'loads',
|
||
|
(game) async {
|
||
3 years ago
|
final behavior = CharacterSelectionBehavior();
|
||
3 years ago
|
await game.pump([behavior]);
|
||
|
expect(game.descendants(), contains(behavior));
|
||
|
},
|
||
|
);
|
||
|
|
||
|
flameTester.test(
|
||
3 years ago
|
'onNewState calls onCharacterSelected on the arcade background bloc',
|
||
|
(game) async {
|
||
|
final arcadeBackgroundBloc = _MockArcadeBackgroundCubit();
|
||
|
whenListen(
|
||
|
arcadeBackgroundBloc,
|
||
|
const Stream<ArcadeBackgroundState>.empty(),
|
||
|
initialState: const ArcadeBackgroundState.initial(),
|
||
|
);
|
||
|
final arcadeBackground =
|
||
|
ArcadeBackground.test(bloc: arcadeBackgroundBloc);
|
||
|
final behavior = CharacterSelectionBehavior();
|
||
|
await game.pump([
|
||
|
arcadeBackground,
|
||
|
behavior,
|
||
|
ZCanvasComponent(),
|
||
|
Plunger.test(compressionDistance: 10),
|
||
|
Ball.test(),
|
||
|
]);
|
||
|
|
||
|
const dinoThemeState = CharacterThemeState(theme.DinoTheme());
|
||
|
behavior.onNewState(dinoThemeState);
|
||
|
await game.ready();
|
||
|
|
||
|
verify(
|
||
|
() => arcadeBackgroundBloc
|
||
|
.onCharacterSelected(dinoThemeState.characterTheme),
|
||
|
).called(1);
|
||
|
},
|
||
|
);
|
||
|
|
||
|
flameTester.test(
|
||
|
'onNewState calls onCharacterSelected on the ball bloc',
|
||
3 years ago
|
(game) async {
|
||
|
final ballBloc = _MockBallCubit();
|
||
|
whenListen(
|
||
|
ballBloc,
|
||
|
const Stream<BallState>.empty(),
|
||
|
initialState: const BallState.initial(),
|
||
|
);
|
||
|
final ball = Ball.test(bloc: ballBloc);
|
||
3 years ago
|
final behavior = CharacterSelectionBehavior();
|
||
3 years ago
|
await game.pump([
|
||
|
ball,
|
||
|
behavior,
|
||
|
ZCanvasComponent(),
|
||
|
Plunger.test(compressionDistance: 10),
|
||
3 years ago
|
ArcadeBackground.test(),
|
||
3 years ago
|
]);
|
||
|
|
||
|
const dinoThemeState = CharacterThemeState(theme.DinoTheme());
|
||
|
behavior.onNewState(dinoThemeState);
|
||
|
await game.ready();
|
||
|
|
||
3 years ago
|
verify(
|
||
|
() => ballBloc.onCharacterSelected(dinoThemeState.characterTheme),
|
||
|
).called(1);
|
||
3 years ago
|
},
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
}
|