test: fixed all changes with tests

pull/40/head
RuiAlonso 4 years ago
parent 8d408da582
commit 1117ee9f11

@ -48,13 +48,13 @@ class JetpackRamp extends Component with HasGameRef<PinballGame> {
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(15), rotation: radians(15),
) )
..initialPosition = position + Vector2(-11, 1) ..initialPosition = position + Vector2(-25.7, 25)
..layer = Layer.opening; ..layer = Layer.opening;
final rightOpening = JetpackRampOpening( final rightOpening = JetpackRampOpening(
orientation: RampOrientation.down, orientation: RampOrientation.down,
rotation: radians(-9), rotation: radians(-9),
) )
..initialPosition = position + Vector2(20.5, 3.4) ..initialPosition = position + Vector2(-10, 26.2)
..layer = Layer.opening; ..layer = Layer.opening;
await addAll([ await addAll([
@ -97,13 +97,11 @@ class JetpackRampOpening extends RampOpening {
RampOrientation get orientation => _orientation; RampOrientation get orientation => _orientation;
@override @override
Shape get shape { Shape get shape => PolygonShape()
final area = PolygonShape()..setAsBoxXY(_size.x, _size.y); ..setAsBox(
// TODO(alestiago): Use shape.rotate() once it's implemented. _size.x,
for (final vertex in area.vertices) { _size.y,
vertex.rotate(_rotation); initialPosition,
} _rotation,
);
return area;
}
} }

@ -42,7 +42,7 @@ class LauncherRamp extends Component with HasGameRef<PinballGame> {
..layer = _layer; ..layer = _layer;
final curvedPath = Pathway.arc( final curvedPath = Pathway.arc(
color: const Color.fromARGB(255, 251, 255, 0), color: const Color.fromARGB(255, 251, 255, 0),
center: position + Vector2(-28.8, -6), center: position + Vector2(-24, -6),
radius: _radius, radius: _radius,
angle: _angle, angle: _angle,
width: _width, width: _width,

@ -17,15 +17,22 @@ mixin Layered<T extends Forge2DGame> on BodyComponent<T> {
_layer = value; _layer = value;
if (!isLoaded) { if (!isLoaded) {
// TODO(alestiago): Use loaded.whenComplete once provided. // TODO(alestiago): Use loaded.whenComplete once provided.
mounted.whenComplete(() => layer = value); mounted.whenComplete(() {
layer = value;
_applyMaskbits();
});
} else { } else {
_applyMaskbits();
}
}
void _applyMaskbits() {
for (final fixture in body.fixtures) { for (final fixture in body.fixtures) {
fixture fixture
..filterData.categoryBits = layer.maskBits ..filterData.categoryBits = layer.maskBits
..filterData.maskBits = layer.maskBits; ..filterData.maskBits = layer.maskBits;
} }
} }
}
} }
/// Indicates the type of a layer. /// Indicates the type of a layer.

@ -1,6 +1,7 @@
// ignore_for_file: avoid_renaming_method_parameters // ignore_for_file: avoid_renaming_method_parameters
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
/// Indicates the orientation of a ramp entrance/exit. /// Indicates the orientation of a ramp entrance/exit.
@ -64,17 +65,18 @@ abstract class RampOpening extends BodyComponent with InitialPosition, Layered {
class RampOpeningBallContactCallback<Opening extends RampOpening> class RampOpeningBallContactCallback<Opening extends RampOpening>
extends ContactCallback<Ball, Opening> { extends ContactCallback<Ball, Opening> {
/// Collection of balls inside ramp pathway. /// Collection of balls inside ramp pathway.
final _ballsInside = <Ball>{}; @visibleForTesting
final ballsInside = <Ball>{};
@override @override
void begin(Ball ball, Opening opening, Contact _) { void begin(Ball ball, Opening opening, Contact _) {
late final Layer layer; late final Layer layer;
if (!_ballsInside.contains(ball)) { if (!ballsInside.contains(ball)) {
layer = opening.pathwayLayer; layer = opening.pathwayLayer;
_ballsInside.add(ball); ballsInside.add(ball);
} else { } else {
layer = Layer.board; layer = Layer.board;
_ballsInside.remove(ball); ballsInside.remove(ball);
} }
ball.layer = layer; ball.layer = layer;

@ -72,13 +72,13 @@ void main() {
); );
flameTester.test( flameTester.test(
'has default filter maskBits', 'has all as default filter maskBits',
(game) async { (game) async {
final ball = Ball(); final ball = Ball();
await game.ensureAdd(ball); await game.ensureAdd(ball);
final fixture = ball.body.fixtures[0]; final fixture = ball.body.fixtures[0];
expect(fixture.filterData.maskBits, equals(Layer.board.maskBits)); expect(fixture.filterData.maskBits, equals(Layer.all.maskBits));
}, },
); );
}); });
@ -164,7 +164,7 @@ void main() {
final fixture = ball.body.fixtures[0]; final fixture = ball.body.fixtures[0];
expect(fixture.filterData.categoryBits, equals(1)); expect(fixture.filterData.categoryBits, equals(1));
expect(fixture.filterData.maskBits, equals(Layer.board.maskBits)); expect(fixture.filterData.maskBits, equals(Layer.all.maskBits));
ball.layer = newLayer; ball.layer = newLayer;

@ -29,21 +29,6 @@ void main() {
}, },
); );
group('constructor', () {
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final ramp = JetpackRamp(
position: position,
);
await game.ensureAdd(ramp);
expect(ramp.position, equals(position));
},
);
});
group('children', () { group('children', () {
flameTester.test( flameTester.test(
'has only one Pathway.arc', 'has only one Pathway.arc',
@ -86,9 +71,8 @@ void main() {
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final ramp = JetpackRampOpening( final ramp = JetpackRampOpening(
position: position,
orientation: RampOrientation.down, orientation: RampOrientation.down,
); )..initialPosition = position;
await game.ready(); await game.ready();
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -96,13 +80,4 @@ void main() {
}, },
); );
}); });
group('JetpackRampOpeningBallContactCallback', () {
test('has no ball inside on creation', () {
expect(
JetpackRampOpeningBallContactCallback().ballsInside,
equals(<Ball>{}),
);
});
});
} }

