blob: 9380c7142fb93dda0133c8eeda569feff6f3b2dc [file] [log] [blame]
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -08001/*
2 * Copyright (C) 2019 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 "EventHub.h"
18
Prabir Pradhane0105c92019-12-26 12:32:13 -080019#include "UinputDevice.h"
20
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080021#include <gtest/gtest.h>
22#include <inttypes.h>
23#include <linux/uinput.h>
24#include <log/log.h>
25#include <chrono>
26
27#define TAG "EventHub_test"
28
Prabir Pradhane0105c92019-12-26 12:32:13 -080029using android::createUinputDevice;
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080030using android::EventHub;
31using android::EventHubInterface;
32using android::InputDeviceIdentifier;
33using android::RawEvent;
34using android::sp;
Prabir Pradhane0105c92019-12-26 12:32:13 -080035using android::UinputHomeKey;
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080036using std::chrono_literals::operator""ms;
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070037using std::chrono_literals::operator""s;
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080038
39static constexpr bool DEBUG = false;
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080040
41static void dumpEvents(const std::vector<RawEvent>& events) {
42 for (const RawEvent& event : events) {
43 if (event.type >= EventHubInterface::FIRST_SYNTHETIC_EVENT) {
44 switch (event.type) {
45 case EventHubInterface::DEVICE_ADDED:
46 ALOGI("Device added: %i", event.deviceId);
47 break;
48 case EventHubInterface::DEVICE_REMOVED:
49 ALOGI("Device removed: %i", event.deviceId);
50 break;
51 case EventHubInterface::FINISHED_DEVICE_SCAN:
52 ALOGI("Finished device scan.");
53 break;
54 }
55 } else {
56 ALOGI("Device %" PRId32 " : time = %" PRId64 ", type %i, code %i, value %i",
57 event.deviceId, event.when, event.type, event.code, event.value);
58 }
59 }
60}
61
62// --- EventHubTest ---
63class EventHubTest : public testing::Test {
64protected:
65 std::unique_ptr<EventHubInterface> mEventHub;
66 // We are only going to emulate a single input device currently.
Prabir Pradhane0105c92019-12-26 12:32:13 -080067 std::unique_ptr<UinputHomeKey> mKeyboard;
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080068 int32_t mDeviceId;
Prabir Pradhane0105c92019-12-26 12:32:13 -080069
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080070 virtual void SetUp() override {
71 mEventHub = std::make_unique<EventHub>();
72 consumeInitialDeviceAddedEvents();
Prabir Pradhane0105c92019-12-26 12:32:13 -080073 mKeyboard = createUinputDevice<UinputHomeKey>();
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070074 ASSERT_NO_FATAL_FAILURE(mDeviceId = waitForDeviceCreation());
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080075 }
76 virtual void TearDown() override {
Prabir Pradhane0105c92019-12-26 12:32:13 -080077 mKeyboard.reset();
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080078 waitForDeviceClose(mDeviceId);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070079 assertNoMoreEvents();
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080080 }
81
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080082 /**
83 * Return the device id of the created device.
84 */
85 int32_t waitForDeviceCreation();
86 void waitForDeviceClose(int32_t deviceId);
87 void consumeInitialDeviceAddedEvents();
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070088 void assertNoMoreEvents();
89 /**
90 * Read events from the EventHub.
91 *
92 * If expectedEvents is set, wait for a significant period of time to try and ensure that
93 * the expected number of events has been read. The number of returned events
94 * may be smaller (if timeout has been reached) or larger than expectedEvents.
95 *
96 * If expectedEvents is not set, return all of the immediately available events.
97 */
98 std::vector<RawEvent> getEvents(std::optional<size_t> expectedEvents = std::nullopt);
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -080099};
100
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700101std::vector<RawEvent> EventHubTest::getEvents(std::optional<size_t> expectedEvents) {
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800102 std::vector<RawEvent> events;
103
104 while (true) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700105 std::chrono::milliseconds timeout = 0s;
106 if (expectedEvents) {
107 timeout = 2s;
108 }
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700109
110 std::vector<RawEvent> newEvents = mEventHub->getEvents(timeout.count());
111 if (newEvents.empty()) {
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800112 break;
113 }
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700114 events.insert(events.end(), newEvents.begin(), newEvents.end());
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700115 if (expectedEvents && events.size() >= *expectedEvents) {
116 break;
117 }
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800118 }
119 if (DEBUG) {
120 dumpEvents(events);
121 }
122 return events;
123}
124
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800125/**
126 * Since the test runs on a real platform, there will be existing devices
127 * in addition to the test devices being added. Therefore, when EventHub is first created,
128 * it will return a lot of "device added" type of events.
129 */
130void EventHubTest::consumeInitialDeviceAddedEvents() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700131 std::vector<RawEvent> events = getEvents();
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800132 std::set<int32_t /*deviceId*/> existingDevices;
133 // All of the events should be DEVICE_ADDED type, except the last one.
134 for (size_t i = 0; i < events.size() - 1; i++) {
135 const RawEvent& event = events[i];
136 EXPECT_EQ(EventHubInterface::DEVICE_ADDED, event.type);
137 existingDevices.insert(event.deviceId);
138 }
139 // None of the existing system devices should be changing while this test is run.
140 // Check that the returned device ids are unique for all of the existing devices.
141 EXPECT_EQ(existingDevices.size(), events.size() - 1);
142 // The last event should be "finished device scan"
143 EXPECT_EQ(EventHubInterface::FINISHED_DEVICE_SCAN, events[events.size() - 1].type);
144}
145
146int32_t EventHubTest::waitForDeviceCreation() {
147 // Wait a little longer than usual, to ensure input device has time to be created
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700148 std::vector<RawEvent> events = getEvents(2);
149 if (events.size() != 2) {
150 ADD_FAILURE() << "Instead of 2 events, received " << events.size();
151 return 0; // this value is unused
152 }
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800153 const RawEvent& deviceAddedEvent = events[0];
154 EXPECT_EQ(static_cast<int32_t>(EventHubInterface::DEVICE_ADDED), deviceAddedEvent.type);
155 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceAddedEvent.deviceId);
156 const int32_t deviceId = deviceAddedEvent.deviceId;
Prabir Pradhane0105c92019-12-26 12:32:13 -0800157 EXPECT_EQ(identifier.name, mKeyboard->getName());
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800158 const RawEvent& finishedDeviceScanEvent = events[1];
159 EXPECT_EQ(static_cast<int32_t>(EventHubInterface::FINISHED_DEVICE_SCAN),
160 finishedDeviceScanEvent.type);
161 return deviceId;
162}
163
164void EventHubTest::waitForDeviceClose(int32_t deviceId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700165 std::vector<RawEvent> events = getEvents(2);
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800166 ASSERT_EQ(2U, events.size());
167 const RawEvent& deviceRemovedEvent = events[0];
168 EXPECT_EQ(static_cast<int32_t>(EventHubInterface::DEVICE_REMOVED), deviceRemovedEvent.type);
169 EXPECT_EQ(deviceId, deviceRemovedEvent.deviceId);
170 const RawEvent& finishedDeviceScanEvent = events[1];
171 EXPECT_EQ(static_cast<int32_t>(EventHubInterface::FINISHED_DEVICE_SCAN),
172 finishedDeviceScanEvent.type);
173}
174
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700175void EventHubTest::assertNoMoreEvents() {
176 std::vector<RawEvent> events = getEvents();
177 ASSERT_TRUE(events.empty());
178}
179
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800180/**
Josh Bartel938632f2022-07-19 15:34:22 -0500181 * Ensure that two identical devices get assigned unique descriptors from EventHub.
182 */
183TEST_F(EventHubTest, DevicesWithMatchingUniqueIdsAreUnique) {
184 std::unique_ptr<UinputHomeKey> keyboard2 = createUinputDevice<UinputHomeKey>();
185 int32_t deviceId2;
186 ASSERT_NO_FATAL_FAILURE(deviceId2 = waitForDeviceCreation());
187
188 ASSERT_NE(mEventHub->getDeviceIdentifier(mDeviceId).descriptor,
189 mEventHub->getDeviceIdentifier(deviceId2).descriptor);
190 keyboard2.reset();
191 waitForDeviceClose(deviceId2);
192}
193
194/**
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800195 * Ensure that input_events are generated with monotonic clock.
196 * That means input_event should receive a timestamp that is in the future of the time
197 * before the event was sent.
198 * Input system uses CLOCK_MONOTONIC everywhere in the code base.
199 */
200TEST_F(EventHubTest, InputEvent_TimestampIsMonotonic) {
201 nsecs_t lastEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800202 ASSERT_NO_FATAL_FAILURE(mKeyboard->pressAndReleaseHomeKey());
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800203
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700204 std::vector<RawEvent> events = getEvents(4);
Siarhei Vishniakou8588cc92018-12-12 18:17:58 -0800205 ASSERT_EQ(4U, events.size()) << "Expected to receive 2 keys and 2 syncs, total of 4 events";
206 for (const RawEvent& event : events) {
207 // Cannot use strict comparison because the events may happen too quickly
208 ASSERT_LE(lastEventTime, event.when) << "Event must have occurred after the key was sent";
209 ASSERT_LT(std::chrono::nanoseconds(event.when - lastEventTime), 100ms)
210 << "Event times are too far apart";
211 lastEventTime = event.when; // Ensure all returned events are monotonic
212 }
213}
Chris Ye66fbac32020-07-06 20:36:43 -0700214
215// --- BitArrayTest ---
216class BitArrayTest : public testing::Test {
217protected:
218 static constexpr size_t SINGLE_ELE_BITS = 32UL;
219 static constexpr size_t MULTI_ELE_BITS = 256UL;
220
221 virtual void SetUp() override {
222 mBitmaskSingle.loadFromBuffer(mBufferSingle);
223 mBitmaskMulti.loadFromBuffer(mBufferMulti);
224 }
225
226 android::BitArray<SINGLE_ELE_BITS> mBitmaskSingle;
227 android::BitArray<MULTI_ELE_BITS> mBitmaskMulti;
228
229private:
230 const typename android::BitArray<SINGLE_ELE_BITS>::Buffer mBufferSingle = {
231 0x800F0F0FUL // bit 0 - 31
232 };
233 const typename android::BitArray<MULTI_ELE_BITS>::Buffer mBufferMulti = {
234 0xFFFFFFFFUL, // bit 0 - 31
235 0x01000001UL, // bit 32 - 63
236 0x00000000UL, // bit 64 - 95
237 0x80000000UL, // bit 96 - 127
238 0x00000000UL, // bit 128 - 159
239 0x00000000UL, // bit 160 - 191
240 0x80000008UL, // bit 192 - 223
241 0x00000000UL, // bit 224 - 255
242 };
243};
244
245TEST_F(BitArrayTest, SetBit) {
246 ASSERT_TRUE(mBitmaskSingle.test(0));
247 ASSERT_TRUE(mBitmaskSingle.test(31));
248 ASSERT_FALSE(mBitmaskSingle.test(7));
249
250 ASSERT_TRUE(mBitmaskMulti.test(32));
251 ASSERT_TRUE(mBitmaskMulti.test(56));
252 ASSERT_FALSE(mBitmaskMulti.test(192));
253 ASSERT_TRUE(mBitmaskMulti.test(223));
254 ASSERT_FALSE(mBitmaskMulti.test(255));
255}
256
257TEST_F(BitArrayTest, AnyBit) {
258 ASSERT_TRUE(mBitmaskSingle.any(31, 32));
259 ASSERT_FALSE(mBitmaskSingle.any(12, 16));
260
261 ASSERT_TRUE(mBitmaskMulti.any(31, 32));
262 ASSERT_FALSE(mBitmaskMulti.any(33, 33));
263 ASSERT_TRUE(mBitmaskMulti.any(32, 55));
264 ASSERT_TRUE(mBitmaskMulti.any(33, 57));
265 ASSERT_FALSE(mBitmaskMulti.any(33, 55));
266 ASSERT_FALSE(mBitmaskMulti.any(130, 190));
267
268 ASSERT_FALSE(mBitmaskMulti.any(128, 195));
269 ASSERT_TRUE(mBitmaskMulti.any(128, 196));
270 ASSERT_TRUE(mBitmaskMulti.any(128, 224));
271 ASSERT_FALSE(mBitmaskMulti.any(255, 256));
272}
273
274TEST_F(BitArrayTest, SetBit_InvalidBitIndex) {
275 ASSERT_FALSE(mBitmaskSingle.test(32));
276 ASSERT_FALSE(mBitmaskMulti.test(256));
277}
278
279TEST_F(BitArrayTest, AnyBit_InvalidBitIndex) {
280 ASSERT_FALSE(mBitmaskSingle.any(32, 32));
281 ASSERT_FALSE(mBitmaskSingle.any(33, 34));
282
283 ASSERT_FALSE(mBitmaskMulti.any(256, 256));
284 ASSERT_FALSE(mBitmaskMulti.any(257, 258));
285 ASSERT_FALSE(mBitmaskMulti.any(0, 0));
286}