-
-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
packages/flame/test/components/button_component_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,112 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/extensions.dart'; | ||
import 'package:flame/input.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import '../game/flame_game_test.dart'; | ||
|
||
void main() async { | ||
group('ButtonComponent', () { | ||
testWithGame<GameWithTappables>( | ||
'correctly registers taps', GameWithTappables.new, (game) async { | ||
var pressedTimes = 0; | ||
var releasedTimes = 0; | ||
final initialGameSize = Vector2.all(100); | ||
final componentSize = Vector2.all(10); | ||
final buttonPosition = Vector2.all(100); | ||
late final ButtonComponent button; | ||
game.onGameResize(initialGameSize); | ||
await game.ensureAdd( | ||
button = ButtonComponent( | ||
button: RectangleComponent(size: componentSize), | ||
onPressed: () => pressedTimes++, | ||
onReleased: () => releasedTimes++, | ||
position: buttonPosition, | ||
size: componentSize, | ||
), | ||
); | ||
|
||
expect(pressedTimes, 0); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapDown(1, createTapDownEvent(game)); | ||
expect(pressedTimes, 0); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapUp( | ||
1, | ||
createTapUpEvent( | ||
game, | ||
globalPosition: button.positionOfAnchor(Anchor.center).toOffset(), | ||
), | ||
); | ||
expect(pressedTimes, 0); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapDown( | ||
1, | ||
createTapDownEvent( | ||
game, | ||
globalPosition: buttonPosition.toOffset(), | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapUp( | ||
1, | ||
createTapUpEvent( | ||
game, | ||
globalPosition: buttonPosition.toOffset(), | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 1); | ||
}); | ||
|
||
testWithGame<GameWithTappables>( | ||
'correctly registers taps onGameResize', GameWithTappables.new, | ||
(game) async { | ||
var pressedTimes = 0; | ||
var releasedTimes = 0; | ||
final initialGameSize = Vector2.all(100); | ||
final componentSize = Vector2.all(10); | ||
final buttonPosition = Vector2.all(100); | ||
late final ButtonComponent button; | ||
game.onGameResize(initialGameSize); | ||
await game.ensureAdd( | ||
button = ButtonComponent( | ||
button: RectangleComponent(size: componentSize), | ||
onPressed: () => pressedTimes++, | ||
onReleased: () => releasedTimes++, | ||
position: buttonPosition, | ||
size: componentSize, | ||
), | ||
); | ||
final previousPosition = | ||
button.positionOfAnchor(Anchor.center).toOffset(); | ||
game.onGameResize(initialGameSize * 2); | ||
|
||
game.onTapDown( | ||
1, | ||
createTapDownEvent( | ||
game, | ||
globalPosition: previousPosition, | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 0); | ||
|
||
game.onTapUp( | ||
1, | ||
createTapUpEvent( | ||
game, | ||
globalPosition: previousPosition, | ||
), | ||
); | ||
expect(pressedTimes, 1); | ||
expect(releasedTimes, 1); | ||
}); | ||
}); | ||
} |