blob: bd5749103f826e76ec00ff39229403965792c757 [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
Tim Kilbourndbc8c162015-05-19 15:04:30 -070017#include "InputDevice.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080018
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070019#include <memory>
20
Tim Kilbourn73475a42015-02-13 10:35:20 -080021#include <linux/input.h>
22
23#include <gtest/gtest.h>
24
25#include <utils/Timers.h>
26
Tim Kilbourn73475a42015-02-13 10:35:20 -080027#include "InputHub.h"
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070028#include "InputMocks.h"
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070029#include "MockInputHost.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080030
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
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070037using ::testing::_;
38using ::testing::NiceMock;
39using ::testing::Return;
40using ::testing::ReturnNull;
41
Tim Kilbourn73475a42015-02-13 10:35:20 -080042namespace android {
43namespace tests {
44
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070045class EvdevDeviceTest : public ::testing::Test {
46protected:
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070047 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 }
Tim Kilbourn73475a42015-02-13 10:35:20 -080062
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070063 MockInputHost mHost;
64 // Ignore uninteresting calls on the report definitions by using NiceMocks.
65 NiceMock<MockInputReportDefinition> mReportDef;
66 NiceMock<MockInputDeviceDefinition> mDeviceDef;
Tim Kilbourn73475a42015-02-13 10:35:20 -080067};
68
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070069TEST_F(EvdevDeviceTest, testOverrideTime) {
Tim Kilbourn73475a42015-02-13 10:35:20 -080070 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn4f3145d2015-05-04 17:26:30 -070071 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -080072 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
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070098TEST_F(EvdevDeviceTest, testWrongClockCorrection) {
Tim Kilbourn73475a42015-02-13 10:35:20 -080099 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700100 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -0800101 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
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700114TEST_F(EvdevDeviceTest, testClockCorrectionOk) {
Tim Kilbourn73475a42015-02-13 10:35:20 -0800115 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700116 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -0800117 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
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700131TEST_F(EvdevDeviceTest, testN7v2Touchscreen) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700132 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getElanTouchscreen());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700133 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700134 EXPECT_EQ(INPUT_DEVICE_CLASS_TOUCH|INPUT_DEVICE_CLASS_TOUCH_MT,
135 device->getInputClasses());
136}
137
138TEST_F(EvdevDeviceTest, testN7v2ButtonJack) {
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700139 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getButtonJack());
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700140 auto device = std::make_unique<EvdevDevice>(&mHost, node);
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700141 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
142}
143
144TEST_F(EvdevDeviceTest, testN7v2HeadsetJack) {
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700145 // 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());
Tim Kilbourn864984c2015-05-19 17:15:52 -0700149 EXPECT_CALL(mHost, createOutputReportDefinition());
150 EXPECT_CALL(mHost, freeReportDefinition(_));
Tim Kilbourn4f3145d2015-05-04 17:26:30 -0700151 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