Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 "TestHelpers.h" |
| 18 | |
| 19 | #include <chrono> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <attestation/HmacKeyManager.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <input/InputTransport.h> |
| 25 | |
| 26 | using namespace std::chrono_literals; |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | struct Pointer { |
| 31 | int32_t id; |
| 32 | float x; |
| 33 | float y; |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 34 | bool isResampled = false; |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct InputEventEntry { |
| 38 | std::chrono::nanoseconds eventTime; |
| 39 | std::vector<Pointer> pointers; |
| 40 | int32_t action; |
| 41 | }; |
| 42 | |
| 43 | class TouchResamplingTest : public testing::Test { |
| 44 | protected: |
| 45 | std::unique_ptr<InputPublisher> mPublisher; |
| 46 | std::unique_ptr<InputConsumer> mConsumer; |
| 47 | PreallocatedInputEventFactory mEventFactory; |
| 48 | |
| 49 | uint32_t mSeq = 1; |
| 50 | |
| 51 | void SetUp() override { |
| 52 | std::unique_ptr<InputChannel> serverChannel, clientChannel; |
| 53 | status_t result = |
| 54 | InputChannel::openInputChannelPair("channel name", serverChannel, clientChannel); |
| 55 | ASSERT_EQ(OK, result); |
| 56 | |
| 57 | mPublisher = std::make_unique<InputPublisher>(std::move(serverChannel)); |
| 58 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel), |
| 59 | true /* enableTouchResampling */); |
| 60 | } |
| 61 | |
| 62 | status_t publishSimpleMotionEventWithCoords(int32_t action, nsecs_t eventTime, |
| 63 | const std::vector<PointerProperties>& properties, |
| 64 | const std::vector<PointerCoords>& coords); |
| 65 | void publishSimpleMotionEvent(int32_t action, nsecs_t eventTime, |
| 66 | const std::vector<Pointer>& pointers); |
| 67 | void publishInputEventEntries(const std::vector<InputEventEntry>& entries); |
| 68 | void consumeInputEventEntries(const std::vector<InputEventEntry>& entries, |
| 69 | std::chrono::nanoseconds frameTime); |
| 70 | void receiveResponseUntilSequence(uint32_t seq); |
| 71 | }; |
| 72 | |
| 73 | status_t TouchResamplingTest::publishSimpleMotionEventWithCoords( |
| 74 | int32_t action, nsecs_t eventTime, const std::vector<PointerProperties>& properties, |
| 75 | const std::vector<PointerCoords>& coords) { |
| 76 | const ui::Transform identityTransform; |
| 77 | const nsecs_t downTime = 0; |
| 78 | |
| 79 | if (action == AMOTION_EVENT_ACTION_DOWN && eventTime != 0) { |
| 80 | ADD_FAILURE() << "Downtime should be equal to 0 (hardcoded for convenience)"; |
| 81 | } |
| 82 | return mPublisher->publishMotionEvent(mSeq++, InputEvent::nextId(), 1 /*deviceId*/, |
| 83 | AINPUT_SOURCE_TOUCHSCREEN, 0 /*displayId*/, INVALID_HMAC, |
| 84 | action, 0 /*actionButton*/, 0 /*flags*/, 0 /*edgeFlags*/, |
| 85 | AMETA_NONE, 0 /*buttonState*/, MotionClassification::NONE, |
| 86 | identityTransform, 0 /*xPrecision*/, 0 /*yPrecision*/, |
| 87 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 88 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, |
| 89 | downTime, eventTime, properties.size(), properties.data(), |
| 90 | coords.data()); |
| 91 | } |
| 92 | |
| 93 | void TouchResamplingTest::publishSimpleMotionEvent(int32_t action, nsecs_t eventTime, |
| 94 | const std::vector<Pointer>& pointers) { |
| 95 | std::vector<PointerProperties> properties; |
| 96 | std::vector<PointerCoords> coords; |
| 97 | |
| 98 | for (const Pointer& pointer : pointers) { |
| 99 | properties.push_back({}); |
| 100 | properties.back().clear(); |
| 101 | properties.back().id = pointer.id; |
| 102 | properties.back().toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 103 | |
| 104 | coords.push_back({}); |
| 105 | coords.back().clear(); |
| 106 | coords.back().setAxisValue(AMOTION_EVENT_AXIS_X, pointer.x); |
| 107 | coords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, pointer.y); |
| 108 | } |
| 109 | |
| 110 | status_t result = publishSimpleMotionEventWithCoords(action, eventTime, properties, coords); |
| 111 | ASSERT_EQ(OK, result); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Each entry is published separately, one entry at a time. As a result, action is used here |
| 116 | * on a per-entry basis. |
| 117 | */ |
| 118 | void TouchResamplingTest::publishInputEventEntries(const std::vector<InputEventEntry>& entries) { |
| 119 | for (const InputEventEntry& entry : entries) { |
| 120 | publishSimpleMotionEvent(entry.action, entry.eventTime.count(), entry.pointers); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Inside the publisher, read responses repeatedly until the desired sequence number is returned. |
| 126 | * |
| 127 | * Sometimes, when you call 'sendFinishedSignal', you would be finishing a batch which is comprised |
| 128 | * of several input events. As a result, consumer will generate multiple 'finish' signals on your |
| 129 | * behalf. |
| 130 | * |
| 131 | * In this function, we call 'receiveConsumerResponse' in a loop until the desired sequence number |
| 132 | * is returned. |
| 133 | */ |
| 134 | void TouchResamplingTest::receiveResponseUntilSequence(uint32_t seq) { |
| 135 | size_t consumedEvents = 0; |
| 136 | while (consumedEvents < 100) { |
| 137 | android::base::Result<InputPublisher::ConsumerResponse> response = |
| 138 | mPublisher->receiveConsumerResponse(); |
| 139 | ASSERT_TRUE(response.ok()); |
| 140 | ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*response)); |
| 141 | const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*response); |
| 142 | ASSERT_TRUE(finish.handled) |
| 143 | << "publisher receiveFinishedSignal should have set handled to consumer's reply"; |
| 144 | if (finish.seq == seq) { |
| 145 | return; |
| 146 | } |
| 147 | consumedEvents++; |
| 148 | } |
| 149 | FAIL() << "Got " << consumedEvents << "events, but still no event with seq=" << seq; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * All entries are compared against a single MotionEvent, but the same data structure |
| 154 | * InputEventEntry is used here for simpler code. As a result, the entire array of InputEventEntry |
| 155 | * must contain identical values for the action field. |
| 156 | */ |
| 157 | void TouchResamplingTest::consumeInputEventEntries(const std::vector<InputEventEntry>& entries, |
| 158 | std::chrono::nanoseconds frameTime) { |
| 159 | ASSERT_GE(entries.size(), 1U) << "Must have at least 1 InputEventEntry to compare against"; |
| 160 | |
| 161 | uint32_t consumeSeq; |
| 162 | InputEvent* event; |
| 163 | |
| 164 | status_t status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, frameTime.count(), |
| 165 | &consumeSeq, &event); |
| 166 | ASSERT_EQ(OK, status); |
| 167 | MotionEvent* motionEvent = static_cast<MotionEvent*>(event); |
| 168 | |
| 169 | ASSERT_EQ(entries.size() - 1, motionEvent->getHistorySize()); |
| 170 | for (size_t i = 0; i < entries.size(); i++) { // most recent sample is last |
| 171 | SCOPED_TRACE(i); |
| 172 | const InputEventEntry& entry = entries[i]; |
| 173 | ASSERT_EQ(entry.action, motionEvent->getAction()); |
| 174 | ASSERT_EQ(entry.eventTime.count(), motionEvent->getHistoricalEventTime(i)); |
| 175 | ASSERT_EQ(entry.pointers.size(), motionEvent->getPointerCount()); |
| 176 | |
| 177 | for (size_t p = 0; p < motionEvent->getPointerCount(); p++) { |
| 178 | SCOPED_TRACE(p); |
| 179 | // The pointers can be in any order, both in MotionEvent as well as InputEventEntry |
| 180 | ssize_t motionEventPointerIndex = motionEvent->findPointerIndex(entry.pointers[p].id); |
| 181 | ASSERT_GE(motionEventPointerIndex, 0) << "Pointer must be present in MotionEvent"; |
| 182 | ASSERT_EQ(entry.pointers[p].x, |
| 183 | motionEvent->getHistoricalAxisValue(AMOTION_EVENT_AXIS_X, |
| 184 | motionEventPointerIndex, i)); |
| 185 | ASSERT_EQ(entry.pointers[p].x, |
| 186 | motionEvent->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_X, |
| 187 | motionEventPointerIndex, i)); |
| 188 | ASSERT_EQ(entry.pointers[p].y, |
| 189 | motionEvent->getHistoricalAxisValue(AMOTION_EVENT_AXIS_Y, |
| 190 | motionEventPointerIndex, i)); |
| 191 | ASSERT_EQ(entry.pointers[p].y, |
| 192 | motionEvent->getHistoricalRawAxisValue(AMOTION_EVENT_AXIS_Y, |
| 193 | motionEventPointerIndex, i)); |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 194 | ASSERT_EQ(entry.pointers[p].isResampled, |
| 195 | motionEvent->isResampled(motionEventPointerIndex, i)); |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 200 | ASSERT_EQ(OK, status); |
| 201 | |
| 202 | receiveResponseUntilSequence(consumeSeq); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Timeline |
| 207 | * ---------+------------------+------------------+--------+-----------------+---------------------- |
| 208 | * 0 ms 10 ms 20 ms 25 ms 35 ms |
| 209 | * ACTION_DOWN ACTION_MOVE ACTION_MOVE ^ ^ |
| 210 | * | | |
| 211 | * resampled value | |
| 212 | * frameTime |
| 213 | * Typically, the prediction is made for time frameTime - RESAMPLE_LATENCY, or 30 ms in this case |
| 214 | * However, that would be 10 ms later than the last real sample (which came in at 20 ms). |
| 215 | * Therefore, the resampling should happen at 20 ms + RESAMPLE_MAX_PREDICTION = 28 ms. |
| 216 | * In this situation, though, resample time is further limited by taking half of the difference |
| 217 | * between the last two real events, which would put this time at: |
| 218 | * 20 ms + (20 ms - 10 ms) / 2 = 25 ms. |
| 219 | */ |
| 220 | TEST_F(TouchResamplingTest, EventIsResampled) { |
| 221 | std::chrono::nanoseconds frameTime; |
| 222 | std::vector<InputEventEntry> entries, expectedEntries; |
| 223 | |
| 224 | // Initial ACTION_DOWN should be separate, because the first consume event will only return |
| 225 | // InputEvent with a single action. |
| 226 | entries = { |
| 227 | // id x y |
| 228 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 229 | }; |
| 230 | publishInputEventEntries(entries); |
| 231 | frameTime = 5ms; |
| 232 | expectedEntries = { |
| 233 | // id x y |
| 234 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 235 | }; |
| 236 | consumeInputEventEntries(expectedEntries, frameTime); |
| 237 | |
| 238 | // Two ACTION_MOVE events 10 ms apart that move in X direction and stay still in Y |
| 239 | entries = { |
| 240 | // id x y |
| 241 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 242 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 243 | }; |
| 244 | publishInputEventEntries(entries); |
| 245 | frameTime = 35ms; |
| 246 | expectedEntries = { |
| 247 | // id x y |
| 248 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 249 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 250 | {25ms, {{0, 35, 30, .isResampled = true}}, AMOTION_EVENT_ACTION_MOVE}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 251 | }; |
| 252 | consumeInputEventEntries(expectedEntries, frameTime); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Same as above test, but use pointer id=1 instead of 0 to make sure that system does not |
| 257 | * have these hardcoded. |
| 258 | */ |
| 259 | TEST_F(TouchResamplingTest, EventIsResampledWithDifferentId) { |
| 260 | std::chrono::nanoseconds frameTime; |
| 261 | std::vector<InputEventEntry> entries, expectedEntries; |
| 262 | |
| 263 | // Initial ACTION_DOWN should be separate, because the first consume event will only return |
| 264 | // InputEvent with a single action. |
| 265 | entries = { |
| 266 | // id x y |
| 267 | {0ms, {{1, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 268 | }; |
| 269 | publishInputEventEntries(entries); |
| 270 | frameTime = 5ms; |
| 271 | expectedEntries = { |
| 272 | // id x y |
| 273 | {0ms, {{1, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 274 | }; |
| 275 | consumeInputEventEntries(expectedEntries, frameTime); |
| 276 | |
| 277 | // Two ACTION_MOVE events 10 ms apart that move in X direction and stay still in Y |
| 278 | entries = { |
| 279 | // id x y |
| 280 | {10ms, {{1, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 281 | {20ms, {{1, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 282 | }; |
| 283 | publishInputEventEntries(entries); |
| 284 | frameTime = 35ms; |
| 285 | expectedEntries = { |
| 286 | // id x y |
| 287 | {10ms, {{1, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 288 | {20ms, {{1, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 289 | {25ms, {{1, 35, 30, .isResampled = true}}, AMOTION_EVENT_ACTION_MOVE}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 290 | }; |
| 291 | consumeInputEventEntries(expectedEntries, frameTime); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Event should not be resampled when sample time is equal to event time. |
| 296 | */ |
| 297 | TEST_F(TouchResamplingTest, SampleTimeEqualsEventTime) { |
| 298 | std::chrono::nanoseconds frameTime; |
| 299 | std::vector<InputEventEntry> entries, expectedEntries; |
| 300 | |
| 301 | // Initial ACTION_DOWN should be separate, because the first consume event will only return |
| 302 | // InputEvent with a single action. |
| 303 | entries = { |
| 304 | // id x y |
| 305 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 306 | }; |
| 307 | publishInputEventEntries(entries); |
| 308 | frameTime = 5ms; |
| 309 | expectedEntries = { |
| 310 | // id x y |
| 311 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 312 | }; |
| 313 | consumeInputEventEntries(expectedEntries, frameTime); |
| 314 | |
| 315 | // Two ACTION_MOVE events 10 ms apart that move in X direction and stay still in Y |
| 316 | entries = { |
| 317 | // id x y |
| 318 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 319 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 320 | }; |
| 321 | publishInputEventEntries(entries); |
| 322 | frameTime = 20ms + 5ms /*RESAMPLE_LATENCY*/; |
| 323 | expectedEntries = { |
| 324 | // id x y |
| 325 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 326 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 327 | // no resampled event because the time of resample falls exactly on the existing event |
| 328 | }; |
| 329 | consumeInputEventEntries(expectedEntries, frameTime); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Once we send a resampled value to the app, we should continue to "lie" if the pointer |
| 334 | * does not move. So, if the pointer keeps the same coordinates, resampled value should continue |
| 335 | * to be used. |
| 336 | */ |
| 337 | TEST_F(TouchResamplingTest, ResampledValueIsUsedForIdenticalCoordinates) { |
| 338 | std::chrono::nanoseconds frameTime; |
| 339 | std::vector<InputEventEntry> entries, expectedEntries; |
| 340 | |
| 341 | // Initial ACTION_DOWN should be separate, because the first consume event will only return |
| 342 | // InputEvent with a single action. |
| 343 | entries = { |
| 344 | // id x y |
| 345 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 346 | }; |
| 347 | publishInputEventEntries(entries); |
| 348 | frameTime = 5ms; |
| 349 | expectedEntries = { |
| 350 | // id x y |
| 351 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 352 | }; |
| 353 | consumeInputEventEntries(expectedEntries, frameTime); |
| 354 | |
| 355 | // Two ACTION_MOVE events 10 ms apart that move in X direction and stay still in Y |
| 356 | entries = { |
| 357 | // id x y |
| 358 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 359 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 360 | }; |
| 361 | publishInputEventEntries(entries); |
| 362 | frameTime = 35ms; |
| 363 | expectedEntries = { |
| 364 | // id x y |
| 365 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 366 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 367 | {25ms, {{0, 35, 30, .isResampled = true}}, AMOTION_EVENT_ACTION_MOVE}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 368 | }; |
| 369 | consumeInputEventEntries(expectedEntries, frameTime); |
| 370 | |
| 371 | // Coordinate value 30 has been resampled to 35. When a new event comes in with value 30 again, |
| 372 | // the system should still report 35. |
| 373 | entries = { |
| 374 | // id x y |
| 375 | {40ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 376 | }; |
| 377 | publishInputEventEntries(entries); |
| 378 | frameTime = 45ms + 5ms /*RESAMPLE_LATENCY*/; |
| 379 | expectedEntries = { |
| 380 | // id x y |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 381 | {40ms, |
| 382 | {{0, 35, 30, .isResampled = true}}, |
| 383 | AMOTION_EVENT_ACTION_MOVE}, // original event, rewritten |
| 384 | {45ms, |
| 385 | {{0, 35, 30, .isResampled = true}}, |
| 386 | AMOTION_EVENT_ACTION_MOVE}, // resampled event, rewritten |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 387 | }; |
| 388 | consumeInputEventEntries(expectedEntries, frameTime); |
| 389 | } |
| 390 | |
| 391 | TEST_F(TouchResamplingTest, OldEventReceivedAfterResampleOccurs) { |
| 392 | std::chrono::nanoseconds frameTime; |
| 393 | std::vector<InputEventEntry> entries, expectedEntries; |
| 394 | |
| 395 | // Initial ACTION_DOWN should be separate, because the first consume event will only return |
| 396 | // InputEvent with a single action. |
| 397 | entries = { |
| 398 | // id x y |
| 399 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 400 | }; |
| 401 | publishInputEventEntries(entries); |
| 402 | frameTime = 5ms; |
| 403 | expectedEntries = { |
| 404 | // id x y |
| 405 | {0ms, {{0, 10, 20}}, AMOTION_EVENT_ACTION_DOWN}, |
| 406 | }; |
| 407 | consumeInputEventEntries(expectedEntries, frameTime); |
| 408 | |
| 409 | // Two ACTION_MOVE events 10 ms apart that move in X direction and stay still in Y |
| 410 | entries = { |
| 411 | // id x y |
| 412 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 413 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 414 | }; |
| 415 | publishInputEventEntries(entries); |
| 416 | frameTime = 35ms; |
| 417 | expectedEntries = { |
| 418 | // id x y |
| 419 | {10ms, {{0, 20, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 420 | {20ms, {{0, 30, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 421 | {25ms, {{0, 35, 30, .isResampled = true}}, AMOTION_EVENT_ACTION_MOVE}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 422 | }; |
| 423 | consumeInputEventEntries(expectedEntries, frameTime); |
| 424 | // Above, the resampled event is at 25ms rather than at 30 ms = 35ms - RESAMPLE_LATENCY |
| 425 | // because we are further bound by how far we can extrapolate by the "last time delta". |
| 426 | // That's 50% of (20 ms - 10ms) => 5ms. So we can't predict more than 5 ms into the future |
| 427 | // from the event at 20ms, which is why the resampled event is at t = 25 ms. |
| 428 | |
| 429 | // We resampled the event to 25 ms. Now, an older 'real' event comes in. |
| 430 | entries = { |
| 431 | // id x y |
| 432 | {24ms, {{0, 40, 30}}, AMOTION_EVENT_ACTION_MOVE}, |
| 433 | }; |
| 434 | publishInputEventEntries(entries); |
| 435 | frameTime = 50ms; |
| 436 | expectedEntries = { |
| 437 | // id x y |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 438 | {24ms, |
| 439 | {{0, 35, 30, .isResampled = true}}, |
| 440 | AMOTION_EVENT_ACTION_MOVE}, // original event, rewritten |
| 441 | {26ms, |
| 442 | {{0, 45, 30, .isResampled = true}}, |
| 443 | AMOTION_EVENT_ACTION_MOVE}, // resampled event, rewritten |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 444 | }; |
| 445 | consumeInputEventEntries(expectedEntries, frameTime); |
| 446 | } |
| 447 | |
| 448 | TEST_F(TouchResamplingTest, TwoPointersAreResampledIndependently) { |
| 449 | std::chrono::nanoseconds frameTime; |
| 450 | std::vector<InputEventEntry> entries, expectedEntries; |
| 451 | |
| 452 | // full action for when a pointer with id=1 appears (some other pointer must already be present) |
| 453 | constexpr int32_t actionPointer1Down = |
| 454 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 455 | |
| 456 | // full action for when a pointer with id=0 disappears (some other pointer must still remain) |
| 457 | constexpr int32_t actionPointer0Up = |
| 458 | AMOTION_EVENT_ACTION_POINTER_UP + (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 459 | |
| 460 | // Initial ACTION_DOWN should be separate, because the first consume event will only return |
| 461 | // InputEvent with a single action. |
| 462 | entries = { |
| 463 | // id x y |
| 464 | {0ms, {{0, 100, 100}}, AMOTION_EVENT_ACTION_DOWN}, |
| 465 | }; |
| 466 | publishInputEventEntries(entries); |
| 467 | frameTime = 5ms; |
| 468 | expectedEntries = { |
| 469 | // id x y |
| 470 | {0ms, {{0, 100, 100}}, AMOTION_EVENT_ACTION_DOWN}, |
| 471 | }; |
| 472 | consumeInputEventEntries(expectedEntries, frameTime); |
| 473 | |
| 474 | entries = { |
| 475 | // id x y |
| 476 | {10ms, {{0, 100, 100}}, AMOTION_EVENT_ACTION_MOVE}, |
| 477 | }; |
| 478 | publishInputEventEntries(entries); |
| 479 | frameTime = 10ms + 5ms /*RESAMPLE_LATENCY*/; |
| 480 | expectedEntries = { |
| 481 | // id x y |
| 482 | {10ms, {{0, 100, 100}}, AMOTION_EVENT_ACTION_MOVE}, |
| 483 | // no resampled value because frameTime - RESAMPLE_LATENCY == eventTime |
| 484 | }; |
| 485 | consumeInputEventEntries(expectedEntries, frameTime); |
| 486 | |
| 487 | // Second pointer id=1 appears |
| 488 | entries = { |
| 489 | // id x y |
| 490 | {15ms, {{0, 100, 100}, {1, 500, 500}}, actionPointer1Down}, |
| 491 | }; |
| 492 | publishInputEventEntries(entries); |
| 493 | frameTime = 20ms + 5ms /*RESAMPLE_LATENCY*/; |
| 494 | expectedEntries = { |
| 495 | // id x y |
| 496 | {15ms, {{0, 100, 100}, {1, 500, 500}}, actionPointer1Down}, |
| 497 | // no resampled value because frameTime - RESAMPLE_LATENCY == eventTime |
| 498 | }; |
| 499 | consumeInputEventEntries(expectedEntries, frameTime); |
| 500 | |
| 501 | // Both pointers move |
| 502 | entries = { |
| 503 | // id x y |
| 504 | {30ms, {{0, 100, 100}, {1, 500, 500}}, AMOTION_EVENT_ACTION_MOVE}, |
| 505 | {40ms, {{0, 120, 120}, {1, 600, 600}}, AMOTION_EVENT_ACTION_MOVE}, |
| 506 | }; |
| 507 | publishInputEventEntries(entries); |
| 508 | frameTime = 45ms + 5ms /*RESAMPLE_LATENCY*/; |
| 509 | expectedEntries = { |
| 510 | // id x y |
| 511 | {30ms, {{0, 100, 100}, {1, 500, 500}}, AMOTION_EVENT_ACTION_MOVE}, |
| 512 | {40ms, {{0, 120, 120}, {1, 600, 600}}, AMOTION_EVENT_ACTION_MOVE}, |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 513 | {45ms, |
| 514 | {{0, 130, 130, .isResampled = true}, {1, 650, 650, .isResampled = true}}, |
| 515 | AMOTION_EVENT_ACTION_MOVE}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 516 | }; |
| 517 | consumeInputEventEntries(expectedEntries, frameTime); |
| 518 | |
| 519 | // Both pointers move again |
| 520 | entries = { |
| 521 | // id x y |
| 522 | {60ms, {{0, 120, 120}, {1, 600, 600}}, AMOTION_EVENT_ACTION_MOVE}, |
| 523 | {70ms, {{0, 130, 130}, {1, 700, 700}}, AMOTION_EVENT_ACTION_MOVE}, |
| 524 | }; |
| 525 | publishInputEventEntries(entries); |
| 526 | frameTime = 75ms + 5ms /*RESAMPLE_LATENCY*/; |
| 527 | /** |
| 528 | * The sample at t = 60, pointer id 0 is not equal to 120, because this value of 120 was |
| 529 | * received twice, and resampled to 130. So if we already reported it as "130", we continue |
| 530 | * to report it as such. Similar with pointer id 1. |
| 531 | */ |
| 532 | expectedEntries = { |
| 533 | {60ms, |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 534 | {{0, 130, 130, .isResampled = true}, // not 120! because it matches previous real event |
| 535 | {1, 650, 650, .isResampled = true}}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 536 | AMOTION_EVENT_ACTION_MOVE}, |
| 537 | {70ms, {{0, 130, 130}, {1, 700, 700}}, AMOTION_EVENT_ACTION_MOVE}, |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 538 | {75ms, |
| 539 | {{0, 135, 135, .isResampled = true}, {1, 750, 750, .isResampled = true}}, |
| 540 | AMOTION_EVENT_ACTION_MOVE}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 541 | }; |
| 542 | consumeInputEventEntries(expectedEntries, frameTime); |
| 543 | |
| 544 | // First pointer id=0 leaves the screen |
| 545 | entries = { |
| 546 | // id x y |
| 547 | {80ms, {{1, 600, 600}}, actionPointer0Up}, |
| 548 | }; |
| 549 | publishInputEventEntries(entries); |
| 550 | frameTime = 90ms; |
| 551 | expectedEntries = { |
| 552 | // id x y |
| 553 | {80ms, {{1, 600, 600}}, actionPointer0Up}, |
| 554 | // no resampled event for ACTION_POINTER_UP |
| 555 | }; |
| 556 | consumeInputEventEntries(expectedEntries, frameTime); |
| 557 | |
| 558 | // Remaining pointer id=1 is still present, but doesn't move |
| 559 | entries = { |
| 560 | // id x y |
| 561 | {90ms, {{1, 600, 600}}, AMOTION_EVENT_ACTION_MOVE}, |
| 562 | }; |
| 563 | publishInputEventEntries(entries); |
| 564 | frameTime = 100ms; |
| 565 | expectedEntries = { |
| 566 | // id x y |
| 567 | {90ms, {{1, 600, 600}}, AMOTION_EVENT_ACTION_MOVE}, |
| 568 | /** |
| 569 | * The latest event with ACTION_MOVE was at t = 70, coord = 700. |
| 570 | * Use that value for resampling here: (600 - 700) / (90 - 70) * 5 + 600 |
| 571 | */ |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame^] | 572 | {95ms, {{1, 575, 575, .isResampled = true}}, AMOTION_EVENT_ACTION_MOVE}, |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 573 | }; |
| 574 | consumeInputEventEntries(expectedEntries, frameTime); |
| 575 | } |
| 576 | |
| 577 | } // namespace android |