refactor: fixed test when removing children from spaceship test constructor

pull/296/head
RuiAlonso 3 years ago
parent 9e71d746fc
commit 6dd5c2c73f

@ -1 +1 @@
export 'ramp_contact_behavior.dart';
export 'ramp_ball_contact_behavior.dart';

@ -4,21 +4,20 @@ import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_contact_behavior}
/// {@template ramp_ball_contact_behavior}
/// Detects a [Ball]that enters in the [SpaceshipRamp].
///
/// The [Ball] can hit with sensor to recognize if [Ball] goes in or out the
/// [SpaceshipRamp].
/// {@endtemplate}
class RampContactBehavior extends ContactBehavior<RampScoringSensor> {
class RampBallContactBehavior extends ContactBehavior<RampScoringSensor> {
@override
void beginContact(Object other, Contact contact) {
super.beginContact(other, contact);
if (other is! Ball) return;
if (other.body.linearVelocity.y < 0) {
parent.parent.bloc.onInside();
parent.parent.bloc.onBallInside();
}
}
}

@ -8,7 +8,7 @@ part 'spaceship_ramp_state.dart';
class SpaceshipRampCubit extends Cubit<SpaceshipRampState> {
SpaceshipRampCubit() : super(const SpaceshipRampState.initial());
void onInside() {
void onBallInside() {
emit(
state.copyWith(
hits: state.hits + 1,

@ -24,7 +24,7 @@ class SpaceshipRamp extends Component {
// only one sensor.
RampScoringSensor(
children: [
RampContactBehavior(),
RampBallContactBehavior(),
],
)..initialPosition = Vector2(1.7, -20.4),
_SpaceshipRampOpening(
@ -57,8 +57,7 @@ class SpaceshipRamp extends Component {
@visibleForTesting
SpaceshipRamp.test({
required this.bloc,
Iterable<Component>? children,
}) : super(children: children);
}) : super();
// TODO(alestiago): Consider refactoring once the following is merged:
// https://github.com/flame-engine/flame/pull/1538

@ -36,12 +36,12 @@ void main() {
final flameTester = FlameTester(() => TestGame(assets));
group(
'RampContactBehavior',
'RampBallContactBehavior',
() {
test('can be instantiated', () {
expect(
RampContactBehavior(),
isA<RampContactBehavior>(),
RampBallContactBehavior(),
isA<RampBallContactBehavior>(),
);
});
@ -57,9 +57,9 @@ void main() {
});
flameTester.test(
"calls 'onInside' when a ball enters into the ramp",
"calls 'onBallInside' when a ball enters into the ramp",
(game) async {
final behavior = RampContactBehavior();
final behavior = RampBallContactBehavior();
final bloc = _MockSpaceshipRampCubit();
whenListen(
bloc,
@ -80,14 +80,14 @@ void main() {
behavior.beginContact(ball, _MockContact());
verify(bloc.onInside).called(1);
verify(bloc.onBallInside).called(1);
},
);
flameTester.test(
"doesn't call 'onInside' when a ball goes out the ramp",
"doesn't call 'onBallInside' when a ball goes out the ramp",
(game) async {
final behavior = RampContactBehavior();
final behavior = RampBallContactBehavior();
final bloc = _MockSpaceshipRampCubit();
whenListen(
bloc,
@ -108,7 +108,7 @@ void main() {
behavior.beginContact(ball, _MockContact());
verifyNever(bloc.onInside);
verifyNever(bloc.onBallInside);
},
);
});

@ -6,14 +6,14 @@ import 'package:pinball_components/pinball_components.dart';
void main() {
group('SpaceshipRampCubit', () {
group('onInside', () {
group('onBallInside', () {
blocTest<SpaceshipRampCubit, SpaceshipRampState>(
'emits hits incremented',
build: SpaceshipRampCubit.new,
act: (bloc) => bloc
..onInside()
..onInside()
..onInside(),
..onBallInside()
..onBallInside()
..onBallInside(),
expect: () => [
SpaceshipRampState(hits: 1),
SpaceshipRampState(hits: 2),

@ -6,6 +6,7 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
import '../../../helpers/helpers.dart';
@ -33,10 +34,184 @@ void main() {
(game) async {
final spaceshipRamp = SpaceshipRamp();
await game.ensureAdd(spaceshipRamp);
expect(game.descendants(), contains(spaceshipRamp));
expect(game.children, contains(spaceshipRamp));
},
);
group('renders correctly', () {
const goldenFilePath = '../golden/spaceship_ramp/';
final centerForSpaceshipRamp = Vector2(-13, -55);
flameTester.testGameWidget(
'inactive sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.inactive,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}inactive.png'),
);
},
);
flameTester.testGameWidget(
'active1 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component.progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active1,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active1.png'),
);
},
);
flameTester.testGameWidget(
'active2 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active2,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active2.png'),
);
},
);
flameTester.testGameWidget(
'active3 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active3,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active3.png'),
);
},
);
flameTester.testGameWidget(
'active4 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress()
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active4,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active4.png'),
);
},
);
flameTester.testGameWidget(
'active5 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress()
..progress()
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active5,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active5.png'),
);
},
);
});
flameTester.test('closes bloc when removed', (game) async {
final bloc = _MockSpaceshipRampCubit();
whenListen(
@ -60,11 +235,12 @@ void main() {
group('adds', () {
flameTester.test('new children', (game) async {
final component = Component();
final spaceshipRamp = SpaceshipRamp(
children: [component],
final ramp = SpaceshipRamp.test(
bloc: _MockSpaceshipRampCubit(),
);
await game.ensureAdd(spaceshipRamp);
expect(spaceshipRamp.children, contains(component));
await ramp.addAll([component]);
await game.ensureAdd(ramp);
expect(ramp.children, contains(component));
});
});
});

@ -1,244 +0,0 @@
// ignore_for_file: cascade_invocations
import 'package:bloc_test/bloc_test.dart';
import 'package:flame/components.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
import '../../helpers/helpers.dart';
class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final assets = [
Assets.images.android.ramp.boardOpening.keyName,
Assets.images.android.ramp.railingForeground.keyName,
Assets.images.android.ramp.railingBackground.keyName,
Assets.images.android.ramp.main.keyName,
Assets.images.android.ramp.arrow.inactive.keyName,
Assets.images.android.ramp.arrow.active1.keyName,
Assets.images.android.ramp.arrow.active2.keyName,
Assets.images.android.ramp.arrow.active3.keyName,
Assets.images.android.ramp.arrow.active4.keyName,
Assets.images.android.ramp.arrow.active5.keyName,
];
final flameTester = FlameTester(() => TestGame(assets));
group('SpaceshipRamp', () {
flameTester.test('loads correctly', (game) async {
final component = SpaceshipRamp();
await game.ensureAdd(component);
expect(game.contains(component), isTrue);
});
group('renders correctly', () {
const goldenFilePath = 'golden/spaceship_ramp/';
final centerForSpaceshipRamp = Vector2(-13, -55);
flameTester.testGameWidget(
'inactive sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.inactive,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}inactive.png'),
);
},
);
flameTester.testGameWidget(
'active1 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component.progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active1,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active1.png'),
);
},
);
flameTester.testGameWidget(
'active2 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active2,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active2.png'),
);
},
);
flameTester.testGameWidget(
'active3 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active3,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active3.png'),
);
},
);
flameTester.testGameWidget(
'active4 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress()
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active4,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active4.png'),
);
},
);
flameTester.testGameWidget(
'active5 sprite',
setUp: (game, tester) async {
await game.images.loadAll(assets);
final component = SpaceshipRamp();
final canvas = ZCanvasComponent(children: [component]);
await game.ensureAdd(canvas);
component
..progress()
..progress()
..progress()
..progress()
..progress();
await tester.pump();
expect(
component.children.whereType<SpriteGroupComponent>().first.current,
SpaceshipRampArrowSpriteState.active5,
);
game.camera.followVector2(centerForSpaceshipRamp);
},
verify: (game, tester) async {
await expectLater(
find.byGame<TestGame>(),
matchesGoldenFile('${goldenFilePath}active5.png'),
);
},
);
});
flameTester.test('closes bloc when removed', (game) async {
final bloc = _MockSpaceshipRampCubit();
whenListen(
bloc,
const Stream<SpaceshipRampState>.empty(),
initialState: const SpaceshipRampState.initial(),
);
when(bloc.close).thenAnswer((_) async {});
final ramp = SpaceshipRamp.test(
bloc: bloc,
);
await game.ensureAdd(ramp);
game.remove(ramp);
await game.ready();
verify(bloc.close).called(1);
});
group('adds', () {
flameTester.test('new children', (game) async {
final component = Component();
final ramp = SpaceshipRamp.test(
bloc: _MockSpaceshipRampCubit(),
children: [component],
);
await game.ensureAdd(ramp);
expect(ramp.children, contains(component));
});
});
});
}
Loading…
Cancel
Save