@ -82,9 +82,8 @@ void main() {
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final ramp = LauncherRampOpening( final ramp = LauncherRampOpening(
position: position,
orientation: RampOrientation.down, orientation: RampOrientation.down,
); )..initialPosition = position;
await game.ready(); await game.ready();
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
@ -92,13 +91,4 @@ void main() {
}, },
); );
}); });
group('LauncherRampOpeningBallContactCallback', () {
test('has no ball inside on creation', () {
expect(
LauncherRampOpeningBallContactCallback().ballsInside,
equals(<Ball>{}),
);
});
});
} }

@ -1,50 +1,7 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockingjay/mockingjay.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import '../../helpers/helpers.dart';
class TestRampOpening extends RampOpening {
TestRampOpening({
required Vector2 position,
required RampOrientation orientation,
required Layer pathwayLayer,
required Layer openingLayer,
}) : _orientation = orientation,
super(
position: position,
pathwayLayer: pathwayLayer,
openingLayer: openingLayer,
);
final RampOrientation _orientation;
@override
RampOrientation get orientation => _orientation;
@override
Shape get shape => PolygonShape()
..set([
Vector2(0, 0),
Vector2(0, 1),
Vector2(1, 1),
Vector2(1, 0),
]);
}
class TestRampOpeningBallContactCallback
extends RampOpeningBallContactCallback<TestRampOpening> {
TestRampOpeningBallContactCallback() : super();
final _ballsInside = <Ball>{};
@override
Set get ballsInside => _ballsInside;
}
void main() { void main() {
group('Layer', () { group('Layer', () {
test('has four values', () { test('has four values', () {
@ -80,271 +37,4 @@ void main() {
expect(Layer.launcher.maskBits, equals(0x0005)); expect(Layer.launcher.maskBits, equals(0x0005));
}); });
}); });
group('RampOpening', () {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create);
flameTester.test(
'loads correctly',
(game) async {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
await game.ready();
await game.ensureAdd(ramp);
expect(game.contains(ramp), isTrue);
},
);
group('body', () {
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final ramp = TestRampOpening(
position: position,
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
await game.ensureAdd(ramp);
game.contains(ramp);
expect(ramp.body.position, position);
},
);
flameTester.test(
'is static',
(game) async {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
await game.ensureAdd(ramp);
expect(ramp.body.bodyType, equals(BodyType.static));
},
);
group('fixture', () {
const pathwayLayer = Layer.jetpack;
const openingLayer = Layer.opening;
flameTester.test(
'exists',
(game) async {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
openingLayer: openingLayer,
);
await game.ensureAdd(ramp);
expect(ramp.body.fixtures[0], isA<Fixture>());
},
);
flameTester.test(
'shape is a polygon',
(game) async {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
openingLayer: openingLayer,
);
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures[0];
expect(fixture.shape.shapeType, equals(ShapeType.polygon));
},
);
flameTester.test(
'is sensor',
(game) async {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
openingLayer: openingLayer,
);
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures[0];
expect(fixture.isSensor, isTrue);
},
);
flameTester.test(
'sets filter categoryBits correctly',
(game) async {
final ramp = TestRampOpening(
position: Vector2.zero(),
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
openingLayer: openingLayer,
);
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures[0];
expect(
fixture.filterData.categoryBits,
equals(ramp.openingLayer.maskBits),
);
},
);
});
});
});
group('RampOpeningBallContactCallback', () {
test(
'a ball enters from bottom into a down oriented path and keeps inside, '
'is saved into collection and set maskBits to path', () {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback();
when(() => body.position).thenReturn(Vector2(0, 20));
when(() => ball.body).thenReturn(body);
expect(callback._ballsInside.isEmpty, isTrue);
callback.begin(ball, area, MockContact());
expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
verifyNever(() => ball.layer = Layer.board);
});
test(
'a ball enters from up into an up oriented path and keeps inside, '
'is saved into collection and set maskBits to path', () {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.up,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback();
when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body);
expect(callback._ballsInside.isEmpty, isTrue);
callback.begin(ball, area, MockContact());
expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
verifyNever(() => ball.layer = Layer.board);
});
test(
'a ball enters into a down oriented path but falls again outside, '
'is removed from collection and set maskBits to collide all', () {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback();
when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body);
expect(callback._ballsInside.isEmpty, isTrue);
callback.begin(ball, area, MockContact());
expect(callback._ballsInside.length, equals(1));
expect(callback._ballsInside.first, ball);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
verify(() => ball.layer = Layer.board);
});
test(
'a ball exits from inside a down oriented path, '
'is removed from collection and set maskBits to collide all', () {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback()
..ballsInside.add(ball);
when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body);
callback.begin(ball, area, MockContact());
expect(callback._ballsInside.isEmpty, isTrue);
verify(() => ball.layer = Layer.board).called(1);
callback.end(ball, area, MockContact());
verify(() => ball.layer = Layer.board).called(1);
});
test(
'a ball exits from inside an up oriented path, '
'is removed from collection and set maskBits to collide all', () {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
position: Vector2(0, 10),
orientation: RampOrientation.up,
pathwayLayer: Layer.jetpack,
openingLayer: Layer.board,
);
final callback = TestRampOpeningBallContactCallback()
..ballsInside.add(ball);
when(() => body.position).thenReturn(Vector2(0, 20));
when(() => ball.body).thenReturn(body);
callback.begin(ball, area, MockContact());
expect(callback._ballsInside.isEmpty, isTrue);
verify(() => ball.layer = Layer.board).called(1);
callback.end(ball, area, MockContact());
verify(() => ball.layer = Layer.board).called(1);
});
});
} }

