blob: bcc6062bf88395c66bdcc02ffddfb82540ff89f1 [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"
David Padlipsky48fd8842024-10-18 03:36:58 +000023#include "TestEventMatchers.h"
Arpit Singhb3b3f732023-07-04 14:30:05 +000024
25#define TAG "KeyboardInputMapper_test"
26
27namespace android {
28
29using testing::_;
David Padlipsky48fd8842024-10-18 03:36:58 +000030using testing::AllOf;
Arpit Singh82e413e2023-10-10 19:30:58 +000031using testing::Args;
Arpit Singhb3b3f732023-07-04 14:30:05 +000032using testing::DoAll;
33using testing::Return;
34using testing::SetArgPointee;
David Padlipsky48fd8842024-10-18 03:36:58 +000035using testing::VariantWith;
Arpit Singhb3b3f732023-07-04 14:30:05 +000036
37/**
38 * Unit tests for KeyboardInputMapper.
39 */
40class KeyboardInputMapperUnitTest : public InputMapperUnitTest {
41protected:
42 sp<FakeInputReaderPolicy> mFakePolicy;
43 const std::unordered_map<int32_t, int32_t> mKeyCodeMap{{KEY_0, AKEYCODE_0},
44 {KEY_A, AKEYCODE_A},
45 {KEY_LEFTCTRL, AKEYCODE_CTRL_LEFT},
46 {KEY_LEFTALT, AKEYCODE_ALT_LEFT},
47 {KEY_RIGHTALT, AKEYCODE_ALT_RIGHT},
48 {KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT},
49 {KEY_RIGHTSHIFT, AKEYCODE_SHIFT_RIGHT},
50 {KEY_FN, AKEYCODE_FUNCTION},
51 {KEY_LEFTCTRL, AKEYCODE_CTRL_LEFT},
52 {KEY_RIGHTCTRL, AKEYCODE_CTRL_RIGHT},
53 {KEY_LEFTMETA, AKEYCODE_META_LEFT},
54 {KEY_RIGHTMETA, AKEYCODE_META_RIGHT},
55 {KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK},
56 {KEY_NUMLOCK, AKEYCODE_NUM_LOCK},
57 {KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK}};
58
59 void SetUp() override {
60 InputMapperUnitTest::SetUp();
61
62 // set key-codes expected in tests
63 for (const auto& [scanCode, outKeycode] : mKeyCodeMap) {
64 EXPECT_CALL(mMockEventHub, mapKey(EVENTHUB_ID, scanCode, _, _, _, _, _))
65 .WillRepeatedly(DoAll(SetArgPointee<4>(outKeycode), Return(NO_ERROR)));
66 }
67
68 mFakePolicy = sp<FakeInputReaderPolicy>::make();
69 EXPECT_CALL(mMockInputReaderContext, getPolicy).WillRepeatedly(Return(mFakePolicy.get()));
70
Prabir Pradhan38636652024-07-23 21:59:36 +000071 ON_CALL((*mDevice), getSources).WillByDefault(Return(AINPUT_SOURCE_KEYBOARD));
72
Arpit Singhb3b3f732023-07-04 14:30:05 +000073 mMapper = createInputMapper<KeyboardInputMapper>(*mDeviceContext, mReaderConfiguration,
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000074 AINPUT_SOURCE_KEYBOARD);
Arpit Singhb3b3f732023-07-04 14:30:05 +000075 }
Arpit Singhb3b3f732023-07-04 14:30:05 +000076};
77
Arpit Singh82e413e2023-10-10 19:30:58 +000078TEST_F(KeyboardInputMapperUnitTest, KeyPressTimestampRecorded) {
79 nsecs_t when = ARBITRARY_TIME;
80 std::vector<int32_t> keyCodes{KEY_0, KEY_A, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTSHIFT};
81 EXPECT_CALL(mMockInputReaderContext, setLastKeyDownTimestamp)
82 .With(Args<0>(when))
83 .Times(keyCodes.size());
84 for (int32_t keyCode : keyCodes) {
85 process(when, EV_KEY, keyCode, 1);
86 process(when, EV_SYN, SYN_REPORT, 0);
87 process(when, EV_KEY, keyCode, 0);
88 process(when, EV_SYN, SYN_REPORT, 0);
89 }
90}
91
David Padlipsky48fd8842024-10-18 03:36:58 +000092TEST_F(KeyboardInputMapperUnitTest, RepeatEventsDiscarded) {
93 std::list<NotifyArgs> args;
94 args += process(ARBITRARY_TIME, EV_KEY, KEY_0, 1);
95 args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
96
97 args += process(ARBITRARY_TIME, EV_KEY, KEY_0, 2);
98 args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
99
100 args += process(ARBITRARY_TIME, EV_KEY, KEY_0, 0);
101 args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
102
103 EXPECT_THAT(args,
104 ElementsAre(VariantWith<NotifyKeyArgs>(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN),
105 WithKeyCode(AKEYCODE_0),
106 WithScanCode(KEY_0))),
107 VariantWith<NotifyKeyArgs>(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
108 WithKeyCode(AKEYCODE_0),
109 WithScanCode(KEY_0)))));
110}
111
Arpit Singhb3b3f732023-07-04 14:30:05 +0000112} // namespace android