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(); |
| 58 | |
| 59 | // set key-codes expected in tests |
| 60 | for (const auto& [scanCode, outKeycode] : mKeyCodeMap) { |
| 61 | EXPECT_CALL(mMockEventHub, mapKey(EVENTHUB_ID, scanCode, _, _, _, _, _)) |
| 62 | .WillRepeatedly(DoAll(SetArgPointee<4>(outKeycode), Return(NO_ERROR))); |
| 63 | } |
| 64 | |
| 65 | mFakePolicy = sp<FakeInputReaderPolicy>::make(); |
| 66 | EXPECT_CALL(mMockInputReaderContext, getPolicy).WillRepeatedly(Return(mFakePolicy.get())); |
| 67 | |
Prabir Pradhan | 3863665 | 2024-07-23 21:59:36 +0000 | [diff] [blame] | 68 | ON_CALL((*mDevice), getSources).WillByDefault(Return(AINPUT_SOURCE_KEYBOARD)); |
| 69 | |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 70 | mMapper = createInputMapper<KeyboardInputMapper>(*mDeviceContext, mReaderConfiguration, |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 71 | AINPUT_SOURCE_KEYBOARD); |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 72 | } |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Arpit Singh | 82e413e | 2023-10-10 19:30:58 +0000 | [diff] [blame] | 75 | TEST_F(KeyboardInputMapperUnitTest, KeyPressTimestampRecorded) { |
| 76 | nsecs_t when = ARBITRARY_TIME; |
| 77 | std::vector<int32_t> keyCodes{KEY_0, KEY_A, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTSHIFT}; |
| 78 | EXPECT_CALL(mMockInputReaderContext, setLastKeyDownTimestamp) |
| 79 | .With(Args<0>(when)) |
| 80 | .Times(keyCodes.size()); |
| 81 | for (int32_t keyCode : keyCodes) { |
| 82 | process(when, EV_KEY, keyCode, 1); |
| 83 | process(when, EV_SYN, SYN_REPORT, 0); |
| 84 | process(when, EV_KEY, keyCode, 0); |
| 85 | process(when, EV_SYN, SYN_REPORT, 0); |
| 86 | } |
| 87 | } |
| 88 | |
Arpit Singh | b3b3f73 | 2023-07-04 14:30:05 +0000 | [diff] [blame] | 89 | } // namespace android |