diff --git a/packages/flutter_test/lib/src/controller.dart b/packages/flutter_test/lib/src/controller.dart index eb737d283e2..9eab83ae8d0 100644 --- a/packages/flutter_test/lib/src/controller.dart +++ b/packages/flutter_test/lib/src/controller.dart @@ -22,8 +22,6 @@ import 'window.dart'; /// This value must be greater than [kTouchSlop]. const double kDragSlopDefault = 20.0; -const String _defaultPlatform = kIsWeb ? 'web' : 'android'; - // Finds the end index (exclusive) of the span at `startIndex`, or `endIndex` if // there are no other spans between `startIndex` and `endIndex`. // The InlineSpan protocol doesn't expose the length of the span so we'll @@ -1957,8 +1955,8 @@ abstract class WidgetController { /// /// Specify `platform` as one of the platforms allowed in /// [platform.Platform.operatingSystem] to make the event appear to be from - /// that type of system. Defaults to "web" on web, and "android" everywhere - /// else. + /// that type of system. If not specified, defaults to "web" on web, and the + /// operating system name based on [defaultTargetPlatform] everywhere else. /// /// Specify the `physicalKey` for the event to override what is included in /// the simulated event. If not specified, it uses a default from the US @@ -1982,7 +1980,7 @@ abstract class WidgetController { /// - [sendKeyUpEvent] to simulate only a key up event. Future sendKeyEvent( LogicalKeyboardKey key, { - String platform = _defaultPlatform, + String? platform, String? character, PhysicalKeyboardKey? physicalKey }) async { @@ -1999,8 +1997,8 @@ abstract class WidgetController { /// /// Specify `platform` as one of the platforms allowed in /// [platform.Platform.operatingSystem] to make the event appear to be from - /// that type of system. Defaults to "web" on web, and "android" everywhere - /// else. + /// that type of system. If not specified, defaults to "web" on web, and the + /// operating system name based on [defaultTargetPlatform] everywhere else. /// /// Specify the `physicalKey` for the event to override what is included in /// the simulated event. If not specified, it uses a default from the US @@ -2021,7 +2019,7 @@ abstract class WidgetController { /// - [sendKeyEvent] to simulate both the key up and key down in the same call. Future sendKeyDownEvent( LogicalKeyboardKey key, { - String platform = _defaultPlatform, + String? platform, String? character, PhysicalKeyboardKey? physicalKey }) async { @@ -2036,8 +2034,8 @@ abstract class WidgetController { /// /// Specify `platform` as one of the platforms allowed in /// [platform.Platform.operatingSystem] to make the event appear to be from - /// that type of system. Defaults to "web" on web, and "android" everywhere - /// else. May not be null. + /// that type of system. If not specified, defaults to "web" on web, and the + /// operating system name based on [defaultTargetPlatform] everywhere else. /// /// Specify the `physicalKey` for the event to override what is included in /// the simulated event. If not specified, it uses a default from the US @@ -2052,7 +2050,7 @@ abstract class WidgetController { /// - [sendKeyEvent] to simulate both the key up and key down in the same call. Future sendKeyUpEvent( LogicalKeyboardKey key, { - String platform = _defaultPlatform, + String? platform, PhysicalKeyboardKey? physicalKey }) async { // Internally wrapped in async guard. @@ -2065,9 +2063,9 @@ abstract class WidgetController { /// from a soft keyboard. /// /// Specify `platform` as one of the platforms allowed in - /// [platform.Platform.operatingSystem] to make the event appear to be from that type - /// of system. Defaults to "web" on web, and "android" everywhere else. Must not be - /// null. + /// [platform.Platform.operatingSystem] to make the event appear to be from + /// that type of system. If not specified, defaults to "web" on web, and the + /// operating system name based on [defaultTargetPlatform] everywhere else. /// /// Specify the `physicalKey` for the event to override what is included in /// the simulated event. If not specified, it uses a default from the US @@ -2088,7 +2086,7 @@ abstract class WidgetController { /// - [sendKeyEvent] to simulate both the key up and key down in the same call. Future sendKeyRepeatEvent( LogicalKeyboardKey key, { - String platform = _defaultPlatform, + String? platform, String? character, PhysicalKeyboardKey? physicalKey }) async { diff --git a/packages/flutter_test/lib/src/event_simulation.dart b/packages/flutter_test/lib/src/event_simulation.dart index 329659509a0..47c60048204 100644 --- a/packages/flutter_test/lib/src/event_simulation.dart +++ b/packages/flutter_test/lib/src/event_simulation.dart @@ -711,7 +711,7 @@ abstract final class KeyEventSimulator { @visibleForTesting static KeyDataTransitMode get transitMode => _transitMode; - static String get _defaultPlatform => kIsWeb ? 'web' : Platform.operatingSystem; + static String get _defaultPlatform => kIsWeb ? 'web' : defaultTargetPlatform.name.toLowerCase(); /// Simulates sending a hardware key down event. /// @@ -720,7 +720,8 @@ abstract final class KeyEventSimulator { /// /// Specify `platform` as one of the platforms allowed in /// [Platform.operatingSystem] to make the event appear to be from that type of - /// system. Defaults to the operating system that the test is running on. + /// system. Defaults to "web" on web, and the operating system name based on + /// [defaultTargetPlatform] everywhere else. /// /// Keys that are down when the test completes are cleared after each test. /// @@ -767,7 +768,8 @@ abstract final class KeyEventSimulator { /// /// Specify `platform` as one of the platforms allowed in /// [Platform.operatingSystem] to make the event appear to be from that type of - /// system. Defaults to the operating system that the test is running on. + /// system. Defaults to "web" on web, and the operating system name based on + /// [defaultTargetPlatform] everywhere else. /// /// Returns true if the key event was handled by the framework. /// @@ -811,7 +813,8 @@ abstract final class KeyEventSimulator { /// /// Specify `platform` as one of the platforms allowed in /// [Platform.operatingSystem] to make the event appear to be from that type of - /// system. Defaults to the operating system that the test is running on. + /// system. Defaults to "web" on web, and the operating system name based on + /// [defaultTargetPlatform] everywhere else. /// /// Returns true if the key event was handled by the framework. /// @@ -860,7 +863,8 @@ abstract final class KeyEventSimulator { /// /// Specify `platform` as one of the platforms allowed in /// [Platform.operatingSystem] to make the event appear to be from that type of -/// system. Defaults to the operating system that the test is running on. +/// system. Defaults to "web" on web, and the operating system name based on +/// [defaultTargetPlatform] everywhere else. /// /// Keys that are down when the test completes are cleared after each test. /// @@ -894,7 +898,8 @@ Future simulateKeyDownEvent( /// /// Specify `platform` as one of the platforms allowed in /// [Platform.operatingSystem] to make the event appear to be from that type of -/// system. Defaults to the operating system that the test is running on. +/// system. Defaults to "web" on web, and the operating system name based on +/// [defaultTargetPlatform] everywhere else. /// /// Returns true if the key event was handled by the framework. /// @@ -922,7 +927,8 @@ Future simulateKeyUpEvent( /// /// Specify `platform` as one of the platforms allowed in /// [Platform.operatingSystem] to make the event appear to be from that type of -/// system. Defaults to the operating system that the test is running on. +/// system. Defaults to "web" on web, and the operating system name based on +/// [defaultTargetPlatform] everywhere else. /// /// Returns true if the key event was handled by the framework. /// diff --git a/packages/flutter_test/test/event_simulation_test.dart b/packages/flutter_test/test/event_simulation_test.dart index eb22da709bd..1d3a356e992 100644 --- a/packages/flutter_test/test/event_simulation_test.dart +++ b/packages/flutter_test/test/event_simulation_test.dart @@ -360,4 +360,33 @@ void main() { debugKeyEventSimulatorTransitModeOverride = null; }); + + testWidgets('Key events are simulated using the default target platform', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/133955. + final List events = []; + final FocusNode focusNode = FocusNode(); + + await tester.pumpWidget( + RawKeyboardListener( + focusNode: focusNode, + onKey: events.add, + child: Container(), + ), + ); + + focusNode.requestFocus(); + await tester.idle(); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.shift); + expect(events.length, 1); + final Type expectedType = isBrowser ? RawKeyEventDataWeb : switch (defaultTargetPlatform) { + TargetPlatform.android => RawKeyEventDataAndroid, + TargetPlatform.fuchsia => RawKeyEventDataFuchsia, + TargetPlatform.iOS => RawKeyEventDataIos, + TargetPlatform.linux => RawKeyEventDataLinux, + TargetPlatform.macOS => RawKeyEventDataMacOs, + TargetPlatform.windows => RawKeyEventDataWindows, + }; + expect(events.first.data.runtimeType, expectedType); + }, variant: TargetPlatformVariant.all()); }