@ -6,81 +6,83 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import '../../helpers/helpers.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(Forge2DGame.new); final flameTester = FlameTester(PinballGameTest.create);
group('Pathway', () { group('Pathway', () {
const width = 50.0; const width = 50.0;
group('straight', () { group('straight', () {
group('color', () {
flameTester.test( flameTester.test(
'has transparent color by default when no color is specified', 'loads correctly',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
expect(pathway.paint, isNotNull);
expect(
pathway.paint.color,
equals(Color.fromARGB(0, 0, 0, 0)),
);
}, },
); );
group('color', () {
flameTester.test( flameTester.test(
'has a color when is specified', 'has transparent color by default when no color is specified',
(game) async { (game) async {
await game.ready();
const defaultColor = Colors.blue;
final pathway = Pathway.straight( final pathway = Pathway.straight(
color: defaultColor,
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
expect(pathway.paint, isNotNull); expect(pathway.paint, isNotNull);
expect(pathway.paint.color.value, equals(defaultColor.value)); expect(
pathway.paint.color,
equals(Color.fromARGB(0, 0, 0, 0)),
);
}, },
); );
});
flameTester.test( flameTester.test(
'loads correctly', 'has a color when is specified',
(game) async { (game) async {
await game.ready(); const defaultColor = Colors.blue;
final pathway = Pathway.straight( final pathway = Pathway.straight(
color: defaultColor,
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
expect(pathway.paint, isNotNull);
expect(pathway.paint.color.value, equals(defaultColor.value));
}, },
); );
});
group('body', () { group('body', () {
flameTester.test( flameTester.test(
'is static', 'is static',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));
@ -92,13 +94,13 @@ void main() {
flameTester.test( flameTester.test(
'has only one ChainShape when singleWall is true', 'has only one ChainShape when singleWall is true',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
singleWall: true, singleWall: true,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.fixtures.length, 1); expect(pathway.body.fixtures.length, 1);
@ -111,12 +113,12 @@ void main() {
flameTester.test( flameTester.test(
'has two ChainShape when singleWall is false (default)', 'has two ChainShape when singleWall is false (default)',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.straight( final pathway = Pathway.straight(
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.fixtures.length, 2); expect(pathway.body.fixtures.length, 2);
@ -156,8 +158,7 @@ void main() {
start: Vector2(10, 10), start: Vector2(10, 10),
end: Vector2(20, 20), end: Vector2(20, 20),
width: width, width: width,
layer: layer, )..layer = layer;
);
await game.ready(); await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
@ -177,13 +178,13 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.arc( final pathway = Pathway.arc(
center: Vector2.zero(), center: Vector2.zero(),
width: width, width: width,
radius: 100, radius: 100,
angle: math.pi / 2, angle: math.pi / 2,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
@ -194,13 +195,13 @@ void main() {
flameTester.test( flameTester.test(
'is static', 'is static',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.arc( final pathway = Pathway.arc(
center: Vector2.zero(), center: Vector2.zero(),
width: width, width: width,
radius: 100, radius: 100,
angle: math.pi / 2, angle: math.pi / 2,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));
@ -220,11 +221,11 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.bezierCurve( final pathway = Pathway.bezierCurve(
controlPoints: controlPoints, controlPoints: controlPoints,
width: width, width: width,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(game.contains(pathway), isTrue); expect(game.contains(pathway), isTrue);
@ -235,11 +236,11 @@ void main() {
flameTester.test( flameTester.test(
'is static', 'is static',
(game) async { (game) async {
await game.ready();
final pathway = Pathway.bezierCurve( final pathway = Pathway.bezierCurve(
controlPoints: controlPoints, controlPoints: controlPoints,
width: width, width: width,
); );
await game.ready();
await game.ensureAdd(pathway); await game.ensureAdd(pathway);
expect(pathway.body.bodyType, equals(BodyType.static)); expect(pathway.body.bodyType, equals(BodyType.static));

@ -0,0 +1,338 @@
// ignore_for_file: cascade_invocations
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockingjay/mockingjay.dart';
import 'package:pinball/game/game.dart';
import '../../helpers/helpers.dart';
class TestRampOpening extends RampOpening {
TestRampOpening({
required RampOrientation orientation,
required Layer pathwayLayer,
}) : _orientation = orientation,
super(
pathwayLayer: pathwayLayer,
);
final RampOrientation _orientation;
@override
RampOrientation get orientation => _orientation;
@override
Shape get shape => PolygonShape()
..set([
Vector2(0, 0),
Vector2(0, 1),
Vector2(1, 1),
Vector2(1, 0),
]);
}
class TestRampOpeningBallContactCallback
extends RampOpeningBallContactCallback<TestRampOpening> {
TestRampOpeningBallContactCallback() : super();
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create);
group('RampOpening', () {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create);
flameTester.test(
'loads correctly',
(game) async {
final ramp = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
)
..initialPosition = Vector2.zero()
..layer = Layer.board;
await game.ready();
await game.ensureAdd(ramp);
expect(game.contains(ramp), isTrue);
},
);
group('body', () {
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final ramp = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
)
..initialPosition = position
..layer = Layer.board;
await game.ensureAdd(ramp);
game.contains(ramp);
expect(ramp.body.position, position);
},
);
flameTester.test(
'is static',
(game) async {
final ramp = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
)
..initialPosition = Vector2.zero()
..layer = Layer.board;
await game.ensureAdd(ramp);
expect(ramp.body.bodyType, equals(BodyType.static));
},
);
group('fixture', () {
const pathwayLayer = Layer.jetpack;
const openingLayer = Layer.opening;
flameTester.test(
'exists',
(game) async {
final ramp = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
)
..initialPosition = Vector2.zero()
..layer = openingLayer;
await game.ensureAdd(ramp);
expect(ramp.body.fixtures[0], isA<Fixture>());
},
);
flameTester.test(
'shape is a polygon',
(game) async {
final ramp = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
)
..initialPosition = Vector2.zero()
..layer = openingLayer;
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures[0];
expect(fixture.shape.shapeType, equals(ShapeType.polygon));
},
);
flameTester.test(
'is sensor',
(game) async {
final ramp = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
)
..initialPosition = Vector2.zero()
..layer = openingLayer;
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures[0];
expect(fixture.isSensor, isTrue);
},
);
flameTester.test(
'sets filter categoryBits correctly',
(game) async {
final ramp = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: pathwayLayer,
)
..initialPosition = Vector2.zero()
..layer = openingLayer;
await game.ready();
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures[0];
expect(
fixture.filterData.categoryBits,
equals(ramp.layer.maskBits),
);
},
);
});
});
});
group('RampOpeningBallContactCallback', () {
test('has no ball inside on creation', () {
expect(
RampOpeningBallContactCallback<TestRampOpening>().ballsInside,
equals(<Ball>{}),
);
});
flameTester.test(
'a ball enters from bottom into a down oriented path and keeps inside, '
'is saved into collection and set maskBits to path', (game) async {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
)
..initialPosition = Vector2(0, 10)
..layer = Layer.board;
final callback = TestRampOpeningBallContactCallback();
when(() => ball.body).thenReturn(body);
when(() => body.position).thenReturn(Vector2(0, 20));
await game.ready();
await game.ensureAdd(area);
expect(callback.ballsInside.isEmpty, isTrue);
callback.begin(ball, area, MockContact());
expect(callback.ballsInside.length, equals(1));
expect(callback.ballsInside.first, ball);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
verifyNever(() => ball.layer = Layer.board);
});
flameTester.test(
'a ball enters from up into an up oriented path and keeps inside, '
'is saved into collection and set maskBits to path', (game) async {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
orientation: RampOrientation.up,
pathwayLayer: Layer.jetpack,
)
..initialPosition = Vector2(0, 10)
..layer = Layer.board;
final callback = TestRampOpeningBallContactCallback();
when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body);
await game.ready();
await game.ensureAdd(area);
expect(callback.ballsInside.isEmpty, isTrue);
callback.begin(ball, area, MockContact());
expect(callback.ballsInside.length, equals(1));
expect(callback.ballsInside.first, ball);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
verifyNever(() => ball.layer = Layer.board);
});
flameTester.test(
'a ball enters into a down oriented path but falls again outside, '
'is removed from collection and set maskBits to collide all',
(game) async {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
)
..initialPosition = Vector2(0, 10)
..layer = Layer.board;
final callback = TestRampOpeningBallContactCallback();
when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body);
await game.ready();
await game.ensureAdd(area);
expect(callback.ballsInside.isEmpty, isTrue);
callback.begin(ball, area, MockContact());
expect(callback.ballsInside.length, equals(1));
expect(callback.ballsInside.first, ball);
verify(() => ball.layer = Layer.jetpack).called(1);
callback.end(ball, area, MockContact());
verify(() => ball.layer = Layer.board);
});
flameTester.test(
'a ball exits from inside a down oriented path, '
'is removed from collection and set maskBits to collide all',
(game) async {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
orientation: RampOrientation.down,
pathwayLayer: Layer.jetpack,
)
..initialPosition = Vector2(0, 10)
..layer = Layer.board;
final callback = TestRampOpeningBallContactCallback()
..ballsInside.add(ball);
when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body);
await game.ready();
await game.ensureAdd(area);
callback.begin(ball, area, MockContact());
expect(callback.ballsInside.isEmpty, isTrue);
verify(() => ball.layer = Layer.board).called(1);
callback.end(ball, area, MockContact());
verify(() => ball.layer = Layer.board).called(1);
});
flameTester.test(
'a ball exits from inside an up oriented path, '
'is removed from collection and set maskBits to collide all',
(game) async {
final ball = MockBall();
final body = MockBody();
final area = TestRampOpening(
orientation: RampOrientation.up,
pathwayLayer: Layer.jetpack,
)
..initialPosition = Vector2(0, 10)
..layer = Layer.board;
final callback = TestRampOpeningBallContactCallback()
..ballsInside.add(ball);
when(() => ball.body).thenReturn(body);
when(() => body.position).thenReturn(Vector2(0, 20));
await game.ready();
await game.ensureAdd(area);
callback.begin(ball, area, MockContact());
expect(callback.ballsInside.isEmpty, isTrue);
verify(() => ball.layer = Layer.board).called(1);
callback.end(ball, area, MockContact());
verify(() => ball.layer = Layer.board).called(1);
});
});
}
Loading…
Cancel
Save