blob: 2ef79999a20d7aa05d75c454ae2cdae221155a9c [file] [log] [blame]
Arpit Singhb3b3f732023-07-04 14:30:05 +00001/*
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
26namespace android {
27
28using testing::_;
Arpit Singh82e413e2023-10-10 19:30:58 +000029using testing::Args;
Arpit Singhb3b3f732023-07-04 14:30:05 +000030using testing::DoAll;
31using testing::Return;
32using testing::SetArgPointee;
33
34/**
35 * Unit tests for KeyboardInputMapper.
36 */
37class KeyboardInputMapperUnitTest : public InputMapperUnitTest {
38protected:
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
68 mMapper = createInputMapper<KeyboardInputMapper>(*mDeviceContext, mReaderConfiguration,
69 AINPUT_SOURCE_KEYBOARD,
70 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
71 }
72
73 void testPointerVisibilityForKeys(const std::vector<int32_t>& keyCodes, bool expectVisible) {
74 EXPECT_CALL(mMockInputReaderContext, fadePointer)
75 .Times(expectVisible ? 0 : keyCodes.size());
76 for (int32_t keyCode : keyCodes) {
77 process(EV_KEY, keyCode, 1);
78 process(EV_SYN, SYN_REPORT, 0);
79 process(EV_KEY, keyCode, 0);
80 process(EV_SYN, SYN_REPORT, 0);
81 }
82 }
Arpit Singha5ea7c12023-07-05 15:39:25 +000083
84 void testTouchpadTapStateForKeys(const std::vector<int32_t>& keyCodes,
85 const bool expectPrevent) {
86 EXPECT_CALL(mMockInputReaderContext, isPreventingTouchpadTaps).Times(keyCodes.size());
87 if (expectPrevent) {
88 EXPECT_CALL(mMockInputReaderContext, setPreventingTouchpadTaps(true))
89 .Times(keyCodes.size());
90 }
91 for (int32_t keyCode : keyCodes) {
92 process(EV_KEY, keyCode, 1);
93 process(EV_SYN, SYN_REPORT, 0);
94 process(EV_KEY, keyCode, 0);
95 process(EV_SYN, SYN_REPORT, 0);
96 }
97 }
Arpit Singhb3b3f732023-07-04 14:30:05 +000098};
99
100/**
101 * Pointer visibility should remain unaffected if there is no active Input Method Connection
102 */
103TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDoesNotHidePointer) {
104 testPointerVisibilityForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectVisible= */ true);
105}
106
107/**
108 * Pointer should hide if there is a active Input Method Connection
109 */
110TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionHidePointer) {
111 mFakePolicy->setIsInputMethodConnectionActive(true);
112 testPointerVisibilityForKeys({KEY_0, KEY_A}, /* expectVisible= */ false);
113}
114
115/**
Arpit Singhc00f5e92023-09-19 13:11:05 +0000116 * Pointer should still hide if touchpad taps are already disabled
117 */
118TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithTouchpadTapDisabledHidePointer) {
119 mFakePolicy->setIsInputMethodConnectionActive(true);
120 EXPECT_CALL(mMockInputReaderContext, isPreventingTouchpadTaps).WillRepeatedly(Return(true));
121 testPointerVisibilityForKeys({KEY_0, KEY_A}, /* expectVisible= */ false);
122}
123
124/**
Arpit Singhb3b3f732023-07-04 14:30:05 +0000125 * Pointer visibility should remain unaffected by meta keys even if Input Method Connection is
126 * active
127 */
128TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDoesNotHidePointer) {
129 mFakePolicy->setIsInputMethodConnectionActive(true);
130 std::vector<int32_t> metaKeys{KEY_LEFTALT, KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
131 KEY_FN, KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA,
132 KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK, KEY_SCROLLLOCK};
133 testPointerVisibilityForKeys(metaKeys, /* expectVisible= */ true);
134}
135
Arpit Singha5ea7c12023-07-05 15:39:25 +0000136/**
137 * Touchpad tap should not be disabled if there is no active Input Method Connection
138 */
139TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDontDisableTouchpadTap) {
140 testTouchpadTapStateForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectPrevent= */ false);
141}
142
143/**
144 * Touchpad tap should be disabled if there is a active Input Method Connection
145 */
146TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionDisableTouchpadTap) {
147 mFakePolicy->setIsInputMethodConnectionActive(true);
148 testTouchpadTapStateForKeys({KEY_0, KEY_A}, /* expectPrevent= */ true);
149}
150
151/**
152 * Touchpad tap should not be disabled by meta keys even if Input Method Connection is active
153 */
154TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDontDisableTouchpadTap) {
155 mFakePolicy->setIsInputMethodConnectionActive(true);
156 std::vector<int32_t> metaKeys{KEY_LEFTALT, KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
157 KEY_FN, KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA,
158 KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK, KEY_SCROLLLOCK};
159 testTouchpadTapStateForKeys(metaKeys, /* expectPrevent= */ false);
160}
161
Arpit Singh82e413e2023-10-10 19:30:58 +0000162TEST_F(KeyboardInputMapperUnitTest, KeyPressTimestampRecorded) {
163 nsecs_t when = ARBITRARY_TIME;
164 std::vector<int32_t> keyCodes{KEY_0, KEY_A, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTSHIFT};
165 EXPECT_CALL(mMockInputReaderContext, setLastKeyDownTimestamp)
166 .With(Args<0>(when))
167 .Times(keyCodes.size());
168 for (int32_t keyCode : keyCodes) {
169 process(when, EV_KEY, keyCode, 1);
170 process(when, EV_SYN, SYN_REPORT, 0);
171 process(when, EV_KEY, keyCode, 0);
172 process(when, EV_SYN, SYN_REPORT, 0);
173 }
174}
175
Arpit Singhb3b3f732023-07-04 14:30:05 +0000176} // namespace android