blob: 2678f2f2fdfd249e0a1edddeea5df65b78fb3643 [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
Yeabkal Wubshita15e5992022-09-29 19:00:19 -070068static void checkVelocity(std::optional<float> Vactual, std::optional<float> Vtarget) {
69 if (Vactual != std::nullopt) {
70 if (Vtarget == std::nullopt) {
71 FAIL() << "Expected no velocity, but found " << *Vactual;
72 }
73 EXPECT_NEAR_BY_FRACTION(*Vactual, *Vtarget, VELOCITY_TOLERANCE);
74 } else if (Vtarget != std::nullopt) {
75 FAIL() << "Expected velocity, but found no velocity";
76 }
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070077}
78
79static void checkCoefficient(float actual, float target) {
80 EXPECT_NEAR_BY_FRACTION(actual, target, COEFFICIENT_TOLERANCE);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010081}
82
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010083struct Position {
Siarhei Vishniakou01ca4862019-03-06 13:22:13 -080084 float x;
85 float y;
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070086
87 /**
88 * If both values are NAN, then this is considered to be an empty entry (no pointer data).
89 * If only one of the values is NAN, this is still a valid entry,
90 * because we may only care about a single axis.
91 */
92 bool isValid() const {
93 return !(isnan(x) && isnan(y));
94 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010095};
96
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -070097struct PlanarMotionEventEntry {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070098 std::chrono::nanoseconds eventTime;
99 std::vector<Position> positions;
100};
101
102static BitSet32 getValidPointers(const std::vector<Position>& positions) {
103 BitSet32 pointers;
104 for (size_t i = 0; i < positions.size(); i++) {
105 if (positions[i].isValid()) {
106 pointers.markBit(i);
107 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100108 }
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700109 return pointers;
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100110}
111
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700112static uint32_t getChangingPointerId(BitSet32 pointers, BitSet32 otherPointers) {
113 BitSet32 difference(pointers.value ^ otherPointers.value);
114 uint32_t pointerId = difference.clearFirstMarkedBit();
115 EXPECT_EQ(0U, difference.value) << "Only 1 pointer can enter or leave at a time";
116 return pointerId;
117}
118
119static int32_t resolveAction(const std::vector<Position>& lastPositions,
120 const std::vector<Position>& currentPositions,
121 const std::vector<Position>& nextPositions) {
122 BitSet32 pointers = getValidPointers(currentPositions);
123 const uint32_t pointerCount = pointers.count();
124
125 BitSet32 lastPointers = getValidPointers(lastPositions);
126 const uint32_t lastPointerCount = lastPointers.count();
127 if (lastPointerCount < pointerCount) {
128 // A new pointer is down
129 uint32_t pointerId = getChangingPointerId(pointers, lastPointers);
130 return AMOTION_EVENT_ACTION_POINTER_DOWN |
131 (pointerId << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
132 }
133
134 BitSet32 nextPointers = getValidPointers(nextPositions);
135 const uint32_t nextPointerCount = nextPointers.count();
136 if (pointerCount > nextPointerCount) {
137 // An existing pointer is leaving
138 uint32_t pointerId = getChangingPointerId(pointers, nextPointers);
139 return AMOTION_EVENT_ACTION_POINTER_UP |
140 (pointerId << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
141 }
142
143 return AMOTION_EVENT_ACTION_MOVE;
144}
145
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700146static std::vector<MotionEvent> createAxisScrollMotionEventStream(
147 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions) {
148 std::vector<MotionEvent> events;
149 for (const auto& [timeStamp, value] : motions) {
150 EXPECT_TRUE(!isnan(value)) << "The entry at pointerId must be valid";
151
152 PointerCoords coords[1];
153 coords[0].setAxisValue(AMOTION_EVENT_AXIS_SCROLL, value);
154
155 PointerProperties properties[1];
156 properties[0].id = DEFAULT_POINTER_ID;
157
158 MotionEvent event;
159 ui::Transform identityTransform;
160 event.initialize(InputEvent::nextId(), 5 /*deviceId*/, AINPUT_SOURCE_ROTARY_ENCODER,
161 ADISPLAY_ID_NONE, INVALID_HMAC, AMOTION_EVENT_ACTION_SCROLL,
162 0 /*actionButton*/, 0 /*flags*/, AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE,
163 0 /*buttonState*/, MotionClassification::NONE, identityTransform,
164 0 /*xPrecision*/, 0 /*yPrecision*/, AMOTION_EVENT_INVALID_CURSOR_POSITION,
165 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, 0 /*downTime*/,
166 timeStamp.count(), 1 /*pointerCount*/, properties, coords);
167
168 events.emplace_back(event);
169 }
170
171 return events;
172}
173
174static std::vector<MotionEvent> createTouchMotionEventStream(
175 const std::vector<PlanarMotionEventEntry>& motions) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700176 if (motions.empty()) {
177 ADD_FAILURE() << "Need at least 1 sample to create a MotionEvent. Received empty vector.";
178 }
179
180 std::vector<MotionEvent> events;
181 for (size_t i = 0; i < motions.size(); i++) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700182 const PlanarMotionEventEntry& entry = motions[i];
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700183 BitSet32 pointers = getValidPointers(entry.positions);
184 const uint32_t pointerCount = pointers.count();
185
186 int32_t action;
187 if (i == 0) {
188 action = AMOTION_EVENT_ACTION_DOWN;
189 EXPECT_EQ(1U, pointerCount) << "First event should only have 1 pointer";
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000190 } else if ((i == motions.size() - 1) && pointerCount == 1) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700191 action = AMOTION_EVENT_ACTION_UP;
192 } else {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700193 const PlanarMotionEventEntry& previousEntry = motions[i-1];
194 const PlanarMotionEventEntry& nextEntry = motions[i+1];
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700195 action = resolveAction(previousEntry.positions, entry.positions, nextEntry.positions);
196 }
197
198 PointerCoords coords[pointerCount];
199 PointerProperties properties[pointerCount];
200 uint32_t pointerIndex = 0;
201 while(!pointers.isEmpty()) {
202 uint32_t pointerId = pointers.clearFirstMarkedBit();
203
204 coords[pointerIndex].clear();
205 // We are treating column positions as pointerId
206 EXPECT_TRUE(entry.positions[pointerId].isValid()) <<
207 "The entry at pointerId must be valid";
208 coords[pointerIndex].setAxisValue(AMOTION_EVENT_AXIS_X, entry.positions[pointerId].x);
209 coords[pointerIndex].setAxisValue(AMOTION_EVENT_AXIS_Y, entry.positions[pointerId].y);
210
211 properties[pointerIndex].id = pointerId;
212 properties[pointerIndex].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
213 pointerIndex++;
214 }
215 EXPECT_EQ(pointerIndex, pointerCount);
216
217 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -0700218 ui::Transform identityTransform;
Garfield Tan4cc839f2020-01-24 11:26:14 -0800219 event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_TOUCHSCREEN,
220 DISPLAY_ID, INVALID_HMAC, action, 0 /*actionButton*/, 0 /*flags*/,
221 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0 /*buttonState*/,
chaviw9eaa22c2020-07-01 16:21:27 -0700222 MotionClassification::NONE, identityTransform, 0 /*xPrecision*/,
223 0 /*yPrecision*/, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700224 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, 0 /*downTime*/,
Evan Rosky09576692021-07-01 12:22:09 -0700225 entry.eventTime.count(), pointerCount, properties, coords);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700226
227 events.emplace_back(event);
228 }
229
230 return events;
231}
232
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700233static std::optional<float> computePlanarVelocity(
234 const VelocityTracker::Strategy strategy,
235 const std::vector<PlanarMotionEventEntry>& motions, int32_t axis,
236 uint32_t pointerId = DEFAULT_POINTER_ID) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700237 VelocityTracker vt(strategy);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100238
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700239 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700240 for (MotionEvent event : events) {
241 vt.addMovement(&event);
242 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100243
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700244 return vt.getVelocity(axis, pointerId);
245}
246
247static std::vector<MotionEvent> createMotionEventStream(
248 int32_t axis, const std::vector<std::pair<std::chrono::nanoseconds, float>>& motion) {
249 switch (axis) {
250 case AMOTION_EVENT_AXIS_SCROLL:
251 return createAxisScrollMotionEventStream(motion);
252 default:
253 ADD_FAILURE() << "Axis " << axis << " is not supported";
254 return {};
255 }
256}
257
258static std::optional<float> computeVelocity(
259 const VelocityTracker::Strategy strategy,
260 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions, int32_t axis) {
261 VelocityTracker vt(strategy);
262
263 for (const MotionEvent& event : createMotionEventStream(axis, motions)) {
264 vt.addMovement(&event);
265 }
266
267 return vt.getVelocity(axis, DEFAULT_POINTER_ID);
268}
269
270static void computeAndCheckVelocity(const VelocityTracker::Strategy strategy,
271 const std::vector<PlanarMotionEventEntry>& motions,
Yeabkal Wubshita15e5992022-09-29 19:00:19 -0700272 int32_t axis, std::optional<float> targetVelocity,
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700273 uint32_t pointerId = DEFAULT_POINTER_ID) {
Yeabkal Wubshita15e5992022-09-29 19:00:19 -0700274 checkVelocity(computePlanarVelocity(strategy, motions, axis, pointerId), targetVelocity);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100275}
276
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700277static void computeAndCheckAxisScrollVelocity(
278 const VelocityTracker::Strategy strategy,
279 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions,
280 std::optional<float> targetVelocity) {
Yeabkal Wubshita15e5992022-09-29 19:00:19 -0700281 checkVelocity(computeVelocity(strategy, motions, AMOTION_EVENT_AXIS_SCROLL), targetVelocity);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700282}
283
284static void computeAndCheckQuadraticEstimate(const std::vector<PlanarMotionEventEntry>& motions,
285 const std::array<float, 3>& coefficients) {
Chris Yef8591482020-04-17 11:49:17 -0700286 VelocityTracker vt(VelocityTracker::Strategy::LSQ2);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700287 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700288 for (MotionEvent event : events) {
289 vt.addMovement(&event);
290 }
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800291 std::optional<VelocityTracker::Estimator> estimatorX = vt.getEstimator(AMOTION_EVENT_AXIS_X, 0);
292 std::optional<VelocityTracker::Estimator> estimatorY = vt.getEstimator(AMOTION_EVENT_AXIS_Y, 0);
293 EXPECT_TRUE(estimatorX);
294 EXPECT_TRUE(estimatorY);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700295 for (size_t i = 0; i< coefficients.size(); i++) {
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800296 checkCoefficient((*estimatorX).coeff[i], coefficients[i]);
297 checkCoefficient((*estimatorY).coeff[i], coefficients[i]);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700298 }
299}
300
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100301/*
Yeabkal Wubshiteca273c2022-10-05 19:06:40 -0700302 *================== VelocityTracker tests that do not require test motion data ====================
303 */
304TEST(SimpleVelocityTrackerTest, TestSupportedAxis) {
305 // Note that we are testing up to the max possible axis value, plus 3 more values. We are going
306 // beyond the max value to add a bit more protection. "3" is chosen arbitrarily to cover a few
307 // more values beyond the max.
308 for (int32_t axis = 0; axis <= AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE + 3; axis++) {
309 switch (axis) {
310 case AMOTION_EVENT_AXIS_X:
311 case AMOTION_EVENT_AXIS_Y:
312 case AMOTION_EVENT_AXIS_SCROLL:
313 EXPECT_TRUE(VelocityTracker::isAxisSupported(axis)) << axis << " is supported";
314 break;
315 default:
316 EXPECT_FALSE(VelocityTracker::isAxisSupported(axis)) << axis << " is NOT supported";
317 }
318 }
319}
320
321/*
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100322 * ================== VelocityTracker tests generated manually =====================================
323 */
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700324TEST_F(VelocityTrackerTest, TestDefaultStrategiesForPlanarAxes) {
325 std::vector<PlanarMotionEventEntry> motions = {{10ms, {{2, 4}}},
326 {20ms, {{4, 12}}},
327 {30ms, {{6, 20}}},
328 {40ms, {{10, 30}}}};
329
330 EXPECT_EQ(computePlanarVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X),
331 computePlanarVelocity(VelocityTracker::Strategy::DEFAULT, motions,
332 AMOTION_EVENT_AXIS_X));
333 EXPECT_EQ(computePlanarVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y),
334 computePlanarVelocity(VelocityTracker::Strategy::DEFAULT, motions,
335 AMOTION_EVENT_AXIS_Y));
336}
337
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000338TEST_F(VelocityTrackerTest, TestComputedVelocity) {
339 VelocityTracker::ComputedVelocity computedVelocity;
340
341 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, 0 /*id*/, 200 /*velocity*/);
342 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, 26U /*id*/, 400 /*velocity*/);
343 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, 27U /*id*/, 650 /*velocity*/);
344 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID, 750 /*velocity*/);
345 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, 0 /*id*/, 1000 /*velocity*/);
346 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, 26U /*id*/, 2000 /*velocity*/);
347 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, 27U /*id*/, 3000 /*velocity*/);
348 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, MAX_POINTER_ID, 4000 /*velocity*/);
349
350 // Check the axes/indices with velocity.
351 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, 0U /*id*/)), 200);
352 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, 26U /*id*/)), 400);
353 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, 27U /*id*/)), 650);
354 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID)), 750);
355 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, 0U /*id*/)), 1000);
356 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, 26U /*id*/)), 2000);
357 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, 27U /*id*/)), 3000);
358 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, MAX_POINTER_ID)), 4000);
359 for (uint32_t id = 0; id <= MAX_POINTER_ID; id++) {
360 // Since no data was added for AXIS_SCROLL, expect empty value for the axis for any id.
361 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_SCROLL, id))
362 << "Empty scroll data expected at id=" << id;
363 if (id == 0 || id == 26U || id == 27U || id == MAX_POINTER_ID) {
364 // Already checked above; continue.
365 continue;
366 }
367 // No data was added to X/Y for this id, expect empty value.
368 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, id))
369 << "Empty X data expected at id=" << id;
370 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, id))
371 << "Empty Y data expected at id=" << id;
372 }
373 // Out-of-bounds ids should given empty values.
374 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, -1));
375 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID + 1));
376}
377
378TEST_F(VelocityTrackerTest, TestGetComputedVelocity) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700379 std::vector<PlanarMotionEventEntry> motions = {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000380 {235089067457000ns, {{528.00, 0}}}, {235089084684000ns, {{527.00, 0}}},
381 {235089093349000ns, {{527.00, 0}}}, {235089095677625ns, {{527.00, 0}}},
382 {235089101859000ns, {{527.00, 0}}}, {235089110378000ns, {{528.00, 0}}},
383 {235089112497111ns, {{528.25, 0}}}, {235089118760000ns, {{531.00, 0}}},
384 {235089126686000ns, {{535.00, 0}}}, {235089129316820ns, {{536.33, 0}}},
385 {235089135199000ns, {{540.00, 0}}}, {235089144297000ns, {{546.00, 0}}},
386 {235089146136443ns, {{547.21, 0}}}, {235089152923000ns, {{553.00, 0}}},
387 {235089160784000ns, {{559.00, 0}}}, {235089162955851ns, {{560.66, 0}}},
388 {235089162955851ns, {{560.66, 0}}}, // ACTION_UP
389 };
390 VelocityTracker vt(VelocityTracker::Strategy::IMPULSE);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700391 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000392 for (const MotionEvent& event : events) {
393 vt.addMovement(&event);
394 }
395
396 float maxFloat = std::numeric_limits<float>::max();
397 VelocityTracker::ComputedVelocity computedVelocity;
398 computedVelocity = vt.getComputedVelocity(1000 /* units */, maxFloat);
399 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)),
400 764.345703);
401
402 // Expect X velocity to be scaled with respective to provided units.
403 computedVelocity = vt.getComputedVelocity(1000000 /* units */, maxFloat);
404 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)),
405 764345.703);
406
407 // Expect X velocity to be clamped by provided max velocity.
408 computedVelocity = vt.getComputedVelocity(1000000 /* units */, 1000);
409 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)), 1000);
410
411 // All 0 data for Y; expect 0 velocity.
412 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, DEFAULT_POINTER_ID)), 0);
413
414 // No data for scroll-axis; expect empty velocity.
415 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_SCROLL, DEFAULT_POINTER_ID));
416}
417
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700418TEST_F(VelocityTrackerTest, TestApiInteractionsWithNoMotionEvents) {
419 VelocityTracker vt(VelocityTracker::Strategy::DEFAULT);
420
421 EXPECT_FALSE(vt.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID));
422
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800423 EXPECT_FALSE(vt.getEstimator(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID));
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700424
425 VelocityTracker::ComputedVelocity computedVelocity = vt.getComputedVelocity(1000, 1000);
426 for (uint32_t id = 0; id <= MAX_POINTER_ID; id++) {
427 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, id));
428 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, id));
429 }
430
431 EXPECT_EQ(-1, vt.getActivePointerId());
432
433 // Make sure that the clearing functions execute without an issue.
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800434 vt.clearPointer(7U);
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700435 vt.clear();
436}
437
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700438TEST_F(VelocityTrackerTest, ThreePointsPositiveVelocityTest) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100439 // Same coordinate is reported 2 times in a row
440 // It is difficult to determine the correct answer here, but at least the direction
441 // of the reported velocity should be positive.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700442 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700443 {0ms, {{273, 0}}},
444 {12585us, {{293, 0}}},
445 {14730us, {{293, 0}}},
446 {14730us, {{293, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100447 };
Chris Yef8591482020-04-17 11:49:17 -0700448 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
449 1600);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100450}
451
452TEST_F(VelocityTrackerTest, ThreePointsZeroVelocityTest) {
453 // Same coordinate is reported 3 times in a row
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700454 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700455 {0ms, {{293, 0}}},
456 {6132us, {{293, 0}}},
457 {11283us, {{293, 0}}},
458 {11283us, {{293, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100459 };
Chris Yef8591482020-04-17 11:49:17 -0700460 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 0);
461 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 0);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100462}
463
464TEST_F(VelocityTrackerTest, ThreePointsLinearVelocityTest) {
465 // Fixed velocity at 5 points per 10 milliseconds
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700466 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700467 {0ms, {{0, 0}}}, {10ms, {{5, 0}}}, {20ms, {{10, 0}}}, {20ms, {{10, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100468 };
Chris Yef8591482020-04-17 11:49:17 -0700469 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 500);
470 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 500);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100471}
472
473
474/**
475 * ================== VelocityTracker tests generated by recording real events =====================
476 *
477 * To add a test, record the input coordinates and event times to all calls
478 * to void VelocityTracker::addMovement(const MotionEvent* event).
479 * Also record all calls to VelocityTracker::clear().
480 * Finally, record the output of VelocityTracker::getVelocity(...)
481 * This will give you the necessary data to create a new test.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700482 *
483 * Another good way to generate this data is to use 'dumpsys input' just after the event has
484 * occurred.
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100485 */
486
487// --------------- Recorded by hand on swordfish ---------------------------------------------------
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700488TEST_F(VelocityTrackerTest, SwordfishFlingDown) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100489 // Recording of a fling on Swordfish that could cause a fling in the wrong direction
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700490 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700491 { 0ms, {{271, 96}} },
492 { 16071042ns, {{269.786346, 106.922775}} },
493 { 35648403ns, {{267.983063, 156.660034}} },
494 { 52313925ns, {{262.638397, 220.339081}} },
495 { 68976522ns, {{266.138824, 331.581116}} },
496 { 85639375ns, {{274.79245, 428.113159}} },
497 { 96948871ns, {{274.79245, 428.113159}} },
498 { 96948871ns, {{274.79245, 428.113159}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100499 };
Chris Yef8591482020-04-17 11:49:17 -0700500 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
501 623.577637);
502 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
503 5970.7309);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100504}
505
506// --------------- Recorded by hand on sailfish, generated by a script -----------------------------
507// For some of these tests, the X-direction velocity checking has been removed, because the lsq2
508// and the impulse VelocityTrackerStrategies did not agree within 20%.
509// Since the flings were recorded in the Y-direction, the intentional user action should only
510// be relevant for the Y axis.
511// There have been also cases where lsq2 and impulse disagreed more than 20% in the Y-direction.
512// Those recordings have been discarded because we didn't feel one strategy's interpretation was
513// more correct than another's but didn't want to increase the tolerance for the entire test suite.
514//
515// There are 18 tests total below: 9 in the positive Y direction and 9 in the opposite.
516// The recordings were loosely binned into 3 categories - slow, faster, and fast, which roughly
517// characterizes the velocity of the finger motion.
518// These can be treated approximately as:
519// slow - less than 1 page gets scrolled
520// faster - more than 1 page gets scrolled, but less than 3
521// fast - entire list is scrolled (fling is done as hard as possible)
522
523TEST_F(VelocityTrackerTest, SailfishFlingUpSlow1) {
524 // Sailfish - fling up - slow - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700525 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700526 { 235089067457000ns, {{528.00, 983.00}} },
527 { 235089084684000ns, {{527.00, 981.00}} },
528 { 235089093349000ns, {{527.00, 977.00}} },
529 { 235089095677625ns, {{527.00, 975.93}} },
530 { 235089101859000ns, {{527.00, 970.00}} },
531 { 235089110378000ns, {{528.00, 960.00}} },
532 { 235089112497111ns, {{528.25, 957.51}} },
533 { 235089118760000ns, {{531.00, 946.00}} },
534 { 235089126686000ns, {{535.00, 931.00}} },
535 { 235089129316820ns, {{536.33, 926.02}} },
536 { 235089135199000ns, {{540.00, 914.00}} },
537 { 235089144297000ns, {{546.00, 896.00}} },
538 { 235089146136443ns, {{547.21, 892.36}} },
539 { 235089152923000ns, {{553.00, 877.00}} },
540 { 235089160784000ns, {{559.00, 851.00}} },
541 { 235089162955851ns, {{560.66, 843.82}} },
542 { 235089162955851ns, {{560.66, 843.82}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100543 };
Chris Yef8591482020-04-17 11:49:17 -0700544 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700545 764.345703);
Chris Yef8591482020-04-17 11:49:17 -0700546 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
547 951.698181);
548 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
549 -3604.819336);
550 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
551 -3044.966064);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100552}
553
554
555TEST_F(VelocityTrackerTest, SailfishFlingUpSlow2) {
556 // Sailfish - fling up - slow - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700557 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700558 { 235110560704000ns, {{522.00, 1107.00}} },
559 { 235110575764000ns, {{522.00, 1107.00}} },
560 { 235110584385000ns, {{522.00, 1107.00}} },
561 { 235110588421179ns, {{521.52, 1106.52}} },
562 { 235110592830000ns, {{521.00, 1106.00}} },
563 { 235110601385000ns, {{520.00, 1104.00}} },
564 { 235110605088160ns, {{519.14, 1102.27}} },
565 { 235110609952000ns, {{518.00, 1100.00}} },
566 { 235110618353000ns, {{517.00, 1093.00}} },
567 { 235110621755146ns, {{516.60, 1090.17}} },
568 { 235110627010000ns, {{517.00, 1081.00}} },
569 { 235110634785000ns, {{518.00, 1063.00}} },
570 { 235110638422450ns, {{518.87, 1052.58}} },
571 { 235110643161000ns, {{520.00, 1039.00}} },
572 { 235110651767000ns, {{524.00, 1011.00}} },
573 { 235110655089581ns, {{525.54, 1000.19}} },
574 { 235110660368000ns, {{530.00, 980.00}} },
575 { 235110660368000ns, {{530.00, 980.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100576 };
Chris Yef8591482020-04-17 11:49:17 -0700577 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
578 -4096.583008);
579 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
580 -3455.094238);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100581}
582
583
584TEST_F(VelocityTrackerTest, SailfishFlingUpSlow3) {
585 // Sailfish - fling up - slow - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700586 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700587 { 792536237000ns, {{580.00, 1317.00}} },
588 { 792541538987ns, {{580.63, 1311.94}} },
589 { 792544613000ns, {{581.00, 1309.00}} },
590 { 792552301000ns, {{583.00, 1295.00}} },
591 { 792558362309ns, {{585.13, 1282.92}} },
592 { 792560828000ns, {{586.00, 1278.00}} },
593 { 792569446000ns, {{589.00, 1256.00}} },
594 { 792575185095ns, {{591.54, 1241.41}} },
595 { 792578491000ns, {{593.00, 1233.00}} },
596 { 792587044000ns, {{597.00, 1211.00}} },
597 { 792592008172ns, {{600.28, 1195.92}} },
598 { 792594616000ns, {{602.00, 1188.00}} },
599 { 792603129000ns, {{607.00, 1167.00}} },
600 { 792608831290ns, {{609.48, 1155.83}} },
601 { 792612321000ns, {{611.00, 1149.00}} },
602 { 792620768000ns, {{615.00, 1131.00}} },
603 { 792625653873ns, {{617.32, 1121.73}} },
604 { 792629200000ns, {{619.00, 1115.00}} },
605 { 792629200000ns, {{619.00, 1115.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100606 };
Chris Yef8591482020-04-17 11:49:17 -0700607 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
608 574.33429);
609 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
610 617.40564);
611 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
612 -2361.982666);
613 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
614 -2500.055664);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100615}
616
617
618TEST_F(VelocityTrackerTest, SailfishFlingUpFaster1) {
619 // Sailfish - fling up - faster - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700620 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700621 { 235160420675000ns, {{610.00, 1042.00}} },
622 { 235160428220000ns, {{609.00, 1026.00}} },
623 { 235160436544000ns, {{609.00, 1024.00}} },
624 { 235160441852394ns, {{609.64, 1020.82}} },
625 { 235160444878000ns, {{610.00, 1019.00}} },
626 { 235160452673000ns, {{613.00, 1006.00}} },
627 { 235160458519743ns, {{617.18, 992.06}} },
628 { 235160461061000ns, {{619.00, 986.00}} },
629 { 235160469798000ns, {{627.00, 960.00}} },
630 { 235160475186713ns, {{632.22, 943.02}} },
631 { 235160478051000ns, {{635.00, 934.00}} },
632 { 235160486489000ns, {{644.00, 906.00}} },
633 { 235160491853697ns, {{649.56, 890.56}} },
634 { 235160495177000ns, {{653.00, 881.00}} },
635 { 235160504148000ns, {{662.00, 858.00}} },
636 { 235160509231495ns, {{666.81, 845.37}} },
637 { 235160512603000ns, {{670.00, 837.00}} },
638 { 235160520366000ns, {{679.00, 814.00}} },
639 { 235160520366000ns, {{679.00, 814.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100640 };
Chris Yef8591482020-04-17 11:49:17 -0700641 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
642 1274.141724);
643 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
644 1438.53186);
645 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
646 -3001.4348);
647 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
648 -3695.859619);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100649}
650
651
652TEST_F(VelocityTrackerTest, SailfishFlingUpFaster2) {
653 // Sailfish - fling up - faster - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700654 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700655 { 847153808000ns, {{576.00, 1264.00}} },
656 { 847171174000ns, {{576.00, 1262.00}} },
657 { 847179640000ns, {{576.00, 1257.00}} },
658 { 847185187540ns, {{577.41, 1249.22}} },
659 { 847187487000ns, {{578.00, 1246.00}} },
660 { 847195710000ns, {{581.00, 1227.00}} },
661 { 847202027059ns, {{583.93, 1209.40}} },
662 { 847204324000ns, {{585.00, 1203.00}} },
663 { 847212672000ns, {{590.00, 1176.00}} },
664 { 847218861395ns, {{594.36, 1157.11}} },
665 { 847221190000ns, {{596.00, 1150.00}} },
666 { 847230484000ns, {{602.00, 1124.00}} },
667 { 847235701400ns, {{607.56, 1103.83}} },
668 { 847237986000ns, {{610.00, 1095.00}} },
669 { 847237986000ns, {{610.00, 1095.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100670 };
Chris Yef8591482020-04-17 11:49:17 -0700671 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
672 -4280.07959);
673 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
674 -4241.004395);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100675}
676
677
678TEST_F(VelocityTrackerTest, SailfishFlingUpFaster3) {
679 // Sailfish - fling up - faster - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700680 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700681 { 235200532789000ns, {{507.00, 1084.00}} },
682 { 235200549221000ns, {{507.00, 1083.00}} },
683 { 235200557841000ns, {{507.00, 1081.00}} },
684 { 235200558051189ns, {{507.00, 1080.95}} },
685 { 235200566314000ns, {{507.00, 1078.00}} },
686 { 235200574876586ns, {{508.97, 1070.12}} },
687 { 235200575006000ns, {{509.00, 1070.00}} },
688 { 235200582900000ns, {{514.00, 1054.00}} },
689 { 235200591276000ns, {{525.00, 1023.00}} },
690 { 235200591701829ns, {{525.56, 1021.42}} },
691 { 235200600064000ns, {{542.00, 976.00}} },
692 { 235200608519000ns, {{563.00, 911.00}} },
693 { 235200608527086ns, {{563.02, 910.94}} },
694 { 235200616933000ns, {{590.00, 844.00}} },
695 { 235200616933000ns, {{590.00, 844.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100696 };
Chris Yef8591482020-04-17 11:49:17 -0700697 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
698 -8715.686523);
699 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
700 -7639.026367);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100701}
702
703
704TEST_F(VelocityTrackerTest, SailfishFlingUpFast1) {
705 // Sailfish - fling up - fast - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700706 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700707 { 920922149000ns, {{561.00, 1412.00}} },
708 { 920930185000ns, {{559.00, 1377.00}} },
709 { 920930262463ns, {{558.98, 1376.66}} },
710 { 920938547000ns, {{559.00, 1371.00}} },
711 { 920947096857ns, {{562.91, 1342.68}} },
712 { 920947302000ns, {{563.00, 1342.00}} },
713 { 920955502000ns, {{577.00, 1272.00}} },
714 { 920963931021ns, {{596.87, 1190.54}} },
715 { 920963987000ns, {{597.00, 1190.00}} },
716 { 920972530000ns, {{631.00, 1093.00}} },
717 { 920980765511ns, {{671.31, 994.68}} },
718 { 920980906000ns, {{672.00, 993.00}} },
719 { 920989261000ns, {{715.00, 903.00}} },
720 { 920989261000ns, {{715.00, 903.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100721 };
Chris Yef8591482020-04-17 11:49:17 -0700722 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
723 5670.329102);
724 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
725 5991.866699);
726 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
727 -13021.101562);
728 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
729 -15093.995117);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100730}
731
732
733TEST_F(VelocityTrackerTest, SailfishFlingUpFast2) {
734 // Sailfish - fling up - fast - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700735 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700736 { 235247153233000ns, {{518.00, 1168.00}} },
737 { 235247170452000ns, {{517.00, 1167.00}} },
738 { 235247178908000ns, {{515.00, 1159.00}} },
739 { 235247179556213ns, {{514.85, 1158.39}} },
740 { 235247186821000ns, {{515.00, 1125.00}} },
741 { 235247195265000ns, {{521.00, 1051.00}} },
742 { 235247196389476ns, {{521.80, 1041.15}} },
743 { 235247203649000ns, {{538.00, 932.00}} },
744 { 235247212253000ns, {{571.00, 794.00}} },
745 { 235247213222491ns, {{574.72, 778.45}} },
746 { 235247220736000ns, {{620.00, 641.00}} },
747 { 235247220736000ns, {{620.00, 641.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100748 };
Chris Yef8591482020-04-17 11:49:17 -0700749 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
750 -20286.958984);
751 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
752 -20494.587891);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100753}
754
755
756TEST_F(VelocityTrackerTest, SailfishFlingUpFast3) {
757 // Sailfish - fling up - fast - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700758 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700759 { 235302568736000ns, {{529.00, 1167.00}} },
760 { 235302576644000ns, {{523.00, 1140.00}} },
761 { 235302579395063ns, {{520.91, 1130.61}} },
762 { 235302585140000ns, {{522.00, 1130.00}} },
763 { 235302593615000ns, {{527.00, 1065.00}} },
764 { 235302596207444ns, {{528.53, 1045.12}} },
765 { 235302602102000ns, {{559.00, 872.00}} },
766 { 235302610545000ns, {{652.00, 605.00}} },
767 { 235302613019881ns, {{679.26, 526.73}} },
768 { 235302613019881ns, {{679.26, 526.73}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100769 };
Chris Yef8591482020-04-17 11:49:17 -0700770 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
771 -39295.941406);
772 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
773 -36461.421875);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100774}
775
776
777TEST_F(VelocityTrackerTest, SailfishFlingDownSlow1) {
778 // Sailfish - fling down - slow - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700779 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700780 { 235655749552755ns, {{582.00, 432.49}} },
781 { 235655750638000ns, {{582.00, 433.00}} },
782 { 235655758865000ns, {{582.00, 440.00}} },
783 { 235655766221523ns, {{581.16, 448.43}} },
784 { 235655767594000ns, {{581.00, 450.00}} },
785 { 235655776044000ns, {{580.00, 462.00}} },
786 { 235655782890696ns, {{579.18, 474.35}} },
787 { 235655784360000ns, {{579.00, 477.00}} },
788 { 235655792795000ns, {{578.00, 496.00}} },
789 { 235655799559531ns, {{576.27, 515.04}} },
790 { 235655800612000ns, {{576.00, 518.00}} },
791 { 235655809535000ns, {{574.00, 542.00}} },
792 { 235655816988015ns, {{572.17, 564.86}} },
793 { 235655817685000ns, {{572.00, 567.00}} },
794 { 235655825981000ns, {{569.00, 595.00}} },
795 { 235655833808653ns, {{566.26, 620.60}} },
796 { 235655834541000ns, {{566.00, 623.00}} },
797 { 235655842893000ns, {{563.00, 649.00}} },
798 { 235655842893000ns, {{563.00, 649.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100799 };
Chris Yef8591482020-04-17 11:49:17 -0700800 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
801 -419.749695);
802 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
803 -398.303894);
804 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
805 3309.016357);
806 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
807 3969.099854);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100808}
809
810
811TEST_F(VelocityTrackerTest, SailfishFlingDownSlow2) {
812 // Sailfish - fling down - slow - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700813 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700814 { 235671152083370ns, {{485.24, 558.28}} },
815 { 235671154126000ns, {{485.00, 559.00}} },
816 { 235671162497000ns, {{484.00, 566.00}} },
817 { 235671168750511ns, {{483.27, 573.29}} },
818 { 235671171071000ns, {{483.00, 576.00}} },
819 { 235671179390000ns, {{482.00, 588.00}} },
820 { 235671185417210ns, {{481.31, 598.98}} },
821 { 235671188173000ns, {{481.00, 604.00}} },
822 { 235671196371000ns, {{480.00, 624.00}} },
823 { 235671202084196ns, {{479.27, 639.98}} },
824 { 235671204235000ns, {{479.00, 646.00}} },
825 { 235671212554000ns, {{478.00, 673.00}} },
826 { 235671219471011ns, {{476.39, 697.12}} },
827 { 235671221159000ns, {{476.00, 703.00}} },
828 { 235671229592000ns, {{474.00, 734.00}} },
829 { 235671236281462ns, {{472.43, 758.38}} },
830 { 235671238098000ns, {{472.00, 765.00}} },
831 { 235671246532000ns, {{470.00, 799.00}} },
832 { 235671246532000ns, {{470.00, 799.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100833 };
Chris Yef8591482020-04-17 11:49:17 -0700834 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
835 -262.80426);
836 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
837 -243.665344);
838 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
839 4215.682129);
840 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
841 4587.986816);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100842}
843
844
845TEST_F(VelocityTrackerTest, SailfishFlingDownSlow3) {
846 // Sailfish - fling down - slow - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700847 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700848 { 170983201000ns, {{557.00, 533.00}} },
849 { 171000668000ns, {{556.00, 534.00}} },
850 { 171007359750ns, {{554.73, 535.27}} },
851 { 171011197000ns, {{554.00, 536.00}} },
852 { 171017660000ns, {{552.00, 540.00}} },
853 { 171024201831ns, {{549.97, 544.73}} },
854 { 171027333000ns, {{549.00, 547.00}} },
855 { 171034603000ns, {{545.00, 557.00}} },
856 { 171041043371ns, {{541.98, 567.55}} },
857 { 171043147000ns, {{541.00, 571.00}} },
858 { 171051052000ns, {{536.00, 586.00}} },
859 { 171051052000ns, {{536.00, 586.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100860 };
Chris Yef8591482020-04-17 11:49:17 -0700861 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
862 -723.413513);
863 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
864 -651.038452);
865 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
866 2091.502441);
867 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
868 1934.517456);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100869}
870
871
872TEST_F(VelocityTrackerTest, SailfishFlingDownFaster1) {
873 // Sailfish - fling down - faster - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700874 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700875 { 235695280333000ns, {{558.00, 451.00}} },
876 { 235695283971237ns, {{558.43, 454.45}} },
877 { 235695289038000ns, {{559.00, 462.00}} },
878 { 235695297388000ns, {{561.00, 478.00}} },
879 { 235695300638465ns, {{561.83, 486.25}} },
880 { 235695305265000ns, {{563.00, 498.00}} },
881 { 235695313591000ns, {{564.00, 521.00}} },
882 { 235695317305492ns, {{564.43, 532.68}} },
883 { 235695322181000ns, {{565.00, 548.00}} },
884 { 235695330709000ns, {{565.00, 577.00}} },
885 { 235695333972227ns, {{565.00, 588.10}} },
886 { 235695339250000ns, {{565.00, 609.00}} },
887 { 235695347839000ns, {{565.00, 642.00}} },
888 { 235695351313257ns, {{565.00, 656.18}} },
889 { 235695356412000ns, {{565.00, 677.00}} },
890 { 235695364899000ns, {{563.00, 710.00}} },
891 { 235695368118682ns, {{562.24, 722.52}} },
892 { 235695373403000ns, {{564.00, 744.00}} },
893 { 235695373403000ns, {{564.00, 744.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100894 };
Chris Yef8591482020-04-17 11:49:17 -0700895 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
896 4254.639648);
897 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
898 4698.415039);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100899}
900
901
902TEST_F(VelocityTrackerTest, SailfishFlingDownFaster2) {
903 // Sailfish - fling down - faster - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700904 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700905 { 235709624766000ns, {{535.00, 579.00}} },
906 { 235709642256000ns, {{534.00, 580.00}} },
907 { 235709643350278ns, {{533.94, 580.06}} },
908 { 235709650760000ns, {{532.00, 584.00}} },
909 { 235709658615000ns, {{530.00, 593.00}} },
910 { 235709660170495ns, {{529.60, 594.78}} },
911 { 235709667095000ns, {{527.00, 606.00}} },
912 { 235709675616000ns, {{524.00, 628.00}} },
913 { 235709676983261ns, {{523.52, 631.53}} },
914 { 235709684289000ns, {{521.00, 652.00}} },
915 { 235709692763000ns, {{518.00, 682.00}} },
916 { 235709693804993ns, {{517.63, 685.69}} },
917 { 235709701438000ns, {{515.00, 709.00}} },
918 { 235709709830000ns, {{512.00, 739.00}} },
919 { 235709710626776ns, {{511.72, 741.85}} },
920 { 235709710626776ns, {{511.72, 741.85}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100921 };
Chris Yef8591482020-04-17 11:49:17 -0700922 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
923 -430.440247);
924 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
925 -447.600311);
926 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
927 3953.859375);
928 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
929 4316.155273);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100930}
931
932
933TEST_F(VelocityTrackerTest, SailfishFlingDownFaster3) {
934 // Sailfish - fling down - faster - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700935 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700936 { 235727628927000ns, {{540.00, 440.00}} },
937 { 235727636810000ns, {{537.00, 454.00}} },
938 { 235727646176000ns, {{536.00, 454.00}} },
939 { 235727653586628ns, {{535.12, 456.65}} },
940 { 235727654557000ns, {{535.00, 457.00}} },
941 { 235727663024000ns, {{534.00, 465.00}} },
942 { 235727670410103ns, {{533.04, 479.45}} },
943 { 235727670691000ns, {{533.00, 480.00}} },
944 { 235727679255000ns, {{531.00, 501.00}} },
945 { 235727687233704ns, {{529.09, 526.73}} },
946 { 235727687628000ns, {{529.00, 528.00}} },
947 { 235727696113000ns, {{526.00, 558.00}} },
948 { 235727704057546ns, {{523.18, 588.98}} },
949 { 235727704576000ns, {{523.00, 591.00}} },
950 { 235727713099000ns, {{520.00, 626.00}} },
951 { 235727720880776ns, {{516.33, 655.36}} },
952 { 235727721580000ns, {{516.00, 658.00}} },
953 { 235727721580000ns, {{516.00, 658.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100954 };
Chris Yef8591482020-04-17 11:49:17 -0700955 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
956 4484.617676);
957 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
958 4927.92627);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100959}
960
961
962TEST_F(VelocityTrackerTest, SailfishFlingDownFast1) {
963 // Sailfish - fling down - fast - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700964 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700965 { 235762352849000ns, {{467.00, 286.00}} },
966 { 235762360250000ns, {{443.00, 344.00}} },
967 { 235762362787412ns, {{434.77, 363.89}} },
968 { 235762368807000ns, {{438.00, 359.00}} },
969 { 235762377220000ns, {{425.00, 423.00}} },
970 { 235762379608561ns, {{421.31, 441.17}} },
971 { 235762385698000ns, {{412.00, 528.00}} },
972 { 235762394133000ns, {{406.00, 648.00}} },
973 { 235762396429369ns, {{404.37, 680.67}} },
974 { 235762396429369ns, {{404.37, 680.67}} }, //ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100975 };
Chris Yef8591482020-04-17 11:49:17 -0700976 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
977 14227.0224);
978 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
979 16064.685547);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100980}
981
982
983TEST_F(VelocityTrackerTest, SailfishFlingDownFast2) {
984 // Sailfish - fling down - fast - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700985 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700986 { 235772487188000ns, {{576.00, 204.00}} },
987 { 235772495159000ns, {{553.00, 236.00}} },
988 { 235772503568000ns, {{551.00, 240.00}} },
989 { 235772508192247ns, {{545.55, 254.17}} },
990 { 235772512051000ns, {{541.00, 266.00}} },
991 { 235772520794000ns, {{520.00, 337.00}} },
992 { 235772525015263ns, {{508.92, 394.43}} },
993 { 235772529174000ns, {{498.00, 451.00}} },
994 { 235772537635000ns, {{484.00, 589.00}} },
995 { 235772537635000ns, {{484.00, 589.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100996 };
Chris Yef8591482020-04-17 11:49:17 -0700997 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
998 18660.048828);
999 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1000 16918.439453);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001001}
1002
1003
1004TEST_F(VelocityTrackerTest, SailfishFlingDownFast3) {
1005 // Sailfish - fling down - fast - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001006 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001007 { 507650295000ns, {{628.00, 233.00}} },
1008 { 507658234000ns, {{605.00, 269.00}} },
1009 { 507666784000ns, {{601.00, 274.00}} },
1010 { 507669660483ns, {{599.65, 275.68}} },
1011 { 507675427000ns, {{582.00, 308.00}} },
1012 { 507683740000ns, {{541.00, 404.00}} },
1013 { 507686506238ns, {{527.36, 435.95}} },
1014 { 507692220000ns, {{487.00, 581.00}} },
1015 { 507700707000ns, {{454.00, 792.00}} },
1016 { 507703352649ns, {{443.71, 857.77}} },
1017 { 507703352649ns, {{443.71, 857.77}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001018 };
Chris Yef8591482020-04-17 11:49:17 -07001019 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1020 -4111.8173);
1021 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1022 -6388.48877);
1023 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
1024 29765.908203);
1025 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1026 28354.796875);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001027}
1028
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001029/**
1030 * ================== Multiple pointers ============================================================
1031 *
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001032 * Three fingers quickly tap the screen. Since this is a tap, the velocities should be empty.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001033 * If the events with POINTER_UP or POINTER_DOWN are not handled correctly (these should not be
1034 * part of the fitted data), this can cause large velocity values to be reported instead.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001035 */
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -07001036TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_ThreeFingerTap) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001037 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001038 { 0us, {{1063, 1128}, {NAN, NAN}, {NAN, NAN}} },
1039 { 10800us, {{1063, 1128}, {682, 1318}, {NAN, NAN}} }, // POINTER_DOWN
1040 { 10800us, {{1063, 1128}, {682, 1318}, {397, 1747}} }, // POINTER_DOWN
1041 { 267300us, {{1063, 1128}, {682, 1318}, {397, 1747}} }, // POINTER_UP
1042 { 267300us, {{1063, 1128}, {NAN, NAN}, {397, 1747}} }, // POINTER_UP
1043 { 272700us, {{1063, 1128}, {NAN, NAN}, {NAN, NAN}} },
1044 };
1045
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001046 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1047 std::nullopt);
1048 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1049 std::nullopt);
1050 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1051 std::nullopt);
1052 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
1053 std::nullopt);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001054}
1055
1056/**
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001057 * ================= Pointer liftoff ===============================================================
1058 */
1059
1060/**
1061 * The last movement of a pointer is always ACTION_POINTER_UP or ACTION_UP. If there's a short delay
1062 * between the last ACTION_MOVE and the next ACTION_POINTER_UP or ACTION_UP, velocity should not be
1063 * affected by the liftoff.
1064 */
1065TEST_F(VelocityTrackerTest, ShortDelayBeforeActionUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001066 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001067 {0ms, {{10, 0}}}, {10ms, {{20, 0}}}, {20ms, {{30, 0}}}, {30ms, {{30, 0}}}, // ACTION_UP
1068 };
1069 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1070 1000);
1071 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 1000);
1072}
1073
1074/**
1075 * The last movement of a single pointer is ACTION_UP. If there's a long delay between the last
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001076 * ACTION_MOVE and the final ACTION_UP, velocity should be reported as empty because the pointer
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001077 * should be assumed to have stopped.
1078 */
1079TEST_F(VelocityTrackerTest, LongDelayBeforeActionUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001080 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001081 {0ms, {{10, 0}}},
1082 {10ms, {{20, 0}}},
1083 {20ms, {{30, 0}}},
1084 {3000ms, {{30, 0}}}, // ACTION_UP
1085 };
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001086 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1087 std::nullopt);
1088 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1089 std::nullopt);
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001090}
1091
1092/**
1093 * The last movement of a pointer is always ACTION_POINTER_UP or ACTION_UP. If there's a long delay
1094 * before ACTION_POINTER_UP event, the movement should be assumed to have stopped.
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001095 * The final velocity should be reported as empty for all pointers.
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001096 */
1097TEST_F(VelocityTrackerTest, LongDelayBeforeActionPointerUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001098 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001099 {0ms, {{10, 0}}},
1100 {10ms, {{20, 0}, {100, 0}}},
1101 {20ms, {{30, 0}, {200, 0}}},
1102 {30ms, {{30, 0}, {300, 0}}},
1103 {40ms, {{30, 0}, {400, 0}}},
1104 {3000ms, {{30, 0}}}, // ACTION_POINTER_UP
1105 };
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001106 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1107 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001108 /*pointerId*/ 0);
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001109 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1110 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001111 /*pointerId*/ 0);
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001112 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1113 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001114 /*pointerId*/ 1);
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001115 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1116 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001117 /*pointerId*/ 1);
1118}
1119
1120/**
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001121 * ================== Tests for least squares fitting ==============================================
1122 *
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001123 * Special care must be taken when constructing tests for LeastSquaresVelocityTrackerStrategy
1124 * getEstimator function. In particular:
1125 * - inside the function, time gets converted from nanoseconds to seconds
1126 * before being used in the fit.
1127 * - any values that are older than 100 ms are being discarded.
1128 * - the newest time gets subtracted from all of the other times before being used in the fit.
1129 * So these tests have to be designed with those limitations in mind.
1130 *
1131 * General approach for the tests below:
1132 * We only used timestamps in milliseconds, 0 ms, 1 ms, and 2 ms, to be sure that
1133 * we are well within the HORIZON range.
1134 * When specifying the expected values of the coefficients, we treat the x values as if
1135 * they were in ms. Then, to adjust for the time units, the coefficients get progressively
1136 * multiplied by powers of 1E3.
1137 * For example:
1138 * data: t(ms), x
1139 * 1 ms, 1
1140 * 2 ms, 4
1141 * 3 ms, 9
1142 * The coefficients are (0, 0, 1).
1143 * In the test, we would convert these coefficients to (0*(1E3)^0, 0*(1E3)^1, 1*(1E3)^2).
1144 */
1145TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Constant) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001146 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001147 { 0ms, {{1, 1}} }, // 0 s
1148 { 1ms, {{1, 1}} }, // 0.001 s
1149 { 2ms, {{1, 1}} }, // 0.002 s
1150 { 2ms, {{1, 1}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001151 };
1152 // The data used for the fit will be as follows:
1153 // time(s), position
1154 // -0.002, 1
1155 // -0.001, 1
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001156 // -0.ms, 1
1157 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({1, 0, 0}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001158}
1159
1160/*
1161 * Straight line y = x :: the constant and quadratic coefficients are zero.
1162 */
1163TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Linear) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001164 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001165 { 0ms, {{-2, -2}} },
1166 { 1ms, {{-1, -1}} },
1167 { 2ms, {{-0, -0}} },
1168 { 2ms, {{-0, -0}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001169 };
1170 // The data used for the fit will be as follows:
1171 // time(s), position
1172 // -0.002, -2
1173 // -0.001, -1
1174 // -0.000, 0
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001175 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({0, 1E3, 0}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001176}
1177
1178/*
1179 * Parabola
1180 */
1181TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001182 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001183 { 0ms, {{1, 1}} },
1184 { 1ms, {{4, 4}} },
1185 { 2ms, {{8, 8}} },
1186 { 2ms, {{8, 8}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001187 };
1188 // The data used for the fit will be as follows:
1189 // time(s), position
1190 // -0.002, 1
1191 // -0.001, 4
1192 // -0.000, 8
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001193 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({8, 4.5E3, 0.5E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001194}
1195
1196/*
1197 * Parabola
1198 */
1199TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic2) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001200 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001201 { 0ms, {{1, 1}} },
1202 { 1ms, {{4, 4}} },
1203 { 2ms, {{9, 9}} },
1204 { 2ms, {{9, 9}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001205 };
1206 // The data used for the fit will be as follows:
1207 // time(s), position
1208 // -0.002, 1
1209 // -0.001, 4
1210 // -0.000, 9
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001211 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({9, 6E3, 1E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001212}
1213
1214/*
1215 * Parabola :: y = x^2 :: the constant and linear coefficients are zero.
1216 */
1217TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic3) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001218 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001219 { 0ms, {{4, 4}} },
1220 { 1ms, {{1, 1}} },
1221 { 2ms, {{0, 0}} },
1222 { 2ms, {{0, 0}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001223 };
1224 // The data used for the fit will be as follows:
1225 // time(s), position
1226 // -0.002, 4
1227 // -0.001, 1
1228 // -0.000, 0
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001229 computeAndCheckQuadraticEstimate(motions, std::array<float, 3>({0, 0E3, 1E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001230}
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001231
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001232// Recorded by hand on sailfish, but only the diffs are taken to test cumulative axis velocity.
1233TEST_F(VelocityTrackerTest, AxisScrollVelocity) {
1234 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1235 {235089067457000ns, 0.00}, {235089084684000ns, -1.00}, {235089093349000ns, 0.00},
1236 {235089095677625ns, 0.00}, {235089101859000ns, 0.00}, {235089110378000ns, 0.00},
1237 {235089112497111ns, 0.25}, {235089118760000ns, 1.75}, {235089126686000ns, 4.00},
1238 {235089129316820ns, 1.33}, {235089135199000ns, 3.67}, {235089144297000ns, 6.00},
1239 {235089146136443ns, 1.21}, {235089152923000ns, 5.79}, {235089160784000ns, 6.00},
1240 {235089162955851ns, 1.66},
1241 };
1242
1243 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {764.345703});
1244}
1245
1246// --------------- Recorded by hand on a Wear OS device using a rotating side button ---------------
1247TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollDown) {
1248 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1249 {224598065152ns, -0.050100}, {224621871104ns, -0.133600}, {224645464064ns, -0.551100},
1250 {224669171712ns, -0.801600}, {224687063040ns, -1.035400}, {224706691072ns, -0.484300},
1251 {224738213888ns, -0.334000}, {224754401280ns, -0.083500},
1252 };
1253
1254 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {-27.86});
1255}
1256
1257TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollUp) {
1258 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1259 {269606010880ns, 0.050100}, {269626064896ns, 0.217100}, {269641973760ns, 0.267200},
1260 {269658079232ns, 0.267200}, {269674217472ns, 0.267200}, {269690683392ns, 0.367400},
1261 {269706133504ns, 0.551100}, {269722173440ns, 0.501000},
1262 };
1263
1264 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {31.92});
1265}
1266
1267TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollDown_ThenUp_ThenDown) {
1268 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1269 {2580534001664ns, -0.033400}, {2580549992448ns, -0.133600},
1270 {2580566769664ns, -0.250500}, {2580581974016ns, -0.183700},
1271 {2580597964800ns, -0.267200}, {2580613955584ns, -0.551100},
1272 {2580635189248ns, -0.601200}, {2580661927936ns, -0.450900},
1273 {2580683161600ns, -0.417500}, {2580705705984ns, -0.150300},
1274 {2580722745344ns, -0.016700}, {2580786446336ns, 0.050100},
1275 {2580801912832ns, 0.150300}, {2580822360064ns, 0.300600},
1276 {2580838088704ns, 0.300600}, {2580854341632ns, 0.400800},
1277 {2580869808128ns, 0.517700}, {2580886061056ns, 0.501000},
1278 {2580905984000ns, 0.350700}, {2580921974784ns, 0.350700},
1279 {2580937965568ns, 0.066800}, {2580974665728ns, 0.016700},
1280 {2581034434560ns, -0.066800}, {2581049901056ns, -0.116900},
1281 {2581070610432ns, -0.317300}, {2581086076928ns, -0.200400},
1282 {2581101805568ns, -0.233800}, {2581118058496ns, -0.417500},
1283 {2581134049280ns, -0.417500}, {2581150040064ns, -0.367400},
1284 {2581166030848ns, -0.267200}, {2581181759488ns, -0.150300},
1285 {2581199847424ns, -0.066800},
1286 };
1287
1288 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {-9.73});
1289}
1290
1291// ------------------------------- Hand generated test cases ---------------------------------------
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -07001292TEST_F(VelocityTrackerTest, TestDefaultStrategyForAxisScroll) {
1293 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1294 {10ms, 20},
1295 {20ms, 25},
1296 {30ms, 50},
1297 {40ms, 100},
1298 };
1299
1300 EXPECT_EQ(computeVelocity(VelocityTracker::Strategy::IMPULSE, motions,
1301 AMOTION_EVENT_AXIS_SCROLL),
1302 computeVelocity(VelocityTracker::Strategy::DEFAULT, motions,
1303 AMOTION_EVENT_AXIS_SCROLL));
1304}
1305
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001306TEST_F(VelocityTrackerTest, AxisScrollVelocity_SimilarDifferentialValues) {
1307 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ns, 2.12}, {3ns, 2.12},
1308 {7ns, 2.12}, {8ns, 2.12},
1309 {15ns, 2.12}, {18ns, 2.12}};
1310
1311 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {1690236059.86});
1312}
1313
1314TEST_F(VelocityTrackerTest, AxisScrollVelocity_OnlyTwoValues) {
1315 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ms, 5}, {2ms, 10}};
1316
1317 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {10000});
1318}
1319
1320TEST_F(VelocityTrackerTest, AxisScrollVelocity_ConstantVelocity) {
1321 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ms, 20}, {2ms, 20},
1322 {3ms, 20}, {4ms, 20},
1323 {5ms, 20}, {6ms, 20}};
1324
1325 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {20000});
1326}
1327
1328TEST_F(VelocityTrackerTest, AxisScrollVelocity_NoMotion) {
1329 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ns, 0}, {2ns, 0},
1330 {3ns, 0}, {4ns, 0},
1331 {5ns, 0}, {6ns, 0}};
1332
1333 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {0});
1334}
1335
1336TEST_F(VelocityTrackerTest, AxisScrollVelocity_NoData) {
1337 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {};
1338
1339 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, std::nullopt);
1340}
1341
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001342} // namespace android