Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simulateKeyDownEvent ENTER throwing exception in integration tests Desktop #148406

Closed
fernandomoraesvr opened this issue May 15, 2024 · 8 comments
Labels
r: solved Issue is closed as solved

Comments

@fernandomoraesvr
Copy link

fernandomoraesvr commented May 15, 2024

Steps to reproduce

Just use await simulateKeyDownEvent(LogicalKeyboardKey.enter) in a integration test targeting desktop platform (linux or windows)

Expected results

Simulate a KeyDown event from a Logical Key enter

Actual results

Every test using the aforementioned function call to simulate enter key thrown an error.

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
A KeyDownEvent is dispatched, but the state shows that the physical key is already pressed. If this
occurs in real application, please report this bug to Flutter. If this occurs in unit tests, please
ensure that simulated events follow Flutter's event model as documented in `HardwareKeyboard`. This
was the event: KeyDownEvent#1c7d8(physicalKey: PhysicalKeyboardKey#e14a9(usbHidUsage: "0x00070028",
debugName: "Enter"), logicalKey: LogicalKeyboardKey#2604c(keyId: "0x10000000d", keyLabel: "Enter",
debugName: "Enter"), character: null, timeStamp: 0:00:00.000000)
'package:flutter/src/services/hardware_keyboard.dart':
Failed assertion: line 505 pos 16: '!_pressedKeys.containsKey(event.physicalKey)'
Either the assertion indicates an error in the framework itself, or we should provide substantially
more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.yml
When the exception was thrown, this was the stack:
#2      HardwareKeyboard._assertEventIsRegular.<anonymous closure> (package:flutter/src/services/hardware_keyboard.dart:505:16)
#3      HardwareKeyboard._assertEventIsRegular (package:flutter/src/services/hardware_keyboard.dart:520:6)
#4      HardwareKeyboard.handleKeyEvent (package:flutter/src/services/hardware_keyboard.dart:643:5)
#5      KeyEventManager.handleRawKeyMessage (package:flutter/src/services/hardware_keyboard.dart:1164:37)
#6      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:235:49)
#7      TestDefaultBinaryMessenger.handlePlatformMessage (package:flutter_test/src/test_default_binary_messenger.dart:99:42)
#8      KeyEventSimulator._simulateKeyEventByRawEvent.<anonymous closure> (package:flutter_test/src/event_simulation.dart:655:79)
#11     TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#12     KeyEventSimulator._simulateKeyEventByRawEvent (package:flutter_test/src/event_simulation.dart:653:27)
#13     KeyEventSimulator.simulateKeyDownEvent.simulateByRawEvent (package:flutter_test/src/event_simulation.dart:740:14)
#14     KeyEventSimulator.simulateKeyDownEvent (package:flutter_test/src/event_simulation.dart:760:23)
#15     simulateKeyDownEvent (package:flutter_test/src/event_simulation.dart:883:48)
#16     startDialogAndSupplyValidOperatorLogin (file:///C:/repositorios/VRCheckout/integration_test/modules/initial/initial_integration_test.dart:3022:9)
<asynchronous suspension>
#17     main.<anonymous closure> (file:///C:/repositorios/VRCheckout/integration_test/e2e_integration_test.dart:301:5)
<asynchronous suspension>
#18     testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:183:15)
<asynchronous suspension>
#19     TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1017:5)
<asynchronous suspension>
<asynchronous suspension>
(elided 5 frames from class _AssertionError, dart:async, and package:stack_trace)

Code sample

Code sample
void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("test", (widgetTester) async {
    app.main();

    await widgetTester.pumpWidget(MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(
        title: "test",
      ),
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en', 'US'),
      ],
    ));

    await widgetTester.pumpAndSettle();

    await simulateKeyDownEvent(LogicalKeyboardKey.enter);

    await widgetTester.pumpAndSettle();

    await simulateKeyDownEvent(LogicalKeyboardKey.enter);

    await widgetTester.pumpAndSettle();

    expect(true, true);
  });
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

[√] Flutter (Channel stable, 3.22.0, on Microsoft Windows [versÆo 10.0.22631.3527], locale pt-BR)
    • Flutter version 3.22.0 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (6 days ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • DevTools version 2.34.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\Fernando-Moraes\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.32)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.34301.259
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2022.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)

