• 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
..
material Loading commit data...
README.md Loading commit data...
action_sheet_test.dart Loading commit data...
activity_indicator_test.dart Loading commit data...
adaptive_text_selection_toolbar_test.dart Loading commit data...
app_test.dart Loading commit data...
bottom_tab_bar_test.dart Loading commit data...
button_test.dart Loading commit data...
checkbox_test.dart Loading commit data...
colors_test.dart Loading commit data...
context_menu_action_test.dart Loading commit data...
context_menu_test.dart Loading commit data...
date_picker_test.dart Loading commit data...
debug_test.dart Loading commit data...
desktop_text_selection_toolbar_button_test.dart Loading commit data...
desktop_text_selection_toolbar_test.dart Loading commit data...
dialog_test.dart Loading commit data...
form_row_test.dart Loading commit data...
form_section_test.dart Loading commit data...
icon_theme_data_test.dart Loading commit data...
list_section_test.dart Loading commit data...
list_tile_test.dart Loading commit data...
localizations_test.dart Loading commit data...
magnifier_test.dart Loading commit data...
nav_bar_test.dart Loading commit data...
nav_bar_transition_test.dart Loading commit data...
page_test.dart Loading commit data...
picker_test.dart Loading commit data...
radio_test.dart Loading commit data...
refresh_test.dart Loading commit data...
route_test.dart Loading commit data...
scaffold_test.dart Loading commit data...
scrollbar_paint_test.dart Loading commit data...
scrollbar_test.dart Loading commit data...
search_field_test.dart Loading commit data...
segmented_control_test.dart Loading commit data...
slider_test.dart Loading commit data...
sliding_segmented_control_test.dart Loading commit data...
spell_check_suggestions_toolbar_test.dart Loading commit data...
switch_test.dart Loading commit data...
tab_scaffold_test.dart Loading commit data...
tab_test.dart Loading commit data...
text_field_restoration_test.dart Loading commit data...
text_field_test.dart Loading commit data...
text_form_field_row_restoration_test.dart Loading commit data...
text_form_field_row_test.dart Loading commit data...
text_selection_test.dart Loading commit data...
text_selection_toolbar_button_test.dart Loading commit data...
text_selection_toolbar_test.dart Loading commit data...
text_theme_test.dart Loading commit data...
theme_test.dart Loading commit data...