Skip to content

Commit 9c69e76

Browse files
Create custom versions of standard Flutter key simulation methods, to keep the simulated platform consistent (Resolves #17) (#18)
1 parent 0e8ffc6 commit 9c69e76

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/src/keyboard.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ extension KeyboardInput on WidgetTester {
5151
}
5252
}
5353

54+
/// Runs [sendKeyEvent], using the current [defaultTargetPlatform] as the key simulators `platform` value.
55+
///
56+
/// {@template flutter_key_simulation_override}
57+
/// This method was created because developers often use convenience methods in this package,
58+
/// along with Flutter's standard simulation methods. But, the convenience methods in this package
59+
/// simulate a key press `platform` based on the current [defaultTargetPlatform], whereas Flutter's
60+
/// standard simulation methods always default to "android". Using mismatched platforms across
61+
/// key simulations leads to unexpected results. By always using methods in this package, instead of
62+
/// standard Flutter methods, the simulated platform is guaranteed to match across calls, and also
63+
/// match the platform that's simulated within the surrounding test, i.e., [defaultTargetPlatform].
64+
/// @{endtemplate}
65+
Future<void> pressKey(LogicalKeyboardKey key) => sendKeyEvent(key, platform: _keyEventPlatform);
66+
67+
/// Runs [simulateKeyDownEvent], using the current [defaultTargetPlatform] as the key simulators `platform` value.
68+
///
69+
/// {@macro flutter_key_simulation_override}
70+
Future<void> pressKeyDown(LogicalKeyboardKey key) => simulateKeyDownEvent(key, platform: _keyEventPlatform);
71+
72+
/// Runs [simulateKeyUpEvent], using the current [defaultTargetPlatform] as the key simulators `platform` value.
73+
///
74+
/// {@macro flutter_key_simulation_override}
75+
Future<void> releaseKeyUp(LogicalKeyboardKey key) => simulateKeyUpEvent(key, platform: _keyEventPlatform);
76+
77+
/// Runs [simulateKeyRepeatEvent], using the current [defaultTargetPlatform] as the key simulators `platform` value.
78+
///
79+
/// {@macro flutter_key_simulation_override}
80+
Future<void> repeatKey(LogicalKeyboardKey key) => simulateKeyRepeatEvent(key, platform: _keyEventPlatform);
81+
5482
Future<void> pressEnter() async {
5583
await sendKeyEvent(LogicalKeyboardKey.enter, platform: _keyEventPlatform);
5684
await pumpAndSettle();

0 commit comments

Comments
 (0)