forked from flame-engine/flame
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Unit tests for Event Mixin - `DragCallbacks` - `TapCallbacks`
- Loading branch information
1 parent
475b226
commit 914cbc7
Showing
2 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
packages/flame/test/events/component_mixins/drag_callbacks_test.dart
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,157 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/experimental.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('DragCallbacks', () { | ||
testWithGame<_GameWithHasDraggableComponents>( | ||
'make sure they can be added to game with HasDraggableComponents', | ||
_GameWithHasDraggableComponents.new, | ||
(game) async { | ||
await game.add(_DragCallbacksComponent()); | ||
await game.ready(); | ||
}, | ||
); | ||
|
||
testWithFlameGame( | ||
'make sure DragCallbacks cannot be added to invalid games', | ||
(game) async { | ||
expect( | ||
() => game.ensureAdd(_DragCallbacksComponent()), | ||
failsAssert( | ||
'The components with DragCallbacks can only be added to a ' | ||
'FlameGame with ' | ||
'the HasDraggableComponents mixin', | ||
), | ||
); | ||
}, | ||
); | ||
|
||
testWithGame<_GameWithHasDraggableComponents>( | ||
'drag event start', | ||
_GameWithHasDraggableComponents.new, | ||
(game) async { | ||
final component = _DragCallbacksComponent() | ||
..x = 10 | ||
..y = 10 | ||
..width = 10 | ||
..height = 10; | ||
|
||
await game.ensureAdd(component); | ||
game.onDragStart( | ||
DragStartEvent( | ||
1, | ||
DragStartDetails( | ||
localPosition: const Offset(12, 12), | ||
globalPosition: const Offset(12, 12), | ||
), | ||
), | ||
); | ||
expect(component.dragStartEvent, 1); | ||
}, | ||
); | ||
|
||
testWithGame<_GameWithHasDraggableComponents>( | ||
'drag event start, update and cancel', | ||
_GameWithHasDraggableComponents.new, | ||
(game) async { | ||
final component = _DragCallbacksComponent() | ||
..x = 10 | ||
..y = 10 | ||
..width = 10 | ||
..height = 10; | ||
|
||
await game.ensureAdd(component); | ||
expect(component.dragStartEvent, 0); | ||
game.onDragStart( | ||
DragStartEvent( | ||
1, | ||
DragStartDetails( | ||
localPosition: const Offset(12, 12), | ||
globalPosition: const Offset(12, 12), | ||
), | ||
), | ||
); | ||
expect(component.dragStartEvent, 1); | ||
expect(component.dragUpdateEvent, 0); | ||
expect(component.dragEndEvent, 0); | ||
|
||
game.onDragUpdate( | ||
DragUpdateEvent( | ||
1, | ||
DragUpdateDetails( | ||
localPosition: const Offset(15, 15), | ||
globalPosition: const Offset(15, 15), | ||
), | ||
), | ||
); | ||
|
||
expect(component.dragUpdateEvent, 1); | ||
|
||
game.onDragEnd( | ||
DragEndEvent( | ||
1, | ||
DragEndDetails(), | ||
), | ||
); | ||
|
||
expect(component.dragEndEvent, 1); | ||
}, | ||
); | ||
|
||
testWithGame<_GameWithHasDraggableComponents>( | ||
'drag event update not called without onDragStart', | ||
_GameWithHasDraggableComponents.new, | ||
(game) async { | ||
final component = _DragCallbacksComponent() | ||
..x = 10 | ||
..y = 10 | ||
..width = 10 | ||
..height = 10; | ||
|
||
await game.ensureAdd(component); | ||
expect(component.dragStartEvent, 0); | ||
expect(component.dragUpdateEvent, 0); | ||
|
||
game.onDragUpdate( | ||
DragUpdateEvent( | ||
1, | ||
DragUpdateDetails( | ||
localPosition: const Offset(15, 15), | ||
globalPosition: const Offset(15, 15), | ||
), | ||
), | ||
); | ||
|
||
expect(component.dragUpdateEvent, 0); | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
class _GameWithHasDraggableComponents extends FlameGame | ||
with HasDraggableComponents {} | ||
|
||
class _DragCallbacksComponent extends PositionComponent with DragCallbacks { | ||
int dragStartEvent = 0; | ||
int dragUpdateEvent = 0; | ||
int dragEndEvent = 0; | ||
|
||
@override | ||
void onDragStart(DragStartEvent event) { | ||
dragStartEvent++; | ||
} | ||
|
||
@override | ||
void onDragUpdate(DragUpdateEvent event) { | ||
dragUpdateEvent++; | ||
} | ||
|
||
@override | ||
void onDragEnd(DragEndEvent event) { | ||
dragEndEvent++; | ||
} | ||
} |
173 changes: 173 additions & 0 deletions
173
packages/flame/test/events/component_mixins/tap_callbacks_test.dart
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,173 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/experimental.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('TapCallbacks', () { | ||
testWithGame<_GameWithHasTappableComponents>( | ||
'make sure they can be added to game with HasTappableComponents', | ||
_GameWithHasTappableComponents.new, | ||
(game) async { | ||
await game.add(_TapCallbacksComponent()); | ||
await game.ready(); | ||
}, | ||
); | ||
|
||
testWithFlameGame( | ||
'make sure TapCallbacks cannot be added to invalid games', | ||
(game) async { | ||
expect( | ||
() => game.ensureAdd(_TapCallbacksComponent()), | ||
failsAssert( | ||
'The components with TapCallbacks can only be added to a ' | ||
'FlameGame with ' | ||
'the HasTappableComponents mixin', | ||
), | ||
); | ||
}, | ||
); | ||
|
||
testWithGame<_GameWithHasTappableComponents>( | ||
'tap up, down event', | ||
_GameWithHasTappableComponents.new, | ||
(game) async { | ||
final component = _TapCallbacksComponent() | ||
..x = 10 | ||
..y = 10 | ||
..width = 10 | ||
..height = 10; | ||
|
||
await game.ensureAdd(component); | ||
|
||
// [onTapUp] will call, if there was an [onTapDown] event before | ||
game.onTapUp( | ||
TapUpEvent( | ||
1, | ||
TapUpDetails( | ||
kind: PointerDeviceKind.touch, | ||
localPosition: const Offset(12, 12), | ||
globalPosition: const Offset(12, 12), | ||
), | ||
), | ||
); | ||
expect(component.tapUpEvent, 0); | ||
|
||
game.onTapDown( | ||
TapDownEvent( | ||
1, | ||
TapDownDetails( | ||
kind: PointerDeviceKind.touch, | ||
localPosition: const Offset(12, 12), | ||
globalPosition: const Offset(12, 12), | ||
), | ||
), | ||
); | ||
expect(component.tapDownEvent, 1); | ||
|
||
// [onTapUp] will call, if there was an [onTapDown] event before | ||
game.onTapUp( | ||
TapUpEvent( | ||
1, | ||
TapUpDetails( | ||
kind: PointerDeviceKind.touch, | ||
localPosition: const Offset(12, 12), | ||
globalPosition: const Offset(12, 12), | ||
), | ||
), | ||
); | ||
expect(component.tapUpEvent, 1); | ||
|
||
// [onTapCancel] will call, when there was an [onTapDown] event | ||
// previously, but the [onTapUp] can no longer occur. | ||
game.onTapCancel( | ||
TapCancelEvent( | ||
1, | ||
), | ||
); | ||
expect(component.tapCancelEvent, 0); | ||
}, | ||
); | ||
|
||
testWithGame<_GameWithHasTappableComponents>( | ||
'longTapDown event', | ||
_GameWithHasTappableComponents.new, | ||
(game) async { | ||
final component = _TapCallbacksComponent() | ||
..x = 10 | ||
..y = 10 | ||
..width = 10 | ||
..height = 10; | ||
|
||
await game.ensureAdd(component); | ||
|
||
game.onTapDown( | ||
TapDownEvent( | ||
1, | ||
TapDownDetails( | ||
kind: PointerDeviceKind.touch, | ||
localPosition: const Offset(12, 12), | ||
globalPosition: const Offset(12, 12), | ||
), | ||
), | ||
); | ||
expect(component.tapDownEvent, 1); | ||
|
||
// [onLongTapDown] will call, if there was an [onTapDown] event before | ||
// ,and who remain at the point where the user is touching the screen. | ||
game.onLongTapDown( | ||
TapDownEvent( | ||
1, | ||
TapDownDetails( | ||
kind: PointerDeviceKind.touch, | ||
localPosition: const Offset(12, 12), | ||
globalPosition: const Offset(12, 12), | ||
), | ||
), | ||
); | ||
expect(component.longTapDownEvent, 1); | ||
|
||
// [onTapCancel] will call, when there was an [onTapDown] event | ||
// previously, but the [onTapUp] can no longer occur. | ||
game.onTapCancel( | ||
TapCancelEvent( | ||
1, | ||
), | ||
); | ||
expect(component.tapCancelEvent, 1); | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
class _GameWithHasTappableComponents extends FlameGame | ||
with HasTappableComponents {} | ||
|
||
class _TapCallbacksComponent extends PositionComponent with TapCallbacks { | ||
int tapDownEvent = 0; | ||
int longTapDownEvent = 0; | ||
int tapUpEvent = 0; | ||
int tapCancelEvent = 0; | ||
|
||
@override | ||
void onTapDown(TapDownEvent event) { | ||
tapDownEvent++; | ||
} | ||
|
||
@override | ||
void onLongTapDown(TapDownEvent event) { | ||
longTapDownEvent++; | ||
} | ||
|
||
@override | ||
void onTapUp(TapUpEvent event) { | ||
tapUpEvent++; | ||
} | ||
|
||
@override | ||
void onTapCancel(TapCancelEvent event) { | ||
tapCancelEvent++; | ||
} | ||
} |