• 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
event_simulation_test.dart 16.9 KB