1 /*
2 * Copyright (C) 2015 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 "InputDevice.h"
18
19 #include <memory>
20
21 #include <linux/input.h>
22
23 #include <gtest/gtest.h>
24
25 #include <utils/Timers.h>
26
27 #include "InputHub.h"
28 #include "InputMocks.h"
29 #include "MockInputHost.h"
30
31 // # of milliseconds to allow for timing measurements
32 #define TIMING_TOLERANCE_MS 25
33
34 #define MSC_ANDROID_TIME_SEC 0x6
35 #define MSC_ANDROID_TIME_USEC 0x7
36
37 using ::testing::_;
38 using ::testing::NiceMock;
39 using ::testing::Return;
40 using ::testing::ReturnNull;
41
42 namespace android {
43 namespace tests {
44
45 class EvdevDeviceTest : public ::testing::Test {
46 protected:
SetUp()47 virtual void SetUp() {
48 // Creating device identifiers and definitions should always happen.
49 EXPECT_CALL(mHost, createDeviceIdentifier(_, _, _, _, _))
50 .WillOnce(ReturnNull());
51 EXPECT_CALL(mHost, createDeviceDefinition())
52 .WillOnce(Return(&mDeviceDef));
53 // InputMappers may cause any of these to be called, but we are not
54 // testing these here.
55 ON_CALL(mHost, createInputReportDefinition())
56 .WillByDefault(Return(&mReportDef));
57 ON_CALL(mHost, createOutputReportDefinition())
58 .WillByDefault(Return(&mReportDef));
59 ON_CALL(mHost, registerDevice(_, _))
60 .WillByDefault(ReturnNull());
61 }
62
63 MockInputHost mHost;
64 // Ignore uninteresting calls on the report definitions by using NiceMocks.
65 NiceMock<MockInputReportDefinition> mReportDef;
66 NiceMock<MockInputDeviceDefinition> mDeviceDef;
67 };
68
TEST_F(EvdevDeviceTest,testOverrideTime)69 TEST_F(EvdevDeviceTest, testOverrideTime) {
70 auto node = std::make_shared<MockInputDeviceNode>();
71 auto device = std::make_unique<EvdevDevice>(&mHost, node);
72 ASSERT_TRUE(device != nullptr);
73
74 // Send two timestamp override events before an input event.
75 nsecs_t when = 2ULL;
76 InputEvent msc1 = { when, EV_MSC, MSC_ANDROID_TIME_SEC, 1 };
77 InputEvent msc2 = { when, EV_MSC, MSC_ANDROID_TIME_USEC, 900000 };
78
79 // Send a key down and syn. Should get the overridden timestamp.
80 InputEvent keyDown = { when, EV_KEY, KEY_HOME, 1 };
81 InputEvent syn = { when, EV_SYN, SYN_REPORT, 0 };
82
83 // Send a key up, which should be at the reported timestamp.
84 InputEvent keyUp = { when, EV_KEY, KEY_HOME, 0 };
85
86 device->processInput(msc1, when);
87 device->processInput(msc2, when);
88 device->processInput(keyDown, when);
89 device->processInput(syn, when);
90 device->processInput(keyUp, when);
91
92 nsecs_t expectedWhen = s2ns(1) + us2ns(900000);
93 EXPECT_EQ(expectedWhen, keyDown.when);
94 EXPECT_EQ(expectedWhen, syn.when);
95 EXPECT_EQ(when, keyUp.when);
96 }
97
TEST_F(EvdevDeviceTest,testWrongClockCorrection)98 TEST_F(EvdevDeviceTest, testWrongClockCorrection) {
99 auto node = std::make_shared<MockInputDeviceNode>();
100 auto device = std::make_unique<EvdevDevice>(&mHost, node);
101 ASSERT_TRUE(device != nullptr);
102
103 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
104
105 // Input event that supposedly comes from 1 minute in the future. In
106 // reality, the timestamps would be much further off.
107 InputEvent event = { now + s2ns(60), EV_KEY, KEY_HOME, 1 };
108
109 device->processInput(event, now);
110
111 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
112 }
113
TEST_F(EvdevDeviceTest,testClockCorrectionOk)114 TEST_F(EvdevDeviceTest, testClockCorrectionOk) {
115 auto node = std::make_shared<MockInputDeviceNode>();
116 auto device = std::make_unique<EvdevDevice>(&mHost, node);
117 ASSERT_TRUE(device != nullptr);
118
119 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
120
121 // Input event from now, but will be reported as if it came early.
122 InputEvent event = { now, EV_KEY, KEY_HOME, 1 };
123
124 // event_time parameter is 11 seconds in the past, so it looks like we used
125 // the wrong clock.
126 device->processInput(event, now - s2ns(11));
127
128 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
129 }
130
TEST_F(EvdevDeviceTest,testN7v2Touchscreen)131 TEST_F(EvdevDeviceTest, testN7v2Touchscreen) {
132 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getElanTouchscreen());
133 auto device = std::make_unique<EvdevDevice>(&mHost, node);
134 EXPECT_EQ(INPUT_DEVICE_CLASS_TOUCH|INPUT_DEVICE_CLASS_TOUCH_MT,
135 device->getInputClasses());
136 }
137
TEST_F(EvdevDeviceTest,testN7v2ButtonJack)138 TEST_F(EvdevDeviceTest, testN7v2ButtonJack) {
139 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getButtonJack());
140 auto device = std::make_unique<EvdevDevice>(&mHost, node);
141 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
142 }
143
TEST_F(EvdevDeviceTest,testN7v2HeadsetJack)144 TEST_F(EvdevDeviceTest, testN7v2HeadsetJack) {
145 // Eventually these mock device tests will all expect these calls. For now
146 // only the SwitchInputMapper has been implemented.
147 // TODO: move this expectation out to a common function
148 EXPECT_CALL(mHost, createInputReportDefinition());
149 EXPECT_CALL(mHost, createOutputReportDefinition());
150 EXPECT_CALL(mHost, freeReportDefinition(_));
151 EXPECT_CALL(mHost, registerDevice(_, _));
152
153 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getHeadsetJack());
154 auto device = std::make_unique<EvdevDevice>(&mHost, node);
155 EXPECT_EQ(INPUT_DEVICE_CLASS_SWITCH, device->getInputClasses());
156 }
157
TEST_F(EvdevDeviceTest,testN7v2H2wButton)158 TEST_F(EvdevDeviceTest, testN7v2H2wButton) {
159 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getH2wButton());
160 auto device = std::make_unique<EvdevDevice>(&mHost, node);
161 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
162 }
163
TEST_F(EvdevDeviceTest,testN7v2GpioKeys)164 TEST_F(EvdevDeviceTest, testN7v2GpioKeys) {
165 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getGpioKeys());
166 auto device = std::make_unique<EvdevDevice>(&mHost, node);
167 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
168 }
169
TEST_F(EvdevDeviceTest,testNexusPlayerGpioKeys)170 TEST_F(EvdevDeviceTest, testNexusPlayerGpioKeys) {
171 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getGpioKeys());
172 auto device = std::make_unique<EvdevDevice>(&mHost, node);
173 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
174 }
175
TEST_F(EvdevDeviceTest,testNexusPlayerMidPowerBtn)176 TEST_F(EvdevDeviceTest, testNexusPlayerMidPowerBtn) {
177 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getMidPowerBtn());
178 auto device = std::make_unique<EvdevDevice>(&mHost, node);
179 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
180 }
181
TEST_F(EvdevDeviceTest,testNexusRemote)182 TEST_F(EvdevDeviceTest, testNexusRemote) {
183 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getNexusRemote());
184 auto device = std::make_unique<EvdevDevice>(&mHost, node);
185 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
186 }
187
TEST_F(EvdevDeviceTest,testAsusGamepad)188 TEST_F(EvdevDeviceTest, testAsusGamepad) {
189 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getAsusGamepad());
190 auto device = std::make_unique<EvdevDevice>(&mHost, node);
191 EXPECT_EQ(INPUT_DEVICE_CLASS_JOYSTICK|INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
192 }
193
TEST_F(EvdevDeviceTest,testMocks)194 TEST_F(EvdevDeviceTest, testMocks) {
195 auto node = std::make_shared<MockInputDeviceNode>();
196 auto device = std::make_unique<EvdevDevice>(&mHost, node);
197 }
198
199 } // namespace tests
200 } // namespace android
201