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