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::_;
29 using testing::Args;
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 
SetUp()56     void SetUp() override {
57         InputMapperUnitTest::SetUp();
58         createDevice();
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     }
72 
testTouchpadTapStateForKeys(const std::vector<int32_t> & keyCodes,const bool expectPrevent)73     void testTouchpadTapStateForKeys(const std::vector<int32_t>& keyCodes,
74                                      const bool expectPrevent) {
75         if (expectPrevent) {
76             EXPECT_CALL(mMockInputReaderContext, setPreventingTouchpadTaps(true))
77                     .Times(keyCodes.size());
78         }
79         for (int32_t keyCode : keyCodes) {
80             process(EV_KEY, keyCode, 1);
81             process(EV_SYN, SYN_REPORT, 0);
82             process(EV_KEY, keyCode, 0);
83             process(EV_SYN, SYN_REPORT, 0);
84         }
85     }
86 };
87 
88 /**
89  * Touchpad tap should not be disabled if there is no active Input Method Connection
90  */
TEST_F(KeyboardInputMapperUnitTest,KeystrokesWithoutIMeConnectionDontDisableTouchpadTap)91 TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDontDisableTouchpadTap) {
92     testTouchpadTapStateForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectPrevent= */ false);
93 }
94 
95 /**
96  * Touchpad tap should be disabled if there is a active Input Method Connection
97  */
TEST_F(KeyboardInputMapperUnitTest,AlphanumericKeystrokesWithIMeConnectionDisableTouchpadTap)98 TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionDisableTouchpadTap) {
99     mFakePolicy->setIsInputMethodConnectionActive(true);
100     testTouchpadTapStateForKeys({KEY_0, KEY_A}, /* expectPrevent= */ true);
101 }
102 
103 /**
104  * Touchpad tap should not be disabled by meta keys even if Input Method Connection is active
105  */
TEST_F(KeyboardInputMapperUnitTest,MetaKeystrokesWithIMeConnectionDontDisableTouchpadTap)106 TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDontDisableTouchpadTap) {
107     mFakePolicy->setIsInputMethodConnectionActive(true);
108     std::vector<int32_t> metaKeys{KEY_LEFTALT,   KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
109                                   KEY_FN,        KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA,
110                                   KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK,   KEY_SCROLLLOCK};
111     testTouchpadTapStateForKeys(metaKeys, /* expectPrevent= */ false);
112 }
113 
TEST_F(KeyboardInputMapperUnitTest,KeyPressTimestampRecorded)114 TEST_F(KeyboardInputMapperUnitTest, KeyPressTimestampRecorded) {
115     nsecs_t when = ARBITRARY_TIME;
116     std::vector<int32_t> keyCodes{KEY_0, KEY_A, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTSHIFT};
117     EXPECT_CALL(mMockInputReaderContext, setLastKeyDownTimestamp)
118             .With(Args<0>(when))
119             .Times(keyCodes.size());
120     for (int32_t keyCode : keyCodes) {
121         process(when, EV_KEY, keyCode, 1);
122         process(when, EV_SYN, SYN_REPORT, 0);
123         process(when, EV_KEY, keyCode, 0);
124         process(when, EV_SYN, SYN_REPORT, 0);
125     }
126 }
127 
128 } // namespace android
129