• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::DoAll;
30 using testing::Return;
31 using testing::SetArgPointee;
32 
33 /**
34  * Unit tests for KeyboardInputMapper.
35  */
36 class KeyboardInputMapperUnitTest : public InputMapperUnitTest {
37 protected:
38     sp<FakeInputReaderPolicy> mFakePolicy;
39     const std::unordered_map<int32_t, int32_t> mKeyCodeMap{{KEY_0, AKEYCODE_0},
40                                                            {KEY_A, AKEYCODE_A},
41                                                            {KEY_LEFTCTRL, AKEYCODE_CTRL_LEFT},
42                                                            {KEY_LEFTALT, AKEYCODE_ALT_LEFT},
43                                                            {KEY_RIGHTALT, AKEYCODE_ALT_RIGHT},
44                                                            {KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT},
45                                                            {KEY_RIGHTSHIFT, AKEYCODE_SHIFT_RIGHT},
46                                                            {KEY_FN, AKEYCODE_FUNCTION},
47                                                            {KEY_LEFTCTRL, AKEYCODE_CTRL_LEFT},
48                                                            {KEY_RIGHTCTRL, AKEYCODE_CTRL_RIGHT},
49                                                            {KEY_LEFTMETA, AKEYCODE_META_LEFT},
50                                                            {KEY_RIGHTMETA, AKEYCODE_META_RIGHT},
51                                                            {KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK},
52                                                            {KEY_NUMLOCK, AKEYCODE_NUM_LOCK},
53                                                            {KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK}};
54 
SetUp()55     void SetUp() override {
56         InputMapperUnitTest::SetUp();
57 
58         // set key-codes expected in tests
59         for (const auto& [scanCode, outKeycode] : mKeyCodeMap) {
60             EXPECT_CALL(mMockEventHub, mapKey(EVENTHUB_ID, scanCode, _, _, _, _, _))
61                     .WillRepeatedly(DoAll(SetArgPointee<4>(outKeycode), Return(NO_ERROR)));
62         }
63 
64         mFakePolicy = sp<FakeInputReaderPolicy>::make();
65         EXPECT_CALL(mMockInputReaderContext, getPolicy).WillRepeatedly(Return(mFakePolicy.get()));
66 
67         mMapper = createInputMapper<KeyboardInputMapper>(*mDeviceContext, mReaderConfiguration,
68                                                          AINPUT_SOURCE_KEYBOARD,
69                                                          AINPUT_KEYBOARD_TYPE_ALPHABETIC);
70     }
71 
testPointerVisibilityForKeys(const std::vector<int32_t> & keyCodes,bool expectVisible)72     void testPointerVisibilityForKeys(const std::vector<int32_t>& keyCodes, bool expectVisible) {
73         EXPECT_CALL(mMockInputReaderContext, fadePointer)
74                 .Times(expectVisible ? 0 : keyCodes.size());
75         for (int32_t keyCode : keyCodes) {
76             process(EV_KEY, keyCode, 1);
77             process(EV_SYN, SYN_REPORT, 0);
78             process(EV_KEY, keyCode, 0);
79             process(EV_SYN, SYN_REPORT, 0);
80         }
81     }
82 
testTouchpadTapStateForKeys(const std::vector<int32_t> & keyCodes,const bool expectPrevent)83     void testTouchpadTapStateForKeys(const std::vector<int32_t>& keyCodes,
84                                      const bool expectPrevent) {
85         EXPECT_CALL(mMockInputReaderContext, isPreventingTouchpadTaps).Times(keyCodes.size());
86         if (expectPrevent) {
87             EXPECT_CALL(mMockInputReaderContext, setPreventingTouchpadTaps(true))
88                     .Times(keyCodes.size());
89         }
90         for (int32_t keyCode : keyCodes) {
91             process(EV_KEY, keyCode, 1);
92             process(EV_SYN, SYN_REPORT, 0);
93             process(EV_KEY, keyCode, 0);
94             process(EV_SYN, SYN_REPORT, 0);
95         }
96     }
97 };
98 
99 /**
100  * Pointer visibility should remain unaffected if there is no active Input Method Connection
101  */
TEST_F(KeyboardInputMapperUnitTest,KeystrokesWithoutIMeConnectionDoesNotHidePointer)102 TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDoesNotHidePointer) {
103     testPointerVisibilityForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectVisible= */ true);
104 }
105 
106 /**
107  * Pointer should hide if there is a active Input Method Connection
108  */
TEST_F(KeyboardInputMapperUnitTest,AlphanumericKeystrokesWithIMeConnectionHidePointer)109 TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionHidePointer) {
110     mFakePolicy->setIsInputMethodConnectionActive(true);
111     testPointerVisibilityForKeys({KEY_0, KEY_A}, /* expectVisible= */ false);
112 }
113 
114 /**
115  * Pointer visibility should remain unaffected by meta keys even if Input Method Connection is
116  * active
117  */
TEST_F(KeyboardInputMapperUnitTest,MetaKeystrokesWithIMeConnectionDoesNotHidePointer)118 TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDoesNotHidePointer) {
119     mFakePolicy->setIsInputMethodConnectionActive(true);
120     std::vector<int32_t> metaKeys{KEY_LEFTALT,   KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
121                                   KEY_FN,        KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA,
122                                   KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK,   KEY_SCROLLLOCK};
123     testPointerVisibilityForKeys(metaKeys, /* expectVisible= */ true);
124 }
125 
126 /**
127  * Touchpad tap should not be disabled if there is no active Input Method Connection
128  */
TEST_F(KeyboardInputMapperUnitTest,KeystrokesWithoutIMeConnectionDontDisableTouchpadTap)129 TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDontDisableTouchpadTap) {
130     testTouchpadTapStateForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectPrevent= */ false);
131 }
132 
133 /**
134  * Touchpad tap should be disabled if there is a active Input Method Connection
135  */
TEST_F(KeyboardInputMapperUnitTest,AlphanumericKeystrokesWithIMeConnectionDisableTouchpadTap)136 TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionDisableTouchpadTap) {
137     mFakePolicy->setIsInputMethodConnectionActive(true);
138     testTouchpadTapStateForKeys({KEY_0, KEY_A}, /* expectPrevent= */ true);
139 }
140 
141 /**
142  * Touchpad tap should not be disabled by meta keys even if Input Method Connection is active
143  */
TEST_F(KeyboardInputMapperUnitTest,MetaKeystrokesWithIMeConnectionDontDisableTouchpadTap)144 TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDontDisableTouchpadTap) {
145     mFakePolicy->setIsInputMethodConnectionActive(true);
146     std::vector<int32_t> metaKeys{KEY_LEFTALT,   KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
147                                   KEY_FN,        KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA,
148                                   KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK,   KEY_SCROLLLOCK};
149     testTouchpadTapStateForKeys(metaKeys, /* expectPrevent= */ false);
150 }
151 
152 } // namespace android
153