diff --git a/packages/flutter/lib/src/widgets/shortcuts.dart b/packages/flutter/lib/src/widgets/shortcuts.dart index 4723620aef..549f79bc2f 100644 --- a/packages/flutter/lib/src/widgets/shortcuts.dart +++ b/packages/flutter/lib/src/widgets/shortcuts.dart @@ -194,7 +194,7 @@ class ShortcutManager extends ChangeNotifier with DiagnosticableMixin { // If there's not a more specific match, We also look for any keys that // have synonyms in the map. This is for things like left and right shift // keys mapping to just the "shift" pseudo-key. - Set pseudoKeys; + final Set pseudoKeys = {}; for (LogicalKeyboardKey setKey in keySet.keys) { final Set synonyms = setKey.synonyms; if (synonyms.isNotEmpty) { diff --git a/packages/flutter/test/widgets/shortcuts_test.dart b/packages/flutter/test/widgets/shortcuts_test.dart index 46fcc7fa69..0b30585440 100644 --- a/packages/flutter/test/widgets/shortcuts_test.dart +++ b/packages/flutter/test/widgets/shortcuts_test.dart @@ -5,9 +5,103 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter/src/services/keyboard_key.dart'; +import 'package:flutter/src/services/keyboard_maps.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; +void sendFakeKeyEvent(Map data) { + defaultBinaryMessenger.handlePlatformMessage( + SystemChannels.keyEvent.name, + SystemChannels.keyEvent.codec.encodeMessage(data), + (ByteData data) {}, + ); +} + +typedef PostInvokeCallback = void Function({Action action, Intent intent, FocusNode focusNode, ActionDispatcher dispatcher}); + +class TestAction extends CallbackAction { + const TestAction({ + @required OnInvokeCallback onInvoke, + }) : assert(onInvoke != null), + super(key, onInvoke: onInvoke); + + static const LocalKey key = ValueKey(TestAction); +} + +class TestDispatcher extends ActionDispatcher { + const TestDispatcher({this.postInvoke}); + + final PostInvokeCallback postInvoke; + + @override + bool invokeAction(Action action, Intent intent, {FocusNode focusNode}) { + final bool result = super.invokeAction(action, intent, focusNode: focusNode); + postInvoke?.call(action: action, intent: intent, focusNode: focusNode, dispatcher: this); + return result; + } +} + +class TestIntent extends Intent { + const TestIntent() : super(TestAction.key); +} + +class DoNothingAction extends Action { + const DoNothingAction({ + @required OnInvokeCallback onInvoke, + }) : assert(onInvoke != null), + super(key); + + static const LocalKey key = ValueKey(DoNothingAction); + + @override + void invoke(FocusNode node, Intent invocation) {} +} + +class DoNothingIntent extends Intent { + const DoNothingIntent() : super(DoNothingAction.key); +} + +class TestShortcutManager extends ShortcutManager { + TestShortcutManager(this.keys); + + List keys; + + @override + bool handleKeypress(BuildContext context, RawKeyEvent event, {LogicalKeySet keysPressed}) { + keys.add(event.logicalKey); + return super.handleKeypress(context, event, keysPressed: keysPressed); + } +} + +void testKeypress(LogicalKeyboardKey key) { + assert(key.debugName != null); + int keyCode; + kAndroidToLogicalKey.forEach((int code, LogicalKeyboardKey codeKey) { + if (key == codeKey) { + keyCode = code; + } + }); + assert(keyCode != null, 'Key $key not found in Android key map'); + int scanCode; + kAndroidToPhysicalKey.forEach((int code, PhysicalKeyboardKey codeKey) { + if (key.debugName == codeKey.debugName) { + scanCode = code; + } + }); + assert(scanCode != null, 'Physical key for $key not found in Android key map'); + sendFakeKeyEvent({ + 'type': 'keydown', + 'keymap': 'android', + 'keyCode': keyCode, + 'plainCodePoint': 0, + 'codePoint': 0, + 'character': null, + 'scanCode': scanCode, + 'metaState': 0, + }); +} + void main() { group(LogicalKeySet, () { test('$LogicalKeySet passes parameters correctly.', () { @@ -106,10 +200,73 @@ void main() { 'keys: {LogicalKeyboardKey#00000(keyId: "0x00000061", keyLabel: "a", debugName: "Key A"), LogicalKeyboardKey#00000(keyId: "0x00000062", keyLabel: "b", debugName: "Key B")}')); }); }); - group(ShortcutManager, () { - test('$ShortcutManager .', () { - + group(Shortcuts, () { + testWidgets('$ShortcutManager handles shortcuts', (WidgetTester tester) async { + final GlobalKey containerKey = GlobalKey(); + final List pressedKeys = []; + final TestShortcutManager testManager = TestShortcutManager(pressedKeys); + bool invoked = false; + await tester.pumpWidget( + Actions( + actions: { + TestAction.key: () => TestAction(onInvoke: (FocusNode node, Intent intent) { + invoked = true; + return true; + }), + }, + child: Shortcuts( + manager: testManager, + shortcuts: { + LogicalKeySet(LogicalKeyboardKey.shift): const TestIntent(), + }, + child: Focus( + autofocus: true, + child: Container(key: containerKey, width: 100, height: 100), + ), + ), + ), + ); + await tester.pump(); + expect(Shortcuts.of(containerKey.currentContext), isNotNull); + testKeypress(LogicalKeyboardKey.shiftLeft); + expect(invoked, isTrue); + expect(pressedKeys, equals([LogicalKeyboardKey.shiftLeft])); + }); + testWidgets("$Shortcuts passes to the next $Shortcuts widget if it doesn't map the key", (WidgetTester tester) async { + final GlobalKey containerKey = GlobalKey(); + final List pressedKeys = []; + final TestShortcutManager testManager = TestShortcutManager(pressedKeys); + bool invoked = false; + await tester.pumpWidget( + Shortcuts( + manager: testManager, + shortcuts: { + LogicalKeySet(LogicalKeyboardKey.shift): const TestIntent(), + }, + child: Actions( + actions: { + TestAction.key: () => TestAction(onInvoke: (FocusNode node, Intent intent) { + invoked = true; + return true; + }), + }, + child: Shortcuts( + shortcuts: { + LogicalKeySet(LogicalKeyboardKey.keyA): const DoNothingIntent(), + }, + child: Focus( + autofocus: true, + child: Container(key: containerKey, width: 100, height: 100), + ), + ), + ), + ), + ); + await tester.pump(); + expect(Shortcuts.of(containerKey.currentContext), isNotNull); + testKeypress(LogicalKeyboardKey.shiftLeft); + expect(invoked, isTrue); + expect(pressedKeys, equals([LogicalKeyboardKey.shiftLeft])); }); }); - group(Shortcuts, () {}); }