Skip to content

Commit 471a828

Browse files
authored
[flutter_test] Use defaultTargetPlatform for key events simulation (#143579)
## Description This PRs changes the default value for the `platform` parameter used to simulate key events. With this PR, the default value is "web" on web, otherwise it is the operating system name retrieved from `defaultTargetPlatform`. Previously, for methods in `WidgetController`, it defaulted to �web� on web, and �android� everywhere else. And for methods in `KeyEventSimulator` it defaulted to �web� on web, and the operating system that the test was running on everywhere else. Because the operating system was based on `Platform.operatingSystem`, it usually differed from the target platform the test was running on. AFAIK, the `platform` parameter is only meaningful for simulating `RawKeyEvent`. Once `RawKeyboard` will be fully removed, the `platform` parameter won�t be needed. @gspencergoog In the meantime, do you think it is worth merging this fix? ## Related Issue Fixes to flutter/flutter#133955 ## Tests Adds one test.
1 parent 4e63a6a commit 471a828

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

packages/flutter_test/lib/src/controller.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import 'window.dart';
2222
/// This value must be greater than [kTouchSlop].
2323
const double kDragSlopDefault = 20.0;
2424

25-
const String _defaultPlatform = kIsWeb ? 'web' : 'android';
26-
2725
// Finds the end index (exclusive) of the span at `startIndex`, or `endIndex` if
2826
// there are no other spans between `startIndex` and `endIndex`.
2927
// The InlineSpan protocol doesn't expose the length of the span so we'll
@@ -1957,8 +1955,8 @@ abstract class WidgetController {
19571955
///
19581956
/// Specify `platform` as one of the platforms allowed in
19591957
/// [platform.Platform.operatingSystem] to make the event appear to be from
1960-
/// that type of system. Defaults to "web" on web, and "android" everywhere
1961-
/// else.
1958+
/// that type of system. If not specified, defaults to "web" on web, and the
1959+
/// operating system name based on [defaultTargetPlatform] everywhere else.
19621960
///
19631961
/// Specify the `physicalKey` for the event to override what is included in
19641962
/// the simulated event. If not specified, it uses a default from the US
@@ -1982,7 +1980,7 @@ abstract class WidgetController {
19821980
/// - [sendKeyUpEvent] to simulate only a key up event.
19831981
Future<bool> sendKeyEvent(
19841982
LogicalKeyboardKey key, {
1985-
String platform = _defaultPlatform,
1983+
String? platform,
19861984
String? character,
19871985
PhysicalKeyboardKey? physicalKey
19881986
}) async {
@@ -1999,8 +1997,8 @@ abstract class WidgetController {
19991997
///
20001998
/// Specify `platform` as one of the platforms allowed in
20011999
/// [platform.Platform.operatingSystem] to make the event appear to be from
2002-
/// that type of system. Defaults to "web" on web, and "android" everywhere
2003-
/// else.
2000+
/// that type of system. If not specified, defaults to "web" on web, and the
2001+
/// operating system name based on [defaultTargetPlatform] everywhere else.
20042002
///
20052003
/// Specify the `physicalKey` for the event to override what is included in
20062004
/// the simulated event. If not specified, it uses a default from the US
@@ -2021,7 +2019,7 @@ abstract class WidgetController {
20212019
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
20222020
Future<bool> sendKeyDownEvent(
20232021
LogicalKeyboardKey key, {
2024-
String platform = _defaultPlatform,
2022+
String? platform,
20252023
String? character,
20262024
PhysicalKeyboardKey? physicalKey
20272025
}) async {
@@ -2036,8 +2034,8 @@ abstract class WidgetController {
20362034
///
20372035
/// Specify `platform` as one of the platforms allowed in
20382036
/// [platform.Platform.operatingSystem] to make the event appear to be from
2039-
/// that type of system. Defaults to "web" on web, and "android" everywhere
2040-
/// else. May not be null.
2037+
/// that type of system. If not specified, defaults to "web" on web, and the
2038+
/// operating system name based on [defaultTargetPlatform] everywhere else.
20412039
///
20422040
/// Specify the `physicalKey` for the event to override what is included in
20432041
/// the simulated event. If not specified, it uses a default from the US
@@ -2052,7 +2050,7 @@ abstract class WidgetController {
20522050
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
20532051
Future<bool> sendKeyUpEvent(
20542052
LogicalKeyboardKey key, {
2055-
String platform = _defaultPlatform,
2053+
String? platform,
20562054
PhysicalKeyboardKey? physicalKey
20572055
}) async {
20582056
// Internally wrapped in async guard.
@@ -2065,9 +2063,9 @@ abstract class WidgetController {
20652063
/// from a soft keyboard.
20662064
///
20672065
/// Specify `platform` as one of the platforms allowed in
2068-
/// [platform.Platform.operatingSystem] to make the event appear to be from that type
2069-
/// of system. Defaults to "web" on web, and "android" everywhere else. Must not be
2070-
/// null.
2066+
/// [platform.Platform.operatingSystem] to make the event appear to be from
2067+
/// that type of system. If not specified, defaults to "web" on web, and the
2068+
/// operating system name based on [defaultTargetPlatform] everywhere else.
20712069
///
20722070
/// Specify the `physicalKey` for the event to override what is included in
20732071
/// the simulated event. If not specified, it uses a default from the US
@@ -2088,7 +2086,7 @@ abstract class WidgetController {
20882086
/// - [sendKeyEvent] to simulate both the key up and key down in the same call.
20892087
Future<bool> sendKeyRepeatEvent(
20902088
LogicalKeyboardKey key, {
2091-
String platform = _defaultPlatform,
2089+
String? platform,
20922090
String? character,
20932091
PhysicalKeyboardKey? physicalKey
20942092
}) async {

packages/flutter_test/lib/src/event_simulation.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ abstract final class KeyEventSimulator {
711711
@visibleForTesting
712712
static KeyDataTransitMode get transitMode => _transitMode;
713713

714-
static String get _defaultPlatform => kIsWeb ? 'web' : Platform.operatingSystem;
714+
static String get _defaultPlatform => kIsWeb ? 'web' : defaultTargetPlatform.name.toLowerCase();
715715

716716
/// Simulates sending a hardware key down event.
717717
///
@@ -720,7 +720,8 @@ abstract final class KeyEventSimulator {
720720
///
721721
/// Specify `platform` as one of the platforms allowed in
722722
/// [Platform.operatingSystem] to make the event appear to be from that type of
723-
/// system. Defaults to the operating system that the test is running on.
723+
/// system. Defaults to "web" on web, and the operating system name based on
724+
/// [defaultTargetPlatform] everywhere else.
724725
///
725726
/// Keys that are down when the test completes are cleared after each test.
726727
///
@@ -767,7 +768,8 @@ abstract final class KeyEventSimulator {
767768
///
768769
/// Specify `platform` as one of the platforms allowed in
769770
/// [Platform.operatingSystem] to make the event appear to be from that type of
770-
/// system. Defaults to the operating system that the test is running on.
771+
/// system. Defaults to "web" on web, and the operating system name based on
772+
/// [defaultTargetPlatform] everywhere else.
771773
///
772774
/// Returns true if the key event was handled by the framework.
773775
///
@@ -811,7 +813,8 @@ abstract final class KeyEventSimulator {
811813
///
812814
/// Specify `platform` as one of the platforms allowed in
813815
/// [Platform.operatingSystem] to make the event appear to be from that type of
814-
/// system. Defaults to the operating system that the test is running on.
816+
/// system. Defaults to "web" on web, and the operating system name based on
817+
/// [defaultTargetPlatform] everywhere else.
815818
///
816819
/// Returns true if the key event was handled by the framework.
817820
///
@@ -860,7 +863,8 @@ abstract final class KeyEventSimulator {
860863
///
861864
/// Specify `platform` as one of the platforms allowed in
862865
/// [Platform.operatingSystem] to make the event appear to be from that type of
863-
/// system. Defaults to the operating system that the test is running on.
866+
/// system. Defaults to "web" on web, and the operating system name based on
867+
/// [defaultTargetPlatform] everywhere else.
864868
///
865869
/// Keys that are down when the test completes are cleared after each test.
866870
///
@@ -894,7 +898,8 @@ Future<bool> simulateKeyDownEvent(
894898
///
895899
/// Specify `platform` as one of the platforms allowed in
896900
/// [Platform.operatingSystem] to make the event appear to be from that type of
897-
/// system. Defaults to the operating system that the test is running on.
901+
/// system. Defaults to "web" on web, and the operating system name based on
902+
/// [defaultTargetPlatform] everywhere else.
898903
///
899904
/// Returns true if the key event was handled by the framework.
900905
///
@@ -922,7 +927,8 @@ Future<bool> simulateKeyUpEvent(
922927
///
923928
/// Specify `platform` as one of the platforms allowed in
924929
/// [Platform.operatingSystem] to make the event appear to be from that type of
925-
/// system. Defaults to the operating system that the test is running on.
930+
/// system. Defaults to "web" on web, and the operating system name based on
931+
/// [defaultTargetPlatform] everywhere else.
926932
///
927933
/// Returns true if the key event was handled by the framework.
928934
///

packages/flutter_test/test/event_simulation_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,33 @@ void main() {
360360

361361
debugKeyEventSimulatorTransitModeOverride = null;
362362
});
363+
364+
testWidgets('Key events are simulated using the default target platform', (WidgetTester tester) async {
365+
// Regression test for https://github.com/flutter/flutter/issues/133955.
366+
final List<RawKeyEvent> events = <RawKeyEvent>[];
367+
final FocusNode focusNode = FocusNode();
368+
369+
await tester.pumpWidget(
370+
RawKeyboardListener(
371+
focusNode: focusNode,
372+
onKey: events.add,
373+
child: Container(),
374+
),
375+
);
376+
377+
focusNode.requestFocus();
378+
await tester.idle();
379+
380+
await tester.sendKeyDownEvent(LogicalKeyboardKey.shift);
381+
expect(events.length, 1);
382+
final Type expectedType = isBrowser ? RawKeyEventDataWeb : switch (defaultTargetPlatform) {
383+
TargetPlatform.android => RawKeyEventDataAndroid,
384+
TargetPlatform.fuchsia => RawKeyEventDataFuchsia,
385+
TargetPlatform.iOS => RawKeyEventDataIos,
386+
TargetPlatform.linux => RawKeyEventDataLinux,
387+
TargetPlatform.macOS => RawKeyEventDataMacOs,
388+
TargetPlatform.windows => RawKeyEventDataWindows,
389+
};
390+
expect(events.first.data.runtimeType, expectedType);
391+
}, variant: TargetPlatformVariant.all());
363392
}

0 commit comments

Comments
 (0)