This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests to camera debugger (#18)
- Loading branch information
1 parent
c95495d
commit f80e872
Showing
2 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import 'package:dash_run/game/game.dart'; | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/events.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
class _TestGame extends FlameGame with HasKeyboardHandlerComponents {} | ||
|
||
class _MockRawKeyUpEvent extends Mock implements RawKeyUpEvent { | ||
@override | ||
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { | ||
return ''; | ||
} | ||
} | ||
|
||
class _MockRawKeyDownEvent extends Mock implements RawKeyDownEvent { | ||
@override | ||
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { | ||
return ''; | ||
} | ||
} | ||
|
||
void main() { | ||
group('CameraDebugger', () { | ||
test('has the correct initial values', () { | ||
final cameraDebugger = CameraDebugger(); | ||
expect(cameraDebugger.size, equals(Vector2.all(150))); | ||
expect(cameraDebugger.paint.color.value, equals(Colors.pink.value)); | ||
expect(cameraDebugger.priority, equals(100)); | ||
}); | ||
|
||
testWithGame( | ||
'moves up when W is pressed', | ||
_TestGame.new, | ||
(game) async { | ||
final cameraDebugger = CameraDebugger(); | ||
await game.ensureAdd(cameraDebugger); | ||
|
||
final initialPosition = cameraDebugger.position.clone(); | ||
|
||
final controller = | ||
cameraDebugger.firstChild<KeyboardListenerComponent>()!; | ||
|
||
final keyDown = _MockRawKeyDownEvent(); | ||
when(() => keyDown.logicalKey).thenReturn(LogicalKeyboardKey.keyW); | ||
|
||
controller.onKeyEvent(keyDown, {LogicalKeyboardKey.keyW}); | ||
|
||
cameraDebugger.update(0.1); | ||
|
||
final updatedPosition = cameraDebugger.position.clone(); | ||
expect(updatedPosition.y, lessThan(initialPosition.y)); | ||
|
||
// Should not move anymore when the key is released | ||
final keyUp = _MockRawKeyUpEvent(); | ||
when(() => keyUp.logicalKey).thenReturn(LogicalKeyboardKey.keyW); | ||
|
||
controller.onKeyEvent(keyUp, {LogicalKeyboardKey.keyW}); | ||
cameraDebugger.update(0.1); | ||
|
||
final finalPosition = cameraDebugger.position.clone(); | ||
expect(finalPosition.y, equals(updatedPosition.y)); | ||
}, | ||
); | ||
|
||
testWithGame('moves down when S is pressed', _TestGame.new, (game) async { | ||
final cameraDebugger = CameraDebugger(); | ||
await game.ensureAdd(cameraDebugger); | ||
|
||
final initialPosition = cameraDebugger.position.clone(); | ||
|
||
final controller = | ||
cameraDebugger.firstChild<KeyboardListenerComponent>()!; | ||
|
||
final keyDown = _MockRawKeyDownEvent(); | ||
when(() => keyDown.logicalKey).thenReturn(LogicalKeyboardKey.keyS); | ||
|
||
controller.onKeyEvent(keyDown, {LogicalKeyboardKey.keyS}); | ||
|
||
cameraDebugger.update(0.1); | ||
|
||
final updatedPosition = cameraDebugger.position.clone(); | ||
expect(updatedPosition.y, greaterThan(initialPosition.y)); | ||
|
||
// Should not move anymore when the key is released | ||
final keyUp = _MockRawKeyUpEvent(); | ||
when(() => keyUp.logicalKey).thenReturn(LogicalKeyboardKey.keyS); | ||
|
||
controller.onKeyEvent(keyUp, {LogicalKeyboardKey.keyS}); | ||
cameraDebugger.update(0.1); | ||
|
||
final finalPosition = cameraDebugger.position.clone(); | ||
expect(finalPosition.y, equals(updatedPosition.y)); | ||
}); | ||
|
||
testWithGame('moves left when A is pressed', _TestGame.new, (game) async { | ||
final cameraDebugger = CameraDebugger(); | ||
await game.ensureAdd(cameraDebugger); | ||
|
||
final initialPosition = cameraDebugger.position.clone(); | ||
|
||
final controller = | ||
cameraDebugger.firstChild<KeyboardListenerComponent>()!; | ||
|
||
final keyDown = _MockRawKeyDownEvent(); | ||
when(() => keyDown.logicalKey).thenReturn(LogicalKeyboardKey.keyA); | ||
|
||
controller.onKeyEvent(keyDown, {LogicalKeyboardKey.keyA}); | ||
|
||
cameraDebugger.update(0.1); | ||
|
||
final updatedPosition = cameraDebugger.position.clone(); | ||
expect(updatedPosition.x, lessThan(initialPosition.x)); | ||
|
||
// Should not move anymore when the key is released | ||
final keyUp = _MockRawKeyUpEvent(); | ||
when(() => keyUp.logicalKey).thenReturn(LogicalKeyboardKey.keyA); | ||
|
||
controller.onKeyEvent(keyUp, {LogicalKeyboardKey.keyA}); | ||
cameraDebugger.update(0.1); | ||
|
||
final finalPosition = cameraDebugger.position.clone(); | ||
expect(finalPosition.x, equals(updatedPosition.x)); | ||
}); | ||
|
||
testWithGame('moves left when D is pressed', _TestGame.new, (game) async { | ||
final cameraDebugger = CameraDebugger(); | ||
await game.ensureAdd(cameraDebugger); | ||
|
||
final initialPosition = cameraDebugger.position.clone(); | ||
|
||
final controller = | ||
cameraDebugger.firstChild<KeyboardListenerComponent>()!; | ||
|
||
final keyDown = _MockRawKeyDownEvent(); | ||
when(() => keyDown.logicalKey).thenReturn(LogicalKeyboardKey.keyD); | ||
|
||
controller.onKeyEvent(keyDown, {LogicalKeyboardKey.keyD}); | ||
|
||
cameraDebugger.update(0.1); | ||
|
||
final updatedPosition = cameraDebugger.position.clone(); | ||
expect(updatedPosition.x, greaterThan(initialPosition.x)); | ||
|
||
// Should not move anymore when the key is released | ||
final keyUp = _MockRawKeyUpEvent(); | ||
when(() => keyUp.logicalKey).thenReturn(LogicalKeyboardKey.keyD); | ||
|
||
controller.onKeyEvent(keyUp, {LogicalKeyboardKey.keyD}); | ||
cameraDebugger.update(0.1); | ||
|
||
final finalPosition = cameraDebugger.position.clone(); | ||
expect(finalPosition.x, equals(updatedPosition.x)); | ||
}); | ||
|
||
testWithGame('increases speed when space is pressed', _TestGame.new, | ||
(game) async { | ||
final cameraDebugger = CameraDebugger(); | ||
await game.ensureAdd(cameraDebugger); | ||
|
||
final controller = | ||
cameraDebugger.firstChild<KeyboardListenerComponent>()!; | ||
|
||
final keyDown = _MockRawKeyDownEvent(); | ||
when(() => keyDown.logicalKey).thenReturn(LogicalKeyboardKey.space); | ||
|
||
controller.onKeyEvent(keyDown, {LogicalKeyboardKey.space}); | ||
|
||
cameraDebugger.update(0.1); | ||
|
||
expect(cameraDebugger.speed, equals(900)); | ||
|
||
// Should not move anymore when the key is released | ||
final keyUp = _MockRawKeyUpEvent(); | ||
when(() => keyUp.logicalKey).thenReturn(LogicalKeyboardKey.space); | ||
|
||
controller.onKeyEvent(keyUp, {LogicalKeyboardKey.space}); | ||
cameraDebugger.update(0.1); | ||
|
||
expect(cameraDebugger.speed, equals(300)); | ||
}); | ||
}); | ||
} |