mirror of https://github.com/flutter/pinball.git
parent
11c5a3b8d3
commit
801deb9ffa
@ -0,0 +1,122 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/behaviors/camera_focusing_behavior.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group(
|
||||
'CameraFocusingBehavior',
|
||||
() {
|
||||
final flameTester = FlameTester(
|
||||
EmptyPinballTestGame.new,
|
||||
);
|
||||
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
CameraFocusingBehavior(),
|
||||
isA<CameraFocusingBehavior>(),
|
||||
);
|
||||
});
|
||||
|
||||
flameTester.test('loads', (game) async {
|
||||
final behavior = CameraFocusingBehavior();
|
||||
await game.ensureAdd(behavior);
|
||||
expect(game.contains(behavior), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'changes focus when loaded',
|
||||
(game) async {
|
||||
final behavior = CameraFocusingBehavior();
|
||||
final previousZoom = game.camera.zoom;
|
||||
expect(game.camera.follow, isNull);
|
||||
|
||||
await game.ensureAdd(behavior);
|
||||
|
||||
expect(game.camera.follow, isNotNull);
|
||||
expect(game.camera.zoom, isNot(equals(previousZoom)));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'listenWhen only listens when status changes',
|
||||
(game) async {
|
||||
final behavior = CameraFocusingBehavior();
|
||||
const waiting = GameState.initial();
|
||||
final playing =
|
||||
const GameState.initial().copyWith(status: GameStatus.playing);
|
||||
final gameOver =
|
||||
const GameState.initial().copyWith(status: GameStatus.gameOver);
|
||||
|
||||
expect(behavior.listenWhen(waiting, waiting), isFalse);
|
||||
expect(behavior.listenWhen(waiting, playing), isTrue);
|
||||
expect(behavior.listenWhen(waiting, gameOver), isTrue);
|
||||
|
||||
expect(behavior.listenWhen(playing, playing), isFalse);
|
||||
expect(behavior.listenWhen(playing, waiting), isTrue);
|
||||
expect(behavior.listenWhen(playing, gameOver), isTrue);
|
||||
|
||||
expect(behavior.listenWhen(gameOver, gameOver), isFalse);
|
||||
expect(behavior.listenWhen(gameOver, waiting), isTrue);
|
||||
expect(behavior.listenWhen(gameOver, playing), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
group('onNewState', () {
|
||||
flameTester.test(
|
||||
'zooms when started playing',
|
||||
(game) async {
|
||||
final playing =
|
||||
const GameState.initial().copyWith(status: GameStatus.playing);
|
||||
|
||||
final behavior = CameraFocusingBehavior();
|
||||
await game.ensureAdd(behavior);
|
||||
|
||||
behavior.onNewState(playing);
|
||||
final previousPosition = game.camera.position.clone();
|
||||
await game.ready();
|
||||
|
||||
final zoom = behavior.children.whereType<CameraZoom>().single;
|
||||
game.update(zoom.controller.duration!);
|
||||
|
||||
expect(
|
||||
game.camera.position,
|
||||
isNot(equals(previousPosition)),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'zooms when lost',
|
||||
(game) async {
|
||||
final playing = const GameState.initial().copyWith(
|
||||
status: GameStatus.gameOver,
|
||||
);
|
||||
|
||||
final behavior = CameraFocusingBehavior();
|
||||
await game.ensureAdd(behavior);
|
||||
|
||||
behavior.onNewState(playing);
|
||||
final previousPosition = game.camera.position.clone();
|
||||
await game.ready();
|
||||
|
||||
final zoom = behavior.children.whereType<CameraZoom>().single;
|
||||
game.update(zoom.controller.duration!);
|
||||
|
||||
expect(
|
||||
game.camera.position,
|
||||
isNot(equals(previousPosition)),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
@ -1,113 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/components/camera_controller.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
void main() {
|
||||
group('CameraController', () {
|
||||
late FlameGame game;
|
||||
late CameraController controller;
|
||||
|
||||
setUp(() async {
|
||||
game = FlameGame()..onGameResize(Vector2(100, 200));
|
||||
|
||||
controller = CameraController(game);
|
||||
await game.ensureAdd(controller);
|
||||
});
|
||||
|
||||
test('loads correctly', () async {
|
||||
expect(game.firstChild<CameraController>(), isNotNull);
|
||||
});
|
||||
|
||||
test('correctly calculates the zooms', () async {
|
||||
expect(controller.gameFocus.zoom.toInt(), equals(12));
|
||||
expect(controller.waitingBackboxFocus.zoom.toInt(), equals(11));
|
||||
});
|
||||
|
||||
test('correctly sets the initial zoom and position', () async {
|
||||
expect(game.camera.zoom, equals(controller.waitingBackboxFocus.zoom));
|
||||
expect(
|
||||
game.camera.follow,
|
||||
equals(controller.waitingBackboxFocus.position),
|
||||
);
|
||||
});
|
||||
|
||||
group('focusOnGame', () {
|
||||
test('changes the zoom', () async {
|
||||
controller.focusOnGame();
|
||||
|
||||
await game.ready();
|
||||
final zoom = game.firstChild<CameraZoom>();
|
||||
expect(zoom, isNotNull);
|
||||
expect(zoom?.value, equals(controller.gameFocus.zoom));
|
||||
});
|
||||
|
||||
test('moves the camera after the zoom is completed', () async {
|
||||
controller.focusOnGame();
|
||||
await game.ready();
|
||||
final cameraZoom = game.firstChild<CameraZoom>()!;
|
||||
final future = cameraZoom.completed;
|
||||
|
||||
game.update(10);
|
||||
game.update(0); // Ensure that the component was removed
|
||||
|
||||
await future;
|
||||
|
||||
expect(game.camera.position, Vector2(-4, -120));
|
||||
});
|
||||
});
|
||||
|
||||
group('focusOnWaitingBackbox', () {
|
||||
test('changes the zoom', () async {
|
||||
controller.focusOnWaitingBackbox();
|
||||
|
||||
await game.ready();
|
||||
final zoom = game.firstChild<CameraZoom>();
|
||||
expect(zoom, isNotNull);
|
||||
expect(zoom?.value, equals(controller.waitingBackboxFocus.zoom));
|
||||
});
|
||||
|
||||
test('moves the camera after the zoom is completed', () async {
|
||||
controller.focusOnWaitingBackbox();
|
||||
await game.ready();
|
||||
final cameraZoom = game.firstChild<CameraZoom>()!;
|
||||
final future = cameraZoom.completed;
|
||||
|
||||
game.update(10);
|
||||
game.update(0); // Ensure that the component was removed
|
||||
|
||||
await future;
|
||||
|
||||
expect(game.camera.position, Vector2(-4.5, -121));
|
||||
});
|
||||
});
|
||||
|
||||
group('focusOnGameOverBackbox', () {
|
||||
test('changes the zoom', () async {
|
||||
controller.focusOnGameOverBackbox();
|
||||
|
||||
await game.ready();
|
||||
final zoom = game.firstChild<CameraZoom>();
|
||||
expect(zoom, isNotNull);
|
||||
expect(zoom?.value, equals(controller.gameOverBackboxFocus.zoom));
|
||||
});
|
||||
|
||||
test('moves the camera after the zoom is completed', () async {
|
||||
controller.focusOnGameOverBackbox();
|
||||
await game.ready();
|
||||
final cameraZoom = game.firstChild<CameraZoom>()!;
|
||||
final future = cameraZoom.completed;
|
||||
|
||||
game.update(10);
|
||||
game.update(0); // Ensure that the component was removed
|
||||
|
||||
await future;
|
||||
|
||||
expect(game.camera.position, Vector2(-2.5, -117));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue