Skip to content

[Widget Tests] - Key character resolution is broken on iOS #133956

@matthew-carroll

Description

@matthew-carroll

In widget tests, when simulating key presses that correspond to characters, the iOS character resolution is broken.

I'll illustrate this with two tests - one that shows the issues with iOS, and one that shows no such issues with Android.

Bug Repro:

testWidgets("bug repro", (widgetTester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.linux;

      await widgetTester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Focus(
              onKey: (FocusNode node, RawKeyEvent keyEvent) {
                print("Key event");
                print("$keyEvent");
                print("Character ${keyEvent.character}");
                print("");

                return KeyEventResult.ignored;
              },
              autofocus: true,
              child: const SizedBox.expand(),
            ),
          ),
        ),
      );

      await widgetTester.sendKeyDownEvent(LogicalKeyboardKey.shift, platform: "ios");

      await widgetTester.sendKeyDownEvent(LogicalKeyboardKey.keyG, platform: "ios", character: "G");
      await widgetTester.sendKeyUpEvent(LogicalKeyboardKey.keyG, platform: "ios");

      await widgetTester.sendKeyUpEvent(LogicalKeyboardKey.shift, platform: "ios");

      debugDefaultTargetPlatformOverride = null;
    });

Bug repro output - notice that there's never a "G" character reported:

Key event
RawKeyDownEvent#3b0d6(logicalKey: LogicalKeyboardKey#df100(keyId: "0x200000102", keyLabel: "Shift Left", debugName: "Shift Left"), physicalKey: PhysicalKeyboardKey#ed430(usbHidUsage: "0x000700e1", debugName: "Shift Left"), repeat: false)
Character null

Key event
RawKeyDownEvent#55276(logicalKey: LogicalKeyboardKey#c9177(keyId: "0x00000047", keyLabel: "G", debugName: "Key G"), physicalKey: PhysicalKeyboardKey#8c52b(usbHidUsage: "0x0007000a", debugName: "Key G"), repeat: false)
Character null

Key event
RawKeyUpEvent#a5680(logicalKey: LogicalKeyboardKey#23b97(keyId: "0x00000067", keyLabel: "G", debugName: "Key G"), physicalKey: PhysicalKeyboardKey#8c52b(usbHidUsage: "0x0007000a", debugName: "Key G"))
Character null

Key event
RawKeyUpEvent#8f6fc(logicalKey: LogicalKeyboardKey#df100(keyId: "0x200000102", keyLabel: "Shift Left", debugName: "Shift Left"), physicalKey: PhysicalKeyboardKey#ed430(usbHidUsage: "0x000700e1", debugName: "Shift Left"))
Character null

Here's the same test except using "android" as the simulated platform:

testWidgets("bug counter example", (widgetTester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.linux;

      await widgetTester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Focus(
              onKey: (FocusNode node, RawKeyEvent keyEvent) {
                print("Key event");
                print("$keyEvent");
                print("Character ${keyEvent.character}");
                print("");

                return KeyEventResult.ignored;
              },
              autofocus: true,
              child: const SizedBox.expand(),
            ),
          ),
        ),
      );

      await widgetTester.sendKeyDownEvent(LogicalKeyboardKey.shift, platform: "android");

      await widgetTester.sendKeyDownEvent(LogicalKeyboardKey.keyG, platform: "android", character: "G");
      await widgetTester.sendKeyUpEvent(LogicalKeyboardKey.keyG, platform: "android");

      await widgetTester.sendKeyUpEvent(LogicalKeyboardKey.shift, platform: "android");

      debugDefaultTargetPlatformOverride = null;
    });

Here's the output that shows things work as expected on Android:

Key event
RawKeyDownEvent#fcd4b(logicalKey: LogicalKeyboardKey#df100(keyId: "0x200000102", keyLabel: "Shift Left", debugName: "Shift Left"), physicalKey: PhysicalKeyboardKey#ed430(usbHidUsage: "0x000700e1", debugName: "Shift Left"), repeat: false)
Character null

Key event
RawKeyDownEvent#e570e(logicalKey: LogicalKeyboardKey#23b97(keyId: "0x00000067", keyLabel: "G", debugName: "Key G"), physicalKey: PhysicalKeyboardKey#8c52b(usbHidUsage: "0x0007000a", debugName: "Key G"), repeat: false)
Character G

Key event
RawKeyUpEvent#2a444(logicalKey: LogicalKeyboardKey#23b97(keyId: "0x00000067", keyLabel: "G", debugName: "Key G"), physicalKey: PhysicalKeyboardKey#8c52b(usbHidUsage: "0x0007000a", debugName: "Key G"))
Character null

Key event
RawKeyUpEvent#c6b9c(logicalKey: LogicalKeyboardKey#df100(keyId: "0x200000102", keyLabel: "Shift Left", debugName: "Shift Left"), physicalKey: PhysicalKeyboardKey#ed430(usbHidUsage: "0x000700e1", debugName: "Shift Left"))
Character null

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our testsa: text inputEntering text in a text field or keyboard related problemsfound in release: 3.13Found to occur in 3.13found in release: 3.14Found to occur in 3.14frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions