• 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
..
animation Loading commit data...
cupertino Loading commit data...
dart Loading commit data...
examples Loading commit data...
foundation Loading commit data...
gestures Loading commit data...
harness Loading commit data...
material Loading commit data...
painting Loading commit data...
physics Loading commit data...
rendering Loading commit data...
scheduler Loading commit data...
semantics Loading commit data...
services Loading commit data...
widgets Loading commit data...
_goldens_io.dart Loading commit data...
_goldens_web.dart Loading commit data...
analysis_options.yaml Loading commit data...
flutter_test_config.dart Loading commit data...
image_data.dart Loading commit data...
impeller_test_helpers.dart Loading commit data...