blob: bed05b867896746a00e4ee65d4c39707c2174e99 [file] [log] [blame]
Tim Kilbourn73475a42015-02-13 10:35:20 -08001/*
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#define LOG_TAG "InputHub_test"
18//#define LOG_NDEBUG 0
19
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070020#include <memory>
21
Tim Kilbourn73475a42015-02-13 10:35:20 -080022#include <linux/input.h>
23
24#include <gtest/gtest.h>
25
26#include <utils/Timers.h>
27
28#include "InputDevice.h"
29#include "InputHub.h"
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070030#include "InputMocks.h"
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070031#include "MockInputHost.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080032
33// # of milliseconds to allow for timing measurements
34#define TIMING_TOLERANCE_MS 25
35
36#define MSC_ANDROID_TIME_SEC 0x6
37#define MSC_ANDROID_TIME_USEC 0x7
38
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070039using ::testing::_;
40using ::testing::NiceMock;
41using ::testing::Return;
42using ::testing::ReturnNull;
43
Tim Kilbourn73475a42015-02-13 10:35:20 -080044namespace android {
45namespace tests {
46
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070047class EvdevDeviceTest : public ::testing::Test {
48protected:
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070049 virtual void SetUp() {
50 // Creating device identifiers and definitions should always happen.
51 EXPECT_CALL(mHost, createDeviceIdentifier(_, _, _, _, _))
52 .WillOnce(ReturnNull());
53 EXPECT_CALL(mHost, createDeviceDefinition())
54 .WillOnce(Return(&mDeviceDef));
55 // InputMappers may cause any of these to be called, but we are not
56 // testing these here.
57 ON_CALL(mHost, createInputReportDefinition())
58 .WillByDefault(Return(&mReportDef));
59 ON_CALL(mHost, createOutputReportDefinition())
60 .WillByDefault(Return(&mReportDef));
61 ON_CALL(mHost, registerDevice(_, _))
62 .WillByDefault(ReturnNull());
63 }
Tim Kilbourn73475a42015-02-13 10:35:20 -080064
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070065 MockInputHost mHost;
66 // Ignore uninteresting calls on the report definitions by using NiceMocks.
67 NiceMock<MockInputReportDefinition> mReportDef;
68 NiceMock<MockInputDeviceDefinition> mDeviceDef;
Tim Kilbourn73475a42015-02-13 10:35:20 -080069};
70
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070071TEST_F(EvdevDeviceTest, testOverrideTime) {
Tim Kilbourn73475a42015-02-13 10:35:20 -080072 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070073 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -080074 ASSERT_TRUE(device != nullptr);
75
76 // Send two timestamp override events before an input event.
77 nsecs_t when = 2ULL;
78 InputEvent msc1 = { when, EV_MSC, MSC_ANDROID_TIME_SEC, 1 };
79 InputEvent msc2 = { when, EV_MSC, MSC_ANDROID_TIME_USEC, 900000 };
80
81 // Send a key down and syn. Should get the overridden timestamp.
82 InputEvent keyDown = { when, EV_KEY, KEY_HOME, 1 };
83 InputEvent syn = { when, EV_SYN, SYN_REPORT, 0 };
84
85 // Send a key up, which should be at the reported timestamp.
86 InputEvent keyUp = { when, EV_KEY, KEY_HOME, 0 };
87
88 device->processInput(msc1, when);
89 device->processInput(msc2, when);
90 device->processInput(keyDown, when);
91 device->processInput(syn, when);
92 device->processInput(keyUp, when);
93
94 nsecs_t expectedWhen = s2ns(1) + us2ns(900000);
95 EXPECT_EQ(expectedWhen, keyDown.when);
96 EXPECT_EQ(expectedWhen, syn.when);
97 EXPECT_EQ(when, keyUp.when);
98}
99
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700100TEST_F(EvdevDeviceTest, testWrongClockCorrection) {
Tim Kilbourn73475a42015-02-13 10:35:20 -0800101 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700102 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -0800103 ASSERT_TRUE(device != nullptr);
104
105 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
106
107 // Input event that supposedly comes from 1 minute in the future. In
108 // reality, the timestamps would be much further off.
109 InputEvent event = { now + s2ns(60), EV_KEY, KEY_HOME, 1 };
110
111 device->processInput(event, now);
112
113 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
114}
115
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700116TEST_F(EvdevDeviceTest, testClockCorrectionOk) {
Tim Kilbourn73475a42015-02-13 10:35:20 -0800117 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700118 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -0800119 ASSERT_TRUE(device != nullptr);
120
121 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
122
123 // Input event from now, but will be reported as if it came early.
124 InputEvent event = { now, EV_KEY, KEY_HOME, 1 };
125
126 // event_time parameter is 11 seconds in the past, so it looks like we used
127 // the wrong clock.
128 device->processInput(event, now - s2ns(11));
129
130 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
131}
132
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700133TEST_F(EvdevDeviceTest, testN7v2Touchscreen) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700134 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getElanTouchscreen());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700135 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700136 EXPECT_EQ(INPUT_DEVICE_CLASS_TOUCH|INPUT_DEVICE_CLASS_TOUCH_MT,
137 device->getInputClasses());
138}
139
140TEST_F(EvdevDeviceTest, testN7v2ButtonJack) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700141 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getButtonJack());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700142 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700143 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
144}
145
146TEST_F(EvdevDeviceTest, testN7v2HeadsetJack) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700147 // Eventually these mock device tests will all expect these calls. For now
148 // only the SwitchInputMapper has been implemented.
149 // TODO: move this expectation out to a common function
150 EXPECT_CALL(mHost, createInputReportDefinition());
151 EXPECT_CALL(mHost, registerDevice(_, _));
152
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700153 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getHeadsetJack());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700154 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700155 EXPECT_EQ(INPUT_DEVICE_CLASS_SWITCH, device->getInputClasses());
156}
157
158TEST_F(EvdevDeviceTest, testN7v2H2wButton) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700159 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getH2wButton());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700160 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700161 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
162}
163
164TEST_F(EvdevDeviceTest, testN7v2GpioKeys) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700165 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getGpioKeys());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700166 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700167 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
168}
169
170TEST_F(EvdevDeviceTest, testNexusPlayerGpioKeys) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700171 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getGpioKeys());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700172 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700173 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
174}
175
176TEST_F(EvdevDeviceTest, testNexusPlayerMidPowerBtn) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700177 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getMidPowerBtn());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700178 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700179 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
180}
181
182TEST_F(EvdevDeviceTest, testNexusRemote) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700183 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getNexusRemote());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700184 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700185 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
186}
187
188TEST_F(EvdevDeviceTest, testAsusGamepad) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700189 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getAsusGamepad());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700190 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700191 EXPECT_EQ(INPUT_DEVICE_CLASS_JOYSTICK|INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
192}
193
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700194TEST_F(EvdevDeviceTest, testMocks) {
195 auto node = std::make_shared<MockInputDeviceNode>();
196 auto device = std::make_unique<EvdevDevice>(&mHost, node);
197}
198
Tim Kilbourn73475a42015-02-13 10:35:20 -0800199} // namespace tests
200} // namespace android