mirror of https://github.com/flutter/pinball.git
parent
4f4f1cdd87
commit
f88dbb7fb8
@ -0,0 +1,131 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
class _MockPinballGame extends Mock implements PinballGame {}
|
||||||
|
|
||||||
|
extension _WidgetTesterX on WidgetTester {
|
||||||
|
Future<void> pumpMobileControls(PinballGame game) async {
|
||||||
|
await pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
localizationsDelegates: const [
|
||||||
|
AppLocalizations.delegate,
|
||||||
|
GlobalMaterialLocalizations.delegate,
|
||||||
|
],
|
||||||
|
home: Scaffold(
|
||||||
|
body: MobileControls(game: game),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension _CommonFindersX on CommonFinders {
|
||||||
|
Finder byPinballDpadDirection(PinballDpadDirection direction) {
|
||||||
|
return byWidgetPredicate((widget) {
|
||||||
|
return widget is PinballDpadButton && widget.direction == direction;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('MobileControls', () {
|
||||||
|
testWidgets('renders', (tester) async {
|
||||||
|
await tester.pumpMobileControls(_MockPinballGame());
|
||||||
|
|
||||||
|
expect(find.byType(PinballButton), findsOneWidget);
|
||||||
|
expect(find.byType(MobileDpad), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow up', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowUp: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.up));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow down', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowDown: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.down));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow right', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowRight: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.right));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the arrow left', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowLeft: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.left));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('correctly triggers the enter', (tester) async {
|
||||||
|
var pressed = false;
|
||||||
|
final component = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.enter: () => pressed = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final game = _MockPinballGame();
|
||||||
|
when(game.descendants).thenReturn([component]);
|
||||||
|
|
||||||
|
await tester.pumpMobileControls(game);
|
||||||
|
await tester.tap(find.byType(PinballButton));
|
||||||
|
await tester.pump();
|
||||||
|
|
||||||
|
expect(pressed, isTrue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,111 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
extension _WidgetTesterX on WidgetTester {
|
||||||
|
Future<void> pumpDpad({
|
||||||
|
required VoidCallback onTapUp,
|
||||||
|
required VoidCallback onTapDown,
|
||||||
|
required VoidCallback onTapLeft,
|
||||||
|
required VoidCallback onTapRight,
|
||||||
|
}) async {
|
||||||
|
await pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: Scaffold(
|
||||||
|
body: MobileDpad(
|
||||||
|
onTapUp: onTapUp,
|
||||||
|
onTapDown: onTapDown,
|
||||||
|
onTapLeft: onTapLeft,
|
||||||
|
onTapRight: onTapRight,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension _CommonFindersX on CommonFinders {
|
||||||
|
Finder byPinballDpadDirection(PinballDpadDirection direction) {
|
||||||
|
return byWidgetPredicate((widget) {
|
||||||
|
return widget is PinballDpadButton && widget.direction == direction;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class _VoidCallbackStubBase {
|
||||||
|
void onCall();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _VoidCallbackStub extends Mock implements _VoidCallbackStubBase {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('MobileDpad', () {
|
||||||
|
testWidgets('renders correctly', (tester) async {
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
find.byType(PinballDpadButton),
|
||||||
|
findsNWidgets(4),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap up', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: stub.onCall,
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.up));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap down', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: stub.onCall,
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.down));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap left', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: stub.onCall,
|
||||||
|
onTapRight: () {},
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.left));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('can tap left', (tester) async {
|
||||||
|
final stub = _VoidCallbackStub();
|
||||||
|
await tester.pumpDpad(
|
||||||
|
onTapUp: () {},
|
||||||
|
onTapDown: () {},
|
||||||
|
onTapLeft: () {},
|
||||||
|
onTapRight: stub.onCall,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(find.byPinballDpadDirection(PinballDpadDirection.right));
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in new issue