• Bruno Leroux's avatar
    [flutter_test] Change KeyEventSimulator default transit mode (#143847) · 8ade81fb
    Bruno Leroux authored
    ## Description
    
    This PRs changes the default value transit mode for key event simulation.
    
    The default transit mode for key event simulation is currently `KeyDataTransitMode.rawKeyData` while on the framework side `KeyDataTransitMode.keyDataThenRawKeyData` is the preferred transit mode.
    
    `KeyDataTransitMode.keyDataThenRawKeyData` is more accurate and can help detect issues.
    
    For instance the following test will fail with `KeyDataTransitMode.rawKeyData` because raw keyboard logic for modifier keys is less accurate:
    
    ```dart
      testWidgets('Press control left once', (WidgetTester tester) async {
        debugKeyEventSimulatorTransitModeOverride = KeyDataTransitMode.keyDataThenRawKeyData;
    
        final List<KeyEvent> events = <KeyEvent>[];
        final FocusNode focusNode = FocusNode();
        addTearDown(focusNode.dispose);
    
        await tester.pumpWidget(
          Focus(
            focusNode: focusNode,
            autofocus: true,
            onKeyEvent: (_, KeyEvent event) {
              events.add(event);
              return KeyEventResult.handled;
            },
            child: Container(),
          ),
        );
    
        await simulateKeyDownEvent(LogicalKeyboardKey.controlLeft);
    
        // This will fail when transit mode is KeyDataTransitMode.rawKeyData
        // because a down event for controlRight is synthesized.
        expect(events.length, 1);
    
        debugKeyEventSimulatorTransitModeOverride = null;
      });
    ```
    
    And the following this test is ok with `KeyDataTransitMode.rawKeyData` but rightly fails with `KeyDataTransitMode.keyDataThenRawKeyData`:
    
    ```dart
      testWidgets('Simulates consecutive key down events', (WidgetTester tester) async {
        debugKeyEventSimulatorTransitModeOverride = KeyDataTransitMode.rawKeyData;
    
        // Simulating several key down events without key up in between is tolerated
        // when transit mode is KeyDataTransitMode.rawKeyData, it will trigger an
        // assert on KeyDataTransitMode.keyDataThenRawKeyData.
        await simulateKeyDownEvent(LogicalKeyboardKey.arrowDown);
        await simulateKeyDownEvent(LogicalKeyboardKey.arrowDown);
    
        debugKeyEventSimulatorTransitModeOverride = null;
      });
    ```
    
    ## Related Issue
    
    Related to https://github.com/flutter/flutter/issues/143845
    
    ## Tests
    
    Adds two tests.
    8ade81fb
Name
Last commit
Last update
.github Loading commit data...
.vscode Loading commit data...
bin Loading commit data...
dev Loading commit data...
examples Loading commit data...
packages Loading commit data...
.ci.yaml Loading commit data...
.gitattributes Loading commit data...
.gitignore Loading commit data...
AUTHORS Loading commit data...
CODEOWNERS Loading commit data...
CODE_OF_CONDUCT.md Loading commit data...
CONTRIBUTING.md Loading commit data...
LICENSE Loading commit data...
PATENT_GRANT Loading commit data...
README.md Loading commit data...
TESTOWNERS Loading commit data...
analysis_options.yaml Loading commit data...
dartdoc_options.yaml Loading commit data...
flutter_console.bat Loading commit data...