Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022 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 | #include <gestures/HardwareStateConverter.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <linux/input-event-codes.h> |
| 21 | |
| 22 | #include "FakeEventHub.h" |
| 23 | #include "FakeInputReaderPolicy.h" |
| 24 | #include "InstrumentedInputReader.h" |
| 25 | #include "TestConstants.h" |
| 26 | #include "TestInputListener.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | class HardwareStateConverterTest : public testing::Test { |
| 31 | protected: |
| 32 | static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000; |
| 33 | static constexpr int32_t EVENTHUB_ID = 1; |
| 34 | |
| 35 | void SetUp() { |
| 36 | mFakeEventHub = std::make_unique<FakeEventHub>(); |
| 37 | mFakePolicy = sp<FakeInputReaderPolicy>::make(); |
| 38 | mFakeListener = std::make_unique<TestInputListener>(); |
| 39 | mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy, |
| 40 | *mFakeListener); |
| 41 | mDevice = newDevice(); |
| 42 | |
| 43 | mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, 0, 7, 0, 0, 0); |
| 44 | } |
| 45 | |
| 46 | std::shared_ptr<InputDevice> newDevice() { |
| 47 | InputDeviceIdentifier identifier; |
| 48 | identifier.name = "device"; |
| 49 | identifier.location = "USB1"; |
| 50 | identifier.bus = 0; |
| 51 | std::shared_ptr<InputDevice> device = |
| 52 | std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, /* generation= */ 2, |
| 53 | identifier); |
| 54 | mReader->pushNextDevice(device); |
| 55 | mFakeEventHub->addDevice(EVENTHUB_ID, identifier.name, InputDeviceClass::TOUCHPAD, |
| 56 | identifier.bus); |
| 57 | mReader->loopOnce(); |
| 58 | return device; |
| 59 | } |
| 60 | |
| 61 | void processAxis(HardwareStateConverter& conv, nsecs_t when, int32_t type, int32_t code, |
| 62 | int32_t value) { |
| 63 | RawEvent event; |
| 64 | event.when = when; |
| 65 | event.readTime = READ_TIME; |
| 66 | event.deviceId = EVENTHUB_ID; |
| 67 | event.type = type; |
| 68 | event.code = code; |
| 69 | event.value = value; |
| 70 | std::optional<SelfContainedHardwareState> schs = conv.processRawEvent(&event); |
| 71 | EXPECT_FALSE(schs.has_value()); |
| 72 | } |
| 73 | |
| 74 | std::optional<SelfContainedHardwareState> processSync(HardwareStateConverter& conv, |
| 75 | nsecs_t when) { |
| 76 | RawEvent event; |
| 77 | event.when = when; |
| 78 | event.readTime = READ_TIME; |
| 79 | event.deviceId = EVENTHUB_ID; |
| 80 | event.type = EV_SYN; |
| 81 | event.code = SYN_REPORT; |
| 82 | event.value = 0; |
| 83 | return conv.processRawEvent(&event); |
| 84 | } |
| 85 | |
| 86 | std::shared_ptr<FakeEventHub> mFakeEventHub; |
| 87 | sp<FakeInputReaderPolicy> mFakePolicy; |
| 88 | std::unique_ptr<TestInputListener> mFakeListener; |
| 89 | std::unique_ptr<InstrumentedInputReader> mReader; |
| 90 | std::shared_ptr<InputDevice> mDevice; |
| 91 | }; |
| 92 | |
| 93 | TEST_F(HardwareStateConverterTest, OneFinger) { |
| 94 | const nsecs_t time = 1500000000; |
| 95 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 96 | HardwareStateConverter conv(deviceContext); |
| 97 | |
| 98 | processAxis(conv, time, EV_ABS, ABS_MT_SLOT, 0); |
| 99 | processAxis(conv, time, EV_ABS, ABS_MT_TRACKING_ID, 123); |
| 100 | processAxis(conv, time, EV_ABS, ABS_MT_POSITION_X, 50); |
| 101 | processAxis(conv, time, EV_ABS, ABS_MT_POSITION_Y, 100); |
| 102 | processAxis(conv, time, EV_ABS, ABS_MT_TOUCH_MAJOR, 5); |
| 103 | processAxis(conv, time, EV_ABS, ABS_MT_TOUCH_MINOR, 4); |
| 104 | processAxis(conv, time, EV_ABS, ABS_MT_PRESSURE, 42); |
| 105 | processAxis(conv, time, EV_ABS, ABS_MT_ORIENTATION, 2); |
| 106 | |
| 107 | processAxis(conv, time, EV_ABS, ABS_X, 50); |
| 108 | processAxis(conv, time, EV_ABS, ABS_Y, 100); |
| 109 | processAxis(conv, time, EV_ABS, ABS_PRESSURE, 42); |
| 110 | |
| 111 | processAxis(conv, time, EV_KEY, BTN_TOUCH, 1); |
| 112 | processAxis(conv, time, EV_KEY, BTN_TOOL_FINGER, 1); |
| 113 | std::optional<SelfContainedHardwareState> schs = processSync(conv, time); |
| 114 | |
| 115 | ASSERT_TRUE(schs.has_value()); |
| 116 | const HardwareState& state = schs->state; |
| 117 | EXPECT_NEAR(1.5, state.timestamp, EPSILON); |
| 118 | EXPECT_EQ(0, state.buttons_down); |
| 119 | EXPECT_EQ(1, state.touch_cnt); |
| 120 | |
| 121 | ASSERT_EQ(1, state.finger_cnt); |
| 122 | const FingerState& finger = state.fingers[0]; |
| 123 | EXPECT_EQ(123, finger.tracking_id); |
| 124 | EXPECT_NEAR(50, finger.position_x, EPSILON); |
| 125 | EXPECT_NEAR(100, finger.position_y, EPSILON); |
| 126 | EXPECT_NEAR(5, finger.touch_major, EPSILON); |
| 127 | EXPECT_NEAR(4, finger.touch_minor, EPSILON); |
| 128 | EXPECT_NEAR(42, finger.pressure, EPSILON); |
| 129 | EXPECT_NEAR(2, finger.orientation, EPSILON); |
| 130 | EXPECT_EQ(0u, finger.flags); |
| 131 | |
| 132 | EXPECT_EQ(0, state.rel_x); |
| 133 | EXPECT_EQ(0, state.rel_y); |
| 134 | EXPECT_EQ(0, state.rel_wheel); |
| 135 | EXPECT_EQ(0, state.rel_wheel_hi_res); |
| 136 | EXPECT_EQ(0, state.rel_hwheel); |
| 137 | EXPECT_NEAR(0.0, state.msc_timestamp, EPSILON); |
| 138 | } |
| 139 | |
| 140 | TEST_F(HardwareStateConverterTest, TwoFingers) { |
| 141 | const nsecs_t time = ARBITRARY_TIME; |
| 142 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 143 | HardwareStateConverter conv(deviceContext); |
| 144 | |
| 145 | processAxis(conv, time, EV_ABS, ABS_MT_SLOT, 0); |
| 146 | processAxis(conv, time, EV_ABS, ABS_MT_TRACKING_ID, 123); |
| 147 | processAxis(conv, time, EV_ABS, ABS_MT_POSITION_X, 50); |
| 148 | processAxis(conv, time, EV_ABS, ABS_MT_POSITION_Y, 100); |
| 149 | processAxis(conv, time, EV_ABS, ABS_MT_TOUCH_MAJOR, 5); |
| 150 | processAxis(conv, time, EV_ABS, ABS_MT_TOUCH_MINOR, 4); |
| 151 | processAxis(conv, time, EV_ABS, ABS_MT_PRESSURE, 42); |
| 152 | processAxis(conv, time, EV_ABS, ABS_MT_ORIENTATION, 2); |
| 153 | |
| 154 | processAxis(conv, time, EV_ABS, ABS_MT_SLOT, 1); |
| 155 | processAxis(conv, time, EV_ABS, ABS_MT_TRACKING_ID, 456); |
| 156 | processAxis(conv, time, EV_ABS, ABS_MT_POSITION_X, -20); |
| 157 | processAxis(conv, time, EV_ABS, ABS_MT_POSITION_Y, 40); |
| 158 | processAxis(conv, time, EV_ABS, ABS_MT_TOUCH_MAJOR, 8); |
| 159 | processAxis(conv, time, EV_ABS, ABS_MT_TOUCH_MINOR, 7); |
| 160 | processAxis(conv, time, EV_ABS, ABS_MT_PRESSURE, 21); |
| 161 | processAxis(conv, time, EV_ABS, ABS_MT_ORIENTATION, 1); |
| 162 | |
| 163 | processAxis(conv, time, EV_ABS, ABS_X, 50); |
| 164 | processAxis(conv, time, EV_ABS, ABS_Y, 100); |
| 165 | processAxis(conv, time, EV_ABS, ABS_PRESSURE, 42); |
| 166 | |
| 167 | processAxis(conv, time, EV_KEY, BTN_TOUCH, 1); |
| 168 | processAxis(conv, time, EV_KEY, BTN_TOOL_DOUBLETAP, 1); |
| 169 | std::optional<SelfContainedHardwareState> schs = processSync(conv, time); |
| 170 | |
| 171 | ASSERT_TRUE(schs.has_value()); |
| 172 | ASSERT_EQ(2, schs->state.finger_cnt); |
| 173 | const FingerState& finger1 = schs->state.fingers[0]; |
| 174 | EXPECT_EQ(123, finger1.tracking_id); |
| 175 | EXPECT_NEAR(50, finger1.position_x, EPSILON); |
| 176 | EXPECT_NEAR(100, finger1.position_y, EPSILON); |
| 177 | EXPECT_NEAR(5, finger1.touch_major, EPSILON); |
| 178 | EXPECT_NEAR(4, finger1.touch_minor, EPSILON); |
| 179 | EXPECT_NEAR(42, finger1.pressure, EPSILON); |
| 180 | EXPECT_NEAR(2, finger1.orientation, EPSILON); |
| 181 | EXPECT_EQ(0u, finger1.flags); |
| 182 | |
| 183 | const FingerState& finger2 = schs->state.fingers[1]; |
| 184 | EXPECT_EQ(456, finger2.tracking_id); |
| 185 | EXPECT_NEAR(-20, finger2.position_x, EPSILON); |
| 186 | EXPECT_NEAR(40, finger2.position_y, EPSILON); |
| 187 | EXPECT_NEAR(8, finger2.touch_major, EPSILON); |
| 188 | EXPECT_NEAR(7, finger2.touch_minor, EPSILON); |
| 189 | EXPECT_NEAR(21, finger2.pressure, EPSILON); |
| 190 | EXPECT_NEAR(1, finger2.orientation, EPSILON); |
| 191 | EXPECT_EQ(0u, finger2.flags); |
| 192 | } |
| 193 | |
| 194 | TEST_F(HardwareStateConverterTest, ButtonPressed) { |
| 195 | const nsecs_t time = ARBITRARY_TIME; |
| 196 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 197 | HardwareStateConverter conv(deviceContext); |
| 198 | |
| 199 | processAxis(conv, time, EV_KEY, BTN_LEFT, 1); |
| 200 | std::optional<SelfContainedHardwareState> schs = processSync(conv, time); |
| 201 | |
| 202 | ASSERT_TRUE(schs.has_value()); |
| 203 | EXPECT_EQ(GESTURES_BUTTON_LEFT, schs->state.buttons_down); |
| 204 | } |
| 205 | |
| 206 | TEST_F(HardwareStateConverterTest, MscTimestamp) { |
| 207 | const nsecs_t time = ARBITRARY_TIME; |
| 208 | mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP); |
| 209 | InputDeviceContext deviceContext(*mDevice, EVENTHUB_ID); |
| 210 | HardwareStateConverter conv(deviceContext); |
| 211 | |
| 212 | processAxis(conv, time, EV_MSC, MSC_TIMESTAMP, 1200000); |
| 213 | std::optional<SelfContainedHardwareState> schs = processSync(conv, time); |
| 214 | |
| 215 | ASSERT_TRUE(schs.has_value()); |
| 216 | EXPECT_NEAR(1.2, schs->state.msc_timestamp, EPSILON); |
| 217 | } |
| 218 | |
| 219 | } // namespace android |