feat: cancel subscription on ramp behavior remove

pull/296/head
RuiAlonso 3 years ago
parent 05d0d4a3ca
commit 04118891f5

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:pinball/game/behaviors/behaviors.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart';
@ -17,15 +18,28 @@ class RampBonusBehavior extends Component
}) : _points = points,
super();
/// Creates a [RampBonusBehavior].
///
/// This can be used for testing [RampBonusBehavior] in isolation.
@visibleForTesting
RampBonusBehavior.test({
required Points points,
required this.subscription,
}) : _points = points,
super();
final Points _points;
StreamSubscription? _subscription;
/// Subscription to [SpaceshipRampState] at [SpaceshipRamp].
@visibleForTesting
StreamSubscription? subscription;
@override
void onMount() {
super.onMount();
_subscription = parent.bloc.stream.listen((state) {
subscription = subscription ??
parent.bloc.stream.listen((state) {
final achievedOneMillionPoints = state.hits % 10 == 0;
if (achievedOneMillionPoints) {
@ -42,7 +56,7 @@ class RampBonusBehavior extends Component
@override
void onRemove() {
_subscription?.cancel();
subscription?.cancel();
super.onRemove();
}
}

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flutter/cupertino.dart';
import 'package:pinball/game/behaviors/behaviors.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart';
@ -17,15 +18,28 @@ class RampShotBehavior extends Component
}) : _points = points,
super();
/// Creates a [RampShotBehavior].
///
/// This can be used for testing [RampShotBehavior] in isolation.
@visibleForTesting
RampShotBehavior.test({
required Points points,
required this.subscription,
}) : _points = points,
super();
final Points _points;
StreamSubscription? _subscription;
/// Subscription to [SpaceshipRampState] at [SpaceshipRamp].
@visibleForTesting
StreamSubscription? subscription;
@override
void onMount() {
super.onMount();
_subscription = parent.bloc.stream.listen((state) {
subscription = subscription ??
parent.bloc.stream.listen((state) {
final achievedOneMillionPoints = state.hits % 10 == 0;
if (!achievedOneMillionPoints) {
@ -43,7 +57,7 @@ class RampShotBehavior extends Component
@override
void onRemove() {
_subscription?.cancel();
subscription?.cancel();
super.onRemove();
}
}

@ -1,6 +1,7 @@
// ignore_for_file: cascade_invocations, prefer_const_constructors
import 'dart:async';
import 'dart:io';
import 'package:bloc_test/bloc_test.dart';
import 'package:flame_test/flame_test.dart';
@ -18,6 +19,9 @@ class _MockGameBloc extends Mock implements GameBloc {}
class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {}
class _MockStreamSubscription extends Mock
implements StreamSubscription<SpaceshipRampState> {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final assets = [
@ -57,8 +61,7 @@ void main() {
);
flameBlocTester.testGameWidget(
'when hits are multiples of 10 times '
'add score and show score points',
'when hits are multiples of 10 times adds a ScoringBehavior',
setUp: (game, tester) async {
final bloc = _MockSpaceshipRampCubit();
final streamController = StreamController<SpaceshipRampState>();
@ -87,8 +90,7 @@ void main() {
);
flameBlocTester.testGameWidget(
'when hits are not multiple of 10 times '
"doesn't add score neither show score points",
"when hits are not multiple of 10 times doesn't add any ScoringBehavior",
setUp: (game, tester) async {
final bloc = _MockSpaceshipRampCubit();
final streamController = StreamController<SpaceshipRampState>();
@ -115,5 +117,37 @@ void main() {
expect(scores.length, 0);
},
);
flameBlocTester.testGameWidget(
'closes subscription when removed',
setUp: (game, tester) async {
final bloc = _MockSpaceshipRampCubit();
whenListen(
bloc,
const Stream<SpaceshipRampState>.empty(),
initialState: SpaceshipRampState.initial(),
);
when(bloc.close).thenAnswer((_) async {});
final subscription = _MockStreamSubscription();
when(subscription.cancel).thenAnswer((_) async {});
final behavior = RampBonusBehavior.test(
points: shotPoints,
subscription: subscription,
);
final parent = SpaceshipRamp.test(
bloc: bloc,
);
await game.ensureAdd(ZCanvasComponent(children: [parent]));
await parent.ensureAdd(behavior);
parent.remove(behavior);
await game.ready();
verify(subscription.cancel).called(1);
},
);
});
}

@ -18,6 +18,9 @@ class _MockGameBloc extends Mock implements GameBloc {}
class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {}
class _MockStreamSubscription extends Mock
implements StreamSubscription<SpaceshipRampState> {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final assets = [
@ -58,7 +61,7 @@ void main() {
flameBlocTester.testGameWidget(
'when hits are not multiple of 10 times '
'increase multiplier, add score and show score points',
'increases multiplier and adds a ScoringBehavior',
setUp: (game, tester) async {
final bloc = _MockSpaceshipRampCubit();
final streamController = StreamController<SpaceshipRampState>();
@ -89,7 +92,7 @@ void main() {
flameBlocTester.testGameWidget(
'when hits multiple of 10 times '
"doesn't increase multiplier, neither add score or show score points",
"doesn't increase multiplier, neither ScoringBehavior",
setUp: (game, tester) async {
final bloc = _MockSpaceshipRampCubit();
final streamController = StreamController<SpaceshipRampState>();
@ -117,5 +120,37 @@ void main() {
expect(scores.length, 0);
},
);
flameBlocTester.testGameWidget(
'closes subscription when removed',
setUp: (game, tester) async {
final bloc = _MockSpaceshipRampCubit();
whenListen(
bloc,
const Stream<SpaceshipRampState>.empty(),
initialState: SpaceshipRampState.initial(),
);
when(bloc.close).thenAnswer((_) async {});
final subscription = _MockStreamSubscription();
when(subscription.cancel).thenAnswer((_) async {});
final behavior = RampShotBehavior.test(
points: shotPoints,
subscription: subscription,
);
final parent = SpaceshipRamp.test(
bloc: bloc,
);
await game.ensureAdd(ZCanvasComponent(children: [parent]));
await parent.ensureAdd(behavior);
parent.remove(behavior);
await game.ready();
verify(subscription.cancel).called(1);
},
);
});
}

Loading…
Cancel
Save