blob: ada841d28d294b81b7c9a62d2d267af607af25d9 [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();
Harry Cutts1b5bde42023-12-18 16:08:29 +000058 createDevice();
Arpit Singhb3b3f732023-07-04 14:30:05 +000059
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,
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000070 AINPUT_SOURCE_KEYBOARD);
Arpit Singhb3b3f732023-07-04 14:30:05 +000071 }
72
73 void testPointerVisibilityForKeys(const std::vector<int32_t>& keyCodes, bool expectVisible) {
Arpit Singhb3b3f732023-07-04 14:30:05 +000074 for (int32_t keyCode : keyCodes) {
75 process(EV_KEY, keyCode, 1);
76 process(EV_SYN, SYN_REPORT, 0);
77 process(EV_KEY, keyCode, 0);
78 process(EV_SYN, SYN_REPORT, 0);
79 }
80 }
Arpit Singha5ea7c12023-07-05 15:39:25 +000081
82 void testTouchpadTapStateForKeys(const std::vector<int32_t>& keyCodes,
83 const bool expectPrevent) {
Arpit Singha5ea7c12023-07-05 15:39:25 +000084 if (expectPrevent) {
85 EXPECT_CALL(mMockInputReaderContext, setPreventingTouchpadTaps(true))
86 .Times(keyCodes.size());
87 }
88 for (int32_t keyCode : keyCodes) {
89 process(EV_KEY, keyCode, 1);
90 process(EV_SYN, SYN_REPORT, 0);
91 process(EV_KEY, keyCode, 0);
92 process(EV_SYN, SYN_REPORT, 0);
93 }
94 }
Arpit Singhb3b3f732023-07-04 14:30:05 +000095};
96
97/**
98 * Pointer visibility should remain unaffected if there is no active Input Method Connection
99 */
100TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDoesNotHidePointer) {
101 testPointerVisibilityForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectVisible= */ true);
102}
103
104/**
105 * Pointer should hide if there is a active Input Method Connection
106 */
107TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionHidePointer) {
108 mFakePolicy->setIsInputMethodConnectionActive(true);
109 testPointerVisibilityForKeys({KEY_0, KEY_A}, /* expectVisible= */ false);
110}
111
112/**
Arpit Singhc00f5e92023-09-19 13:11:05 +0000113 * Pointer should still hide if touchpad taps are already disabled
114 */
115TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithTouchpadTapDisabledHidePointer) {
116 mFakePolicy->setIsInputMethodConnectionActive(true);
117 EXPECT_CALL(mMockInputReaderContext, isPreventingTouchpadTaps).WillRepeatedly(Return(true));
118 testPointerVisibilityForKeys({KEY_0, KEY_A}, /* expectVisible= */ false);
119}
120
121/**
Arpit Singhb3b3f732023-07-04 14:30:05 +0000122 * Pointer visibility should remain unaffected by meta keys even if Input Method Connection is
123 * active
124 */
125TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDoesNotHidePointer) {
126 mFakePolicy->setIsInputMethodConnectionActive(true);
127 std::vector<int32_t> metaKeys{KEY_LEFTALT, KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
128 KEY_FN, KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA,
129 KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK, KEY_SCROLLLOCK};
130 testPointerVisibilityForKeys(metaKeys, /* expectVisible= */ true);
131}
132
Arpit Singha5ea7c12023-07-05 15:39:25 +0000133/**
134 * Touchpad tap should not be disabled if there is no active Input Method Connection
135 */
136TEST_F(KeyboardInputMapperUnitTest, KeystrokesWithoutIMeConnectionDontDisableTouchpadTap) {
137 testTouchpadTapStateForKeys({KEY_0, KEY_A, KEY_LEFTCTRL}, /* expectPrevent= */ false);
138}
139
140/**
141 * Touchpad tap should be disabled if there is a active Input Method Connection
142 */
143TEST_F(KeyboardInputMapperUnitTest, AlphanumericKeystrokesWithIMeConnectionDisableTouchpadTap) {
144 mFakePolicy->setIsInputMethodConnectionActive(true);
145 testTouchpadTapStateForKeys({KEY_0, KEY_A}, /* expectPrevent= */ true);
146}
147
148/**
149 * Touchpad tap should not be disabled by meta keys even if Input Method Connection is active
150 */
151TEST_F(KeyboardInputMapperUnitTest, MetaKeystrokesWithIMeConnectionDontDisableTouchpadTap) {
152 mFakePolicy->setIsInputMethodConnectionActive(true);
153 std::vector<int32_t> metaKeys{KEY_LEFTALT, KEY_RIGHTALT, KEY_LEFTSHIFT, KEY_RIGHTSHIFT,
154 KEY_FN, KEY_LEFTCTRL, KEY_RIGHTCTRL, KEY_LEFTMETA,
155 KEY_RIGHTMETA, KEY_CAPSLOCK, KEY_NUMLOCK, KEY_SCROLLLOCK};
156 testTouchpadTapStateForKeys(metaKeys, /* expectPrevent= */ false);
157}
158
Arpit Singh82e413e2023-10-10 19:30:58 +0000159TEST_F(KeyboardInputMapperUnitTest, KeyPressTimestampRecorded) {
160 nsecs_t when = ARBITRARY_TIME;
161 std::vector<int32_t> keyCodes{KEY_0, KEY_A, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTSHIFT};
162 EXPECT_CALL(mMockInputReaderContext, setLastKeyDownTimestamp)
163 .With(Args<0>(when))
164 .Times(keyCodes.size());
165 for (int32_t keyCode : keyCodes) {
166 process(when, EV_KEY, keyCode, 1);
167 process(when, EV_SYN, SYN_REPORT, 0);
168 process(when, EV_KEY, keyCode, 0);
169 process(when, EV_SYN, SYN_REPORT, 0);
170 }
171}
172
Arpit Singhb3b3f732023-07-04 14:30:05 +0000173} // namespace android