Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "KeyboardInputMapper.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include "InputMapperTest.h" |
| 22 | #include "InterfaceMocks.h" |
| 23 | |
| 24 | #define TAG "KeyboardInputMapper_test" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | using testing::_; |
Arpit Singh | 82e413e | 2023-10-10 19:30:58 +0000 | [diff] [blame] | 29 | using testing::Args; |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 30 | using testing::DoAll; |
| 31 | using testing::Return; |
| 32 | using testing::SetArgPointee; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for KeyboardInputMapper. |
| 36 | */ |
| 37 | class KeyboardInputMapperUnitTest : public InputMapperUnitTest { |
| 38 | protected: |
| 39 | sp<FakeInputReaderPolicy> mFakePolicy; |
| 40 | const std::unordered_map<int32_t, int32_t> mKeyCodeMap{{KEY_0, AKEYCODE_0}, |
| 41 | {KEY_A, AKEYCODE_A}, |
| 42 | {KEY_LEFTCTRL, AKEYCODE_CTRL_LEFT}, |
| 43 | {KEY_LEFTALT, AKEYCODE_ALT_LEFT}, |
| 44 | {KEY_RIGHTALT, AKEYCODE_ALT_RIGHT}, |
| 45 | {KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT}, |
| 46 | {KEY_RIGHTSHIFT, AKEYCODE_SHIFT_RIGHT}, |
| 47 | {KEY_FN, AKEYCODE_FUNCTION}, |
| 48 | {KEY_LEFTCTRL, AKEYCODE_CTRL_LEFT}, |
| 49 | {KEY_RIGHTCTRL, AKEYCODE_CTRL_RIGHT}, |
| 50 | {KEY_LEFTMETA, AKEYCODE_META_LEFT}, |
| 51 | {KEY_RIGHTMETA, AKEYCODE_META_RIGHT}, |
| 52 | {KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK}, |
| 53 | {KEY_NUMLOCK, AKEYCODE_NUM_LOCK}, |
| 54 | {KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK}}; |
| 55 | |
| 56 | void SetUp() override { |
| 57 | InputMapperUnitTest::SetUp(); |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame^] | 58 | createDevice(); |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 59 | |
| 60 | // set key-codes expected in tests |
| 61 | for (const auto& [scanCode, outKeycode] : mKeyCodeMap) { |
| 62 | EXPECT_CALL(mMockEventHub, mapKey(EVENTHUB_ID, scanCode, _, _, _, _, _)) |
| 63 | .WillRepeatedly(DoAll(SetArgPointee<4>(outKeycode), Return(NO_ERROR))); |
| 64 | } |
| 65 | |
| 66 | mFakePolicy = sp<FakeInputReaderPolicy>::make(); |
| 67 | EXPECT_CALL(mMockInputReaderContext, getPolicy).WillRepeatedly(Return(mFakePolicy.get())); |
| 68 | |
| 69 | mMapper = createInputMapper<KeyboardInputMapper>(*mDeviceContext, mReaderConfiguration, |
| 70 | AINPUT_SOURCE_KEYBOARD, |
| 71 | AINPUT_KEYBOARD_TYPE_ALPHABETIC); |
| 72 | } |
| 73 | |
| 74 | void testPointerVisibilityForKeys(const std::vector<int32_t>& keyCodes, bool expectVisible) { |
| 75 | EXPECT_CALL(mMockInputReaderContext, fadePointer) |
| 76 | .Times(expectVisible ? 0 : keyCodes.size()); |
| 77 | for (int32_t keyCode : keyCodes) { |
| 78 | process(EV_KEY, keyCode, 1); |
| 79 | process(EV_SYN, SYN_REPORT, 0); |
| 80 | process(EV_KEY, keyCode, 0); |
| 81 | process(EV_SYN, SYN_REPORT, 0); |
| 82 | } |
| 83 | } |
Arpit Singh | a5ea7c1 | 2023-07-05 15:39:25 +0000 | [diff] [blame] | 84 | |
| 85 | void testTouchpadTapStateForKeys(const std::vector<int32_t>& keyCodes, |
| 86 | const bool expectPrevent) { |
| 87 | EXPECT_CALL(mMockInputReaderContext, isPreventingTouchpadTaps).Times(keyCodes.size()); |
| 88 | if (expectPrevent) { |
| 89 | EXPECT_CALL(mMockInputReaderContext, setPreventingTouchpadTaps(true)) |
| 90 | .Times(keyCodes.size()); |
| 91 | } |
| 92 | for (int32_t keyCode : keyCodes) { |
| 93 | process(EV_KEY, keyCode, 1); |
| 94 | process(EV_SYN, SYN_REPORT, 0); |
| 95 | process(EV_KEY, keyCode, 0); |
| 96 | process(EV_SYN, SYN_REPORT, 0); |
| 97 | } |
| 98 | } |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * Pointer visibility should remain unaffected if there is no active Input Method Connection |
| 103 | */ |
| 104 | TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDoesNotHidePointer) { |
| 105 | testPointerVisibilityForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectVisible= */ true); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Pointer should hide if there is a active Input Method Connection |
| 110 | */ |
| 111 | TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionHidePointer) { |
| 112 | mFakePolicy->setIsInputMethodConnectionActive(true); |
| 113 | testPointerVisibilityForKeys({KEY_0, KEY_A}, /* expectVisible= */ false); |
| 114 | } |
| 115 | |
| 116 | /** |
Arpit Singh | c00f5e9 | 2023-09-19 13:11:05 +0000 | [diff] [blame] | 117 | * Pointer should still hide if touchpad taps are already disabled |
| 118 | */ |
| 119 | TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithTouchpadTapDisabledHidePointer) { |
| 120 | mFakePolicy->setIsInputMethodConnectionActive(true); |
| 121 | EXPECT_CALL(mMockInputReaderContext, isPreventingTouchpadTaps).WillRepeatedly(Return(true)); |
| 122 | testPointerVisibilityForKeys({KEY_0, KEY_A}, /* expectVisible= */ false); |
| 123 | } |
| 124 | |
| 125 | /** |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 126 | * Pointer visibility should remain unaffected by meta keys even if Input Method Connection is |
| 127 | * active |
| 128 | */ |
| 129 | TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDoesNotHidePointer) { |
| 130 | mFakePolicy->setIsInputMethodConnectionActive(true); |
| 131 | std::vector<int32_t> metaKeys{KEY_LEFTALT, KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT, |
| 132 | KEY_FN, KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA, |
| 133 | KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK, KEY_SCROLLLOCK}; |
| 134 | testPointerVisibilityForKeys(metaKeys, /* expectVisible= */ true); |
| 135 | } |
| 136 | |
Arpit Singh | a5ea7c1 | 2023-07-05 15:39:25 +0000 | [diff] [blame] | 137 | /** |
| 138 | * Touchpad tap should not be disabled if there is no active Input Method Connection |
| 139 | */ |
| 140 | TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDontDisableTouchpadTap) { |
| 141 | testTouchpadTapStateForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectPrevent= */ false); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Touchpad tap should be disabled if there is a active Input Method Connection |
| 146 | */ |
| 147 | TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionDisableTouchpadTap) { |
| 148 | mFakePolicy->setIsInputMethodConnectionActive(true); |
| 149 | testTouchpadTapStateForKeys({KEY_0, KEY_A}, /* expectPrevent= */ true); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Touchpad tap should not be disabled by meta keys even if Input Method Connection is active |
| 154 | */ |
| 155 | TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDontDisableTouchpadTap) { |
| 156 | mFakePolicy->setIsInputMethodConnectionActive(true); |
| 157 | std::vector<int32_t> metaKeys{KEY_LEFTALT, KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT, |
| 158 | KEY_FN, KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA, |
| 159 | KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK, KEY_SCROLLLOCK}; |
| 160 | testTouchpadTapStateForKeys(metaKeys, /* expectPrevent= */ false); |
| 161 | } |
| 162 | |
Arpit Singh | 82e413e | 2023-10-10 19:30:58 +0000 | [diff] [blame] | 163 | TEST_F(KeyboardInputMapperUnitTest, KeyPressTimestampRecorded) { |
| 164 | nsecs_t when = ARBITRARY_TIME; |
| 165 | std::vector<int32_t> keyCodes{KEY_0, KEY_A, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTSHIFT}; |
| 166 | EXPECT_CALL(mMockInputReaderContext, setLastKeyDownTimestamp) |
| 167 | .With(Args<0>(when)) |
| 168 | .Times(keyCodes.size()); |
| 169 | for (int32_t keyCode : keyCodes) { |
| 170 | process(when, EV_KEY, keyCode, 1); |
| 171 | process(when, EV_SYN, SYN_REPORT, 0); |
| 172 | process(when, EV_KEY, keyCode, 0); |
| 173 | process(when, EV_SYN, SYN_REPORT, 0); |
| 174 | } |
| 175 | } |
| 176 | |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 177 | } // namespace android |