blob: e48012bda6539013be5a494717d0bbdd70f2204a [file] [log] [blame]
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001/*
2 * Copyright (C) 2017 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#define LOG_TAG "VelocityTracker_test"
18
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000019#include <math.h>
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070020#include <array>
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070021#include <chrono>
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000022#include <limits>
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010023
24#include <android-base/stringprintf.h>
chaviw09c8d2d2020-08-24 15:48:26 -070025#include <attestation/HmacKeyManager.h>
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010026#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050027#include <gui/constants.h>
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010028#include <input/VelocityTracker.h>
29
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +000030using std::literals::chrono_literals::operator""ms;
31using std::literals::chrono_literals::operator""ns;
32using std::literals::chrono_literals::operator""us;
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010033using android::base::StringPrintf;
34
35namespace android {
36
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080037constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; // default display id
38
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010039constexpr int32_t DEFAULT_POINTER_ID = 0; // pointer ID used for manually defined tests
40
41// velocity must be in the range (1-tol)*EV <= velocity <= (1+tol)*EV
42// here EV = expected value, tol = VELOCITY_TOLERANCE
43constexpr float VELOCITY_TOLERANCE = 0.2;
44
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070045// estimate coefficients must be within 0.001% of the target value
46constexpr float COEFFICIENT_TOLERANCE = 0.00001;
47
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010048// --- VelocityTrackerTest ---
49class VelocityTrackerTest : public testing::Test { };
50
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070051/*
52 * Similar to EXPECT_NEAR, but ensures that the difference between the two float values
53 * is at most a certain fraction of the target value.
54 * If fraction is zero, require exact match.
55 */
56static void EXPECT_NEAR_BY_FRACTION(float actual, float target, float fraction) {
57 float tolerance = fabsf(target * fraction);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010058
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070059 if (target == 0 && fraction != 0) {
60 // If target is zero, this would force actual == target, which is too harsh.
61 // Relax this requirement a little. The value is determined empirically from the
62 // coefficients computed by the quadratic least squares algorithms.
63 tolerance = 1E-6;
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010064 }
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070065 EXPECT_NEAR(actual, target, tolerance);
66}
67
68static void checkVelocity(float Vactual, float Vtarget) {
69 EXPECT_NEAR_BY_FRACTION(Vactual, Vtarget, VELOCITY_TOLERANCE);
70}
71
72static void checkCoefficient(float actual, float target) {
73 EXPECT_NEAR_BY_FRACTION(actual, target, COEFFICIENT_TOLERANCE);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010074}
75
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010076struct Position {
Siarhei Vishniakou01ca4862019-03-06 13:22:13 -080077 float x;
78 float y;
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070079
80 /**
81 * If both values are NAN, then this is considered to be an empty entry (no pointer data).
82 * If only one of the values is NAN, this is still a valid entry,
83 * because we may only care about a single axis.
84 */
85 bool isValid() const {
86 return !(isnan(x) && isnan(y));
87 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010088};
89
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -070090struct PlanarMotionEventEntry {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070091 std::chrono::nanoseconds eventTime;
92 std::vector<Position> positions;
93};
94
95static BitSet32 getValidPointers(const std::vector<Position>& positions) {
96 BitSet32 pointers;
97 for (size_t i = 0; i < positions.size(); i++) {
98 if (positions[i].isValid()) {
99 pointers.markBit(i);
100 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100101 }
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700102 return pointers;
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100103}
104
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700105static uint32_t getChangingPointerId(BitSet32 pointers, BitSet32 otherPointers) {
106 BitSet32 difference(pointers.value ^ otherPointers.value);
107 uint32_t pointerId = difference.clearFirstMarkedBit();
108 EXPECT_EQ(0U, difference.value) << "Only 1 pointer can enter or leave at a time";
109 return pointerId;
110}
111
112static int32_t resolveAction(const std::vector<Position>& lastPositions,
113 const std::vector<Position>& currentPositions,
114 const std::vector<Position>& nextPositions) {
115 BitSet32 pointers = getValidPointers(currentPositions);
116 const uint32_t pointerCount = pointers.count();
117
118 BitSet32 lastPointers = getValidPointers(lastPositions);
119 const uint32_t lastPointerCount = lastPointers.count();
120 if (lastPointerCount < pointerCount) {
121 // A new pointer is down
122 uint32_t pointerId = getChangingPointerId(pointers, lastPointers);
123 return AMOTION_EVENT_ACTION_POINTER_DOWN |
124 (pointerId << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
125 }
126
127 BitSet32 nextPointers = getValidPointers(nextPositions);
128 const uint32_t nextPointerCount = nextPointers.count();
129 if (pointerCount > nextPointerCount) {
130 // An existing pointer is leaving
131 uint32_t pointerId = getChangingPointerId(pointers, nextPointers);
132 return AMOTION_EVENT_ACTION_POINTER_UP |
133 (pointerId << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
134 }
135
136 return AMOTION_EVENT_ACTION_MOVE;
137}
138
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700139static std::vector<MotionEvent> createAxisScrollMotionEventStream(
140 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions) {
141 std::vector<MotionEvent> events;
142 for (const auto& [timeStamp, value] : motions) {
143 EXPECT_TRUE(!isnan(value)) << "The entry at pointerId must be valid";
144
145 PointerCoords coords[1];
146 coords[0].setAxisValue(AMOTION_EVENT_AXIS_SCROLL, value);
147
148 PointerProperties properties[1];
149 properties[0].id = DEFAULT_POINTER_ID;
150
151 MotionEvent event;
152 ui::Transform identityTransform;
153 event.initialize(InputEvent::nextId(), 5 /*deviceId*/, AINPUT_SOURCE_ROTARY_ENCODER,
154 ADISPLAY_ID_NONE, INVALID_HMAC, AMOTION_EVENT_ACTION_SCROLL,
155 0 /*actionButton*/, 0 /*flags*/, AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE,
156 0 /*buttonState*/, MotionClassification::NONE, identityTransform,
157 0 /*xPrecision*/, 0 /*yPrecision*/, AMOTION_EVENT_INVALID_CURSOR_POSITION,
158 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, 0 /*downTime*/,
159 timeStamp.count(), 1 /*pointerCount*/, properties, coords);
160
161 events.emplace_back(event);
162 }
163
164 return events;
165}
166
167static std::vector<MotionEvent> createTouchMotionEventStream(
168 const std::vector<PlanarMotionEventEntry>& motions) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700169 if (motions.empty()) {
170 ADD_FAILURE() << "Need at least 1 sample to create a MotionEvent. Received empty vector.";
171 }
172
173 std::vector<MotionEvent> events;
174 for (size_t i = 0; i < motions.size(); i++) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700175 const PlanarMotionEventEntry& entry = motions[i];
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700176 BitSet32 pointers = getValidPointers(entry.positions);
177 const uint32_t pointerCount = pointers.count();
178
179 int32_t action;
180 if (i == 0) {
181 action = AMOTION_EVENT_ACTION_DOWN;
182 EXPECT_EQ(1U, pointerCount) << "First event should only have 1 pointer";
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000183 } else if ((i == motions.size() - 1) && pointerCount == 1) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700184 action = AMOTION_EVENT_ACTION_UP;
185 } else {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700186 const PlanarMotionEventEntry& previousEntry = motions[i-1];
187 const PlanarMotionEventEntry& nextEntry = motions[i+1];
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700188 action = resolveAction(previousEntry.positions, entry.positions, nextEntry.positions);
189 }
190
191 PointerCoords coords[pointerCount];
192 PointerProperties properties[pointerCount];
193 uint32_t pointerIndex = 0;
194 while(!pointers.isEmpty()) {
195 uint32_t pointerId = pointers.clearFirstMarkedBit();
196
197 coords[pointerIndex].clear();
198 // We are treating column positions as pointerId
199 EXPECT_TRUE(entry.positions[pointerId].isValid()) <<
200 "The entry at pointerId must be valid";
201 coords[pointerIndex].setAxisValue(AMOTION_EVENT_AXIS_X, entry.positions[pointerId].x);
202 coords[pointerIndex].setAxisValue(AMOTION_EVENT_AXIS_Y, entry.positions[pointerId].y);
203
204 properties[pointerIndex].id = pointerId;
205 properties[pointerIndex].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
206 pointerIndex++;
207 }
208 EXPECT_EQ(pointerIndex, pointerCount);
209
210 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -0700211 ui::Transform identityTransform;
Garfield Tan4cc839f2020-01-24 11:26:14 -0800212 event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_TOUCHSCREEN,
213 DISPLAY_ID, INVALID_HMAC, action, 0 /*actionButton*/, 0 /*flags*/,
214 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0 /*buttonState*/,
chaviw9eaa22c2020-07-01 16:21:27 -0700215 MotionClassification::NONE, identityTransform, 0 /*xPrecision*/,
216 0 /*yPrecision*/, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700217 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, 0 /*downTime*/,
Evan Rosky09576692021-07-01 12:22:09 -0700218 entry.eventTime.count(), pointerCount, properties, coords);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700219
220 events.emplace_back(event);
221 }
222
223 return events;
224}
225
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700226static std::optional<float> computePlanarVelocity(
227 const VelocityTracker::Strategy strategy,
228 const std::vector<PlanarMotionEventEntry>& motions, int32_t axis,
229 uint32_t pointerId = DEFAULT_POINTER_ID) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700230 VelocityTracker vt(strategy);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100231
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700232 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700233 for (MotionEvent event : events) {
234 vt.addMovement(&event);
235 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100236
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700237 return vt.getVelocity(axis, pointerId);
238}
239
240static std::vector<MotionEvent> createMotionEventStream(
241 int32_t axis, const std::vector<std::pair<std::chrono::nanoseconds, float>>& motion) {
242 switch (axis) {
243 case AMOTION_EVENT_AXIS_SCROLL:
244 return createAxisScrollMotionEventStream(motion);
245 default:
246 ADD_FAILURE() << "Axis " << axis << " is not supported";
247 return {};
248 }
249}
250
251static std::optional<float> computeVelocity(
252 const VelocityTracker::Strategy strategy,
253 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions, int32_t axis) {
254 VelocityTracker vt(strategy);
255
256 for (const MotionEvent& event : createMotionEventStream(axis, motions)) {
257 vt.addMovement(&event);
258 }
259
260 return vt.getVelocity(axis, DEFAULT_POINTER_ID);
261}
262
263static void computeAndCheckVelocity(const VelocityTracker::Strategy strategy,
264 const std::vector<PlanarMotionEventEntry>& motions,
265 int32_t axis, float targetVelocity,
266 uint32_t pointerId = DEFAULT_POINTER_ID) {
267 checkVelocity(computePlanarVelocity(strategy, motions, axis, pointerId).value_or(0),
268 targetVelocity);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100269}
270
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700271static void computeAndCheckAxisScrollVelocity(
272 const VelocityTracker::Strategy strategy,
273 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions,
274 std::optional<float> targetVelocity) {
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700275 std::optional<float> velocity = computeVelocity(strategy, motions, AMOTION_EVENT_AXIS_SCROLL);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700276 if (velocity && !targetVelocity) {
277 FAIL() << "Expected no velocity, but found " << *velocity;
278 }
279 if (!velocity && targetVelocity) {
280 FAIL() << "Expected velocity, but found no velocity";
281 }
282 if (velocity) {
283 checkVelocity(*velocity, *targetVelocity);
284 }
285}
286
287static void computeAndCheckQuadraticEstimate(const std::vector<PlanarMotionEventEntry>& motions,
288 const std::array<float, 3>& coefficients) {
Chris Yef8591482020-04-17 11:49:17 -0700289 VelocityTracker vt(VelocityTracker::Strategy::LSQ2);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700290 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700291 for (MotionEvent event : events) {
292 vt.addMovement(&event);
293 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000294 VelocityTracker::Estimator estimatorX;
295 VelocityTracker::Estimator estimatorY;
296 EXPECT_TRUE(vt.getEstimator(AMOTION_EVENT_AXIS_X, 0, &estimatorX));
297 EXPECT_TRUE(vt.getEstimator(AMOTION_EVENT_AXIS_Y, 0, &estimatorY));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700298 for (size_t i = 0; i< coefficients.size(); i++) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000299 checkCoefficient(estimatorX.coeff[i], coefficients[i]);
300 checkCoefficient(estimatorY.coeff[i], coefficients[i]);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700301 }
302}
303
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100304/*
305 * ================== VelocityTracker tests generated manually =====================================
306 */
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700307TEST_F(VelocityTrackerTest, TestDefaultStrategiesForPlanarAxes) {
308 std::vector<PlanarMotionEventEntry> motions = {{10ms, {{2, 4}}},
309 {20ms, {{4, 12}}},
310 {30ms, {{6, 20}}},
311 {40ms, {{10, 30}}}};
312
313 EXPECT_EQ(computePlanarVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X),
314 computePlanarVelocity(VelocityTracker::Strategy::DEFAULT, motions,
315 AMOTION_EVENT_AXIS_X));
316 EXPECT_EQ(computePlanarVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y),
317 computePlanarVelocity(VelocityTracker::Strategy::DEFAULT, motions,
318 AMOTION_EVENT_AXIS_Y));
319}
320
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000321TEST_F(VelocityTrackerTest, TestComputedVelocity) {
322 VelocityTracker::ComputedVelocity computedVelocity;
323
324 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, 0 /*id*/, 200 /*velocity*/);
325 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, 26U /*id*/, 400 /*velocity*/);
326 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, 27U /*id*/, 650 /*velocity*/);
327 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID, 750 /*velocity*/);
328 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, 0 /*id*/, 1000 /*velocity*/);
329 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, 26U /*id*/, 2000 /*velocity*/);
330 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, 27U /*id*/, 3000 /*velocity*/);
331 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, MAX_POINTER_ID, 4000 /*velocity*/);
332
333 // Check the axes/indices with velocity.
334 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, 0U /*id*/)), 200);
335 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, 26U /*id*/)), 400);
336 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, 27U /*id*/)), 650);
337 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID)), 750);
338 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, 0U /*id*/)), 1000);
339 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, 26U /*id*/)), 2000);
340 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, 27U /*id*/)), 3000);
341 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, MAX_POINTER_ID)), 4000);
342 for (uint32_t id = 0; id <= MAX_POINTER_ID; id++) {
343 // Since no data was added for AXIS_SCROLL, expect empty value for the axis for any id.
344 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_SCROLL, id))
345 << "Empty scroll data expected at id=" << id;
346 if (id == 0 || id == 26U || id == 27U || id == MAX_POINTER_ID) {
347 // Already checked above; continue.
348 continue;
349 }
350 // No data was added to X/Y for this id, expect empty value.
351 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, id))
352 << "Empty X data expected at id=" << id;
353 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, id))
354 << "Empty Y data expected at id=" << id;
355 }
356 // Out-of-bounds ids should given empty values.
357 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, -1));
358 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID + 1));
359}
360
361TEST_F(VelocityTrackerTest, TestGetComputedVelocity) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700362 std::vector<PlanarMotionEventEntry> motions = {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000363 {235089067457000ns, {{528.00, 0}}}, {235089084684000ns, {{527.00, 0}}},
364 {235089093349000ns, {{527.00, 0}}}, {235089095677625ns, {{527.00, 0}}},
365 {235089101859000ns, {{527.00, 0}}}, {235089110378000ns, {{528.00, 0}}},
366 {235089112497111ns, {{528.25, 0}}}, {235089118760000ns, {{531.00, 0}}},
367 {235089126686000ns, {{535.00, 0}}}, {235089129316820ns, {{536.33, 0}}},
368 {235089135199000ns, {{540.00, 0}}}, {235089144297000ns, {{546.00, 0}}},
369 {235089146136443ns, {{547.21, 0}}}, {235089152923000ns, {{553.00, 0}}},
370 {235089160784000ns, {{559.00, 0}}}, {235089162955851ns, {{560.66, 0}}},
371 {235089162955851ns, {{560.66, 0}}}, // ACTION_UP
372 };
373 VelocityTracker vt(VelocityTracker::Strategy::IMPULSE);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700374 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000375 for (const MotionEvent& event : events) {
376 vt.addMovement(&event);
377 }
378
379 float maxFloat = std::numeric_limits<float>::max();
380 VelocityTracker::ComputedVelocity computedVelocity;
381 computedVelocity = vt.getComputedVelocity(1000 /* units */, maxFloat);
382 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)),
383 764.345703);
384
385 // Expect X velocity to be scaled with respective to provided units.
386 computedVelocity = vt.getComputedVelocity(1000000 /* units */, maxFloat);
387 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)),
388 764345.703);
389
390 // Expect X velocity to be clamped by provided max velocity.
391 computedVelocity = vt.getComputedVelocity(1000000 /* units */, 1000);
392 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)), 1000);
393
394 // All 0 data for Y; expect 0 velocity.
395 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, DEFAULT_POINTER_ID)), 0);
396
397 // No data for scroll-axis; expect empty velocity.
398 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_SCROLL, DEFAULT_POINTER_ID));
399}
400
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700401TEST_F(VelocityTrackerTest, TestApiInteractionsWithNoMotionEvents) {
402 VelocityTracker vt(VelocityTracker::Strategy::DEFAULT);
403
404 EXPECT_FALSE(vt.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID));
405
406 VelocityTracker::Estimator estimator;
407 EXPECT_FALSE(vt.getEstimator(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID, &estimator));
408
409 VelocityTracker::ComputedVelocity computedVelocity = vt.getComputedVelocity(1000, 1000);
410 for (uint32_t id = 0; id <= MAX_POINTER_ID; id++) {
411 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, id));
412 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, id));
413 }
414
415 EXPECT_EQ(-1, vt.getActivePointerId());
416
417 // Make sure that the clearing functions execute without an issue.
418 vt.clearPointers(BitSet32(7U));
419 vt.clear();
420}
421
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700422TEST_F(VelocityTrackerTest, ThreePointsPositiveVelocityTest) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100423 // Same coordinate is reported 2 times in a row
424 // It is difficult to determine the correct answer here, but at least the direction
425 // of the reported velocity should be positive.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700426 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700427 {0ms, {{273, 0}}},
428 {12585us, {{293, 0}}},
429 {14730us, {{293, 0}}},
430 {14730us, {{293, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100431 };
Chris Yef8591482020-04-17 11:49:17 -0700432 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
433 1600);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100434}
435
436TEST_F(VelocityTrackerTest, ThreePointsZeroVelocityTest) {
437 // Same coordinate is reported 3 times in a row
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700438 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700439 {0ms, {{293, 0}}},
440 {6132us, {{293, 0}}},
441 {11283us, {{293, 0}}},
442 {11283us, {{293, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100443 };
Chris Yef8591482020-04-17 11:49:17 -0700444 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 0);
445 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 0);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100446}
447
448TEST_F(VelocityTrackerTest, ThreePointsLinearVelocityTest) {
449 // Fixed velocity at 5 points per 10 milliseconds
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700450 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700451 {0ms, {{0, 0}}}, {10ms, {{5, 0}}}, {20ms, {{10, 0}}}, {20ms, {{10, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100452 };
Chris Yef8591482020-04-17 11:49:17 -0700453 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 500);
454 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 500);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100455}
456
457
458/**
459 * ================== VelocityTracker tests generated by recording real events =====================
460 *
461 * To add a test, record the input coordinates and event times to all calls
462 * to void VelocityTracker::addMovement(const MotionEvent* event).
463 * Also record all calls to VelocityTracker::clear().
464 * Finally, record the output of VelocityTracker::getVelocity(...)
465 * This will give you the necessary data to create a new test.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700466 *
467 * Another good way to generate this data is to use 'dumpsys input' just after the event has
468 * occurred.
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100469 */
470
471// --------------- Recorded by hand on swordfish ---------------------------------------------------
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700472TEST_F(VelocityTrackerTest, SwordfishFlingDown) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100473 // Recording of a fling on Swordfish that could cause a fling in the wrong direction
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700474 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700475 { 0ms, {{271, 96}} },
476 { 16071042ns, {{269.786346, 106.922775}} },
477 { 35648403ns, {{267.983063, 156.660034}} },
478 { 52313925ns, {{262.638397, 220.339081}} },
479 { 68976522ns, {{266.138824, 331.581116}} },
480 { 85639375ns, {{274.79245, 428.113159}} },
481 { 96948871ns, {{274.79245, 428.113159}} },
482 { 96948871ns, {{274.79245, 428.113159}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100483 };
Chris Yef8591482020-04-17 11:49:17 -0700484 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
485 623.577637);
486 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
487 5970.7309);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100488}
489
490// --------------- Recorded by hand on sailfish, generated by a script -----------------------------
491// For some of these tests, the X-direction velocity checking has been removed, because the lsq2
492// and the impulse VelocityTrackerStrategies did not agree within 20%.
493// Since the flings were recorded in the Y-direction, the intentional user action should only
494// be relevant for the Y axis.
495// There have been also cases where lsq2 and impulse disagreed more than 20% in the Y-direction.
496// Those recordings have been discarded because we didn't feel one strategy's interpretation was
497// more correct than another's but didn't want to increase the tolerance for the entire test suite.
498//
499// There are 18 tests total below: 9 in the positive Y direction and 9 in the opposite.
500// The recordings were loosely binned into 3 categories - slow, faster, and fast, which roughly
501// characterizes the velocity of the finger motion.
502// These can be treated approximately as:
503// slow - less than 1 page gets scrolled
504// faster - more than 1 page gets scrolled, but less than 3
505// fast - entire list is scrolled (fling is done as hard as possible)
506
507TEST_F(VelocityTrackerTest, SailfishFlingUpSlow1) {
508 // Sailfish - fling up - slow - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700509 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700510 { 235089067457000ns, {{528.00, 983.00}} },
511 { 235089084684000ns, {{527.00, 981.00}} },
512 { 235089093349000ns, {{527.00, 977.00}} },
513 { 235089095677625ns, {{527.00, 975.93}} },
514 { 235089101859000ns, {{527.00, 970.00}} },
515 { 235089110378000ns, {{528.00, 960.00}} },
516 { 235089112497111ns, {{528.25, 957.51}} },
517 { 235089118760000ns, {{531.00, 946.00}} },
518 { 235089126686000ns, {{535.00, 931.00}} },
519 { 235089129316820ns, {{536.33, 926.02}} },
520 { 235089135199000ns, {{540.00, 914.00}} },
521 { 235089144297000ns, {{546.00, 896.00}} },
522 { 235089146136443ns, {{547.21, 892.36}} },
523 { 235089152923000ns, {{553.00, 877.00}} },
524 { 235089160784000ns, {{559.00, 851.00}} },
525 { 235089162955851ns, {{560.66, 843.82}} },
526 { 235089162955851ns, {{560.66, 843.82}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100527 };
Chris Yef8591482020-04-17 11:49:17 -0700528 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700529 764.345703);
Chris Yef8591482020-04-17 11:49:17 -0700530 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
531 951.698181);
532 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
533 -3604.819336);
534 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
535 -3044.966064);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100536}
537
538
539TEST_F(VelocityTrackerTest, SailfishFlingUpSlow2) {
540 // Sailfish - fling up - slow - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700541 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700542 { 235110560704000ns, {{522.00, 1107.00}} },
543 { 235110575764000ns, {{522.00, 1107.00}} },
544 { 235110584385000ns, {{522.00, 1107.00}} },
545 { 235110588421179ns, {{521.52, 1106.52}} },
546 { 235110592830000ns, {{521.00, 1106.00}} },
547 { 235110601385000ns, {{520.00, 1104.00}} },
548 { 235110605088160ns, {{519.14, 1102.27}} },
549 { 235110609952000ns, {{518.00, 1100.00}} },
550 { 235110618353000ns, {{517.00, 1093.00}} },
551 { 235110621755146ns, {{516.60, 1090.17}} },
552 { 235110627010000ns, {{517.00, 1081.00}} },
553 { 235110634785000ns, {{518.00, 1063.00}} },
554 { 235110638422450ns, {{518.87, 1052.58}} },
555 { 235110643161000ns, {{520.00, 1039.00}} },
556 { 235110651767000ns, {{524.00, 1011.00}} },
557 { 235110655089581ns, {{525.54, 1000.19}} },
558 { 235110660368000ns, {{530.00, 980.00}} },
559 { 235110660368000ns, {{530.00, 980.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100560 };
Chris Yef8591482020-04-17 11:49:17 -0700561 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
562 -4096.583008);
563 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
564 -3455.094238);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100565}
566
567
568TEST_F(VelocityTrackerTest, SailfishFlingUpSlow3) {
569 // Sailfish - fling up - slow - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700570 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700571 { 792536237000ns, {{580.00, 1317.00}} },
572 { 792541538987ns, {{580.63, 1311.94}} },
573 { 792544613000ns, {{581.00, 1309.00}} },
574 { 792552301000ns, {{583.00, 1295.00}} },
575 { 792558362309ns, {{585.13, 1282.92}} },
576 { 792560828000ns, {{586.00, 1278.00}} },
577 { 792569446000ns, {{589.00, 1256.00}} },
578 { 792575185095ns, {{591.54, 1241.41}} },
579 { 792578491000ns, {{593.00, 1233.00}} },
580 { 792587044000ns, {{597.00, 1211.00}} },
581 { 792592008172ns, {{600.28, 1195.92}} },
582 { 792594616000ns, {{602.00, 1188.00}} },
583 { 792603129000ns, {{607.00, 1167.00}} },
584 { 792608831290ns, {{609.48, 1155.83}} },
585 { 792612321000ns, {{611.00, 1149.00}} },
586 { 792620768000ns, {{615.00, 1131.00}} },
587 { 792625653873ns, {{617.32, 1121.73}} },
588 { 792629200000ns, {{619.00, 1115.00}} },
589 { 792629200000ns, {{619.00, 1115.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100590 };
Chris Yef8591482020-04-17 11:49:17 -0700591 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
592 574.33429);
593 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
594 617.40564);
595 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
596 -2361.982666);
597 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
598 -2500.055664);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100599}
600
601
602TEST_F(VelocityTrackerTest, SailfishFlingUpFaster1) {
603 // Sailfish - fling up - faster - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700604 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700605 { 235160420675000ns, {{610.00, 1042.00}} },
606 { 235160428220000ns, {{609.00, 1026.00}} },
607 { 235160436544000ns, {{609.00, 1024.00}} },
608 { 235160441852394ns, {{609.64, 1020.82}} },
609 { 235160444878000ns, {{610.00, 1019.00}} },
610 { 235160452673000ns, {{613.00, 1006.00}} },
611 { 235160458519743ns, {{617.18, 992.06}} },
612 { 235160461061000ns, {{619.00, 986.00}} },
613 { 235160469798000ns, {{627.00, 960.00}} },
614 { 235160475186713ns, {{632.22, 943.02}} },
615 { 235160478051000ns, {{635.00, 934.00}} },
616 { 235160486489000ns, {{644.00, 906.00}} },
617 { 235160491853697ns, {{649.56, 890.56}} },
618 { 235160495177000ns, {{653.00, 881.00}} },
619 { 235160504148000ns, {{662.00, 858.00}} },
620 { 235160509231495ns, {{666.81, 845.37}} },
621 { 235160512603000ns, {{670.00, 837.00}} },
622 { 235160520366000ns, {{679.00, 814.00}} },
623 { 235160520366000ns, {{679.00, 814.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100624 };
Chris Yef8591482020-04-17 11:49:17 -0700625 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
626 1274.141724);
627 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
628 1438.53186);
629 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
630 -3001.4348);
631 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
632 -3695.859619);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100633}
634
635
636TEST_F(VelocityTrackerTest, SailfishFlingUpFaster2) {
637 // Sailfish - fling up - faster - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700638 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700639 { 847153808000ns, {{576.00, 1264.00}} },
640 { 847171174000ns, {{576.00, 1262.00}} },
641 { 847179640000ns, {{576.00, 1257.00}} },
642 { 847185187540ns, {{577.41, 1249.22}} },
643 { 847187487000ns, {{578.00, 1246.00}} },
644 { 847195710000ns, {{581.00, 1227.00}} },
645 { 847202027059ns, {{583.93, 1209.40}} },
646 { 847204324000ns, {{585.00, 1203.00}} },
647 { 847212672000ns, {{590.00, 1176.00}} },
648 { 847218861395ns, {{594.36, 1157.11}} },
649 { 847221190000ns, {{596.00, 1150.00}} },
650 { 847230484000ns, {{602.00, 1124.00}} },
651 { 847235701400ns, {{607.56, 1103.83}} },
652 { 847237986000ns, {{610.00, 1095.00}} },
653 { 847237986000ns, {{610.00, 1095.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100654 };
Chris Yef8591482020-04-17 11:49:17 -0700655 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
656 -4280.07959);
657 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
658 -4241.004395);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100659}
660
661
662TEST_F(VelocityTrackerTest, SailfishFlingUpFaster3) {
663 // Sailfish - fling up - faster - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700664 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700665 { 235200532789000ns, {{507.00, 1084.00}} },
666 { 235200549221000ns, {{507.00, 1083.00}} },
667 { 235200557841000ns, {{507.00, 1081.00}} },
668 { 235200558051189ns, {{507.00, 1080.95}} },
669 { 235200566314000ns, {{507.00, 1078.00}} },
670 { 235200574876586ns, {{508.97, 1070.12}} },
671 { 235200575006000ns, {{509.00, 1070.00}} },
672 { 235200582900000ns, {{514.00, 1054.00}} },
673 { 235200591276000ns, {{525.00, 1023.00}} },
674 { 235200591701829ns, {{525.56, 1021.42}} },
675 { 235200600064000ns, {{542.00, 976.00}} },
676 { 235200608519000ns, {{563.00, 911.00}} },
677 { 235200608527086ns, {{563.02, 910.94}} },
678 { 235200616933000ns, {{590.00, 844.00}} },
679 { 235200616933000ns, {{590.00, 844.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100680 };
Chris Yef8591482020-04-17 11:49:17 -0700681 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
682 -8715.686523);
683 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
684 -7639.026367);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100685}
686
687
688TEST_F(VelocityTrackerTest, SailfishFlingUpFast1) {
689 // Sailfish - fling up - fast - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700690 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700691 { 920922149000ns, {{561.00, 1412.00}} },
692 { 920930185000ns, {{559.00, 1377.00}} },
693 { 920930262463ns, {{558.98, 1376.66}} },
694 { 920938547000ns, {{559.00, 1371.00}} },
695 { 920947096857ns, {{562.91, 1342.68}} },
696 { 920947302000ns, {{563.00, 1342.00}} },
697 { 920955502000ns, {{577.00, 1272.00}} },
698 { 920963931021ns, {{596.87, 1190.54}} },
699 { 920963987000ns, {{597.00, 1190.00}} },
700 { 920972530000ns, {{631.00, 1093.00}} },
701 { 920980765511ns, {{671.31, 994.68}} },
702 { 920980906000ns, {{672.00, 993.00}} },
703 { 920989261000ns, {{715.00, 903.00}} },
704 { 920989261000ns, {{715.00, 903.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100705 };
Chris Yef8591482020-04-17 11:49:17 -0700706 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
707 5670.329102);
708 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
709 5991.866699);
710 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
711 -13021.101562);
712 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
713 -15093.995117);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100714}
715
716
717TEST_F(VelocityTrackerTest, SailfishFlingUpFast2) {
718 // Sailfish - fling up - fast - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700719 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700720 { 235247153233000ns, {{518.00, 1168.00}} },
721 { 235247170452000ns, {{517.00, 1167.00}} },
722 { 235247178908000ns, {{515.00, 1159.00}} },
723 { 235247179556213ns, {{514.85, 1158.39}} },
724 { 235247186821000ns, {{515.00, 1125.00}} },
725 { 235247195265000ns, {{521.00, 1051.00}} },
726 { 235247196389476ns, {{521.80, 1041.15}} },
727 { 235247203649000ns, {{538.00, 932.00}} },
728 { 235247212253000ns, {{571.00, 794.00}} },
729 { 235247213222491ns, {{574.72, 778.45}} },
730 { 235247220736000ns, {{620.00, 641.00}} },
731 { 235247220736000ns, {{620.00, 641.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100732 };
Chris Yef8591482020-04-17 11:49:17 -0700733 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
734 -20286.958984);
735 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
736 -20494.587891);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100737}
738
739
740TEST_F(VelocityTrackerTest, SailfishFlingUpFast3) {
741 // Sailfish - fling up - fast - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700742 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700743 { 235302568736000ns, {{529.00, 1167.00}} },
744 { 235302576644000ns, {{523.00, 1140.00}} },
745 { 235302579395063ns, {{520.91, 1130.61}} },
746 { 235302585140000ns, {{522.00, 1130.00}} },
747 { 235302593615000ns, {{527.00, 1065.00}} },
748 { 235302596207444ns, {{528.53, 1045.12}} },
749 { 235302602102000ns, {{559.00, 872.00}} },
750 { 235302610545000ns, {{652.00, 605.00}} },
751 { 235302613019881ns, {{679.26, 526.73}} },
752 { 235302613019881ns, {{679.26, 526.73}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100753 };
Chris Yef8591482020-04-17 11:49:17 -0700754 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
755 -39295.941406);
756 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
757 -36461.421875);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100758}
759
760
761TEST_F(VelocityTrackerTest, SailfishFlingDownSlow1) {
762 // Sailfish - fling down - slow - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700763 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700764 { 235655749552755ns, {{582.00, 432.49}} },
765 { 235655750638000ns, {{582.00, 433.00}} },
766 { 235655758865000ns, {{582.00, 440.00}} },
767 { 235655766221523ns, {{581.16, 448.43}} },
768 { 235655767594000ns, {{581.00, 450.00}} },
769 { 235655776044000ns, {{580.00, 462.00}} },
770 { 235655782890696ns, {{579.18, 474.35}} },
771 { 235655784360000ns, {{579.00, 477.00}} },
772 { 235655792795000ns, {{578.00, 496.00}} },
773 { 235655799559531ns, {{576.27, 515.04}} },
774 { 235655800612000ns, {{576.00, 518.00}} },
775 { 235655809535000ns, {{574.00, 542.00}} },
776 { 235655816988015ns, {{572.17, 564.86}} },
777 { 235655817685000ns, {{572.00, 567.00}} },
778 { 235655825981000ns, {{569.00, 595.00}} },
779 { 235655833808653ns, {{566.26, 620.60}} },
780 { 235655834541000ns, {{566.00, 623.00}} },
781 { 235655842893000ns, {{563.00, 649.00}} },
782 { 235655842893000ns, {{563.00, 649.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100783 };
Chris Yef8591482020-04-17 11:49:17 -0700784 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
785 -419.749695);
786 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
787 -398.303894);
788 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
789 3309.016357);
790 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
791 3969.099854);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100792}
793
794
795TEST_F(VelocityTrackerTest, SailfishFlingDownSlow2) {
796 // Sailfish - fling down - slow - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700797 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700798 { 235671152083370ns, {{485.24, 558.28}} },
799 { 235671154126000ns, {{485.00, 559.00}} },
800 { 235671162497000ns, {{484.00, 566.00}} },
801 { 235671168750511ns, {{483.27, 573.29}} },
802 { 235671171071000ns, {{483.00, 576.00}} },
803 { 235671179390000ns, {{482.00, 588.00}} },
804 { 235671185417210ns, {{481.31, 598.98}} },
805 { 235671188173000ns, {{481.00, 604.00}} },
806 { 235671196371000ns, {{480.00, 624.00}} },
807 { 235671202084196ns, {{479.27, 639.98}} },
808 { 235671204235000ns, {{479.00, 646.00}} },
809 { 235671212554000ns, {{478.00, 673.00}} },
810 { 235671219471011ns, {{476.39, 697.12}} },
811 { 235671221159000ns, {{476.00, 703.00}} },
812 { 235671229592000ns, {{474.00, 734.00}} },
813 { 235671236281462ns, {{472.43, 758.38}} },
814 { 235671238098000ns, {{472.00, 765.00}} },
815 { 235671246532000ns, {{470.00, 799.00}} },
816 { 235671246532000ns, {{470.00, 799.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100817 };
Chris Yef8591482020-04-17 11:49:17 -0700818 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
819 -262.80426);
820 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
821 -243.665344);
822 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
823 4215.682129);
824 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
825 4587.986816);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100826}
827
828
829TEST_F(VelocityTrackerTest, SailfishFlingDownSlow3) {
830 // Sailfish - fling down - slow - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700831 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700832 { 170983201000ns, {{557.00, 533.00}} },
833 { 171000668000ns, {{556.00, 534.00}} },
834 { 171007359750ns, {{554.73, 535.27}} },
835 { 171011197000ns, {{554.00, 536.00}} },
836 { 171017660000ns, {{552.00, 540.00}} },
837 { 171024201831ns, {{549.97, 544.73}} },
838 { 171027333000ns, {{549.00, 547.00}} },
839 { 171034603000ns, {{545.00, 557.00}} },
840 { 171041043371ns, {{541.98, 567.55}} },
841 { 171043147000ns, {{541.00, 571.00}} },
842 { 171051052000ns, {{536.00, 586.00}} },
843 { 171051052000ns, {{536.00, 586.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100844 };
Chris Yef8591482020-04-17 11:49:17 -0700845 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
846 -723.413513);
847 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
848 -651.038452);
849 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
850 2091.502441);
851 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
852 1934.517456);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100853}
854
855
856TEST_F(VelocityTrackerTest, SailfishFlingDownFaster1) {
857 // Sailfish - fling down - faster - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700858 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700859 { 235695280333000ns, {{558.00, 451.00}} },
860 { 235695283971237ns, {{558.43, 454.45}} },
861 { 235695289038000ns, {{559.00, 462.00}} },
862 { 235695297388000ns, {{561.00, 478.00}} },
863 { 235695300638465ns, {{561.83, 486.25}} },
864 { 235695305265000ns, {{563.00, 498.00}} },
865 { 235695313591000ns, {{564.00, 521.00}} },
866 { 235695317305492ns, {{564.43, 532.68}} },
867 { 235695322181000ns, {{565.00, 548.00}} },
868 { 235695330709000ns, {{565.00, 577.00}} },
869 { 235695333972227ns, {{565.00, 588.10}} },
870 { 235695339250000ns, {{565.00, 609.00}} },
871 { 235695347839000ns, {{565.00, 642.00}} },
872 { 235695351313257ns, {{565.00, 656.18}} },
873 { 235695356412000ns, {{565.00, 677.00}} },
874 { 235695364899000ns, {{563.00, 710.00}} },
875 { 235695368118682ns, {{562.24, 722.52}} },
876 { 235695373403000ns, {{564.00, 744.00}} },
877 { 235695373403000ns, {{564.00, 744.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100878 };
Chris Yef8591482020-04-17 11:49:17 -0700879 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
880 4254.639648);
881 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
882 4698.415039);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100883}
884
885
886TEST_F(VelocityTrackerTest, SailfishFlingDownFaster2) {
887 // Sailfish - fling down - faster - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700888 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700889 { 235709624766000ns, {{535.00, 579.00}} },
890 { 235709642256000ns, {{534.00, 580.00}} },
891 { 235709643350278ns, {{533.94, 580.06}} },
892 { 235709650760000ns, {{532.00, 584.00}} },
893 { 235709658615000ns, {{530.00, 593.00}} },
894 { 235709660170495ns, {{529.60, 594.78}} },
895 { 235709667095000ns, {{527.00, 606.00}} },
896 { 235709675616000ns, {{524.00, 628.00}} },
897 { 235709676983261ns, {{523.52, 631.53}} },
898 { 235709684289000ns, {{521.00, 652.00}} },
899 { 235709692763000ns, {{518.00, 682.00}} },
900 { 235709693804993ns, {{517.63, 685.69}} },
901 { 235709701438000ns, {{515.00, 709.00}} },
902 { 235709709830000ns, {{512.00, 739.00}} },
903 { 235709710626776ns, {{511.72, 741.85}} },
904 { 235709710626776ns, {{511.72, 741.85}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100905 };
Chris Yef8591482020-04-17 11:49:17 -0700906 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
907 -430.440247);
908 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
909 -447.600311);
910 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
911 3953.859375);
912 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
913 4316.155273);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100914}
915
916
917TEST_F(VelocityTrackerTest, SailfishFlingDownFaster3) {
918 // Sailfish - fling down - faster - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700919 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700920 { 235727628927000ns, {{540.00, 440.00}} },
921 { 235727636810000ns, {{537.00, 454.00}} },
922 { 235727646176000ns, {{536.00, 454.00}} },
923 { 235727653586628ns, {{535.12, 456.65}} },
924 { 235727654557000ns, {{535.00, 457.00}} },
925 { 235727663024000ns, {{534.00, 465.00}} },
926 { 235727670410103ns, {{533.04, 479.45}} },
927 { 235727670691000ns, {{533.00, 480.00}} },
928 { 235727679255000ns, {{531.00, 501.00}} },
929 { 235727687233704ns, {{529.09, 526.73}} },
930 { 235727687628000ns, {{529.00, 528.00}} },
931 { 235727696113000ns, {{526.00, 558.00}} },
932 { 235727704057546ns, {{523.18, 588.98}} },
933 { 235727704576000ns, {{523.00, 591.00}} },
934 { 235727713099000ns, {{520.00, 626.00}} },
935 { 235727720880776ns, {{516.33, 655.36}} },
936 { 235727721580000ns, {{516.00, 658.00}} },
937 { 235727721580000ns, {{516.00, 658.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100938 };
Chris Yef8591482020-04-17 11:49:17 -0700939 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
940 4484.617676);
941 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
942 4927.92627);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100943}
944
945
946TEST_F(VelocityTrackerTest, SailfishFlingDownFast1) {
947 // Sailfish - fling down - fast - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700948 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700949 { 235762352849000ns, {{467.00, 286.00}} },
950 { 235762360250000ns, {{443.00, 344.00}} },
951 { 235762362787412ns, {{434.77, 363.89}} },
952 { 235762368807000ns, {{438.00, 359.00}} },
953 { 235762377220000ns, {{425.00, 423.00}} },
954 { 235762379608561ns, {{421.31, 441.17}} },
955 { 235762385698000ns, {{412.00, 528.00}} },
956 { 235762394133000ns, {{406.00, 648.00}} },
957 { 235762396429369ns, {{404.37, 680.67}} },
958 { 235762396429369ns, {{404.37, 680.67}} }, //ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100959 };
Chris Yef8591482020-04-17 11:49:17 -0700960 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
961 14227.0224);
962 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
963 16064.685547);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100964}
965
966
967TEST_F(VelocityTrackerTest, SailfishFlingDownFast2) {
968 // Sailfish - fling down - fast - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700969 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700970 { 235772487188000ns, {{576.00, 204.00}} },
971 { 235772495159000ns, {{553.00, 236.00}} },
972 { 235772503568000ns, {{551.00, 240.00}} },
973 { 235772508192247ns, {{545.55, 254.17}} },
974 { 235772512051000ns, {{541.00, 266.00}} },
975 { 235772520794000ns, {{520.00, 337.00}} },
976 { 235772525015263ns, {{508.92, 394.43}} },
977 { 235772529174000ns, {{498.00, 451.00}} },
978 { 235772537635000ns, {{484.00, 589.00}} },
979 { 235772537635000ns, {{484.00, 589.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100980 };
Chris Yef8591482020-04-17 11:49:17 -0700981 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
982 18660.048828);
983 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
984 16918.439453);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100985}
986
987
988TEST_F(VelocityTrackerTest, SailfishFlingDownFast3) {
989 // Sailfish - fling down - fast - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700990 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700991 { 507650295000ns, {{628.00, 233.00}} },
992 { 507658234000ns, {{605.00, 269.00}} },
993 { 507666784000ns, {{601.00, 274.00}} },
994 { 507669660483ns, {{599.65, 275.68}} },
995 { 507675427000ns, {{582.00, 308.00}} },
996 { 507683740000ns, {{541.00, 404.00}} },
997 { 507686506238ns, {{527.36, 435.95}} },
998 { 507692220000ns, {{487.00, 581.00}} },
999 { 507700707000ns, {{454.00, 792.00}} },
1000 { 507703352649ns, {{443.71, 857.77}} },
1001 { 507703352649ns, {{443.71, 857.77}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001002 };
Chris Yef8591482020-04-17 11:49:17 -07001003 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1004 -4111.8173);
1005 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1006 -6388.48877);
1007 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
1008 29765.908203);
1009 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1010 28354.796875);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001011}
1012
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001013/**
1014 * ================== Multiple pointers ============================================================
1015 *
1016 * Three fingers quickly tap the screen. Since this is a tap, the velocities should be zero.
1017 * If the events with POINTER_UP or POINTER_DOWN are not handled correctly (these should not be
1018 * part of the fitted data), this can cause large velocity values to be reported instead.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001019 */
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -07001020TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_ThreeFingerTap) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001021 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001022 { 0us, {{1063, 1128}, {NAN, NAN}, {NAN, NAN}} },
1023 { 10800us, {{1063, 1128}, {682, 1318}, {NAN, NAN}} }, // POINTER_DOWN
1024 { 10800us, {{1063, 1128}, {682, 1318}, {397, 1747}} }, // POINTER_DOWN
1025 { 267300us, {{1063, 1128}, {682, 1318}, {397, 1747}} }, // POINTER_UP
1026 { 267300us, {{1063, 1128}, {NAN, NAN}, {397, 1747}} }, // POINTER_UP
1027 { 272700us, {{1063, 1128}, {NAN, NAN}, {NAN, NAN}} },
1028 };
1029
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -07001030 // Velocity should actually be zero, but we expect 0.016 here instead.
1031 // This is close enough to zero, and is likely caused by division by a very small number.
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001032 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 0);
1033 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y, 0);
Chris Yef8591482020-04-17 11:49:17 -07001034 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 0);
1035 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y, 0);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001036}
1037
1038/**
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001039 * ================= Pointer liftoff ===============================================================
1040 */
1041
1042/**
1043 * The last movement of a pointer is always ACTION_POINTER_UP or ACTION_UP. If there's a short delay
1044 * between the last ACTION_MOVE and the next ACTION_POINTER_UP or ACTION_UP, velocity should not be
1045 * affected by the liftoff.
1046 */
1047TEST_F(VelocityTrackerTest, ShortDelayBeforeActionUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001048 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001049 {0ms, {{10, 0}}}, {10ms, {{20, 0}}}, {20ms, {{30, 0}}}, {30ms, {{30, 0}}}, // ACTION_UP
1050 };
1051 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1052 1000);
1053 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 1000);
1054}
1055
1056/**
1057 * The last movement of a single pointer is ACTION_UP. If there's a long delay between the last
1058 * ACTION_MOVE and the final ACTION_UP, velocity should be reported as zero because the pointer
1059 * should be assumed to have stopped.
1060 */
1061TEST_F(VelocityTrackerTest, LongDelayBeforeActionUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001062 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001063 {0ms, {{10, 0}}},
1064 {10ms, {{20, 0}}},
1065 {20ms, {{30, 0}}},
1066 {3000ms, {{30, 0}}}, // ACTION_UP
1067 };
1068 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 0);
1069 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 0);
1070}
1071
1072/**
1073 * The last movement of a pointer is always ACTION_POINTER_UP or ACTION_UP. If there's a long delay
1074 * before ACTION_POINTER_UP event, the movement should be assumed to have stopped.
1075 * The final velocity should be reported as zero for all pointers.
1076 */
1077TEST_F(VelocityTrackerTest, LongDelayBeforeActionPointerUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001078 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001079 {0ms, {{10, 0}}},
1080 {10ms, {{20, 0}, {100, 0}}},
1081 {20ms, {{30, 0}, {200, 0}}},
1082 {30ms, {{30, 0}, {300, 0}}},
1083 {40ms, {{30, 0}, {400, 0}}},
1084 {3000ms, {{30, 0}}}, // ACTION_POINTER_UP
1085 };
1086 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 0,
1087 /*pointerId*/ 0);
1088 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 0,
1089 /*pointerId*/ 0);
1090 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 0,
1091 /*pointerId*/ 1);
1092 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 0,
1093 /*pointerId*/ 1);
1094}
1095
1096/**
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001097 * ================== Tests for least squares fitting ==============================================
1098 *
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001099 * Special care must be taken when constructing tests for LeastSquaresVelocityTrackerStrategy
1100 * getEstimator function. In particular:
1101 * - inside the function, time gets converted from nanoseconds to seconds
1102 * before being used in the fit.
1103 * - any values that are older than 100 ms are being discarded.
1104 * - the newest time gets subtracted from all of the other times before being used in the fit.
1105 * So these tests have to be designed with those limitations in mind.
1106 *
1107 * General approach for the tests below:
1108 * We only used timestamps in milliseconds, 0 ms, 1 ms, and 2 ms, to be sure that
1109 * we are well within the HORIZON range.
1110 * When specifying the expected values of the coefficients, we treat the x values as if
1111 * they were in ms. Then, to adjust for the time units, the coefficients get progressively
1112 * multiplied by powers of 1E3.
1113 * For example:
1114 * data: t(ms), x
1115 * 1 ms, 1
1116 * 2 ms, 4
1117 * 3 ms, 9
1118 * The coefficients are (0, 0, 1).
1119 * In the test, we would convert these coefficients to (0*(1E3)^0, 0*(1E3)^1, 1*(1E3)^2).
1120 */
1121TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Constant) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001122 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001123 { 0ms, {{1, 1}} }, // 0 s
1124 { 1ms, {{1, 1}} }, // 0.001 s
1125 { 2ms, {{1, 1}} }, // 0.002 s
1126 { 2ms, {{1, 1}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001127 };
1128 // The data used for the fit will be as follows:
1129 // time(s), position
1130 // -0.002, 1
1131 // -0.001, 1
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001132 // -0.ms, 1
1133 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({1, 0, 0}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001134}
1135
1136/*
1137 * Straight line y = x :: the constant and quadratic coefficients are zero.
1138 */
1139TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Linear) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001140 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001141 { 0ms, {{-2, -2}} },
1142 { 1ms, {{-1, -1}} },
1143 { 2ms, {{-0, -0}} },
1144 { 2ms, {{-0, -0}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001145 };
1146 // The data used for the fit will be as follows:
1147 // time(s), position
1148 // -0.002, -2
1149 // -0.001, -1
1150 // -0.000, 0
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001151 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({0, 1E3, 0}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001152}
1153
1154/*
1155 * Parabola
1156 */
1157TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001158 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001159 { 0ms, {{1, 1}} },
1160 { 1ms, {{4, 4}} },
1161 { 2ms, {{8, 8}} },
1162 { 2ms, {{8, 8}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001163 };
1164 // The data used for the fit will be as follows:
1165 // time(s), position
1166 // -0.002, 1
1167 // -0.001, 4
1168 // -0.000, 8
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001169 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({8, 4.5E3, 0.5E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001170}
1171
1172/*
1173 * Parabola
1174 */
1175TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic2) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001176 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001177 { 0ms, {{1, 1}} },
1178 { 1ms, {{4, 4}} },
1179 { 2ms, {{9, 9}} },
1180 { 2ms, {{9, 9}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001181 };
1182 // The data used for the fit will be as follows:
1183 // time(s), position
1184 // -0.002, 1
1185 // -0.001, 4
1186 // -0.000, 9
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001187 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({9, 6E3, 1E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001188}
1189
1190/*
1191 * Parabola :: y = x^2 :: the constant and linear coefficients are zero.
1192 */
1193TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic3) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001194 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001195 { 0ms, {{4, 4}} },
1196 { 1ms, {{1, 1}} },
1197 { 2ms, {{0, 0}} },
1198 { 2ms, {{0, 0}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001199 };
1200 // The data used for the fit will be as follows:
1201 // time(s), position
1202 // -0.002, 4
1203 // -0.001, 1
1204 // -0.000, 0
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001205 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({0, 0E3, 1E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001206}
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001207
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001208// Recorded by hand on sailfish, but only the diffs are taken to test cumulative axis velocity.
1209TEST_F(VelocityTrackerTest, AxisScrollVelocity) {
1210 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1211 {235089067457000ns, 0.00}, {235089084684000ns, -1.00}, {235089093349000ns, 0.00},
1212 {235089095677625ns, 0.00}, {235089101859000ns, 0.00}, {235089110378000ns, 0.00},
1213 {235089112497111ns, 0.25}, {235089118760000ns, 1.75}, {235089126686000ns, 4.00},
1214 {235089129316820ns, 1.33}, {235089135199000ns, 3.67}, {235089144297000ns, 6.00},
1215 {235089146136443ns, 1.21}, {235089152923000ns, 5.79}, {235089160784000ns, 6.00},
1216 {235089162955851ns, 1.66},
1217 };
1218
1219 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {764.345703});
1220}
1221
1222// --------------- Recorded by hand on a Wear OS device using a rotating side button ---------------
1223TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollDown) {
1224 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1225 {224598065152ns, -0.050100}, {224621871104ns, -0.133600}, {224645464064ns, -0.551100},
1226 {224669171712ns, -0.801600}, {224687063040ns, -1.035400}, {224706691072ns, -0.484300},
1227 {224738213888ns, -0.334000}, {224754401280ns, -0.083500},
1228 };
1229
1230 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {-27.86});
1231}
1232
1233TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollUp) {
1234 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1235 {269606010880ns, 0.050100}, {269626064896ns, 0.217100}, {269641973760ns, 0.267200},
1236 {269658079232ns, 0.267200}, {269674217472ns, 0.267200}, {269690683392ns, 0.367400},
1237 {269706133504ns, 0.551100}, {269722173440ns, 0.501000},
1238 };
1239
1240 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {31.92});
1241}
1242
1243TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollDown_ThenUp_ThenDown) {
1244 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1245 {2580534001664ns, -0.033400}, {2580549992448ns, -0.133600},
1246 {2580566769664ns, -0.250500}, {2580581974016ns, -0.183700},
1247 {2580597964800ns, -0.267200}, {2580613955584ns, -0.551100},
1248 {2580635189248ns, -0.601200}, {2580661927936ns, -0.450900},
1249 {2580683161600ns, -0.417500}, {2580705705984ns, -0.150300},
1250 {2580722745344ns, -0.016700}, {2580786446336ns, 0.050100},
1251 {2580801912832ns, 0.150300}, {2580822360064ns, 0.300600},
1252 {2580838088704ns, 0.300600}, {2580854341632ns, 0.400800},
1253 {2580869808128ns, 0.517700}, {2580886061056ns, 0.501000},
1254 {2580905984000ns, 0.350700}, {2580921974784ns, 0.350700},
1255 {2580937965568ns, 0.066800}, {2580974665728ns, 0.016700},
1256 {2581034434560ns, -0.066800}, {2581049901056ns, -0.116900},
1257 {2581070610432ns, -0.317300}, {2581086076928ns, -0.200400},
1258 {2581101805568ns, -0.233800}, {2581118058496ns, -0.417500},
1259 {2581134049280ns, -0.417500}, {2581150040064ns, -0.367400},
1260 {2581166030848ns, -0.267200}, {2581181759488ns, -0.150300},
1261 {2581199847424ns, -0.066800},
1262 };
1263
1264 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {-9.73});
1265}
1266
1267// ------------------------------- Hand generated test cases ---------------------------------------
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -07001268TEST_F(VelocityTrackerTest, TestDefaultStrategyForAxisScroll) {
1269 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1270 {10ms, 20},
1271 {20ms, 25},
1272 {30ms, 50},
1273 {40ms, 100},
1274 };
1275
1276 EXPECT_EQ(computeVelocity(VelocityTracker::Strategy::IMPULSE, motions,
1277 AMOTION_EVENT_AXIS_SCROLL),
1278 computeVelocity(VelocityTracker::Strategy::DEFAULT, motions,
1279 AMOTION_EVENT_AXIS_SCROLL));
1280}
1281
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001282TEST_F(VelocityTrackerTest, AxisScrollVelocity_SimilarDifferentialValues) {
1283 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ns, 2.12}, {3ns, 2.12},
1284 {7ns, 2.12}, {8ns, 2.12},
1285 {15ns, 2.12}, {18ns, 2.12}};
1286
1287 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {1690236059.86});
1288}
1289
1290TEST_F(VelocityTrackerTest, AxisScrollVelocity_OnlyTwoValues) {
1291 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ms, 5}, {2ms, 10}};
1292
1293 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {10000});
1294}
1295
1296TEST_F(VelocityTrackerTest, AxisScrollVelocity_ConstantVelocity) {
1297 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ms, 20}, {2ms, 20},
1298 {3ms, 20}, {4ms, 20},
1299 {5ms, 20}, {6ms, 20}};
1300
1301 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {20000});
1302}
1303
1304TEST_F(VelocityTrackerTest, AxisScrollVelocity_NoMotion) {
1305 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ns, 0}, {2ns, 0},
1306 {3ns, 0}, {4ns, 0},
1307 {5ns, 0}, {6ns, 0}};
1308
1309 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {0});
1310}
1311
1312TEST_F(VelocityTrackerTest, AxisScrollVelocity_NoData) {
1313 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {};
1314
1315 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, std::nullopt);
1316}
1317
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001318} // namespace android