Skip to content

Commit

Permalink
test : Widget test for SpriteButtonComponent (flame-engine#2040)
Browse files Browse the repository at this point in the history
- 🚨 Widget test for `SpriteButtonComponent`

Co-authored-by: Shweta_C <[email protected]>
  • Loading branch information
ShwetaChauhan18 and shwetachauhan-simform authored Oct 7, 2022
1 parent 6b78b10 commit 4d51331
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame/src/anchor.dart';
import 'package:flame/src/components/sprite_group_component.dart';
import 'package:flame/src/spritesheet.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../game/flame_game_test.dart';
Expand Down Expand Up @@ -126,5 +128,83 @@ Future<void> main() async {
);
expect(component.current, _ButtonState.up);
});

testWidgets(
'Button can be pressed while the engine is paused',
(tester) async {
final game = GameWithTappables();

final buttonSheet = SpriteSheet.fromColumnsAndRows(
image: image,
columns: 1,
rows: 2,
);

final component = SpriteGroupComponent<_ButtonState>(
sprites: {
_ButtonState.up: buttonSheet.getSpriteById(0),
_ButtonState.down: buttonSheet.getSpriteById(1),
},
);
component.current = _ButtonState.down;

game.add(
SpriteButtonComponent(
button: buttonSheet.getSpriteById(0),
buttonDown: buttonSheet.getSpriteById(1),
onPressed: () {
game.pauseEngine();
game.overlays.add('pause-menu');
},
position: Vector2(400, 300),
size: Vector2.all(10),
),
);
await tester.pumpWidget(
GameWidget(
game: game,
overlayBuilderMap: {
'pause-menu': (context, _) {
return SimpleStatelessWidget(
build: (context) {
return Center(
child: OutlinedButton(
onPressed: () {
game.overlays.remove('pause-menu');
game.resumeEngine();
},
child: const Text('Resume'),
),
);
},
);
},
},
),
);
await tester.pump();
await tester.pump();

await tester.tapAt(const Offset(400, 300));
await tester.pump(const Duration(seconds: 1));
expect(game.paused, true);

await tester.tapAt(const Offset(400, 300));
await tester.pump(const Duration(seconds: 1));
expect(game.paused, false);
},
);
});
}

class SimpleStatelessWidget extends StatelessWidget {
const SimpleStatelessWidget({
required Widget Function(BuildContext) build,
super.key,
}) : _build = build;

final Widget Function(BuildContext) _build;

@override
Widget build(BuildContext context) => _build(context);
}

0 comments on commit 4d51331

Please sign in to comment.