| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 19 | #include "UinputDevice.h" | 
|  | 20 |  | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 21 | #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 Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 29 | using android::createUinputDevice; | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 30 | using android::EventHub; | 
|  | 31 | using android::EventHubInterface; | 
|  | 32 | using android::InputDeviceIdentifier; | 
|  | 33 | using android::RawEvent; | 
|  | 34 | using android::sp; | 
| Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 35 | using android::UinputHomeKey; | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 36 | using std::chrono_literals::operator""ms; | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 37 | using std::chrono_literals::operator""s; | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | static constexpr bool DEBUG = false; | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 40 |  | 
|  | 41 | static 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 --- | 
|  | 63 | class EventHubTest : public testing::Test { | 
|  | 64 | protected: | 
|  | 65 | std::unique_ptr<EventHubInterface> mEventHub; | 
|  | 66 | // We are only going to emulate a single input device currently. | 
| Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 67 | std::unique_ptr<UinputHomeKey> mKeyboard; | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 68 | int32_t mDeviceId; | 
| Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 69 |  | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 70 | virtual void SetUp() override { | 
|  | 71 | mEventHub = std::make_unique<EventHub>(); | 
|  | 72 | consumeInitialDeviceAddedEvents(); | 
| Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 73 | mKeyboard = createUinputDevice<UinputHomeKey>(); | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 74 | ASSERT_NO_FATAL_FAILURE(mDeviceId = waitForDeviceCreation()); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 75 | } | 
|  | 76 | virtual void TearDown() override { | 
| Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 77 | mKeyboard.reset(); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 78 | waitForDeviceClose(mDeviceId); | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 79 | assertNoMoreEvents(); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 82 | /** | 
|  | 83 | * Return the device id of the created device. | 
|  | 84 | */ | 
|  | 85 | int32_t waitForDeviceCreation(); | 
|  | 86 | void waitForDeviceClose(int32_t deviceId); | 
|  | 87 | void consumeInitialDeviceAddedEvents(); | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 88 | 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 Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 99 | }; | 
|  | 100 |  | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 101 | std::vector<RawEvent> EventHubTest::getEvents(std::optional<size_t> expectedEvents) { | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 102 | static constexpr size_t EVENT_BUFFER_SIZE = 256; | 
|  | 103 | std::array<RawEvent, EVENT_BUFFER_SIZE> eventBuffer; | 
|  | 104 | std::vector<RawEvent> events; | 
|  | 105 |  | 
|  | 106 | while (true) { | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 107 | std::chrono::milliseconds timeout = 0s; | 
|  | 108 | if (expectedEvents) { | 
|  | 109 | timeout = 2s; | 
|  | 110 | } | 
|  | 111 | const size_t count = | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 112 | mEventHub->getEvents(timeout.count(), eventBuffer.data(), eventBuffer.size()); | 
|  | 113 | if (count == 0) { | 
|  | 114 | break; | 
|  | 115 | } | 
|  | 116 | events.insert(events.end(), eventBuffer.begin(), eventBuffer.begin() + count); | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 117 | if (expectedEvents && events.size() >= *expectedEvents) { | 
|  | 118 | break; | 
|  | 119 | } | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 120 | } | 
|  | 121 | if (DEBUG) { | 
|  | 122 | dumpEvents(events); | 
|  | 123 | } | 
|  | 124 | return events; | 
|  | 125 | } | 
|  | 126 |  | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 127 | /** | 
|  | 128 | * Since the test runs on a real platform, there will be existing devices | 
|  | 129 | * in addition to the test devices being added. Therefore, when EventHub is first created, | 
|  | 130 | * it will return a lot of "device added" type of events. | 
|  | 131 | */ | 
|  | 132 | void EventHubTest::consumeInitialDeviceAddedEvents() { | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 133 | std::vector<RawEvent> events = getEvents(); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 134 | std::set<int32_t /*deviceId*/> existingDevices; | 
|  | 135 | // All of the events should be DEVICE_ADDED type, except the last one. | 
|  | 136 | for (size_t i = 0; i < events.size() - 1; i++) { | 
|  | 137 | const RawEvent& event = events[i]; | 
|  | 138 | EXPECT_EQ(EventHubInterface::DEVICE_ADDED, event.type); | 
|  | 139 | existingDevices.insert(event.deviceId); | 
|  | 140 | } | 
|  | 141 | // None of the existing system devices should be changing while this test is run. | 
|  | 142 | // Check that the returned device ids are unique for all of the existing devices. | 
|  | 143 | EXPECT_EQ(existingDevices.size(), events.size() - 1); | 
|  | 144 | // The last event should be "finished device scan" | 
|  | 145 | EXPECT_EQ(EventHubInterface::FINISHED_DEVICE_SCAN, events[events.size() - 1].type); | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | int32_t EventHubTest::waitForDeviceCreation() { | 
|  | 149 | // Wait a little longer than usual, to ensure input device has time to be created | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 150 | std::vector<RawEvent> events = getEvents(2); | 
|  | 151 | if (events.size() != 2) { | 
|  | 152 | ADD_FAILURE() << "Instead of 2 events, received " << events.size(); | 
|  | 153 | return 0; // this value is unused | 
|  | 154 | } | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 155 | const RawEvent& deviceAddedEvent = events[0]; | 
|  | 156 | EXPECT_EQ(static_cast<int32_t>(EventHubInterface::DEVICE_ADDED), deviceAddedEvent.type); | 
|  | 157 | InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceAddedEvent.deviceId); | 
|  | 158 | const int32_t deviceId = deviceAddedEvent.deviceId; | 
| Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 159 | EXPECT_EQ(identifier.name, mKeyboard->getName()); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 160 | const RawEvent& finishedDeviceScanEvent = events[1]; | 
|  | 161 | EXPECT_EQ(static_cast<int32_t>(EventHubInterface::FINISHED_DEVICE_SCAN), | 
|  | 162 | finishedDeviceScanEvent.type); | 
|  | 163 | return deviceId; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | void EventHubTest::waitForDeviceClose(int32_t deviceId) { | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 167 | std::vector<RawEvent> events = getEvents(2); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 168 | ASSERT_EQ(2U, events.size()); | 
|  | 169 | const RawEvent& deviceRemovedEvent = events[0]; | 
|  | 170 | EXPECT_EQ(static_cast<int32_t>(EventHubInterface::DEVICE_REMOVED), deviceRemovedEvent.type); | 
|  | 171 | EXPECT_EQ(deviceId, deviceRemovedEvent.deviceId); | 
|  | 172 | const RawEvent& finishedDeviceScanEvent = events[1]; | 
|  | 173 | EXPECT_EQ(static_cast<int32_t>(EventHubInterface::FINISHED_DEVICE_SCAN), | 
|  | 174 | finishedDeviceScanEvent.type); | 
|  | 175 | } | 
|  | 176 |  | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 177 | void EventHubTest::assertNoMoreEvents() { | 
|  | 178 | std::vector<RawEvent> events = getEvents(); | 
|  | 179 | ASSERT_TRUE(events.empty()); | 
|  | 180 | } | 
|  | 181 |  | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 182 | /** | 
|  | 183 | * Ensure that input_events are generated with monotonic clock. | 
|  | 184 | * That means input_event should receive a timestamp that is in the future of the time | 
|  | 185 | * before the event was sent. | 
|  | 186 | * Input system uses CLOCK_MONOTONIC everywhere in the code base. | 
|  | 187 | */ | 
|  | 188 | TEST_F(EventHubTest, InputEvent_TimestampIsMonotonic) { | 
|  | 189 | nsecs_t lastEventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 190 | ASSERT_NO_FATAL_FAILURE(mKeyboard->pressAndReleaseHomeKey()); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 191 |  | 
| Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 192 | std::vector<RawEvent> events = getEvents(4); | 
| Siarhei Vishniakou | 8588cc9 | 2018-12-12 18:17:58 -0800 | [diff] [blame] | 193 | ASSERT_EQ(4U, events.size()) << "Expected to receive 2 keys and 2 syncs, total of 4 events"; | 
|  | 194 | for (const RawEvent& event : events) { | 
|  | 195 | // Cannot use strict comparison because the events may happen too quickly | 
|  | 196 | ASSERT_LE(lastEventTime, event.when) << "Event must have occurred after the key was sent"; | 
|  | 197 | ASSERT_LT(std::chrono::nanoseconds(event.when - lastEventTime), 100ms) | 
|  | 198 | << "Event times are too far apart"; | 
|  | 199 | lastEventTime = event.when; // Ensure all returned events are monotonic | 
|  | 200 | } | 
|  | 201 | } | 
| Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 202 |  | 
|  | 203 | // --- BitArrayTest --- | 
|  | 204 | class BitArrayTest : public testing::Test { | 
|  | 205 | protected: | 
|  | 206 | static constexpr size_t SINGLE_ELE_BITS = 32UL; | 
|  | 207 | static constexpr size_t MULTI_ELE_BITS = 256UL; | 
|  | 208 |  | 
|  | 209 | virtual void SetUp() override { | 
|  | 210 | mBitmaskSingle.loadFromBuffer(mBufferSingle); | 
|  | 211 | mBitmaskMulti.loadFromBuffer(mBufferMulti); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | android::BitArray<SINGLE_ELE_BITS> mBitmaskSingle; | 
|  | 215 | android::BitArray<MULTI_ELE_BITS> mBitmaskMulti; | 
|  | 216 |  | 
|  | 217 | private: | 
|  | 218 | const typename android::BitArray<SINGLE_ELE_BITS>::Buffer mBufferSingle = { | 
|  | 219 | 0x800F0F0FUL // bit 0 - 31 | 
|  | 220 | }; | 
|  | 221 | const typename android::BitArray<MULTI_ELE_BITS>::Buffer mBufferMulti = { | 
|  | 222 | 0xFFFFFFFFUL, // bit 0 - 31 | 
|  | 223 | 0x01000001UL, // bit 32 - 63 | 
|  | 224 | 0x00000000UL, // bit 64 - 95 | 
|  | 225 | 0x80000000UL, // bit 96 - 127 | 
|  | 226 | 0x00000000UL, // bit 128 - 159 | 
|  | 227 | 0x00000000UL, // bit 160 - 191 | 
|  | 228 | 0x80000008UL, // bit 192 - 223 | 
|  | 229 | 0x00000000UL, // bit 224 - 255 | 
|  | 230 | }; | 
|  | 231 | }; | 
|  | 232 |  | 
|  | 233 | TEST_F(BitArrayTest, SetBit) { | 
|  | 234 | ASSERT_TRUE(mBitmaskSingle.test(0)); | 
|  | 235 | ASSERT_TRUE(mBitmaskSingle.test(31)); | 
|  | 236 | ASSERT_FALSE(mBitmaskSingle.test(7)); | 
|  | 237 |  | 
|  | 238 | ASSERT_TRUE(mBitmaskMulti.test(32)); | 
|  | 239 | ASSERT_TRUE(mBitmaskMulti.test(56)); | 
|  | 240 | ASSERT_FALSE(mBitmaskMulti.test(192)); | 
|  | 241 | ASSERT_TRUE(mBitmaskMulti.test(223)); | 
|  | 242 | ASSERT_FALSE(mBitmaskMulti.test(255)); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | TEST_F(BitArrayTest, AnyBit) { | 
|  | 246 | ASSERT_TRUE(mBitmaskSingle.any(31, 32)); | 
|  | 247 | ASSERT_FALSE(mBitmaskSingle.any(12, 16)); | 
|  | 248 |  | 
|  | 249 | ASSERT_TRUE(mBitmaskMulti.any(31, 32)); | 
|  | 250 | ASSERT_FALSE(mBitmaskMulti.any(33, 33)); | 
|  | 251 | ASSERT_TRUE(mBitmaskMulti.any(32, 55)); | 
|  | 252 | ASSERT_TRUE(mBitmaskMulti.any(33, 57)); | 
|  | 253 | ASSERT_FALSE(mBitmaskMulti.any(33, 55)); | 
|  | 254 | ASSERT_FALSE(mBitmaskMulti.any(130, 190)); | 
|  | 255 |  | 
|  | 256 | ASSERT_FALSE(mBitmaskMulti.any(128, 195)); | 
|  | 257 | ASSERT_TRUE(mBitmaskMulti.any(128, 196)); | 
|  | 258 | ASSERT_TRUE(mBitmaskMulti.any(128, 224)); | 
|  | 259 | ASSERT_FALSE(mBitmaskMulti.any(255, 256)); | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | TEST_F(BitArrayTest, SetBit_InvalidBitIndex) { | 
|  | 263 | ASSERT_FALSE(mBitmaskSingle.test(32)); | 
|  | 264 | ASSERT_FALSE(mBitmaskMulti.test(256)); | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | TEST_F(BitArrayTest, AnyBit_InvalidBitIndex) { | 
|  | 268 | ASSERT_FALSE(mBitmaskSingle.any(32, 32)); | 
|  | 269 | ASSERT_FALSE(mBitmaskSingle.any(33, 34)); | 
|  | 270 |  | 
|  | 271 | ASSERT_FALSE(mBitmaskMulti.any(256, 256)); | 
|  | 272 | ASSERT_FALSE(mBitmaskMulti.any(257, 258)); | 
|  | 273 | ASSERT_FALSE(mBitmaskMulti.any(0, 0)); | 
|  | 274 | } |