blob: 123f2b88ba80711f03f1aaa961e1098b59e357a5 [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"
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070029#include "InputHost.h"
Tim Kilbourn73475a42015-02-13 10:35:20 -080030#include "InputHub.h"
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070031#include "InputMocks.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
39namespace android {
40namespace tests {
41
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070042class EvdevDeviceTest : public ::testing::Test {
43protected:
44 virtual void SetUp() override {
45 mMockHost.reset(new MockInputHost());
46 }
Tim Kilbourn73475a42015-02-13 10:35:20 -080047
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070048 virtual void TearDown() override {
49 ASSERT_TRUE(mMockHost->checkAllocations());
50 }
Tim Kilbourn73475a42015-02-13 10:35:20 -080051
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070052 std::unique_ptr<MockInputHost> mMockHost;
Tim Kilbourn73475a42015-02-13 10:35:20 -080053};
54
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070055TEST_F(EvdevDeviceTest, testOverrideTime) {
56 InputHost host = {mMockHost.get(), kTestCallbacks};
Tim Kilbourn73475a42015-02-13 10:35:20 -080057 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070058 auto device = std::make_unique<EvdevDevice>(host, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -080059 ASSERT_TRUE(device != nullptr);
60
61 // Send two timestamp override events before an input event.
62 nsecs_t when = 2ULL;
63 InputEvent msc1 = { when, EV_MSC, MSC_ANDROID_TIME_SEC, 1 };
64 InputEvent msc2 = { when, EV_MSC, MSC_ANDROID_TIME_USEC, 900000 };
65
66 // Send a key down and syn. Should get the overridden timestamp.
67 InputEvent keyDown = { when, EV_KEY, KEY_HOME, 1 };
68 InputEvent syn = { when, EV_SYN, SYN_REPORT, 0 };
69
70 // Send a key up, which should be at the reported timestamp.
71 InputEvent keyUp = { when, EV_KEY, KEY_HOME, 0 };
72
73 device->processInput(msc1, when);
74 device->processInput(msc2, when);
75 device->processInput(keyDown, when);
76 device->processInput(syn, when);
77 device->processInput(keyUp, when);
78
79 nsecs_t expectedWhen = s2ns(1) + us2ns(900000);
80 EXPECT_EQ(expectedWhen, keyDown.when);
81 EXPECT_EQ(expectedWhen, syn.when);
82 EXPECT_EQ(when, keyUp.when);
83}
84
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070085TEST_F(EvdevDeviceTest, testWrongClockCorrection) {
86 InputHost host = {mMockHost.get(), kTestCallbacks};
Tim Kilbourn73475a42015-02-13 10:35:20 -080087 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070088 auto device = std::make_unique<EvdevDevice>(host, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -080089 ASSERT_TRUE(device != nullptr);
90
91 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
92
93 // Input event that supposedly comes from 1 minute in the future. In
94 // reality, the timestamps would be much further off.
95 InputEvent event = { now + s2ns(60), EV_KEY, KEY_HOME, 1 };
96
97 device->processInput(event, now);
98
99 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
100}
101
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700102TEST_F(EvdevDeviceTest, testClockCorrectionOk) {
103 InputHost host = {mMockHost.get(), kTestCallbacks};
Tim Kilbourn73475a42015-02-13 10:35:20 -0800104 auto node = std::make_shared<MockInputDeviceNode>();
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700105 auto device = std::make_unique<EvdevDevice>(host, node);
Tim Kilbourn73475a42015-02-13 10:35:20 -0800106 ASSERT_TRUE(device != nullptr);
107
108 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
109
110 // Input event from now, but will be reported as if it came early.
111 InputEvent event = { now, EV_KEY, KEY_HOME, 1 };
112
113 // event_time parameter is 11 seconds in the past, so it looks like we used
114 // the wrong clock.
115 device->processInput(event, now - s2ns(11));
116
117 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
118}
119
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700120TEST_F(EvdevDeviceTest, testN7v2Touchscreen) {
121 InputHost host = {mMockHost.get(), kTestCallbacks};
122 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getElanTouchscreen());
123 auto device = std::make_unique<EvdevDevice>(host, node);
124 EXPECT_EQ(INPUT_DEVICE_CLASS_TOUCH|INPUT_DEVICE_CLASS_TOUCH_MT,
125 device->getInputClasses());
126}
127
128TEST_F(EvdevDeviceTest, testN7v2ButtonJack) {
129 InputHost host = {mMockHost.get(), kTestCallbacks};
130 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getButtonJack());
131 auto device = std::make_unique<EvdevDevice>(host, node);
132 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
133}
134
135TEST_F(EvdevDeviceTest, testN7v2HeadsetJack) {
136 InputHost host = {mMockHost.get(), kTestCallbacks};
137 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getHeadsetJack());
138 auto device = std::make_unique<EvdevDevice>(host, node);
139 EXPECT_EQ(INPUT_DEVICE_CLASS_SWITCH, device->getInputClasses());
140}
141
142TEST_F(EvdevDeviceTest, testN7v2H2wButton) {
143 InputHost host = {mMockHost.get(), kTestCallbacks};
144 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getH2wButton());
145 auto device = std::make_unique<EvdevDevice>(host, node);
146 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
147}
148
149TEST_F(EvdevDeviceTest, testN7v2GpioKeys) {
150 InputHost host = {mMockHost.get(), kTestCallbacks};
151 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getGpioKeys());
152 auto device = std::make_unique<EvdevDevice>(host, node);
153 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
154}
155
156TEST_F(EvdevDeviceTest, testNexusPlayerGpioKeys) {
157 InputHost host = {mMockHost.get(), kTestCallbacks};
158 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getGpioKeys());
159 auto device = std::make_unique<EvdevDevice>(host, node);
160 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
161}
162
163TEST_F(EvdevDeviceTest, testNexusPlayerMidPowerBtn) {
164 InputHost host = {mMockHost.get(), kTestCallbacks};
165 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getMidPowerBtn());
166 auto device = std::make_unique<EvdevDevice>(host, node);
167 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
168}
169
170TEST_F(EvdevDeviceTest, testNexusRemote) {
171 InputHost host = {mMockHost.get(), kTestCallbacks};
172 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getNexusRemote());
173 auto device = std::make_unique<EvdevDevice>(host, node);
174 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
175}
176
177TEST_F(EvdevDeviceTest, testAsusGamepad) {
178 InputHost host = {mMockHost.get(), kTestCallbacks};
179 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getAsusGamepad());
180 auto device = std::make_unique<EvdevDevice>(host, node);
181 EXPECT_EQ(INPUT_DEVICE_CLASS_JOYSTICK|INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
182}
183
Tim Kilbourn73475a42015-02-13 10:35:20 -0800184} // namespace tests
185} // namespace android