diff --git a/packages/flutter/lib/src/services/hardware_keyboard.dart b/packages/flutter/lib/src/services/hardware_keyboard.dart index 90f7000aa87..9fae9c83edf 100644 --- a/packages/flutter/lib/src/services/hardware_keyboard.dart +++ b/packages/flutter/lib/src/services/hardware_keyboard.dart @@ -504,33 +504,42 @@ class HardwareKeyboard { isLogicalKeyPressed(LogicalKeyboardKey.metaRight); } - void _assertEventIsRegular(KeyEvent event) { + // Print debug messages if the event is inconsistent + // with the current state, and if [debugPrintKeyboardEvents] is true. + void _logEventIfIrregular(KeyEvent event) { assert(() { const common = - '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: '; + 'This is typically either due to https://github.com/flutter/flutter/issues/125975, ' + "or a bug in the embedding's key event conciliation logic."; if (event is KeyDownEvent) { - assert( - !_pressedKeys.containsKey(event.physicalKey), - 'A ${event.runtimeType} is dispatched, but the state shows that the physical ' - 'key is already pressed. $common$event', - ); + if (_pressedKeys.containsKey(event.physicalKey)) { + _keyboardDebug( + () => + 'ERROR: Received unexpected ${event.runtimeType} for key that is already pressed.\n' + '$common\n' + ' Event: $event\n' + ' Pressed logical key: ${_pressedKeys[event.physicalKey]}', + ); + } } else if (event is KeyRepeatEvent || event is KeyUpEvent) { - assert( - _pressedKeys.containsKey(event.physicalKey), - 'A ${event.runtimeType} is dispatched, but the state shows that the physical ' - 'key is not pressed. $common$event', - ); - assert( - _pressedKeys[event.physicalKey] == event.logicalKey, - 'A ${event.runtimeType} is dispatched, but the state shows that the physical ' - 'key is pressed on a different logical key. $common$event ' - 'and the recorded logical key ${_pressedKeys[event.physicalKey]}', - ); + if (!_pressedKeys.containsKey(event.physicalKey)) { + _keyboardDebug( + () => + 'ERROR: Received unexpected ${event.runtimeType} for key that is not pressed:\n' + '$common\n' + ' Event: $event', + ); + } else if (_pressedKeys[event.physicalKey] != event.logicalKey) { + _keyboardDebug( + () => + 'ERROR: Received unexpected ${event.runtimeType} for key with mismatched logical key:\n' + '$common\n' + ' Event: $event\n' + ' Pressed logical key: ${_pressedKeys[event.physicalKey]}', + ); + } } else { - assert(false, 'Unexpected key event class ${event.runtimeType}'); + assert(false, 'Received unexpected key event class ${event.runtimeType}'); } return true; }()); @@ -652,12 +661,14 @@ class HardwareKeyboard { /// Process a new [KeyEvent] by recording the state changes and dispatching /// to handlers. + /// + /// Returns true if any handler handled the event. bool handleKeyEvent(KeyEvent event) { assert(_keyboardDebug(() => 'Key event received: $event')); assert( _keyboardDebug(() => 'Pressed state before processing the event:', _debugPressedKeysDetails), ); - _assertEventIsRegular(event); + _logEventIfIrregular(event); final PhysicalKeyboardKey physicalKey = event.physicalKey; final LogicalKeyboardKey logicalKey = event.logicalKey; if (event is KeyDownEvent) { @@ -673,7 +684,8 @@ class HardwareKeyboard { } else if (event is KeyUpEvent) { _pressedKeys.remove(physicalKey); } else if (event is KeyRepeatEvent) { - // Empty + // Update the logical key in case it has changed. + _pressedKeys[physicalKey] = logicalKey; } assert( diff --git a/packages/flutter/test/services/hardware_keyboard_test.dart b/packages/flutter/test/services/hardware_keyboard_test.dart index ec69b413da1..772e0ce3f1a 100644 --- a/packages/flutter/test/services/hardware_keyboard_test.dart +++ b/packages/flutter/test/services/hardware_keyboard_test.dart @@ -9,6 +9,11 @@ import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; +void expectState(Map expectedKeys) { + expect(HardwareKeyboard.instance.physicalKeysPressed, equals(expectedKeys.keys.toSet())); + expect(HardwareKeyboard.instance.logicalKeysPressed, equals(expectedKeys.values.toSet())); +} + void main() { testWidgets( 'HardwareKeyboard records pressed keys and enabled locks', @@ -596,6 +601,82 @@ void main() { expect(messagesStr, contains('KEYBOARD: Pressed state before processing the event:')); expect(messagesStr, contains('KEYBOARD: Pressed state after processing the event:')); }); + + testWidgets( + 'Irregular key events are processed', + (WidgetTester tester) async { + final logs = []; + + debugPrintKeyboardEvents = true; + final DebugPrintCallback oldDebugPrint = debugPrint; + debugPrint = (String? message, {int? wrapWidth}) { + if (message != null) { + logs.add(message); + } + }; + + Future expectIsRegular(Future simulateEvent, bool isRegular) async { + // All simulated events should return a `handled` result of false to + // indicate that they are indeed dispatched to handlers. + expect(await simulateEvent, isFalse); + + final String log = logs.join('\n'); + final Matcher errorCheck = contains('ERROR'); + expect(log, isRegular ? isNot(errorCheck) : errorCheck); + logs.clear(); + } + + // 1. Press keyA. + await expectIsRegular(simulateKeyDownEvent(LogicalKeyboardKey.keyA), true); + expectState({ + PhysicalKeyboardKey.keyA: LogicalKeyboardKey.keyA, + }); + // Press keyA again with a mismatched logical key, which should affect the + // state. + await expectIsRegular( + simulateKeyDownEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA), + false, + ); + expectState({ + PhysicalKeyboardKey.keyA: LogicalKeyboardKey.keyB, + }); + + // 2. Release keyA. + await expectIsRegular( + simulateKeyUpEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA), + true, + ); + expectState({}); + + // Release keyA again. + await expectIsRegular(simulateKeyUpEvent(LogicalKeyboardKey.keyA), false); + expectState({}); + + // 3. Send a repeat event for keyA, which should affect the state. + await expectIsRegular(simulateKeyRepeatEvent(LogicalKeyboardKey.keyA), false); + expectState({ + PhysicalKeyboardKey.keyA: LogicalKeyboardKey.keyA, + }); + + // 4. Send a repeat event with a mismatched logical key, which should + // affect the state. + await expectIsRegular( + simulateKeyDownEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA), + false, + ); + expectState({ + PhysicalKeyboardKey.keyA: LogicalKeyboardKey.keyB, + }); + + // 5. Send a key up event with a mismatched logical key, which should affect the state. + await expectIsRegular(simulateKeyUpEvent(LogicalKeyboardKey.keyA), false); + expectState({}); + + debugPrintKeyboardEvents = false; + debugPrint = oldDebugPrint; + }, + variant: KeySimulatorTransitModeVariant.keyDataThenRawKeyData(), + ); } Future _runWhileOverridingOnError( diff --git a/packages/flutter_test/test/event_simulation_test.dart b/packages/flutter_test/test/event_simulation_test.dart index 59d83611df6..bde0c8605c5 100644 --- a/packages/flutter_test/test/event_simulation_test.dart +++ b/packages/flutter_test/test/event_simulation_test.dart @@ -46,6 +46,22 @@ Future _shouldThrow(AsyncValueGetter func) async { } } +Future _shouldDebugPrint(AsyncValueGetter func, String containsString) async { + final printedMessages = []; + final DebugPrintCallback oldDebugPrint = debugPrint; + debugPrint = (String? message, {int? wrapWidth}) { + printedMessages.add(message ?? ''); + }; + + try { + await func(); + } finally { + expect(printedMessages, isNotEmpty); + expect(printedMessages.join('\n'), contains(containsString)); + debugPrint = oldDebugPrint; + } +} + void main() { testWidgets('default transit mode is keyDataThenRawKeyData', (WidgetTester tester) async { expect(KeyEventSimulator.transitMode, KeyDataTransitMode.keyDataThenRawKeyData); @@ -551,15 +567,19 @@ void main() { ); events.clear(); + // Enable irregular key event warning. + debugPrintKeyboardEvents = true; // A (physical keyA, logical keyB) is released. // - // Since this event is transmitted to HardwareKeyboard as-is, it will be rejected due to - // inconsistent logical key. This does not indicate behavioral difference, - // since KeyData is will never send malformed data sequence in real applications. - await _shouldThrow( - () => simulateKeyUpEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA), - ); + // Since this event is transmitted to HardwareKeyboard as-is, it will + // trigger irregular key event warning due to inconsistent logical key. This + // does not indicate behavioral difference, since KeyData will never send + // malformed data sequence in real applications. + await _shouldDebugPrint(() { + return simulateKeyUpEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyA); + }, 'Received unexpected KeyUpEvent for key with mismatched logical key:'); + debugPrintKeyboardEvents = false; debugKeyEventSimulatorTransitModeOverride = null; });