xref: /aosp_15_r20/hardware/libhardware/tests/input/evdev/InputDevice_test.cpp (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker  *
4*e01b6f76SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker  *
8*e01b6f76SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker  *
10*e01b6f76SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker  * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker  */
16*e01b6f76SAndroid Build Coastguard Worker 
17*e01b6f76SAndroid Build Coastguard Worker #include "InputDevice.h"
18*e01b6f76SAndroid Build Coastguard Worker 
19*e01b6f76SAndroid Build Coastguard Worker #include <memory>
20*e01b6f76SAndroid Build Coastguard Worker 
21*e01b6f76SAndroid Build Coastguard Worker #include <linux/input.h>
22*e01b6f76SAndroid Build Coastguard Worker 
23*e01b6f76SAndroid Build Coastguard Worker #include <gtest/gtest.h>
24*e01b6f76SAndroid Build Coastguard Worker 
25*e01b6f76SAndroid Build Coastguard Worker #include <utils/Timers.h>
26*e01b6f76SAndroid Build Coastguard Worker 
27*e01b6f76SAndroid Build Coastguard Worker #include "InputHub.h"
28*e01b6f76SAndroid Build Coastguard Worker #include "InputMocks.h"
29*e01b6f76SAndroid Build Coastguard Worker #include "MockInputHost.h"
30*e01b6f76SAndroid Build Coastguard Worker 
31*e01b6f76SAndroid Build Coastguard Worker // # of milliseconds to allow for timing measurements
32*e01b6f76SAndroid Build Coastguard Worker #define TIMING_TOLERANCE_MS 25
33*e01b6f76SAndroid Build Coastguard Worker 
34*e01b6f76SAndroid Build Coastguard Worker 
35*e01b6f76SAndroid Build Coastguard Worker using ::testing::_;
36*e01b6f76SAndroid Build Coastguard Worker using ::testing::NiceMock;
37*e01b6f76SAndroid Build Coastguard Worker using ::testing::Return;
38*e01b6f76SAndroid Build Coastguard Worker using ::testing::ReturnNull;
39*e01b6f76SAndroid Build Coastguard Worker 
40*e01b6f76SAndroid Build Coastguard Worker namespace android {
41*e01b6f76SAndroid Build Coastguard Worker namespace tests {
42*e01b6f76SAndroid Build Coastguard Worker 
43*e01b6f76SAndroid Build Coastguard Worker class EvdevDeviceTest : public ::testing::Test {
44*e01b6f76SAndroid Build Coastguard Worker protected:
SetUp()45*e01b6f76SAndroid Build Coastguard Worker     virtual void SetUp() {
46*e01b6f76SAndroid Build Coastguard Worker         // Creating device identifiers and definitions should always happen.
47*e01b6f76SAndroid Build Coastguard Worker         EXPECT_CALL(mHost, createDeviceIdentifier(_, _, _, _, _))
48*e01b6f76SAndroid Build Coastguard Worker             .WillOnce(ReturnNull());
49*e01b6f76SAndroid Build Coastguard Worker         EXPECT_CALL(mHost, createDeviceDefinition())
50*e01b6f76SAndroid Build Coastguard Worker             .WillOnce(Return(&mDeviceDef));
51*e01b6f76SAndroid Build Coastguard Worker         // InputMappers may cause any of these to be called, but we are not
52*e01b6f76SAndroid Build Coastguard Worker         // testing these here.
53*e01b6f76SAndroid Build Coastguard Worker         ON_CALL(mHost, createInputReportDefinition())
54*e01b6f76SAndroid Build Coastguard Worker             .WillByDefault(Return(&mReportDef));
55*e01b6f76SAndroid Build Coastguard Worker         ON_CALL(mHost, createOutputReportDefinition())
56*e01b6f76SAndroid Build Coastguard Worker             .WillByDefault(Return(&mReportDef));
57*e01b6f76SAndroid Build Coastguard Worker         ON_CALL(mHost, registerDevice(_, _))
58*e01b6f76SAndroid Build Coastguard Worker             .WillByDefault(ReturnNull());
59*e01b6f76SAndroid Build Coastguard Worker     }
60*e01b6f76SAndroid Build Coastguard Worker 
61*e01b6f76SAndroid Build Coastguard Worker     MockInputHost mHost;
62*e01b6f76SAndroid Build Coastguard Worker     // Ignore uninteresting calls on the report definitions by using NiceMocks.
63*e01b6f76SAndroid Build Coastguard Worker     NiceMock<MockInputReportDefinition> mReportDef;
64*e01b6f76SAndroid Build Coastguard Worker     NiceMock<MockInputDeviceDefinition> mDeviceDef;
65*e01b6f76SAndroid Build Coastguard Worker };
66*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testWrongClockCorrection)67*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testWrongClockCorrection) {
68*e01b6f76SAndroid Build Coastguard Worker     auto node = std::make_shared<MockInputDeviceNode>();
69*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
70*e01b6f76SAndroid Build Coastguard Worker     ASSERT_TRUE(device != nullptr);
71*e01b6f76SAndroid Build Coastguard Worker 
72*e01b6f76SAndroid Build Coastguard Worker     auto now = systemTime(SYSTEM_TIME_MONOTONIC);
73*e01b6f76SAndroid Build Coastguard Worker 
74*e01b6f76SAndroid Build Coastguard Worker     // Input event that supposedly comes from 1 minute in the future. In
75*e01b6f76SAndroid Build Coastguard Worker     // reality, the timestamps would be much further off.
76*e01b6f76SAndroid Build Coastguard Worker     InputEvent event = { now + s2ns(60), EV_KEY, KEY_HOME, 1 };
77*e01b6f76SAndroid Build Coastguard Worker 
78*e01b6f76SAndroid Build Coastguard Worker     device->processInput(event, now);
79*e01b6f76SAndroid Build Coastguard Worker 
80*e01b6f76SAndroid Build Coastguard Worker     EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
81*e01b6f76SAndroid Build Coastguard Worker }
82*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testClockCorrectionOk)83*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testClockCorrectionOk) {
84*e01b6f76SAndroid Build Coastguard Worker     auto node = std::make_shared<MockInputDeviceNode>();
85*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
86*e01b6f76SAndroid Build Coastguard Worker     ASSERT_TRUE(device != nullptr);
87*e01b6f76SAndroid Build Coastguard Worker 
88*e01b6f76SAndroid Build Coastguard Worker     auto now = systemTime(SYSTEM_TIME_MONOTONIC);
89*e01b6f76SAndroid Build Coastguard Worker 
90*e01b6f76SAndroid Build Coastguard Worker     // Input event from now, but will be reported as if it came early.
91*e01b6f76SAndroid Build Coastguard Worker     InputEvent event = { now, EV_KEY, KEY_HOME, 1 };
92*e01b6f76SAndroid Build Coastguard Worker 
93*e01b6f76SAndroid Build Coastguard Worker     // event_time parameter is 11 seconds in the past, so it looks like we used
94*e01b6f76SAndroid Build Coastguard Worker     // the wrong clock.
95*e01b6f76SAndroid Build Coastguard Worker     device->processInput(event, now - s2ns(11));
96*e01b6f76SAndroid Build Coastguard Worker 
97*e01b6f76SAndroid Build Coastguard Worker     EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
98*e01b6f76SAndroid Build Coastguard Worker }
99*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testN7v2Touchscreen)100*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testN7v2Touchscreen) {
101*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getElanTouchscreen());
102*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
103*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_TOUCH|INPUT_DEVICE_CLASS_TOUCH_MT,
104*e01b6f76SAndroid Build Coastguard Worker             device->getInputClasses());
105*e01b6f76SAndroid Build Coastguard Worker }
106*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testN7v2ButtonJack)107*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testN7v2ButtonJack) {
108*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getButtonJack());
109*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
110*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
111*e01b6f76SAndroid Build Coastguard Worker }
112*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testN7v2HeadsetJack)113*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testN7v2HeadsetJack) {
114*e01b6f76SAndroid Build Coastguard Worker     // Eventually these mock device tests will all expect these calls. For now
115*e01b6f76SAndroid Build Coastguard Worker     // only the SwitchInputMapper has been implemented.
116*e01b6f76SAndroid Build Coastguard Worker     // TODO: move this expectation out to a common function
117*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(mHost, createInputReportDefinition());
118*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(mHost, createOutputReportDefinition());
119*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(mHost, freeReportDefinition(_));
120*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(mHost, registerDevice(_, _));
121*e01b6f76SAndroid Build Coastguard Worker 
122*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getHeadsetJack());
123*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
124*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_SWITCH, device->getInputClasses());
125*e01b6f76SAndroid Build Coastguard Worker }
126*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testN7v2H2wButton)127*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testN7v2H2wButton) {
128*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getH2wButton());
129*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
130*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
131*e01b6f76SAndroid Build Coastguard Worker }
132*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testN7v2GpioKeys)133*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testN7v2GpioKeys) {
134*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getGpioKeys());
135*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
136*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
137*e01b6f76SAndroid Build Coastguard Worker }
138*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testNexusPlayerGpioKeys)139*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testNexusPlayerGpioKeys) {
140*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getGpioKeys());
141*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
142*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
143*e01b6f76SAndroid Build Coastguard Worker }
144*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testNexusPlayerMidPowerBtn)145*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testNexusPlayerMidPowerBtn) {
146*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getMidPowerBtn());
147*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
148*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
149*e01b6f76SAndroid Build Coastguard Worker }
150*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testNexusRemote)151*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testNexusRemote) {
152*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getNexusRemote());
153*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
154*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
155*e01b6f76SAndroid Build Coastguard Worker }
156*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testAsusGamepad)157*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testAsusGamepad) {
158*e01b6f76SAndroid Build Coastguard Worker     auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getAsusGamepad());
159*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
160*e01b6f76SAndroid Build Coastguard Worker     EXPECT_EQ(INPUT_DEVICE_CLASS_JOYSTICK|INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
161*e01b6f76SAndroid Build Coastguard Worker }
162*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(EvdevDeviceTest,testMocks)163*e01b6f76SAndroid Build Coastguard Worker TEST_F(EvdevDeviceTest, testMocks) {
164*e01b6f76SAndroid Build Coastguard Worker     auto node = std::make_shared<MockInputDeviceNode>();
165*e01b6f76SAndroid Build Coastguard Worker     auto device = std::make_unique<EvdevDevice>(&mHost, node);
166*e01b6f76SAndroid Build Coastguard Worker }
167*e01b6f76SAndroid Build Coastguard Worker 
168*e01b6f76SAndroid Build Coastguard Worker }  // namespace tests
169*e01b6f76SAndroid Build Coastguard Worker }  // namespace android
170