blob: 1c8ec903734c14604105e3c17ffb1af1f92bc60f [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
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -080045// quadratic velocity must be within 0.001% of the target value
46constexpr float QUADRATIC_VELOCITY_TOLERANCE = 0.00001;
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070047
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
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010079struct Position {
Siarhei Vishniakou01ca4862019-03-06 13:22:13 -080080 float x;
81 float y;
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070082
Siarhei Vishniakouc36d21e2023-01-17 09:59:38 -080083 bool isResampled = false;
84
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070085 /**
86 * If both values are NAN, then this is considered to be an empty entry (no pointer data).
87 * If only one of the values is NAN, this is still a valid entry,
88 * because we may only care about a single axis.
89 */
90 bool isValid() const {
91 return !(isnan(x) && isnan(y));
92 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010093};
94
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -070095struct PlanarMotionEventEntry {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -070096 std::chrono::nanoseconds eventTime;
97 std::vector<Position> positions;
98};
99
100static BitSet32 getValidPointers(const std::vector<Position>& positions) {
101 BitSet32 pointers;
102 for (size_t i = 0; i < positions.size(); i++) {
103 if (positions[i].isValid()) {
104 pointers.markBit(i);
105 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100106 }
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700107 return pointers;
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100108}
109
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700110static uint32_t getChangingPointerId(BitSet32 pointers, BitSet32 otherPointers) {
111 BitSet32 difference(pointers.value ^ otherPointers.value);
112 uint32_t pointerId = difference.clearFirstMarkedBit();
113 EXPECT_EQ(0U, difference.value) << "Only 1 pointer can enter or leave at a time";
114 return pointerId;
115}
116
117static int32_t resolveAction(const std::vector<Position>& lastPositions,
118 const std::vector<Position>& currentPositions,
119 const std::vector<Position>& nextPositions) {
120 BitSet32 pointers = getValidPointers(currentPositions);
121 const uint32_t pointerCount = pointers.count();
122
123 BitSet32 lastPointers = getValidPointers(lastPositions);
124 const uint32_t lastPointerCount = lastPointers.count();
125 if (lastPointerCount < pointerCount) {
126 // A new pointer is down
127 uint32_t pointerId = getChangingPointerId(pointers, lastPointers);
128 return AMOTION_EVENT_ACTION_POINTER_DOWN |
129 (pointerId << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
130 }
131
132 BitSet32 nextPointers = getValidPointers(nextPositions);
133 const uint32_t nextPointerCount = nextPointers.count();
134 if (pointerCount > nextPointerCount) {
135 // An existing pointer is leaving
136 uint32_t pointerId = getChangingPointerId(pointers, nextPointers);
137 return AMOTION_EVENT_ACTION_POINTER_UP |
138 (pointerId << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
139 }
140
141 return AMOTION_EVENT_ACTION_MOVE;
142}
143
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700144static std::vector<MotionEvent> createAxisScrollMotionEventStream(
145 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions) {
146 std::vector<MotionEvent> events;
147 for (const auto& [timeStamp, value] : motions) {
148 EXPECT_TRUE(!isnan(value)) << "The entry at pointerId must be valid";
149
150 PointerCoords coords[1];
151 coords[0].setAxisValue(AMOTION_EVENT_AXIS_SCROLL, value);
152
153 PointerProperties properties[1];
154 properties[0].id = DEFAULT_POINTER_ID;
155
156 MotionEvent event;
157 ui::Transform identityTransform;
Harry Cutts82c791c2023-03-10 17:15:07 +0000158 event.initialize(InputEvent::nextId(), /*deviceId=*/5, AINPUT_SOURCE_ROTARY_ENCODER,
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700159 ADISPLAY_ID_NONE, INVALID_HMAC, AMOTION_EVENT_ACTION_SCROLL,
Harry Cutts82c791c2023-03-10 17:15:07 +0000160 /*actionButton=*/0, /*flags=*/0, AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE,
161 /*buttonState=*/0, MotionClassification::NONE, identityTransform,
162 /*xPrecision=*/0, /*yPrecision=*/0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
163 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, /*downTime=*/0,
164 timeStamp.count(), /*pointerCount=*/1, properties, coords);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700165
166 events.emplace_back(event);
167 }
168
169 return events;
170}
171
172static std::vector<MotionEvent> createTouchMotionEventStream(
173 const std::vector<PlanarMotionEventEntry>& motions) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700174 if (motions.empty()) {
175 ADD_FAILURE() << "Need at least 1 sample to create a MotionEvent. Received empty vector.";
176 }
177
178 std::vector<MotionEvent> events;
179 for (size_t i = 0; i < motions.size(); i++) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700180 const PlanarMotionEventEntry& entry = motions[i];
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700181 BitSet32 pointers = getValidPointers(entry.positions);
182 const uint32_t pointerCount = pointers.count();
183
184 int32_t action;
185 if (i == 0) {
186 action = AMOTION_EVENT_ACTION_DOWN;
187 EXPECT_EQ(1U, pointerCount) << "First event should only have 1 pointer";
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000188 } else if ((i == motions.size() - 1) && pointerCount == 1) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700189 action = AMOTION_EVENT_ACTION_UP;
190 } else {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700191 const PlanarMotionEventEntry& previousEntry = motions[i-1];
192 const PlanarMotionEventEntry& nextEntry = motions[i+1];
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700193 action = resolveAction(previousEntry.positions, entry.positions, nextEntry.positions);
194 }
195
196 PointerCoords coords[pointerCount];
197 PointerProperties properties[pointerCount];
198 uint32_t pointerIndex = 0;
199 while(!pointers.isEmpty()) {
200 uint32_t pointerId = pointers.clearFirstMarkedBit();
201
202 coords[pointerIndex].clear();
203 // We are treating column positions as pointerId
Siarhei Vishniakouc36d21e2023-01-17 09:59:38 -0800204 const Position& position = entry.positions[pointerId];
205 EXPECT_TRUE(position.isValid()) << "The entry at " << pointerId << " must be valid";
206 coords[pointerIndex].setAxisValue(AMOTION_EVENT_AXIS_X, position.x);
207 coords[pointerIndex].setAxisValue(AMOTION_EVENT_AXIS_Y, position.y);
208 coords[pointerIndex].isResampled = position.isResampled;
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700209
210 properties[pointerIndex].id = pointerId;
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -0700211 properties[pointerIndex].toolType = ToolType::FINGER;
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700212 pointerIndex++;
213 }
214 EXPECT_EQ(pointerIndex, pointerCount);
215
216 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -0700217 ui::Transform identityTransform;
Harry Cutts82c791c2023-03-10 17:15:07 +0000218 event.initialize(InputEvent::nextId(), /*deviceId=*/0, AINPUT_SOURCE_TOUCHSCREEN,
219 DISPLAY_ID, INVALID_HMAC, action, /*actionButton=*/0, /*flags=*/0,
220 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, /*buttonState=*/0,
221 MotionClassification::NONE, identityTransform, /*xPrecision=*/0,
222 /*yPrecision=*/0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
223 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, /*downTime=*/0,
Evan Rosky09576692021-07-01 12:22:09 -0700224 entry.eventTime.count(), pointerCount, properties, coords);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700225
226 events.emplace_back(event);
227 }
228
229 return events;
230}
231
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700232static std::optional<float> computePlanarVelocity(
233 const VelocityTracker::Strategy strategy,
234 const std::vector<PlanarMotionEventEntry>& motions, int32_t axis,
235 uint32_t pointerId = DEFAULT_POINTER_ID) {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700236 VelocityTracker vt(strategy);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100237
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700238 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700239 for (MotionEvent event : events) {
240 vt.addMovement(&event);
241 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100242
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700243 return vt.getVelocity(axis, pointerId);
244}
245
246static std::vector<MotionEvent> createMotionEventStream(
247 int32_t axis, const std::vector<std::pair<std::chrono::nanoseconds, float>>& motion) {
248 switch (axis) {
249 case AMOTION_EVENT_AXIS_SCROLL:
250 return createAxisScrollMotionEventStream(motion);
251 default:
252 ADD_FAILURE() << "Axis " << axis << " is not supported";
253 return {};
254 }
255}
256
257static std::optional<float> computeVelocity(
258 const VelocityTracker::Strategy strategy,
259 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions, int32_t axis) {
260 VelocityTracker vt(strategy);
261
262 for (const MotionEvent& event : createMotionEventStream(axis, motions)) {
263 vt.addMovement(&event);
264 }
265
266 return vt.getVelocity(axis, DEFAULT_POINTER_ID);
267}
268
269static void computeAndCheckVelocity(const VelocityTracker::Strategy strategy,
270 const std::vector<PlanarMotionEventEntry>& motions,
Yeabkal Wubshita15e5992022-09-29 19:00:19 -0700271 int32_t axis, std::optional<float> targetVelocity,
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700272 uint32_t pointerId = DEFAULT_POINTER_ID) {
Yeabkal Wubshita15e5992022-09-29 19:00:19 -0700273 checkVelocity(computePlanarVelocity(strategy, motions, axis, pointerId), targetVelocity);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100274}
275
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700276static void computeAndCheckAxisScrollVelocity(
277 const VelocityTracker::Strategy strategy,
278 const std::vector<std::pair<std::chrono::nanoseconds, float>>& motions,
279 std::optional<float> targetVelocity) {
Yeabkal Wubshita15e5992022-09-29 19:00:19 -0700280 checkVelocity(computeVelocity(strategy, motions, AMOTION_EVENT_AXIS_SCROLL), targetVelocity);
Siarhei Vishniakou8a2e5892023-08-14 13:59:40 -0700281 // The strategy LSQ2 is not compatible with AXIS_SCROLL. In those situations, we should fall
282 // back to a strategy that supports differential axes.
283 checkVelocity(computeVelocity(VelocityTracker::Strategy::LSQ2, motions,
284 AMOTION_EVENT_AXIS_SCROLL),
285 targetVelocity);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700286}
287
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800288static void computeAndCheckQuadraticVelocity(const std::vector<PlanarMotionEventEntry>& motions,
289 float velocity) {
Chris Yef8591482020-04-17 11:49:17 -0700290 VelocityTracker vt(VelocityTracker::Strategy::LSQ2);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700291 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700292 for (MotionEvent event : events) {
293 vt.addMovement(&event);
294 }
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800295 std::optional<float> velocityX = vt.getVelocity(AMOTION_EVENT_AXIS_X, 0);
296 std::optional<float> velocityY = vt.getVelocity(AMOTION_EVENT_AXIS_Y, 0);
297 ASSERT_TRUE(velocityX);
298 ASSERT_TRUE(velocityY);
299
300 EXPECT_NEAR_BY_FRACTION(*velocityX, velocity, QUADRATIC_VELOCITY_TOLERANCE);
301 EXPECT_NEAR_BY_FRACTION(*velocityY, velocity, QUADRATIC_VELOCITY_TOLERANCE);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700302}
303
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100304/*
Yeabkal Wubshiteca273c2022-10-05 19:06:40 -0700305 *================== VelocityTracker tests that do not require test motion data ====================
306 */
307TEST(SimpleVelocityTrackerTest, TestSupportedAxis) {
308 // Note that we are testing up to the max possible axis value, plus 3 more values. We are going
309 // beyond the max value to add a bit more protection. "3" is chosen arbitrarily to cover a few
310 // more values beyond the max.
311 for (int32_t axis = 0; axis <= AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE + 3; axis++) {
312 switch (axis) {
313 case AMOTION_EVENT_AXIS_X:
314 case AMOTION_EVENT_AXIS_Y:
315 case AMOTION_EVENT_AXIS_SCROLL:
316 EXPECT_TRUE(VelocityTracker::isAxisSupported(axis)) << axis << " is supported";
317 break;
318 default:
319 EXPECT_FALSE(VelocityTracker::isAxisSupported(axis)) << axis << " is NOT supported";
320 }
321 }
322}
323
324/*
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100325 * ================== VelocityTracker tests generated manually =====================================
326 */
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -0700327TEST_F(VelocityTrackerTest, TestDefaultStrategiesForPlanarAxes) {
328 std::vector<PlanarMotionEventEntry> motions = {{10ms, {{2, 4}}},
329 {20ms, {{4, 12}}},
330 {30ms, {{6, 20}}},
331 {40ms, {{10, 30}}}};
332
333 EXPECT_EQ(computePlanarVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X),
334 computePlanarVelocity(VelocityTracker::Strategy::DEFAULT, motions,
335 AMOTION_EVENT_AXIS_X));
336 EXPECT_EQ(computePlanarVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y),
337 computePlanarVelocity(VelocityTracker::Strategy::DEFAULT, motions,
338 AMOTION_EVENT_AXIS_Y));
339}
340
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000341TEST_F(VelocityTrackerTest, TestComputedVelocity) {
342 VelocityTracker::ComputedVelocity computedVelocity;
343
Harry Cutts82c791c2023-03-10 17:15:07 +0000344 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, /*id=*/0, /*velocity=*/200);
345 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, /*id=*/26U, /*velocity=*/400);
346 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, /*id=*/27U, /*velocity=*/650);
347 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID, /*velocity=*/750);
348 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, /*id=*/0, /*velocity=*/1000);
349 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, /*id=*/26U, /*velocity=*/2000);
350 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, /*id=*/27U, /*velocity=*/3000);
351 computedVelocity.addVelocity(AMOTION_EVENT_AXIS_Y, MAX_POINTER_ID, /*velocity=*/4000);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000352
353 // Check the axes/indices with velocity.
Harry Cutts82c791c2023-03-10 17:15:07 +0000354 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, /*id=*/0U)), 200);
355 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, /*id=*/26U)), 400);
356 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, /*id=*/27U)), 650);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000357 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID)), 750);
Harry Cutts82c791c2023-03-10 17:15:07 +0000358 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, /*id=*/0U)), 1000);
359 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, /*id=*/26U)), 2000);
360 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, /*id=*/27U)), 3000);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000361 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, MAX_POINTER_ID)), 4000);
362 for (uint32_t id = 0; id <= MAX_POINTER_ID; id++) {
363 // Since no data was added for AXIS_SCROLL, expect empty value for the axis for any id.
364 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_SCROLL, id))
365 << "Empty scroll data expected at id=" << id;
366 if (id == 0 || id == 26U || id == 27U || id == MAX_POINTER_ID) {
367 // Already checked above; continue.
368 continue;
369 }
370 // No data was added to X/Y for this id, expect empty value.
371 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, id))
372 << "Empty X data expected at id=" << id;
373 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, id))
374 << "Empty Y data expected at id=" << id;
375 }
376 // Out-of-bounds ids should given empty values.
377 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, -1));
378 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, MAX_POINTER_ID + 1));
379}
380
Siarhei Vishniakouc36d21e2023-01-17 09:59:38 -0800381/**
382 * For a single pointer, the resampled data is ignored.
383 */
384TEST_F(VelocityTrackerTest, SinglePointerResampledData) {
385 std::vector<PlanarMotionEventEntry> motions = {{10ms, {{1, 2}}},
386 {20ms, {{2, 4}}},
387 {30ms, {{3, 6}}},
388 {35ms, {{30, 60, .isResampled = true}}},
389 {40ms, {{4, 8}}}};
390
391 computeAndCheckVelocity(VelocityTracker::Strategy::DEFAULT, motions, AMOTION_EVENT_AXIS_X, 100);
392 computeAndCheckVelocity(VelocityTracker::Strategy::DEFAULT, motions, AMOTION_EVENT_AXIS_Y, 200);
393}
394
395/**
396 * For multiple pointers, the resampled data is ignored on a per-pointer basis. If a certain pointer
397 * does not have a resampled value, all of the points are used.
398 */
399TEST_F(VelocityTrackerTest, MultiPointerResampledData) {
400 std::vector<PlanarMotionEventEntry> motions = {
401 {0ms, {{0, 0}}},
402 {10ms, {{1, 0}, {1, 0}}},
403 {20ms, {{2, 0}, {2, 0}}},
404 {30ms, {{3, 0}, {3, 0}}},
405 {35ms, {{30, 0, .isResampled = true}, {30, 0}}},
406 {40ms, {{4, 0}, {4, 0}}},
407 {45ms, {{5, 0}}}, // ACTION_UP
408 };
409
410 // Sample at t=35ms breaks trend. It's marked as resampled for the first pointer, so it should
411 // be ignored, and the resulting velocity should be linear. For the second pointer, it's not
412 // resampled, so it should cause the velocity to be non-linear.
413 computeAndCheckVelocity(VelocityTracker::Strategy::DEFAULT, motions, AMOTION_EVENT_AXIS_X, 100,
414 /*pointerId=*/0);
415 computeAndCheckVelocity(VelocityTracker::Strategy::DEFAULT, motions, AMOTION_EVENT_AXIS_X, 3455,
416 /*pointerId=*/1);
417}
418
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000419TEST_F(VelocityTrackerTest, TestGetComputedVelocity) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700420 std::vector<PlanarMotionEventEntry> motions = {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000421 {235089067457000ns, {{528.00, 0}}}, {235089084684000ns, {{527.00, 0}}},
422 {235089093349000ns, {{527.00, 0}}}, {235089095677625ns, {{527.00, 0}}},
423 {235089101859000ns, {{527.00, 0}}}, {235089110378000ns, {{528.00, 0}}},
424 {235089112497111ns, {{528.25, 0}}}, {235089118760000ns, {{531.00, 0}}},
425 {235089126686000ns, {{535.00, 0}}}, {235089129316820ns, {{536.33, 0}}},
426 {235089135199000ns, {{540.00, 0}}}, {235089144297000ns, {{546.00, 0}}},
427 {235089146136443ns, {{547.21, 0}}}, {235089152923000ns, {{553.00, 0}}},
428 {235089160784000ns, {{559.00, 0}}}, {235089162955851ns, {{560.66, 0}}},
429 {235089162955851ns, {{560.66, 0}}}, // ACTION_UP
430 };
431 VelocityTracker vt(VelocityTracker::Strategy::IMPULSE);
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700432 std::vector<MotionEvent> events = createTouchMotionEventStream(motions);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000433 for (const MotionEvent& event : events) {
434 vt.addMovement(&event);
435 }
436
437 float maxFloat = std::numeric_limits<float>::max();
438 VelocityTracker::ComputedVelocity computedVelocity;
Harry Cutts82c791c2023-03-10 17:15:07 +0000439 computedVelocity = vt.getComputedVelocity(/*units=*/1000, maxFloat);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000440 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)),
441 764.345703);
442
443 // Expect X velocity to be scaled with respective to provided units.
Harry Cutts82c791c2023-03-10 17:15:07 +0000444 computedVelocity = vt.getComputedVelocity(/*units=*/1000000, maxFloat);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000445 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)),
446 764345.703);
447
448 // Expect X velocity to be clamped by provided max velocity.
Harry Cutts82c791c2023-03-10 17:15:07 +0000449 computedVelocity = vt.getComputedVelocity(/*units=*/1000000, 1000);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000450 checkVelocity(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID)), 1000);
451
452 // All 0 data for Y; expect 0 velocity.
453 EXPECT_EQ(*(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, DEFAULT_POINTER_ID)), 0);
454
455 // No data for scroll-axis; expect empty velocity.
456 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_SCROLL, DEFAULT_POINTER_ID));
457}
458
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700459TEST_F(VelocityTrackerTest, TestApiInteractionsWithNoMotionEvents) {
460 VelocityTracker vt(VelocityTracker::Strategy::DEFAULT);
461
462 EXPECT_FALSE(vt.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID));
463
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700464 VelocityTracker::ComputedVelocity computedVelocity = vt.getComputedVelocity(1000, 1000);
465 for (uint32_t id = 0; id <= MAX_POINTER_ID; id++) {
466 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_X, id));
467 EXPECT_FALSE(computedVelocity.getVelocity(AMOTION_EVENT_AXIS_Y, id));
468 }
469
470 EXPECT_EQ(-1, vt.getActivePointerId());
471
472 // Make sure that the clearing functions execute without an issue.
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800473 vt.clearPointer(7U);
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700474 vt.clear();
475}
476
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700477TEST_F(VelocityTrackerTest, ThreePointsPositiveVelocityTest) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100478 // Same coordinate is reported 2 times in a row
479 // It is difficult to determine the correct answer here, but at least the direction
480 // of the reported velocity should be positive.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700481 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700482 {0ms, {{273, 0}}},
483 {12585us, {{293, 0}}},
484 {14730us, {{293, 0}}},
485 {14730us, {{293, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100486 };
Chris Yef8591482020-04-17 11:49:17 -0700487 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
488 1600);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100489}
490
491TEST_F(VelocityTrackerTest, ThreePointsZeroVelocityTest) {
492 // Same coordinate is reported 3 times in a row
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700493 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700494 {0ms, {{293, 0}}},
495 {6132us, {{293, 0}}},
496 {11283us, {{293, 0}}},
497 {11283us, {{293, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100498 };
Chris Yef8591482020-04-17 11:49:17 -0700499 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 0);
500 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 0);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100501}
502
503TEST_F(VelocityTrackerTest, ThreePointsLinearVelocityTest) {
504 // Fixed velocity at 5 points per 10 milliseconds
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700505 std::vector<PlanarMotionEventEntry> motions = {
chaviw9eaa22c2020-07-01 16:21:27 -0700506 {0ms, {{0, 0}}}, {10ms, {{5, 0}}}, {20ms, {{10, 0}}}, {20ms, {{10, 0}}}, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100507 };
Chris Yef8591482020-04-17 11:49:17 -0700508 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X, 500);
509 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 500);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100510}
511
512
513/**
514 * ================== VelocityTracker tests generated by recording real events =====================
515 *
516 * To add a test, record the input coordinates and event times to all calls
517 * to void VelocityTracker::addMovement(const MotionEvent* event).
518 * Also record all calls to VelocityTracker::clear().
519 * Finally, record the output of VelocityTracker::getVelocity(...)
520 * This will give you the necessary data to create a new test.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700521 *
522 * Another good way to generate this data is to use 'dumpsys input' just after the event has
523 * occurred.
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100524 */
525
526// --------------- Recorded by hand on swordfish ---------------------------------------------------
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700527TEST_F(VelocityTrackerTest, SwordfishFlingDown) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100528 // Recording of a fling on Swordfish that could cause a fling in the wrong direction
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700529 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700530 { 0ms, {{271, 96}} },
531 { 16071042ns, {{269.786346, 106.922775}} },
532 { 35648403ns, {{267.983063, 156.660034}} },
533 { 52313925ns, {{262.638397, 220.339081}} },
534 { 68976522ns, {{266.138824, 331.581116}} },
535 { 85639375ns, {{274.79245, 428.113159}} },
536 { 96948871ns, {{274.79245, 428.113159}} },
537 { 96948871ns, {{274.79245, 428.113159}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100538 };
Chris Yef8591482020-04-17 11:49:17 -0700539 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
540 623.577637);
541 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
542 5970.7309);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100543}
544
545// --------------- Recorded by hand on sailfish, generated by a script -----------------------------
546// For some of these tests, the X-direction velocity checking has been removed, because the lsq2
547// and the impulse VelocityTrackerStrategies did not agree within 20%.
548// Since the flings were recorded in the Y-direction, the intentional user action should only
549// be relevant for the Y axis.
550// There have been also cases where lsq2 and impulse disagreed more than 20% in the Y-direction.
551// Those recordings have been discarded because we didn't feel one strategy's interpretation was
552// more correct than another's but didn't want to increase the tolerance for the entire test suite.
553//
554// There are 18 tests total below: 9 in the positive Y direction and 9 in the opposite.
555// The recordings were loosely binned into 3 categories - slow, faster, and fast, which roughly
556// characterizes the velocity of the finger motion.
557// These can be treated approximately as:
558// slow - less than 1 page gets scrolled
559// faster - more than 1 page gets scrolled, but less than 3
560// fast - entire list is scrolled (fling is done as hard as possible)
561
562TEST_F(VelocityTrackerTest, SailfishFlingUpSlow1) {
563 // Sailfish - fling up - slow - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700564 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700565 { 235089067457000ns, {{528.00, 983.00}} },
566 { 235089084684000ns, {{527.00, 981.00}} },
567 { 235089093349000ns, {{527.00, 977.00}} },
568 { 235089095677625ns, {{527.00, 975.93}} },
569 { 235089101859000ns, {{527.00, 970.00}} },
570 { 235089110378000ns, {{528.00, 960.00}} },
571 { 235089112497111ns, {{528.25, 957.51}} },
572 { 235089118760000ns, {{531.00, 946.00}} },
573 { 235089126686000ns, {{535.00, 931.00}} },
574 { 235089129316820ns, {{536.33, 926.02}} },
575 { 235089135199000ns, {{540.00, 914.00}} },
576 { 235089144297000ns, {{546.00, 896.00}} },
577 { 235089146136443ns, {{547.21, 892.36}} },
578 { 235089152923000ns, {{553.00, 877.00}} },
579 { 235089160784000ns, {{559.00, 851.00}} },
580 { 235089162955851ns, {{560.66, 843.82}} },
581 { 235089162955851ns, {{560.66, 843.82}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100582 };
Chris Yef8591482020-04-17 11:49:17 -0700583 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700584 764.345703);
Chris Yef8591482020-04-17 11:49:17 -0700585 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
586 951.698181);
587 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
588 -3604.819336);
589 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
590 -3044.966064);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100591}
592
593
594TEST_F(VelocityTrackerTest, SailfishFlingUpSlow2) {
595 // Sailfish - fling up - slow - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700596 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700597 { 235110560704000ns, {{522.00, 1107.00}} },
598 { 235110575764000ns, {{522.00, 1107.00}} },
599 { 235110584385000ns, {{522.00, 1107.00}} },
600 { 235110588421179ns, {{521.52, 1106.52}} },
601 { 235110592830000ns, {{521.00, 1106.00}} },
602 { 235110601385000ns, {{520.00, 1104.00}} },
603 { 235110605088160ns, {{519.14, 1102.27}} },
604 { 235110609952000ns, {{518.00, 1100.00}} },
605 { 235110618353000ns, {{517.00, 1093.00}} },
606 { 235110621755146ns, {{516.60, 1090.17}} },
607 { 235110627010000ns, {{517.00, 1081.00}} },
608 { 235110634785000ns, {{518.00, 1063.00}} },
609 { 235110638422450ns, {{518.87, 1052.58}} },
610 { 235110643161000ns, {{520.00, 1039.00}} },
611 { 235110651767000ns, {{524.00, 1011.00}} },
612 { 235110655089581ns, {{525.54, 1000.19}} },
613 { 235110660368000ns, {{530.00, 980.00}} },
614 { 235110660368000ns, {{530.00, 980.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100615 };
Chris Yef8591482020-04-17 11:49:17 -0700616 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
617 -4096.583008);
618 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
619 -3455.094238);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100620}
621
622
623TEST_F(VelocityTrackerTest, SailfishFlingUpSlow3) {
624 // Sailfish - fling up - slow - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700625 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700626 { 792536237000ns, {{580.00, 1317.00}} },
627 { 792541538987ns, {{580.63, 1311.94}} },
628 { 792544613000ns, {{581.00, 1309.00}} },
629 { 792552301000ns, {{583.00, 1295.00}} },
630 { 792558362309ns, {{585.13, 1282.92}} },
631 { 792560828000ns, {{586.00, 1278.00}} },
632 { 792569446000ns, {{589.00, 1256.00}} },
633 { 792575185095ns, {{591.54, 1241.41}} },
634 { 792578491000ns, {{593.00, 1233.00}} },
635 { 792587044000ns, {{597.00, 1211.00}} },
636 { 792592008172ns, {{600.28, 1195.92}} },
637 { 792594616000ns, {{602.00, 1188.00}} },
638 { 792603129000ns, {{607.00, 1167.00}} },
639 { 792608831290ns, {{609.48, 1155.83}} },
640 { 792612321000ns, {{611.00, 1149.00}} },
641 { 792620768000ns, {{615.00, 1131.00}} },
642 { 792625653873ns, {{617.32, 1121.73}} },
643 { 792629200000ns, {{619.00, 1115.00}} },
644 { 792629200000ns, {{619.00, 1115.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100645 };
Chris Yef8591482020-04-17 11:49:17 -0700646 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
647 574.33429);
648 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
649 617.40564);
650 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
651 -2361.982666);
652 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
653 -2500.055664);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100654}
655
656
657TEST_F(VelocityTrackerTest, SailfishFlingUpFaster1) {
658 // Sailfish - fling up - faster - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700659 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700660 { 235160420675000ns, {{610.00, 1042.00}} },
661 { 235160428220000ns, {{609.00, 1026.00}} },
662 { 235160436544000ns, {{609.00, 1024.00}} },
663 { 235160441852394ns, {{609.64, 1020.82}} },
664 { 235160444878000ns, {{610.00, 1019.00}} },
665 { 235160452673000ns, {{613.00, 1006.00}} },
666 { 235160458519743ns, {{617.18, 992.06}} },
667 { 235160461061000ns, {{619.00, 986.00}} },
668 { 235160469798000ns, {{627.00, 960.00}} },
669 { 235160475186713ns, {{632.22, 943.02}} },
670 { 235160478051000ns, {{635.00, 934.00}} },
671 { 235160486489000ns, {{644.00, 906.00}} },
672 { 235160491853697ns, {{649.56, 890.56}} },
673 { 235160495177000ns, {{653.00, 881.00}} },
674 { 235160504148000ns, {{662.00, 858.00}} },
675 { 235160509231495ns, {{666.81, 845.37}} },
676 { 235160512603000ns, {{670.00, 837.00}} },
677 { 235160520366000ns, {{679.00, 814.00}} },
678 { 235160520366000ns, {{679.00, 814.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100679 };
Chris Yef8591482020-04-17 11:49:17 -0700680 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
681 1274.141724);
682 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
683 1438.53186);
684 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
685 -3001.4348);
686 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
687 -3695.859619);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100688}
689
690
691TEST_F(VelocityTrackerTest, SailfishFlingUpFaster2) {
692 // Sailfish - fling up - faster - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700693 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700694 { 847153808000ns, {{576.00, 1264.00}} },
695 { 847171174000ns, {{576.00, 1262.00}} },
696 { 847179640000ns, {{576.00, 1257.00}} },
697 { 847185187540ns, {{577.41, 1249.22}} },
698 { 847187487000ns, {{578.00, 1246.00}} },
699 { 847195710000ns, {{581.00, 1227.00}} },
700 { 847202027059ns, {{583.93, 1209.40}} },
701 { 847204324000ns, {{585.00, 1203.00}} },
702 { 847212672000ns, {{590.00, 1176.00}} },
703 { 847218861395ns, {{594.36, 1157.11}} },
704 { 847221190000ns, {{596.00, 1150.00}} },
705 { 847230484000ns, {{602.00, 1124.00}} },
706 { 847235701400ns, {{607.56, 1103.83}} },
707 { 847237986000ns, {{610.00, 1095.00}} },
708 { 847237986000ns, {{610.00, 1095.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100709 };
Chris Yef8591482020-04-17 11:49:17 -0700710 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
711 -4280.07959);
712 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
713 -4241.004395);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100714}
715
716
717TEST_F(VelocityTrackerTest, SailfishFlingUpFaster3) {
718 // Sailfish - fling up - faster - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700719 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700720 { 235200532789000ns, {{507.00, 1084.00}} },
721 { 235200549221000ns, {{507.00, 1083.00}} },
722 { 235200557841000ns, {{507.00, 1081.00}} },
723 { 235200558051189ns, {{507.00, 1080.95}} },
724 { 235200566314000ns, {{507.00, 1078.00}} },
725 { 235200574876586ns, {{508.97, 1070.12}} },
726 { 235200575006000ns, {{509.00, 1070.00}} },
727 { 235200582900000ns, {{514.00, 1054.00}} },
728 { 235200591276000ns, {{525.00, 1023.00}} },
729 { 235200591701829ns, {{525.56, 1021.42}} },
730 { 235200600064000ns, {{542.00, 976.00}} },
731 { 235200608519000ns, {{563.00, 911.00}} },
732 { 235200608527086ns, {{563.02, 910.94}} },
733 { 235200616933000ns, {{590.00, 844.00}} },
734 { 235200616933000ns, {{590.00, 844.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100735 };
Chris Yef8591482020-04-17 11:49:17 -0700736 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
737 -8715.686523);
738 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
739 -7639.026367);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100740}
741
742
743TEST_F(VelocityTrackerTest, SailfishFlingUpFast1) {
744 // Sailfish - fling up - fast - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700745 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700746 { 920922149000ns, {{561.00, 1412.00}} },
747 { 920930185000ns, {{559.00, 1377.00}} },
748 { 920930262463ns, {{558.98, 1376.66}} },
749 { 920938547000ns, {{559.00, 1371.00}} },
750 { 920947096857ns, {{562.91, 1342.68}} },
751 { 920947302000ns, {{563.00, 1342.00}} },
752 { 920955502000ns, {{577.00, 1272.00}} },
753 { 920963931021ns, {{596.87, 1190.54}} },
754 { 920963987000ns, {{597.00, 1190.00}} },
755 { 920972530000ns, {{631.00, 1093.00}} },
756 { 920980765511ns, {{671.31, 994.68}} },
757 { 920980906000ns, {{672.00, 993.00}} },
758 { 920989261000ns, {{715.00, 903.00}} },
759 { 920989261000ns, {{715.00, 903.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100760 };
Chris Yef8591482020-04-17 11:49:17 -0700761 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
762 5670.329102);
763 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
764 5991.866699);
765 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
766 -13021.101562);
767 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
768 -15093.995117);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100769}
770
771
772TEST_F(VelocityTrackerTest, SailfishFlingUpFast2) {
773 // Sailfish - fling up - fast - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700774 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700775 { 235247153233000ns, {{518.00, 1168.00}} },
776 { 235247170452000ns, {{517.00, 1167.00}} },
777 { 235247178908000ns, {{515.00, 1159.00}} },
778 { 235247179556213ns, {{514.85, 1158.39}} },
779 { 235247186821000ns, {{515.00, 1125.00}} },
780 { 235247195265000ns, {{521.00, 1051.00}} },
781 { 235247196389476ns, {{521.80, 1041.15}} },
782 { 235247203649000ns, {{538.00, 932.00}} },
783 { 235247212253000ns, {{571.00, 794.00}} },
784 { 235247213222491ns, {{574.72, 778.45}} },
785 { 235247220736000ns, {{620.00, 641.00}} },
786 { 235247220736000ns, {{620.00, 641.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100787 };
Chris Yef8591482020-04-17 11:49:17 -0700788 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
789 -20286.958984);
790 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
791 -20494.587891);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100792}
793
794
795TEST_F(VelocityTrackerTest, SailfishFlingUpFast3) {
796 // Sailfish - fling up - fast - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700797 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700798 { 235302568736000ns, {{529.00, 1167.00}} },
799 { 235302576644000ns, {{523.00, 1140.00}} },
800 { 235302579395063ns, {{520.91, 1130.61}} },
801 { 235302585140000ns, {{522.00, 1130.00}} },
802 { 235302593615000ns, {{527.00, 1065.00}} },
803 { 235302596207444ns, {{528.53, 1045.12}} },
804 { 235302602102000ns, {{559.00, 872.00}} },
805 { 235302610545000ns, {{652.00, 605.00}} },
806 { 235302613019881ns, {{679.26, 526.73}} },
807 { 235302613019881ns, {{679.26, 526.73}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100808 };
Chris Yef8591482020-04-17 11:49:17 -0700809 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
810 -39295.941406);
811 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
812 -36461.421875);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100813}
814
815
816TEST_F(VelocityTrackerTest, SailfishFlingDownSlow1) {
817 // Sailfish - fling down - slow - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700818 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700819 { 235655749552755ns, {{582.00, 432.49}} },
820 { 235655750638000ns, {{582.00, 433.00}} },
821 { 235655758865000ns, {{582.00, 440.00}} },
822 { 235655766221523ns, {{581.16, 448.43}} },
823 { 235655767594000ns, {{581.00, 450.00}} },
824 { 235655776044000ns, {{580.00, 462.00}} },
825 { 235655782890696ns, {{579.18, 474.35}} },
826 { 235655784360000ns, {{579.00, 477.00}} },
827 { 235655792795000ns, {{578.00, 496.00}} },
828 { 235655799559531ns, {{576.27, 515.04}} },
829 { 235655800612000ns, {{576.00, 518.00}} },
830 { 235655809535000ns, {{574.00, 542.00}} },
831 { 235655816988015ns, {{572.17, 564.86}} },
832 { 235655817685000ns, {{572.00, 567.00}} },
833 { 235655825981000ns, {{569.00, 595.00}} },
834 { 235655833808653ns, {{566.26, 620.60}} },
835 { 235655834541000ns, {{566.00, 623.00}} },
836 { 235655842893000ns, {{563.00, 649.00}} },
837 { 235655842893000ns, {{563.00, 649.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100838 };
Chris Yef8591482020-04-17 11:49:17 -0700839 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
840 -419.749695);
841 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
842 -398.303894);
843 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
844 3309.016357);
845 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
846 3969.099854);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100847}
848
849
850TEST_F(VelocityTrackerTest, SailfishFlingDownSlow2) {
851 // Sailfish - fling down - slow - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700852 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700853 { 235671152083370ns, {{485.24, 558.28}} },
854 { 235671154126000ns, {{485.00, 559.00}} },
855 { 235671162497000ns, {{484.00, 566.00}} },
856 { 235671168750511ns, {{483.27, 573.29}} },
857 { 235671171071000ns, {{483.00, 576.00}} },
858 { 235671179390000ns, {{482.00, 588.00}} },
859 { 235671185417210ns, {{481.31, 598.98}} },
860 { 235671188173000ns, {{481.00, 604.00}} },
861 { 235671196371000ns, {{480.00, 624.00}} },
862 { 235671202084196ns, {{479.27, 639.98}} },
863 { 235671204235000ns, {{479.00, 646.00}} },
864 { 235671212554000ns, {{478.00, 673.00}} },
865 { 235671219471011ns, {{476.39, 697.12}} },
866 { 235671221159000ns, {{476.00, 703.00}} },
867 { 235671229592000ns, {{474.00, 734.00}} },
868 { 235671236281462ns, {{472.43, 758.38}} },
869 { 235671238098000ns, {{472.00, 765.00}} },
870 { 235671246532000ns, {{470.00, 799.00}} },
871 { 235671246532000ns, {{470.00, 799.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100872 };
Chris Yef8591482020-04-17 11:49:17 -0700873 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
874 -262.80426);
875 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
876 -243.665344);
877 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
878 4215.682129);
879 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
880 4587.986816);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100881}
882
883
884TEST_F(VelocityTrackerTest, SailfishFlingDownSlow3) {
885 // Sailfish - fling down - slow - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700886 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700887 { 170983201000ns, {{557.00, 533.00}} },
888 { 171000668000ns, {{556.00, 534.00}} },
889 { 171007359750ns, {{554.73, 535.27}} },
890 { 171011197000ns, {{554.00, 536.00}} },
891 { 171017660000ns, {{552.00, 540.00}} },
892 { 171024201831ns, {{549.97, 544.73}} },
893 { 171027333000ns, {{549.00, 547.00}} },
894 { 171034603000ns, {{545.00, 557.00}} },
895 { 171041043371ns, {{541.98, 567.55}} },
896 { 171043147000ns, {{541.00, 571.00}} },
897 { 171051052000ns, {{536.00, 586.00}} },
898 { 171051052000ns, {{536.00, 586.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100899 };
Chris Yef8591482020-04-17 11:49:17 -0700900 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
901 -723.413513);
902 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
903 -651.038452);
904 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
905 2091.502441);
906 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
907 1934.517456);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100908}
909
910
911TEST_F(VelocityTrackerTest, SailfishFlingDownFaster1) {
912 // Sailfish - fling down - faster - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700913 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700914 { 235695280333000ns, {{558.00, 451.00}} },
915 { 235695283971237ns, {{558.43, 454.45}} },
916 { 235695289038000ns, {{559.00, 462.00}} },
917 { 235695297388000ns, {{561.00, 478.00}} },
918 { 235695300638465ns, {{561.83, 486.25}} },
919 { 235695305265000ns, {{563.00, 498.00}} },
920 { 235695313591000ns, {{564.00, 521.00}} },
921 { 235695317305492ns, {{564.43, 532.68}} },
922 { 235695322181000ns, {{565.00, 548.00}} },
923 { 235695330709000ns, {{565.00, 577.00}} },
924 { 235695333972227ns, {{565.00, 588.10}} },
925 { 235695339250000ns, {{565.00, 609.00}} },
926 { 235695347839000ns, {{565.00, 642.00}} },
927 { 235695351313257ns, {{565.00, 656.18}} },
928 { 235695356412000ns, {{565.00, 677.00}} },
929 { 235695364899000ns, {{563.00, 710.00}} },
930 { 235695368118682ns, {{562.24, 722.52}} },
931 { 235695373403000ns, {{564.00, 744.00}} },
932 { 235695373403000ns, {{564.00, 744.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100933 };
Chris Yef8591482020-04-17 11:49:17 -0700934 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
935 4254.639648);
936 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
937 4698.415039);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100938}
939
940
941TEST_F(VelocityTrackerTest, SailfishFlingDownFaster2) {
942 // Sailfish - fling down - faster - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700943 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700944 { 235709624766000ns, {{535.00, 579.00}} },
945 { 235709642256000ns, {{534.00, 580.00}} },
946 { 235709643350278ns, {{533.94, 580.06}} },
947 { 235709650760000ns, {{532.00, 584.00}} },
948 { 235709658615000ns, {{530.00, 593.00}} },
949 { 235709660170495ns, {{529.60, 594.78}} },
950 { 235709667095000ns, {{527.00, 606.00}} },
951 { 235709675616000ns, {{524.00, 628.00}} },
952 { 235709676983261ns, {{523.52, 631.53}} },
953 { 235709684289000ns, {{521.00, 652.00}} },
954 { 235709692763000ns, {{518.00, 682.00}} },
955 { 235709693804993ns, {{517.63, 685.69}} },
956 { 235709701438000ns, {{515.00, 709.00}} },
957 { 235709709830000ns, {{512.00, 739.00}} },
958 { 235709710626776ns, {{511.72, 741.85}} },
959 { 235709710626776ns, {{511.72, 741.85}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100960 };
Chris Yef8591482020-04-17 11:49:17 -0700961 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
962 -430.440247);
963 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
964 -447.600311);
965 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
966 3953.859375);
967 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
968 4316.155273);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100969}
970
971
972TEST_F(VelocityTrackerTest, SailfishFlingDownFaster3) {
973 // Sailfish - fling down - faster - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700974 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -0700975 { 235727628927000ns, {{540.00, 440.00}} },
976 { 235727636810000ns, {{537.00, 454.00}} },
977 { 235727646176000ns, {{536.00, 454.00}} },
978 { 235727653586628ns, {{535.12, 456.65}} },
979 { 235727654557000ns, {{535.00, 457.00}} },
980 { 235727663024000ns, {{534.00, 465.00}} },
981 { 235727670410103ns, {{533.04, 479.45}} },
982 { 235727670691000ns, {{533.00, 480.00}} },
983 { 235727679255000ns, {{531.00, 501.00}} },
984 { 235727687233704ns, {{529.09, 526.73}} },
985 { 235727687628000ns, {{529.00, 528.00}} },
986 { 235727696113000ns, {{526.00, 558.00}} },
987 { 235727704057546ns, {{523.18, 588.98}} },
988 { 235727704576000ns, {{523.00, 591.00}} },
989 { 235727713099000ns, {{520.00, 626.00}} },
990 { 235727720880776ns, {{516.33, 655.36}} },
991 { 235727721580000ns, {{516.00, 658.00}} },
992 { 235727721580000ns, {{516.00, 658.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100993 };
Chris Yef8591482020-04-17 11:49:17 -0700994 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
995 4484.617676);
996 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
997 4927.92627);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100998}
999
1000
1001TEST_F(VelocityTrackerTest, SailfishFlingDownFast1) {
1002 // Sailfish - fling down - fast - 1
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001003 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001004 { 235762352849000ns, {{467.00, 286.00}} },
1005 { 235762360250000ns, {{443.00, 344.00}} },
1006 { 235762362787412ns, {{434.77, 363.89}} },
1007 { 235762368807000ns, {{438.00, 359.00}} },
1008 { 235762377220000ns, {{425.00, 423.00}} },
1009 { 235762379608561ns, {{421.31, 441.17}} },
1010 { 235762385698000ns, {{412.00, 528.00}} },
1011 { 235762394133000ns, {{406.00, 648.00}} },
1012 { 235762396429369ns, {{404.37, 680.67}} },
1013 { 235762396429369ns, {{404.37, 680.67}} }, //ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001014 };
Chris Yef8591482020-04-17 11:49:17 -07001015 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
1016 14227.0224);
1017 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1018 16064.685547);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001019}
1020
1021
1022TEST_F(VelocityTrackerTest, SailfishFlingDownFast2) {
1023 // Sailfish - fling down - fast - 2
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001024 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001025 { 235772487188000ns, {{576.00, 204.00}} },
1026 { 235772495159000ns, {{553.00, 236.00}} },
1027 { 235772503568000ns, {{551.00, 240.00}} },
1028 { 235772508192247ns, {{545.55, 254.17}} },
1029 { 235772512051000ns, {{541.00, 266.00}} },
1030 { 235772520794000ns, {{520.00, 337.00}} },
1031 { 235772525015263ns, {{508.92, 394.43}} },
1032 { 235772529174000ns, {{498.00, 451.00}} },
1033 { 235772537635000ns, {{484.00, 589.00}} },
1034 { 235772537635000ns, {{484.00, 589.00}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001035 };
Chris Yef8591482020-04-17 11:49:17 -07001036 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
1037 18660.048828);
1038 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1039 16918.439453);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001040}
1041
1042
1043TEST_F(VelocityTrackerTest, SailfishFlingDownFast3) {
1044 // Sailfish - fling down - fast - 3
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001045 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001046 { 507650295000ns, {{628.00, 233.00}} },
1047 { 507658234000ns, {{605.00, 269.00}} },
1048 { 507666784000ns, {{601.00, 274.00}} },
1049 { 507669660483ns, {{599.65, 275.68}} },
1050 { 507675427000ns, {{582.00, 308.00}} },
1051 { 507683740000ns, {{541.00, 404.00}} },
1052 { 507686506238ns, {{527.36, 435.95}} },
1053 { 507692220000ns, {{487.00, 581.00}} },
1054 { 507700707000ns, {{454.00, 792.00}} },
1055 { 507703352649ns, {{443.71, 857.77}} },
1056 { 507703352649ns, {{443.71, 857.77}} }, // ACTION_UP
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001057 };
Chris Yef8591482020-04-17 11:49:17 -07001058 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1059 -4111.8173);
1060 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1061 -6388.48877);
1062 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
1063 29765.908203);
1064 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1065 28354.796875);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001066}
1067
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001068/**
1069 * ================== Multiple pointers ============================================================
1070 *
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001071 * Three fingers quickly tap the screen. Since this is a tap, the velocities should be empty.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001072 * If the events with POINTER_UP or POINTER_DOWN are not handled correctly (these should not be
1073 * part of the fitted data), this can cause large velocity values to be reported instead.
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001074 */
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001075TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategy_ThreeFingerTap) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001076 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001077 { 0us, {{1063, 1128}, {NAN, NAN}, {NAN, NAN}} },
1078 { 10800us, {{1063, 1128}, {682, 1318}, {NAN, NAN}} }, // POINTER_DOWN
1079 { 10800us, {{1063, 1128}, {682, 1318}, {397, 1747}} }, // POINTER_DOWN
1080 { 267300us, {{1063, 1128}, {682, 1318}, {397, 1747}} }, // POINTER_UP
1081 { 267300us, {{1063, 1128}, {NAN, NAN}, {397, 1747}} }, // POINTER_UP
1082 { 272700us, {{1063, 1128}, {NAN, NAN}, {NAN, NAN}} },
1083 };
1084
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001085 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1086 std::nullopt);
1087 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_Y,
1088 std::nullopt);
1089 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1090 std::nullopt);
1091 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_Y,
1092 std::nullopt);
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001093}
1094
1095/**
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001096 * ================= Pointer liftoff ===============================================================
1097 */
1098
1099/**
1100 * The last movement of a pointer is always ACTION_POINTER_UP or ACTION_UP. If there's a short delay
1101 * between the last ACTION_MOVE and the next ACTION_POINTER_UP or ACTION_UP, velocity should not be
1102 * affected by the liftoff.
1103 */
1104TEST_F(VelocityTrackerTest, ShortDelayBeforeActionUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001105 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001106 {0ms, {{10, 0}}}, {10ms, {{20, 0}}}, {20ms, {{30, 0}}}, {30ms, {{30, 0}}}, // ACTION_UP
1107 };
1108 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1109 1000);
1110 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X, 1000);
1111}
1112
1113/**
1114 * 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 -07001115 * ACTION_MOVE and the final ACTION_UP, velocity should be reported as empty because the pointer
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001116 * should be assumed to have stopped.
1117 */
1118TEST_F(VelocityTrackerTest, LongDelayBeforeActionUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001119 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001120 {0ms, {{10, 0}}},
1121 {10ms, {{20, 0}}},
1122 {20ms, {{30, 0}}},
1123 {3000ms, {{30, 0}}}, // ACTION_UP
1124 };
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001125 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1126 std::nullopt);
1127 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1128 std::nullopt);
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001129}
1130
1131/**
1132 * The last movement of a pointer is always ACTION_POINTER_UP or ACTION_UP. If there's a long delay
1133 * before ACTION_POINTER_UP event, the movement should be assumed to have stopped.
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001134 * The final velocity should be reported as empty for all pointers.
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001135 */
1136TEST_F(VelocityTrackerTest, LongDelayBeforeActionPointerUp) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001137 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001138 {0ms, {{10, 0}}},
1139 {10ms, {{20, 0}, {100, 0}}},
1140 {20ms, {{30, 0}, {200, 0}}},
1141 {30ms, {{30, 0}, {300, 0}}},
1142 {40ms, {{30, 0}, {400, 0}}},
1143 {3000ms, {{30, 0}}}, // ACTION_POINTER_UP
1144 };
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001145 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1146 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001147 /*pointerId*/ 0);
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001148 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1149 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001150 /*pointerId*/ 0);
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001151 computeAndCheckVelocity(VelocityTracker::Strategy::IMPULSE, motions, AMOTION_EVENT_AXIS_X,
1152 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001153 /*pointerId*/ 1);
Yeabkal Wubshita15e5992022-09-29 19:00:19 -07001154 computeAndCheckVelocity(VelocityTracker::Strategy::LSQ2, motions, AMOTION_EVENT_AXIS_X,
1155 std::nullopt,
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001156 /*pointerId*/ 1);
1157}
1158
1159/**
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001160 * ================== Tests for least squares fitting ==============================================
1161 *
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001162 * Special care must be taken when constructing tests for LeastSquaresVelocityTrackerStrategy
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001163 * getVelocity function. In particular:
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001164 * - inside the function, time gets converted from nanoseconds to seconds
1165 * before being used in the fit.
1166 * - any values that are older than 100 ms are being discarded.
1167 * - the newest time gets subtracted from all of the other times before being used in the fit.
1168 * So these tests have to be designed with those limitations in mind.
1169 *
1170 * General approach for the tests below:
1171 * We only used timestamps in milliseconds, 0 ms, 1 ms, and 2 ms, to be sure that
1172 * we are well within the HORIZON range.
1173 * When specifying the expected values of the coefficients, we treat the x values as if
1174 * they were in ms. Then, to adjust for the time units, the coefficients get progressively
1175 * multiplied by powers of 1E3.
1176 * For example:
1177 * data: t(ms), x
1178 * 1 ms, 1
1179 * 2 ms, 4
1180 * 3 ms, 9
1181 * The coefficients are (0, 0, 1).
1182 * In the test, we would convert these coefficients to (0*(1E3)^0, 0*(1E3)^1, 1*(1E3)^2).
1183 */
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001184TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategy_Constant) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001185 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001186 { 0ms, {{1, 1}} }, // 0 s
1187 { 1ms, {{1, 1}} }, // 0.001 s
1188 { 2ms, {{1, 1}} }, // 0.002 s
1189 { 2ms, {{1, 1}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001190 };
1191 // The data used for the fit will be as follows:
1192 // time(s), position
1193 // -0.002, 1
1194 // -0.001, 1
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001195 // -0.ms, 1
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001196 computeAndCheckQuadraticVelocity(motions, 0);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001197}
1198
1199/*
1200 * Straight line y = x :: the constant and quadratic coefficients are zero.
1201 */
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001202TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategy_Linear) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001203 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001204 { 0ms, {{-2, -2}} },
1205 { 1ms, {{-1, -1}} },
1206 { 2ms, {{-0, -0}} },
1207 { 2ms, {{-0, -0}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001208 };
1209 // The data used for the fit will be as follows:
1210 // time(s), position
1211 // -0.002, -2
1212 // -0.001, -1
1213 // -0.000, 0
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001214 computeAndCheckQuadraticVelocity(motions, 1E3);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001215}
1216
1217/*
1218 * Parabola
1219 */
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001220TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategy_Parabolic) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001221 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001222 { 0ms, {{1, 1}} },
1223 { 1ms, {{4, 4}} },
1224 { 2ms, {{8, 8}} },
1225 { 2ms, {{8, 8}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001226 };
1227 // The data used for the fit will be as follows:
1228 // time(s), position
1229 // -0.002, 1
1230 // -0.001, 4
1231 // -0.000, 8
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001232 computeAndCheckQuadraticVelocity(motions, 4.5E3);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001233}
1234
1235/*
1236 * Parabola
1237 */
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001238TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategy_Parabolic2) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001239 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001240 { 0ms, {{1, 1}} },
1241 { 1ms, {{4, 4}} },
1242 { 2ms, {{9, 9}} },
1243 { 2ms, {{9, 9}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001244 };
1245 // The data used for the fit will be as follows:
1246 // time(s), position
1247 // -0.002, 1
1248 // -0.001, 4
1249 // -0.000, 9
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001250 computeAndCheckQuadraticVelocity(motions, 6E3);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001251}
1252
1253/*
1254 * Parabola :: y = x^2 :: the constant and linear coefficients are zero.
1255 */
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001256TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategy_Parabolic3) {
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001257 std::vector<PlanarMotionEventEntry> motions = {
Siarhei Vishniakou651c1ea2019-04-09 14:12:41 -07001258 { 0ms, {{4, 4}} },
1259 { 1ms, {{1, 1}} },
1260 { 2ms, {{0, 0}} },
1261 { 2ms, {{0, 0}} }, // ACTION_UP
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001262 };
1263 // The data used for the fit will be as follows:
1264 // time(s), position
1265 // -0.002, 4
1266 // -0.001, 1
1267 // -0.000, 0
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -08001268 computeAndCheckQuadraticVelocity(motions, 0E3);
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -07001269}
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001270
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001271// Recorded by hand on sailfish, but only the diffs are taken to test cumulative axis velocity.
1272TEST_F(VelocityTrackerTest, AxisScrollVelocity) {
1273 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1274 {235089067457000ns, 0.00}, {235089084684000ns, -1.00}, {235089093349000ns, 0.00},
1275 {235089095677625ns, 0.00}, {235089101859000ns, 0.00}, {235089110378000ns, 0.00},
1276 {235089112497111ns, 0.25}, {235089118760000ns, 1.75}, {235089126686000ns, 4.00},
1277 {235089129316820ns, 1.33}, {235089135199000ns, 3.67}, {235089144297000ns, 6.00},
1278 {235089146136443ns, 1.21}, {235089152923000ns, 5.79}, {235089160784000ns, 6.00},
1279 {235089162955851ns, 1.66},
1280 };
1281
1282 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {764.345703});
1283}
1284
1285// --------------- Recorded by hand on a Wear OS device using a rotating side button ---------------
1286TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollDown) {
1287 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1288 {224598065152ns, -0.050100}, {224621871104ns, -0.133600}, {224645464064ns, -0.551100},
1289 {224669171712ns, -0.801600}, {224687063040ns, -1.035400}, {224706691072ns, -0.484300},
1290 {224738213888ns, -0.334000}, {224754401280ns, -0.083500},
1291 };
1292
1293 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {-27.86});
1294}
1295
1296TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollUp) {
1297 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1298 {269606010880ns, 0.050100}, {269626064896ns, 0.217100}, {269641973760ns, 0.267200},
1299 {269658079232ns, 0.267200}, {269674217472ns, 0.267200}, {269690683392ns, 0.367400},
1300 {269706133504ns, 0.551100}, {269722173440ns, 0.501000},
1301 };
1302
1303 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {31.92});
1304}
1305
1306TEST_F(VelocityTrackerTest, AxisScrollVelocity_ScrollDown_ThenUp_ThenDown) {
1307 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1308 {2580534001664ns, -0.033400}, {2580549992448ns, -0.133600},
1309 {2580566769664ns, -0.250500}, {2580581974016ns, -0.183700},
1310 {2580597964800ns, -0.267200}, {2580613955584ns, -0.551100},
1311 {2580635189248ns, -0.601200}, {2580661927936ns, -0.450900},
1312 {2580683161600ns, -0.417500}, {2580705705984ns, -0.150300},
1313 {2580722745344ns, -0.016700}, {2580786446336ns, 0.050100},
1314 {2580801912832ns, 0.150300}, {2580822360064ns, 0.300600},
1315 {2580838088704ns, 0.300600}, {2580854341632ns, 0.400800},
1316 {2580869808128ns, 0.517700}, {2580886061056ns, 0.501000},
1317 {2580905984000ns, 0.350700}, {2580921974784ns, 0.350700},
1318 {2580937965568ns, 0.066800}, {2580974665728ns, 0.016700},
1319 {2581034434560ns, -0.066800}, {2581049901056ns, -0.116900},
1320 {2581070610432ns, -0.317300}, {2581086076928ns, -0.200400},
1321 {2581101805568ns, -0.233800}, {2581118058496ns, -0.417500},
1322 {2581134049280ns, -0.417500}, {2581150040064ns, -0.367400},
1323 {2581166030848ns, -0.267200}, {2581181759488ns, -0.150300},
1324 {2581199847424ns, -0.066800},
1325 };
1326
1327 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {-9.73});
1328}
1329
1330// ------------------------------- Hand generated test cases ---------------------------------------
Yeabkal Wubshit871cc8a2022-09-26 17:46:28 -07001331TEST_F(VelocityTrackerTest, TestDefaultStrategyForAxisScroll) {
1332 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {
1333 {10ms, 20},
1334 {20ms, 25},
1335 {30ms, 50},
1336 {40ms, 100},
1337 };
1338
1339 EXPECT_EQ(computeVelocity(VelocityTracker::Strategy::IMPULSE, motions,
1340 AMOTION_EVENT_AXIS_SCROLL),
1341 computeVelocity(VelocityTracker::Strategy::DEFAULT, motions,
1342 AMOTION_EVENT_AXIS_SCROLL));
1343}
1344
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -07001345TEST_F(VelocityTrackerTest, AxisScrollVelocity_SimilarDifferentialValues) {
1346 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ns, 2.12}, {3ns, 2.12},
1347 {7ns, 2.12}, {8ns, 2.12},
1348 {15ns, 2.12}, {18ns, 2.12}};
1349
1350 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {1690236059.86});
1351}
1352
1353TEST_F(VelocityTrackerTest, AxisScrollVelocity_OnlyTwoValues) {
1354 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ms, 5}, {2ms, 10}};
1355
1356 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {10000});
1357}
1358
1359TEST_F(VelocityTrackerTest, AxisScrollVelocity_ConstantVelocity) {
1360 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ms, 20}, {2ms, 20},
1361 {3ms, 20}, {4ms, 20},
1362 {5ms, 20}, {6ms, 20}};
1363
1364 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {20000});
1365}
1366
1367TEST_F(VelocityTrackerTest, AxisScrollVelocity_NoMotion) {
1368 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {{1ns, 0}, {2ns, 0},
1369 {3ns, 0}, {4ns, 0},
1370 {5ns, 0}, {6ns, 0}};
1371
1372 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, {0});
1373}
1374
1375TEST_F(VelocityTrackerTest, AxisScrollVelocity_NoData) {
1376 std::vector<std::pair<std::chrono::nanoseconds, float>> motions = {};
1377
1378 computeAndCheckAxisScrollVelocity(VelocityTracker::Strategy::IMPULSE, motions, std::nullopt);
1379}
1380
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +01001381} // namespace android