blob: eb60a749eab5d5b5480bf8bfce01b09920dc7c4c [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
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070019#include <array>
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010020#include <math.h>
21
22#include <android-base/stringprintf.h>
23#include <gtest/gtest.h>
24#include <input/VelocityTracker.h>
25
26using android::base::StringPrintf;
27
28namespace android {
29
30constexpr int32_t DEFAULT_POINTER_ID = 0; // pointer ID used for manually defined tests
31
32// velocity must be in the range (1-tol)*EV <= velocity <= (1+tol)*EV
33// here EV = expected value, tol = VELOCITY_TOLERANCE
34constexpr float VELOCITY_TOLERANCE = 0.2;
35
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070036// estimate coefficients must be within 0.001% of the target value
37constexpr float COEFFICIENT_TOLERANCE = 0.00001;
38
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010039// --- VelocityTrackerTest ---
40class VelocityTrackerTest : public testing::Test { };
41
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070042/*
43 * Similar to EXPECT_NEAR, but ensures that the difference between the two float values
44 * is at most a certain fraction of the target value.
45 * If fraction is zero, require exact match.
46 */
47static void EXPECT_NEAR_BY_FRACTION(float actual, float target, float fraction) {
48 float tolerance = fabsf(target * fraction);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010049
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070050 if (target == 0 && fraction != 0) {
51 // If target is zero, this would force actual == target, which is too harsh.
52 // Relax this requirement a little. The value is determined empirically from the
53 // coefficients computed by the quadratic least squares algorithms.
54 tolerance = 1E-6;
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010055 }
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -070056 EXPECT_NEAR(actual, target, tolerance);
57}
58
59static void checkVelocity(float Vactual, float Vtarget) {
60 EXPECT_NEAR_BY_FRACTION(Vactual, Vtarget, VELOCITY_TOLERANCE);
61}
62
63static void checkCoefficient(float actual, float target) {
64 EXPECT_NEAR_BY_FRACTION(actual, target, COEFFICIENT_TOLERANCE);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010065}
66
Siarhei Vishniakouf1ec7ad2019-03-06 13:01:10 -080067static void failWithMessage(std::string message) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010068 FAIL() << message; // cannot do this directly from a non-void function
69}
70
71struct Position {
Siarhei Vishniakou87b58012019-03-06 13:22:13 -080072 nsecs_t time;
73 float x;
74 float y;
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010075};
76
Siarhei Vishniakou87b58012019-03-06 13:22:13 -080077static std::unique_ptr<MotionEvent> createSimpleMotionEvent(
78 const std::vector<Position>& positions) {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010079 /**
80 * Only populate the basic fields of a MotionEvent, such as time and a single axis
81 * Designed for use with manually-defined tests.
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010082 */
Siarhei Vishniakou87b58012019-03-06 13:22:13 -080083 if (positions.empty()) {
84 failWithMessage("Need at least 1 sample to create a MotionEvent. Received empty vector.");
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010085 }
86
Siarhei Vishniakouf1ec7ad2019-03-06 13:01:10 -080087 std::unique_ptr<MotionEvent> event = std::make_unique<MotionEvent>();
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010088
Siarhei Vishniakouf1ec7ad2019-03-06 13:01:10 -080089 constexpr size_t pointerCount = 1;
90 PointerCoords coords[pointerCount];
91 coords[0].clear();
92
93 PointerProperties properties[pointerCount];
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010094 properties[0].id = DEFAULT_POINTER_ID;
95 properties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
96
97 // First sample added separately with initialize
Siarhei Vishniakouf1ec7ad2019-03-06 13:01:10 -080098 coords[0].setAxisValue(AMOTION_EVENT_AXIS_X, positions[0].x);
99 coords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, positions[0].y);
100 event->initialize(0 /*deviceId*/, AINPUT_SOURCE_TOUCHSCREEN, AMOTION_EVENT_ACTION_MOVE,
101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, positions[0].time, pointerCount, properties, coords);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100102
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800103 for (size_t i = 1; i < positions.size(); i++) {
Siarhei Vishniakouf1ec7ad2019-03-06 13:01:10 -0800104 coords[0].setAxisValue(AMOTION_EVENT_AXIS_X, positions[i].x);
105 coords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, positions[i].y);
106 event->addSample(positions[i].time, coords);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100107 }
108 return event;
109}
110
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800111static void computeAndCheckVelocity(const std::vector<Position>& positions,
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100112 int32_t axis, float targetVelocity) {
113 VelocityTracker vt(nullptr);
114 float Vx, Vy;
115
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800116 std::unique_ptr<MotionEvent> event = createSimpleMotionEvent(positions);
Siarhei Vishniakouf1ec7ad2019-03-06 13:01:10 -0800117 vt.addMovement(event.get());
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100118
119 vt.getVelocity(DEFAULT_POINTER_ID, &Vx, &Vy);
120
121 switch (axis) {
122 case AMOTION_EVENT_AXIS_X:
123 checkVelocity(Vx, targetVelocity);
124 break;
125 case AMOTION_EVENT_AXIS_Y:
126 checkVelocity(Vy, targetVelocity);
127 break;
128 default:
129 FAIL() << "Axis must be either AMOTION_EVENT_AXIS_X or AMOTION_EVENT_AXIS_Y";
130 }
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100131}
132
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800133static void computeAndCheckQuadraticEstimate(const std::vector<Position>& positions,
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700134 const std::array<float, 3>& coefficients) {
135 VelocityTracker vt("lsq2");
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800136 std::unique_ptr<MotionEvent> event = createSimpleMotionEvent(positions);
Siarhei Vishniakouf1ec7ad2019-03-06 13:01:10 -0800137 vt.addMovement(event.get());
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700138 VelocityTracker::Estimator estimator;
139 EXPECT_TRUE(vt.getEstimator(0, &estimator));
140 for (size_t i = 0; i< coefficients.size(); i++) {
141 checkCoefficient(estimator.xCoeff[i], coefficients[i]);
142 checkCoefficient(estimator.yCoeff[i], coefficients[i]);
143 }
144}
145
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100146/*
147 * ================== VelocityTracker tests generated manually =====================================
148 */
149 // @todo Currently disabled, enable when switching away from lsq2 VelocityTrackerStrategy
150TEST_F(VelocityTrackerTest, DISABLED_ThreePointsPositiveVelocityTest) {
151 // Same coordinate is reported 2 times in a row
152 // It is difficult to determine the correct answer here, but at least the direction
153 // of the reported velocity should be positive.
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800154 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100155 { 0, 273, NAN },
156 { 12585000, 293, NAN },
157 { 14730000, 293, NAN },
158 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800159 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 1600);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100160}
161
162TEST_F(VelocityTrackerTest, ThreePointsZeroVelocityTest) {
163 // Same coordinate is reported 3 times in a row
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800164 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100165 { 0, 293, NAN },
166 { 6132000, 293, NAN },
167 { 11283000, 293, NAN },
168 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800169 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 0);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100170}
171
172TEST_F(VelocityTrackerTest, ThreePointsLinearVelocityTest) {
173 // Fixed velocity at 5 points per 10 milliseconds
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800174 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100175 { 0, 0, NAN },
176 { 10000000, 5, NAN },
177 { 20000000, 10, NAN },
178 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800179 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 500);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100180}
181
182
183/**
184 * ================== VelocityTracker tests generated by recording real events =====================
185 *
186 * To add a test, record the input coordinates and event times to all calls
187 * to void VelocityTracker::addMovement(const MotionEvent* event).
188 * Also record all calls to VelocityTracker::clear().
189 * Finally, record the output of VelocityTracker::getVelocity(...)
190 * This will give you the necessary data to create a new test.
191 */
192
193// --------------- Recorded by hand on swordfish ---------------------------------------------------
194// @todo Currently disabled, enable when switching away from lsq2 VelocityTrackerStrategy
195TEST_F(VelocityTrackerTest, DISABLED_SwordfishFlingDown) {
196 // Recording of a fling on Swordfish that could cause a fling in the wrong direction
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800197 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100198 { 0, 271, 96 },
199 { 16071042, 269.786346, 106.922775 },
200 { 35648403, 267.983063, 156.660034 },
201 { 52313925, 262.638397, 220.339081 },
202 { 68976522, 266.138824, 331.581116 },
203 { 85639375, 274.79245, 428.113159 },
204 { 96948871, 274.79245, 428.113159 },
205 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800206 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 623.577637);
207 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 8523.348633);
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100208}
209
210// --------------- Recorded by hand on sailfish, generated by a script -----------------------------
211// For some of these tests, the X-direction velocity checking has been removed, because the lsq2
212// and the impulse VelocityTrackerStrategies did not agree within 20%.
213// Since the flings were recorded in the Y-direction, the intentional user action should only
214// be relevant for the Y axis.
215// There have been also cases where lsq2 and impulse disagreed more than 20% in the Y-direction.
216// Those recordings have been discarded because we didn't feel one strategy's interpretation was
217// more correct than another's but didn't want to increase the tolerance for the entire test suite.
218//
219// There are 18 tests total below: 9 in the positive Y direction and 9 in the opposite.
220// The recordings were loosely binned into 3 categories - slow, faster, and fast, which roughly
221// characterizes the velocity of the finger motion.
222// These can be treated approximately as:
223// slow - less than 1 page gets scrolled
224// faster - more than 1 page gets scrolled, but less than 3
225// fast - entire list is scrolled (fling is done as hard as possible)
226
227TEST_F(VelocityTrackerTest, SailfishFlingUpSlow1) {
228 // Sailfish - fling up - slow - 1
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800229 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100230 { 235089067457000, 528.00, 983.00 },
231 { 235089084684000, 527.00, 981.00 },
232 { 235089093349000, 527.00, 977.00 },
233 { 235089095677625, 527.00, 975.93 },
234 { 235089101859000, 527.00, 970.00 },
235 { 235089110378000, 528.00, 960.00 },
236 { 235089112497111, 528.25, 957.51 },
237 { 235089118760000, 531.00, 946.00 },
238 { 235089126686000, 535.00, 931.00 },
239 { 235089129316820, 536.33, 926.02 },
240 { 235089135199000, 540.00, 914.00 },
241 { 235089144297000, 546.00, 896.00 },
242 { 235089146136443, 547.21, 892.36 },
243 { 235089152923000, 553.00, 877.00 },
244 { 235089160784000, 559.00, 851.00 },
245 { 235089162955851, 560.66, 843.82 },
246 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800247 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 872.794617); // impulse
248 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 951.698181); // lsq2
249 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -3604.819336); // impulse
250 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -3044.966064); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100251}
252
253
254TEST_F(VelocityTrackerTest, SailfishFlingUpSlow2) {
255 // Sailfish - fling up - slow - 2
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800256 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100257 { 235110560704000, 522.00, 1107.00 },
258 { 235110575764000, 522.00, 1107.00 },
259 { 235110584385000, 522.00, 1107.00 },
260 { 235110588421179, 521.52, 1106.52 },
261 { 235110592830000, 521.00, 1106.00 },
262 { 235110601385000, 520.00, 1104.00 },
263 { 235110605088160, 519.14, 1102.27 },
264 { 235110609952000, 518.00, 1100.00 },
265 { 235110618353000, 517.00, 1093.00 },
266 { 235110621755146, 516.60, 1090.17 },
267 { 235110627010000, 517.00, 1081.00 },
268 { 235110634785000, 518.00, 1063.00 },
269 { 235110638422450, 518.87, 1052.58 },
270 { 235110643161000, 520.00, 1039.00 },
271 { 235110651767000, 524.00, 1011.00 },
272 { 235110655089581, 525.54, 1000.19 },
273 { 235110660368000, 530.00, 980.00 },
274 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800275 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -4096.583008); // impulse
276 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -3455.094238); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100277}
278
279
280TEST_F(VelocityTrackerTest, SailfishFlingUpSlow3) {
281 // Sailfish - fling up - slow - 3
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800282 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100283 { 792536237000, 580.00, 1317.00 },
284 { 792541538987, 580.63, 1311.94 },
285 { 792544613000, 581.00, 1309.00 },
286 { 792552301000, 583.00, 1295.00 },
287 { 792558362309, 585.13, 1282.92 },
288 { 792560828000, 586.00, 1278.00 },
289 { 792569446000, 589.00, 1256.00 },
290 { 792575185095, 591.54, 1241.41 },
291 { 792578491000, 593.00, 1233.00 },
292 { 792587044000, 597.00, 1211.00 },
293 { 792592008172, 600.28, 1195.92 },
294 { 792594616000, 602.00, 1188.00 },
295 { 792603129000, 607.00, 1167.00 },
296 { 792608831290, 609.48, 1155.83 },
297 { 792612321000, 611.00, 1149.00 },
298 { 792620768000, 615.00, 1131.00 },
299 { 792625653873, 617.32, 1121.73 },
300 { 792629200000, 619.00, 1115.00 },
301 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800302 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 574.33429); // impulse
303 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 617.40564); // lsq2
304 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -2361.982666); // impulse
305 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -2500.055664); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100306}
307
308
309TEST_F(VelocityTrackerTest, SailfishFlingUpFaster1) {
310 // Sailfish - fling up - faster - 1
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800311 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100312 { 235160420675000, 610.00, 1042.00 },
313 { 235160428220000, 609.00, 1026.00 },
314 { 235160436544000, 609.00, 1024.00 },
315 { 235160441852394, 609.64, 1020.82 },
316 { 235160444878000, 610.00, 1019.00 },
317 { 235160452673000, 613.00, 1006.00 },
318 { 235160458519743, 617.18, 992.06 },
319 { 235160461061000, 619.00, 986.00 },
320 { 235160469798000, 627.00, 960.00 },
321 { 235160475186713, 632.22, 943.02 },
322 { 235160478051000, 635.00, 934.00 },
323 { 235160486489000, 644.00, 906.00 },
324 { 235160491853697, 649.56, 890.56 },
325 { 235160495177000, 653.00, 881.00 },
326 { 235160504148000, 662.00, 858.00 },
327 { 235160509231495, 666.81, 845.37 },
328 { 235160512603000, 670.00, 837.00 },
329 { 235160520366000, 679.00, 814.00 },
330 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800331 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 1274.141724); // impulse
332 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 1438.53186); // lsq2
333 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -3877.35498); // impulse
334 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -3695.859619); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100335}
336
337
338TEST_F(VelocityTrackerTest, SailfishFlingUpFaster2) {
339 // Sailfish - fling up - faster - 2
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800340 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100341 { 847153808000, 576.00, 1264.00 },
342 { 847171174000, 576.00, 1262.00 },
343 { 847179640000, 576.00, 1257.00 },
344 { 847185187540, 577.41, 1249.22 },
345 { 847187487000, 578.00, 1246.00 },
346 { 847195710000, 581.00, 1227.00 },
347 { 847202027059, 583.93, 1209.40 },
348 { 847204324000, 585.00, 1203.00 },
349 { 847212672000, 590.00, 1176.00 },
350 { 847218861395, 594.36, 1157.11 },
351 { 847221190000, 596.00, 1150.00 },
352 { 847230484000, 602.00, 1124.00 },
353 { 847235701400, 607.56, 1103.83 },
354 { 847237986000, 610.00, 1095.00 },
355 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800356 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -4280.07959); // impulse
357 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -4241.004395); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100358}
359
360
361TEST_F(VelocityTrackerTest, SailfishFlingUpFaster3) {
362 // Sailfish - fling up - faster - 3
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800363 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100364 { 235200532789000, 507.00, 1084.00 },
365 { 235200549221000, 507.00, 1083.00 },
366 { 235200557841000, 507.00, 1081.00 },
367 { 235200558051189, 507.00, 1080.95 },
368 { 235200566314000, 507.00, 1078.00 },
369 { 235200574876586, 508.97, 1070.12 },
370 { 235200575006000, 509.00, 1070.00 },
371 { 235200582900000, 514.00, 1054.00 },
372 { 235200591276000, 525.00, 1023.00 },
373 { 235200591701829, 525.56, 1021.42 },
374 { 235200600064000, 542.00, 976.00 },
375 { 235200608519000, 563.00, 911.00 },
376 { 235200608527086, 563.02, 910.94 },
377 { 235200616933000, 590.00, 844.00 },
378 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800379 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -8715.686523); // impulse
380 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -7639.026367); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100381}
382
383
384TEST_F(VelocityTrackerTest, SailfishFlingUpFast1) {
385 // Sailfish - fling up - fast - 1
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800386 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100387 { 920922149000, 561.00, 1412.00 },
388 { 920930185000, 559.00, 1377.00 },
389 { 920930262463, 558.98, 1376.66 },
390 { 920938547000, 559.00, 1371.00 },
391 { 920947096857, 562.91, 1342.68 },
392 { 920947302000, 563.00, 1342.00 },
393 { 920955502000, 577.00, 1272.00 },
394 { 920963931021, 596.87, 1190.54 },
395 { 920963987000, 597.00, 1190.00 },
396 { 920972530000, 631.00, 1093.00 },
397 { 920980765511, 671.31, 994.68 },
398 { 920980906000, 672.00, 993.00 },
399 { 920989261000, 715.00, 903.00 },
400 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800401 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 5670.329102); // impulse
402 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, 5991.866699); // lsq2
403 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -13021.101562); // impulse
404 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -15093.995117); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100405}
406
407
408TEST_F(VelocityTrackerTest, SailfishFlingUpFast2) {
409 // Sailfish - fling up - fast - 2
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800410 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100411 { 235247153233000, 518.00, 1168.00 },
412 { 235247170452000, 517.00, 1167.00 },
413 { 235247178908000, 515.00, 1159.00 },
414 { 235247179556213, 514.85, 1158.39 },
415 { 235247186821000, 515.00, 1125.00 },
416 { 235247195265000, 521.00, 1051.00 },
417 { 235247196389476, 521.80, 1041.15 },
418 { 235247203649000, 538.00, 932.00 },
419 { 235247212253000, 571.00, 794.00 },
420 { 235247213222491, 574.72, 778.45 },
421 { 235247220736000, 620.00, 641.00 },
422 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800423 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -20286.958984); // impulse
424 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -20494.587891); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100425}
426
427
428TEST_F(VelocityTrackerTest, SailfishFlingUpFast3) {
429 // Sailfish - fling up - fast - 3
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800430 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100431 { 235302568736000, 529.00, 1167.00 },
432 { 235302576644000, 523.00, 1140.00 },
433 { 235302579395063, 520.91, 1130.61 },
434 { 235302585140000, 522.00, 1130.00 },
435 { 235302593615000, 527.00, 1065.00 },
436 { 235302596207444, 528.53, 1045.12 },
437 { 235302602102000, 559.00, 872.00 },
438 { 235302610545000, 652.00, 605.00 },
439 { 235302613019881, 679.26, 526.73 },
440 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800441 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -39295.941406); // impulse
442 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, -36461.421875); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100443}
444
445
446TEST_F(VelocityTrackerTest, SailfishFlingDownSlow1) {
447 // Sailfish - fling down - slow - 1
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800448 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100449 { 235655749552755, 582.00, 432.49 },
450 { 235655750638000, 582.00, 433.00 },
451 { 235655758865000, 582.00, 440.00 },
452 { 235655766221523, 581.16, 448.43 },
453 { 235655767594000, 581.00, 450.00 },
454 { 235655776044000, 580.00, 462.00 },
455 { 235655782890696, 579.18, 474.35 },
456 { 235655784360000, 579.00, 477.00 },
457 { 235655792795000, 578.00, 496.00 },
458 { 235655799559531, 576.27, 515.04 },
459 { 235655800612000, 576.00, 518.00 },
460 { 235655809535000, 574.00, 542.00 },
461 { 235655816988015, 572.17, 564.86 },
462 { 235655817685000, 572.00, 567.00 },
463 { 235655825981000, 569.00, 595.00 },
464 { 235655833808653, 566.26, 620.60 },
465 { 235655834541000, 566.00, 623.00 },
466 { 235655842893000, 563.00, 649.00 },
467 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800468 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -419.749695); // impulse
469 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -398.303894); // lsq2
470 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 3309.016357); // impulse
471 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 3969.099854); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100472}
473
474
475TEST_F(VelocityTrackerTest, SailfishFlingDownSlow2) {
476 // Sailfish - fling down - slow - 2
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800477 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100478 { 235671152083370, 485.24, 558.28 },
479 { 235671154126000, 485.00, 559.00 },
480 { 235671162497000, 484.00, 566.00 },
481 { 235671168750511, 483.27, 573.29 },
482 { 235671171071000, 483.00, 576.00 },
483 { 235671179390000, 482.00, 588.00 },
484 { 235671185417210, 481.31, 598.98 },
485 { 235671188173000, 481.00, 604.00 },
486 { 235671196371000, 480.00, 624.00 },
487 { 235671202084196, 479.27, 639.98 },
488 { 235671204235000, 479.00, 646.00 },
489 { 235671212554000, 478.00, 673.00 },
490 { 235671219471011, 476.39, 697.12 },
491 { 235671221159000, 476.00, 703.00 },
492 { 235671229592000, 474.00, 734.00 },
493 { 235671236281462, 472.43, 758.38 },
494 { 235671238098000, 472.00, 765.00 },
495 { 235671246532000, 470.00, 799.00 },
496 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800497 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -262.80426); // impulse
498 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -243.665344); // lsq2
499 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 4215.682129); // impulse
500 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 4587.986816); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100501}
502
503
504TEST_F(VelocityTrackerTest, SailfishFlingDownSlow3) {
505 // Sailfish - fling down - slow - 3
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800506 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100507 { 170983201000, 557.00, 533.00 },
508 { 171000668000, 556.00, 534.00 },
509 { 171007359750, 554.73, 535.27 },
510 { 171011197000, 554.00, 536.00 },
511 { 171017660000, 552.00, 540.00 },
512 { 171024201831, 549.97, 544.73 },
513 { 171027333000, 549.00, 547.00 },
514 { 171034603000, 545.00, 557.00 },
515 { 171041043371, 541.98, 567.55 },
516 { 171043147000, 541.00, 571.00 },
517 { 171051052000, 536.00, 586.00 },
518 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800519 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -723.413513); // impulse
520 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -651.038452); // lsq2
521 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 2091.502441); // impulse
522 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 1934.517456); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100523}
524
525
526TEST_F(VelocityTrackerTest, SailfishFlingDownFaster1) {
527 // Sailfish - fling down - faster - 1
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800528 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100529 { 235695280333000, 558.00, 451.00 },
530 { 235695283971237, 558.43, 454.45 },
531 { 235695289038000, 559.00, 462.00 },
532 { 235695297388000, 561.00, 478.00 },
533 { 235695300638465, 561.83, 486.25 },
534 { 235695305265000, 563.00, 498.00 },
535 { 235695313591000, 564.00, 521.00 },
536 { 235695317305492, 564.43, 532.68 },
537 { 235695322181000, 565.00, 548.00 },
538 { 235695330709000, 565.00, 577.00 },
539 { 235695333972227, 565.00, 588.10 },
540 { 235695339250000, 565.00, 609.00 },
541 { 235695347839000, 565.00, 642.00 },
542 { 235695351313257, 565.00, 656.18 },
543 { 235695356412000, 565.00, 677.00 },
544 { 235695364899000, 563.00, 710.00 },
545 { 235695368118682, 562.24, 722.52 },
546 { 235695373403000, 564.00, 744.00 },
547 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800548 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 4254.639648); // impulse
549 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 4698.415039); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100550}
551
552
553TEST_F(VelocityTrackerTest, SailfishFlingDownFaster2) {
554 // Sailfish - fling down - faster - 2
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800555 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100556 { 235709624766000, 535.00, 579.00 },
557 { 235709642256000, 534.00, 580.00 },
558 { 235709643350278, 533.94, 580.06 },
559 { 235709650760000, 532.00, 584.00 },
560 { 235709658615000, 530.00, 593.00 },
561 { 235709660170495, 529.60, 594.78 },
562 { 235709667095000, 527.00, 606.00 },
563 { 235709675616000, 524.00, 628.00 },
564 { 235709676983261, 523.52, 631.53 },
565 { 235709684289000, 521.00, 652.00 },
566 { 235709692763000, 518.00, 682.00 },
567 { 235709693804993, 517.63, 685.69 },
568 { 235709701438000, 515.00, 709.00 },
569 { 235709709830000, 512.00, 739.00 },
570 { 235709710626776, 511.72, 741.85 },
571 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800572 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -430.440247); // impulse
573 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -447.600311); // lsq2
574 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 3953.859375); // impulse
575 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 4316.155273); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100576}
577
578
579TEST_F(VelocityTrackerTest, SailfishFlingDownFaster3) {
580 // Sailfish - fling down - faster - 3
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800581 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100582 { 235727628927000, 540.00, 440.00 },
583 { 235727636810000, 537.00, 454.00 },
584 { 235727646176000, 536.00, 454.00 },
585 { 235727653586628, 535.12, 456.65 },
586 { 235727654557000, 535.00, 457.00 },
587 { 235727663024000, 534.00, 465.00 },
588 { 235727670410103, 533.04, 479.45 },
589 { 235727670691000, 533.00, 480.00 },
590 { 235727679255000, 531.00, 501.00 },
591 { 235727687233704, 529.09, 526.73 },
592 { 235727687628000, 529.00, 528.00 },
593 { 235727696113000, 526.00, 558.00 },
594 { 235727704057546, 523.18, 588.98 },
595 { 235727704576000, 523.00, 591.00 },
596 { 235727713099000, 520.00, 626.00 },
597 { 235727720880776, 516.33, 655.36 },
598 { 235727721580000, 516.00, 658.00 },
599 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800600 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 4484.617676); // impulse
601 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 4927.92627); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100602}
603
604
605TEST_F(VelocityTrackerTest, SailfishFlingDownFast1) {
606 // Sailfish - fling down - fast - 1
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800607 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100608 { 235762352849000, 467.00, 286.00 },
609 { 235762360250000, 443.00, 344.00 },
610 { 235762362787412, 434.77, 363.89 },
611 { 235762368807000, 438.00, 359.00 },
612 { 235762377220000, 425.00, 423.00 },
613 { 235762379608561, 421.31, 441.17 },
614 { 235762385698000, 412.00, 528.00 },
615 { 235762394133000, 406.00, 648.00 },
616 { 235762396429369, 404.37, 680.67 },
617 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800618 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 19084.931641); // impulse
619 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 16064.685547); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100620}
621
622
623TEST_F(VelocityTrackerTest, SailfishFlingDownFast2) {
624 // Sailfish - fling down - fast - 2
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800625 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100626 { 235772487188000, 576.00, 204.00 },
627 { 235772495159000, 553.00, 236.00 },
628 { 235772503568000, 551.00, 240.00 },
629 { 235772508192247, 545.55, 254.17 },
630 { 235772512051000, 541.00, 266.00 },
631 { 235772520794000, 520.00, 337.00 },
632 { 235772525015263, 508.92, 394.43 },
633 { 235772529174000, 498.00, 451.00 },
634 { 235772537635000, 484.00, 589.00 },
635 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800636 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 18660.048828); // impulse
637 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 16918.439453); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100638}
639
640
641TEST_F(VelocityTrackerTest, SailfishFlingDownFast3) {
642 // Sailfish - fling down - fast - 3
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800643 std::vector<Position> values = {
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100644 { 507650295000, 628.00, 233.00 },
645 { 507658234000, 605.00, 269.00 },
646 { 507666784000, 601.00, 274.00 },
647 { 507669660483, 599.65, 275.68 },
648 { 507675427000, 582.00, 308.00 },
649 { 507683740000, 541.00, 404.00 },
650 { 507686506238, 527.36, 435.95 },
651 { 507692220000, 487.00, 581.00 },
652 { 507700707000, 454.00, 792.00 },
653 { 507703352649, 443.71, 857.77 },
654 };
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800655 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -6772.508301); // impulse
656 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_X, -6388.48877); // lsq2
657 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 29765.908203); // impulse
658 computeAndCheckVelocity(values, AMOTION_EVENT_AXIS_Y, 28354.796875); // lsq2
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100659}
660
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700661/*
662 * Special care must be taken when constructing tests for LeastSquaresVelocityTrackerStrategy
663 * getEstimator function. In particular:
664 * - inside the function, time gets converted from nanoseconds to seconds
665 * before being used in the fit.
666 * - any values that are older than 100 ms are being discarded.
667 * - the newest time gets subtracted from all of the other times before being used in the fit.
668 * So these tests have to be designed with those limitations in mind.
669 *
670 * General approach for the tests below:
671 * We only used timestamps in milliseconds, 0 ms, 1 ms, and 2 ms, to be sure that
672 * we are well within the HORIZON range.
673 * When specifying the expected values of the coefficients, we treat the x values as if
674 * they were in ms. Then, to adjust for the time units, the coefficients get progressively
675 * multiplied by powers of 1E3.
676 * For example:
677 * data: t(ms), x
678 * 1 ms, 1
679 * 2 ms, 4
680 * 3 ms, 9
681 * The coefficients are (0, 0, 1).
682 * In the test, we would convert these coefficients to (0*(1E3)^0, 0*(1E3)^1, 1*(1E3)^2).
683 */
684TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Constant) {
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800685 std::vector<Position> values = {
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700686 { 0000000, 1, 1 }, // 0 s
687 { 1000000, 1, 1 }, // 0.001 s
688 { 2000000, 1, 1 }, // 0.002 s
689 };
690 // The data used for the fit will be as follows:
691 // time(s), position
692 // -0.002, 1
693 // -0.001, 1
694 // -0.000, 1
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800695 computeAndCheckQuadraticEstimate(values, std::array<float, 3>({1, 0, 0}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700696}
697
698/*
699 * Straight line y = x :: the constant and quadratic coefficients are zero.
700 */
701TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Linear) {
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800702 std::vector<Position> values = {
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700703 { 0000000, -2, -2 },
704 { 1000000, -1, -1 },
705 { 2000000, -0, -0 },
706 };
707 // The data used for the fit will be as follows:
708 // time(s), position
709 // -0.002, -2
710 // -0.001, -1
711 // -0.000, 0
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800712 computeAndCheckQuadraticEstimate(values, std::array<float, 3>({0, 1E3, 0}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700713}
714
715/*
716 * Parabola
717 */
718TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic) {
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800719 std::vector<Position> values = {
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700720 { 0000000, 1, 1 },
721 { 1000000, 4, 4 },
722 { 2000000, 8, 8 },
723 };
724 // The data used for the fit will be as follows:
725 // time(s), position
726 // -0.002, 1
727 // -0.001, 4
728 // -0.000, 8
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800729 computeAndCheckQuadraticEstimate(values, std::array<float, 3>({8, 4.5E3, 0.5E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700730}
731
732/*
733 * Parabola
734 */
735TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic2) {
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800736 std::vector<Position> values = {
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700737 { 0000000, 1, 1 },
738 { 1000000, 4, 4 },
739 { 2000000, 9, 9 },
740 };
741 // The data used for the fit will be as follows:
742 // time(s), position
743 // -0.002, 1
744 // -0.001, 4
745 // -0.000, 9
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800746 computeAndCheckQuadraticEstimate(values, std::array<float, 3>({9, 6E3, 1E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700747}
748
749/*
750 * Parabola :: y = x^2 :: the constant and linear coefficients are zero.
751 */
752TEST_F(VelocityTrackerTest, LeastSquaresVelocityTrackerStrategyEstimator_Parabolic3) {
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800753 std::vector<Position> values = {
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700754 { 0000000, 4, 4 },
755 { 1000000, 1, 1 },
756 { 2000000, 0, 0 },
757 };
758 // The data used for the fit will be as follows:
759 // time(s), position
760 // -0.002, 4
761 // -0.001, 1
762 // -0.000, 0
Siarhei Vishniakou87b58012019-03-06 13:22:13 -0800763 computeAndCheckQuadraticEstimate(values, std::array<float, 3>({0, 0E3, 1E6}));
Siarhei Vishniakouf7e2d3e2018-09-07 16:38:18 -0700764}
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +0100765
766} // namespace android