From ab5cdaed399f03487b297c1dab3a653cc1639295 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Mon, 22 Jul 2024 09:43:31 -0700 Subject: [PATCH 1/3] Remove incorrect line --- .../io/flutter/embedding/android/KeyEmbedderResponder.java | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/android/io/flutter/embedding/android/KeyEmbedderResponder.java b/shell/platform/android/io/flutter/embedding/android/KeyEmbedderResponder.java index eb42164d833ad..de5af6b0ff013 100644 --- a/shell/platform/android/io/flutter/embedding/android/KeyEmbedderResponder.java +++ b/shell/platform/android/io/flutter/embedding/android/KeyEmbedderResponder.java @@ -369,7 +369,6 @@ private boolean handleEventImpl( output.physicalKey = physicalKey; output.character = character; output.synthesized = false; - output.deviceType = KeyData.DeviceType.kKeyboard; sendKeyEvent(output, onKeyEventHandledCallback); for (final Runnable postSyncEvent : postSynchronizeEvents) { From df8e0238fba539fdf8e1c2eeb28bed8ab3770cea Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Tue, 23 Jul 2024 14:03:11 -0700 Subject: [PATCH 2/3] Add test --- .../android/KeyboardManagerTest.java | 60 ++++++++++++++++++- .../test/io/flutter/util/FakeKeyEvent.java | 6 ++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java b/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java index 8cf0c7e4629ab..c48e459cd95f7 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java @@ -14,6 +14,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import android.view.InputDevice; import android.view.KeyCharacterMap; import android.view.KeyEvent; import androidx.annotation.NonNull; @@ -88,7 +89,7 @@ enum Kind { /** * Construct an empty call record. * - *

Use the static functions to constuct specific types instead. + *

Use the static functions to construct specific types instead. */ private CallRecord() {} @@ -1921,4 +1922,61 @@ public void getKeyboardState() { new FakeKeyEvent(ACTION_UP, SCAN_KEY_A, KEYCODE_A, 0, 'a', 0)); assertEquals(tester.keyboardManager.getKeyboardState(), Map.of()); } + + @Test + public void deviceTypeFromInputDevice() { + final KeyboardTester tester = new KeyboardTester(); + final ArrayList calls = new ArrayList<>(); + + tester.recordEmbedderCallsTo(calls); + tester.respondToTextInputWith(true); + + // Keyboard + final KeyEvent keyboardEvent = new FakeKeyEvent( + ACTION_DOWN, SCAN_KEY_A, KEYCODE_A, 0, 'a', 0, InputDevice.SOURCE_KEYBOARD); + assertEquals(true, tester.keyboardManager.handleEvent(keyboardEvent)); + verifyEmbedderEvents(calls, + new KeyData[] { + buildKeyData(Type.kDown, PHYSICAL_KEY_A, LOGICAL_KEY_A, "a", false, + DeviceType.kKeyboard), + }); + calls.clear(); + + // Directional pad + final KeyEvent directionalPadEvent = new FakeKeyEvent( + ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_DPAD_LEFT, 0, '\0', 0, InputDevice.SOURCE_DPAD); + assertEquals(true, tester.keyboardManager.handleEvent(directionalPadEvent)); + verifyEmbedderEvents(calls, + new KeyData[] { + buildKeyData(Type.kDown, PHYSICAL_ARROW_LEFT, LOGICAL_ARROW_LEFT, null, false, + DeviceType.kDirectionalPad), + }); + calls.clear(); + + // Gamepad + final KeyEvent gamepadEvent = new FakeKeyEvent(ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_BUTTON_A, + 0, '\0', 0, InputDevice.SOURCE_GAMEPAD); + assertEquals(true, tester.keyboardManager.handleEvent(gamepadEvent)); + verifyEmbedderEvents(calls, + new KeyData[] { + buildKeyData(Type.kUp, PHYSICAL_ARROW_LEFT, LOGICAL_ARROW_LEFT, null, true, + DeviceType.kKeyboard), + buildKeyData(Type.kDown, PHYSICAL_ARROW_LEFT, LOGICAL_GAME_BUTTON_A, null, + false, DeviceType.kGamepad), + }); + calls.clear(); + + // HDMI + final KeyEvent hdmiEvent = new FakeKeyEvent( + ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_BUTTON_A, 0, '\0', 0, InputDevice.SOURCE_HDMI); + assertEquals(true, tester.keyboardManager.handleEvent(hdmiEvent)); + verifyEmbedderEvents(calls, + new KeyData[] { + buildKeyData(Type.kUp, PHYSICAL_ARROW_LEFT, LOGICAL_GAME_BUTTON_A, null, true, + DeviceType.kKeyboard), + buildKeyData(Type.kDown, PHYSICAL_ARROW_LEFT, LOGICAL_GAME_BUTTON_A, null, + false, DeviceType.kHdmi), + }); + calls.clear(); + } } diff --git a/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java b/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java index 4faa39107f50b..35ef6731d6ec8 100644 --- a/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java +++ b/shell/platform/android/test/io/flutter/util/FakeKeyEvent.java @@ -20,6 +20,12 @@ public FakeKeyEvent( this.character = character; } + public FakeKeyEvent( + int action, int scancode, int code, int repeat, char character, int metaState, int source) { + super(0, 0, action, code, repeat, metaState, 0, scancode, 0, source); + this.character = character; + } + private char character = 0; public final int getUnicodeChar() { From 6fc471d18b52222abb1a0ac890f22fa86a32dca9 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Wed, 24 Jul 2024 09:10:15 -0700 Subject: [PATCH 3/3] Fix formatting --- .../android/KeyboardManagerTest.java | 133 +++++++++++------- 1 file changed, 80 insertions(+), 53 deletions(-) diff --git a/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java b/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java index c48e459cd95f7..d59d01ffb5e8c 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/KeyboardManagerTest.java @@ -1925,58 +1925,85 @@ public void getKeyboardState() { @Test public void deviceTypeFromInputDevice() { - final KeyboardTester tester = new KeyboardTester(); - final ArrayList calls = new ArrayList<>(); - - tester.recordEmbedderCallsTo(calls); - tester.respondToTextInputWith(true); - - // Keyboard - final KeyEvent keyboardEvent = new FakeKeyEvent( - ACTION_DOWN, SCAN_KEY_A, KEYCODE_A, 0, 'a', 0, InputDevice.SOURCE_KEYBOARD); - assertEquals(true, tester.keyboardManager.handleEvent(keyboardEvent)); - verifyEmbedderEvents(calls, - new KeyData[] { - buildKeyData(Type.kDown, PHYSICAL_KEY_A, LOGICAL_KEY_A, "a", false, - DeviceType.kKeyboard), - }); - calls.clear(); - - // Directional pad - final KeyEvent directionalPadEvent = new FakeKeyEvent( - ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_DPAD_LEFT, 0, '\0', 0, InputDevice.SOURCE_DPAD); - assertEquals(true, tester.keyboardManager.handleEvent(directionalPadEvent)); - verifyEmbedderEvents(calls, - new KeyData[] { - buildKeyData(Type.kDown, PHYSICAL_ARROW_LEFT, LOGICAL_ARROW_LEFT, null, false, - DeviceType.kDirectionalPad), - }); - calls.clear(); - - // Gamepad - final KeyEvent gamepadEvent = new FakeKeyEvent(ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_BUTTON_A, - 0, '\0', 0, InputDevice.SOURCE_GAMEPAD); - assertEquals(true, tester.keyboardManager.handleEvent(gamepadEvent)); - verifyEmbedderEvents(calls, - new KeyData[] { - buildKeyData(Type.kUp, PHYSICAL_ARROW_LEFT, LOGICAL_ARROW_LEFT, null, true, - DeviceType.kKeyboard), - buildKeyData(Type.kDown, PHYSICAL_ARROW_LEFT, LOGICAL_GAME_BUTTON_A, null, - false, DeviceType.kGamepad), - }); - calls.clear(); - - // HDMI - final KeyEvent hdmiEvent = new FakeKeyEvent( - ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_BUTTON_A, 0, '\0', 0, InputDevice.SOURCE_HDMI); - assertEquals(true, tester.keyboardManager.handleEvent(hdmiEvent)); - verifyEmbedderEvents(calls, - new KeyData[] { - buildKeyData(Type.kUp, PHYSICAL_ARROW_LEFT, LOGICAL_GAME_BUTTON_A, null, true, - DeviceType.kKeyboard), - buildKeyData(Type.kDown, PHYSICAL_ARROW_LEFT, LOGICAL_GAME_BUTTON_A, null, - false, DeviceType.kHdmi), - }); - calls.clear(); + final KeyboardTester tester = new KeyboardTester(); + final ArrayList calls = new ArrayList<>(); + + tester.recordEmbedderCallsTo(calls); + tester.respondToTextInputWith(true); + + // Keyboard + final KeyEvent keyboardEvent = + new FakeKeyEvent( + ACTION_DOWN, SCAN_KEY_A, KEYCODE_A, 0, 'a', 0, InputDevice.SOURCE_KEYBOARD); + assertEquals(true, tester.keyboardManager.handleEvent(keyboardEvent)); + verifyEmbedderEvents( + calls, + new KeyData[] { + buildKeyData(Type.kDown, PHYSICAL_KEY_A, LOGICAL_KEY_A, "a", false, DeviceType.kKeyboard), + }); + calls.clear(); + + // Directional pad + final KeyEvent directionalPadEvent = + new FakeKeyEvent( + ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_DPAD_LEFT, 0, '\0', 0, InputDevice.SOURCE_DPAD); + assertEquals(true, tester.keyboardManager.handleEvent(directionalPadEvent)); + verifyEmbedderEvents( + calls, + new KeyData[] { + buildKeyData( + Type.kDown, + PHYSICAL_ARROW_LEFT, + LOGICAL_ARROW_LEFT, + null, + false, + DeviceType.kDirectionalPad), + }); + calls.clear(); + + // Gamepad + final KeyEvent gamepadEvent = + new FakeKeyEvent( + ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_BUTTON_A, 0, '\0', 0, InputDevice.SOURCE_GAMEPAD); + assertEquals(true, tester.keyboardManager.handleEvent(gamepadEvent)); + verifyEmbedderEvents( + calls, + new KeyData[] { + buildKeyData( + Type.kUp, PHYSICAL_ARROW_LEFT, LOGICAL_ARROW_LEFT, null, true, DeviceType.kKeyboard), + buildKeyData( + Type.kDown, + PHYSICAL_ARROW_LEFT, + LOGICAL_GAME_BUTTON_A, + null, + false, + DeviceType.kGamepad), + }); + calls.clear(); + + // HDMI + final KeyEvent hdmiEvent = + new FakeKeyEvent( + ACTION_DOWN, SCAN_ARROW_LEFT, KEYCODE_BUTTON_A, 0, '\0', 0, InputDevice.SOURCE_HDMI); + assertEquals(true, tester.keyboardManager.handleEvent(hdmiEvent)); + verifyEmbedderEvents( + calls, + new KeyData[] { + buildKeyData( + Type.kUp, + PHYSICAL_ARROW_LEFT, + LOGICAL_GAME_BUTTON_A, + null, + true, + DeviceType.kKeyboard), + buildKeyData( + Type.kDown, + PHYSICAL_ARROW_LEFT, + LOGICAL_GAME_BUTTON_A, + null, + false, + DeviceType.kHdmi), + }); + calls.clear(); } }