[√] VS Code (version 1.89.1)
    • VS Code at C:\Users\Fernando-Moraes\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.88.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [versÆo 10.0.22631.3527]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 124.0.6367.203
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 124.0.2478.67

[√] Network resources
    • All expected network resources are available.

• No issues found!
@danagbemava-nc danagbemava-nc added the in triage Presently being triaged by the triage team label May 15, 2024
@danagbemava-nc
Copy link
Member

Hi @fernandomoraesvr, how are you running your integration tests? If you are using flutter driver, this may be a limitation. See #9383

@danagbemava-nc danagbemava-nc added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 15, 2024
@fernandomoraesvr
Copy link
Author

@

Hi @fernandomoraesvr, how are you running your integration tests? If you are using flutter driver, this may be a limitation. See #9383

Hi, i'm using the integration test driver, i was not able to reproduce my issue in 3.19.6, await simulateKeyDownEvent(LogicalKeyboardKey.enter) is throwing exception after update to 3.22

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 15, 2024
@danagbemava-nc
Copy link
Member

Hi @fernandomoraesvr, please provide a complete minimal runnable code sample so that we can properly investigate this.

Thank you

@danagbemava-nc danagbemava-nc added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 16, 2024
@fernandomoraesvr
Copy link
Author

Hi @fernandomoraesvr, please provide a complete minimal runnable code sample so that we can properly investigate this.

Thank you

I created a new Flutter project using version 3.22 and was able to reproduce the issue with the newly created project, without changing code in the initial generated project, i wrote the following integration test:

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("test", (widgetTester) async {
    app.main();

    await widgetTester.pumpWidget(MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(
        title: "test",
      ),
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en', 'US'),
      ],
    ));

    await widgetTester.pumpAndSettle();

    await simulateKeyDownEvent(LogicalKeyboardKey.enter);

    await widgetTester.pumpAndSettle();

    await simulateKeyDownEvent(LogicalKeyboardKey.enter);

    await widgetTester.pumpAndSettle();

    expect(true, true);
  });
}

It looks like it requires two await simulateKeyDownEvent(LogicalKeyboardKey.enter) to thrown an error, as the minimal runnable code suggests, it does not require KeyboardListener (Alltough it throws with an listener too).

MyHomePage in the sample code is the MyHomePage created by flutter create command. I'm running the integration test in Windows, but in Linux is throwing as well.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 16, 2024
@fernandomoraesvr
Copy link
Author

fernandomoraesvr commented May 16, 2024

@danagbemava-nc

I did a deep investigation and found out that the change made by this PR #143847 is causing this issue.

Reverting the transitMode in event_simulation.dart fixed the issue:

  static const KeyDataTransitMode _defaultTransitMode =
      KeyDataTransitMode.rawKeyData;

@bleroux
Copy link
Contributor

bleroux commented May 17, 2024

The test above simulates two key down events on the same key, this is why it triggers an error (previously, with version 3.19, the error was not thrown due to a bug).

There are two ways to fix this test:

  • calling simulateKeyUpEvent before simulating the second press.
  • relying on tester sendKeyEvent which simulates a down event followed by an up event.

Thanks for trying these.

@danagbemava-nc danagbemava-nc added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 17, 2024
@fernandomoraesvr
Copy link
Author

The test above simulates two key down events on the same key, this is why it triggers an error (previously, with version 3.19, the error was not thrown due to a bug).

There are two ways to fix this test:

* calling simulateKeyUpEvent before simulating the second press.

* relying on tester sendKeyEvent which simulates a down event followed by an up event.

Thanks for trying these.

I did not remember why we're using simulateKeyDownEvent instead of tester sendKeyEvent, sendKeyEvent worked fine in our integration tests rn (So obviously that a Up is needed to remove from keyPressedEvents and i didn't noticed lmao)

Thank you bleroux, this issue can be closed now.

TL;DR: Fixed after changing simulateKeyDownEvent to sendKeyEvent or using and Up after Down.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 17, 2024
@danagbemava-nc danagbemava-nc added r: solved Issue is closed as solved and removed in triage Presently being triaged by the triage team labels May 17, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: solved Issue is closed as solved
Projects
None yet
Development

No branches or pull requests

3 participants