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
35 using ::testing::_;
36 using ::testing::NiceMock;
37 using ::testing::Return;
38 using ::testing::ReturnNull;
39
40 namespace android {
41 namespace tests {
42
43 class EvdevDeviceTest : public ::testing::Test {
44 protected:
SetUp()45 virtual void SetUp() {
46 // Creating device identifiers and definitions should always happen.
47 EXPECT_CALL(mHost, createDeviceIdentifier(_, _, _, _, _))
48 .WillOnce(ReturnNull());
49 EXPECT_CALL(mHost, createDeviceDefinition())
50 .WillOnce(Return(&mDeviceDef));
51 // InputMappers may cause any of these to be called, but we are not
52 // testing these here.
53 ON_CALL(mHost, createInputReportDefinition())
54 .WillByDefault(Return(&mReportDef));
55 ON_CALL(mHost, createOutputReportDefinition())
56 .WillByDefault(Return(&mReportDef));
57 ON_CALL(mHost, registerDevice(_, _))
58 .WillByDefault(ReturnNull());
59 }
60
61 MockInputHost mHost;
62 // Ignore uninteresting calls on the report definitions by using NiceMocks.
63 NiceMock<MockInputReportDefinition> mReportDef;
64 NiceMock<MockInputDeviceDefinition> mDeviceDef;
65 };
66
TEST_F(EvdevDeviceTest,testWrongClockCorrection)67 TEST_F(EvdevDeviceTest, testWrongClockCorrection) {
68 auto node = std::make_shared<MockInputDeviceNode>();
69 auto device = std::make_unique<EvdevDevice>(&mHost, node);
70 ASSERT_TRUE(device != nullptr);
71
72 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
73
74 // Input event that supposedly comes from 1 minute in the future. In
75 // reality, the timestamps would be much further off.
76 InputEvent event = { now + s2ns(60), EV_KEY, KEY_HOME, 1 };
77
78 device->processInput(event, now);
79
80 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
81 }
82
TEST_F(EvdevDeviceTest,testClockCorrectionOk)83 TEST_F(EvdevDeviceTest, testClockCorrectionOk) {
84 auto node = std::make_shared<MockInputDeviceNode>();
85 auto device = std::make_unique<EvdevDevice>(&mHost, node);
86 ASSERT_TRUE(device != nullptr);
87
88 auto now = systemTime(SYSTEM_TIME_MONOTONIC);
89
90 // Input event from now, but will be reported as if it came early.
91 InputEvent event = { now, EV_KEY, KEY_HOME, 1 };
92
93 // event_time parameter is 11 seconds in the past, so it looks like we used
94 // the wrong clock.
95 device->processInput(event, now - s2ns(11));
96
97 EXPECT_NEAR(now, event.when, ms2ns(TIMING_TOLERANCE_MS));
98 }
99
TEST_F(EvdevDeviceTest,testN7v2Touchscreen)100 TEST_F(EvdevDeviceTest, testN7v2Touchscreen) {
101 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getElanTouchscreen());
102 auto device = std::make_unique<EvdevDevice>(&mHost, node);
103 EXPECT_EQ(INPUT_DEVICE_CLASS_TOUCH|INPUT_DEVICE_CLASS_TOUCH_MT,
104 device->getInputClasses());
105 }
106
TEST_F(EvdevDeviceTest,testN7v2ButtonJack)107 TEST_F(EvdevDeviceTest, testN7v2ButtonJack) {
108 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getButtonJack());
109 auto device = std::make_unique<EvdevDevice>(&mHost, node);
110 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
111 }
112
TEST_F(EvdevDeviceTest,testN7v2HeadsetJack)113 TEST_F(EvdevDeviceTest, testN7v2HeadsetJack) {
114 // Eventually these mock device tests will all expect these calls. For now
115 // only the SwitchInputMapper has been implemented.
116 // TODO: move this expectation out to a common function
117 EXPECT_CALL(mHost, createInputReportDefinition());
118 EXPECT_CALL(mHost, createOutputReportDefinition());
119 EXPECT_CALL(mHost, freeReportDefinition(_));
120 EXPECT_CALL(mHost, registerDevice(_, _));
121
122 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getHeadsetJack());
123 auto device = std::make_unique<EvdevDevice>(&mHost, node);
124 EXPECT_EQ(INPUT_DEVICE_CLASS_SWITCH, device->getInputClasses());
125 }
126
TEST_F(EvdevDeviceTest,testN7v2H2wButton)127 TEST_F(EvdevDeviceTest, testN7v2H2wButton) {
128 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getH2wButton());
129 auto device = std::make_unique<EvdevDevice>(&mHost, node);
130 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
131 }
132
TEST_F(EvdevDeviceTest,testN7v2GpioKeys)133 TEST_F(EvdevDeviceTest, testN7v2GpioKeys) {
134 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexus7v2::getGpioKeys());
135 auto device = std::make_unique<EvdevDevice>(&mHost, node);
136 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
137 }
138
TEST_F(EvdevDeviceTest,testNexusPlayerGpioKeys)139 TEST_F(EvdevDeviceTest, testNexusPlayerGpioKeys) {
140 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getGpioKeys());
141 auto device = std::make_unique<EvdevDevice>(&mHost, node);
142 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
143 }
144
TEST_F(EvdevDeviceTest,testNexusPlayerMidPowerBtn)145 TEST_F(EvdevDeviceTest, testNexusPlayerMidPowerBtn) {
146 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getMidPowerBtn());
147 auto device = std::make_unique<EvdevDevice>(&mHost, node);
148 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
149 }
150
TEST_F(EvdevDeviceTest,testNexusRemote)151 TEST_F(EvdevDeviceTest, testNexusRemote) {
152 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getNexusRemote());
153 auto device = std::make_unique<EvdevDevice>(&mHost, node);
154 EXPECT_EQ(INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
155 }
156
TEST_F(EvdevDeviceTest,testAsusGamepad)157 TEST_F(EvdevDeviceTest, testAsusGamepad) {
158 auto node = std::shared_ptr<MockInputDeviceNode>(MockNexusPlayer::getAsusGamepad());
159 auto device = std::make_unique<EvdevDevice>(&mHost, node);
160 EXPECT_EQ(INPUT_DEVICE_CLASS_JOYSTICK|INPUT_DEVICE_CLASS_KEYBOARD, device->getInputClasses());
161 }
162
TEST_F(EvdevDeviceTest,testMocks)163 TEST_F(EvdevDeviceTest, testMocks) {
164 auto node = std::make_shared<MockInputDeviceNode>();
165 auto device = std::make_unique<EvdevDevice>(&mHost, node);
166 }
167
168 } // namespace tests
169 } // namespace android
170