summaryrefslogtreecommitdiffstats
path: root/ui/events/gesture_detection/velocity_tracker_unittest.cc
diff options
context:
space:
mode:
authortdresser <tdresser@chromium.org>2014-10-28 09:40:25 -0700
committerCommit bot <commit-bot@chromium.org>2014-10-28 16:40:37 +0000
commit294e5f2d7277a47df084a4a914f3bfc96fdb8d80 (patch)
treed02628fb44b22fab03ac51db7fbf669f5aff9264 /ui/events/gesture_detection/velocity_tracker_unittest.cc
parentb401132fe08270db2d5e3c7a9038e4ac2a8fcccb (diff)
downloadchromium_src-294e5f2d7277a47df084a4a914f3bfc96fdb8d80.zip
chromium_src-294e5f2d7277a47df084a4a914f3bfc96fdb8d80.tar.gz
chromium_src-294e5f2d7277a47df084a4a914f3bfc96fdb8d80.tar.bz2
Prevent flings in the direction opposite the user's finger movement.
This previously could occur, as we use a quadratic regression to estimate finger velocity. Rapid deceleration would result in a fling in the wrong direction. BUG=417855 TEST=VelocityTrackerTest.NoDirectionReversal Review URL: https://codereview.chromium.org/610583002 Cr-Commit-Position: refs/heads/master@{#301648}
Diffstat (limited to 'ui/events/gesture_detection/velocity_tracker_unittest.cc')
-rw-r--r--ui/events/gesture_detection/velocity_tracker_unittest.cc44
1 files changed, 42 insertions, 2 deletions
diff --git a/ui/events/gesture_detection/velocity_tracker_unittest.cc b/ui/events/gesture_detection/velocity_tracker_unittest.cc
index 1fa5edc..85a1f5d 100644
--- a/ui/events/gesture_detection/velocity_tracker_unittest.cc
+++ b/ui/events/gesture_detection/velocity_tracker_unittest.cc
@@ -27,6 +27,7 @@ const char* GetStrategyName(VelocityTracker::Strategy strategy) {
switch (strategy) {
case VelocityTracker::LSQ1: return "LSQ1";
case VelocityTracker::LSQ2: return "LSQ2";
+ case VelocityTracker::LSQ2_RESTRICTED: return "LSQ2_RESTRICTED";
case VelocityTracker::LSQ3: return "LSQ3";
case VelocityTracker::WLSQ2_DELTA: return "WLSQ2_DELTA";
case VelocityTracker::WLSQ2_CENTRAL: return "WLSQ2_CENTRAL";
@@ -127,7 +128,7 @@ TEST_F(VelocityTrackerTest, MaxVelocity) {
const size_t samples = 3;
const base::TimeDelta dt = kTenMillis * 2;
- VelocityTrackerState state;
+ VelocityTrackerState state(VelocityTracker::Strategy::LSQ2);
ApplyMovementSequence(&state, p0, v, TimeTicks::Now(), dt, samples);
// The computed velocity should be restricted to the provided maximum.
@@ -197,7 +198,7 @@ TEST_F(VelocityTrackerTest, DelayedActionUp) {
const base::TimeTicks t0 = base::TimeTicks::Now();
const base::TimeDelta dt = kTenMillis * 2;
- VelocityTrackerState state;
+ VelocityTrackerState state(VelocityTracker::Strategy::LSQ2);
state.AddMovement(
Sample(MotionEvent::ACTION_DOWN, p0, t0, v, base::TimeDelta()));
@@ -219,4 +220,43 @@ TEST_F(VelocityTrackerTest, DelayedActionUp) {
EXPECT_EQ(0.f, state.GetYVelocity(0));
}
+// Tests that a rapid deceleration won't result in a velocity going in the
+// opposite direction to the pointers primary movement, with the LSQ_RESTRICTED
+// strategy. See crbug.com/417855.
+TEST_F(VelocityTrackerTest, NoDirectionReversal) {
+ VelocityTrackerState state_unrestricted(VelocityTracker::LSQ2);
+ VelocityTrackerState state_restricted(VelocityTracker::LSQ2_RESTRICTED);
+ const base::TimeTicks t0 = base::TimeTicks::Now();
+ const base::TimeDelta dt = base::TimeDelta::FromMilliseconds(1);
+ const size_t samples = 60;
+
+ gfx::PointF p(0, 0);
+
+ MockMotionEvent m1(MotionEvent::ACTION_DOWN, t0, p.x(), p.y());
+ state_unrestricted.AddMovement(m1);
+ state_restricted.AddMovement(m1);
+
+ for (size_t i = 0; i < samples; ++i) {
+ if (i < 50)
+ p.set_y(p.y() + 10);
+ MockMotionEvent mi(MotionEvent::ACTION_MOVE, t0 + dt * i, p.x(), p.y());
+ state_unrestricted.AddMovement(mi);
+ state_restricted.AddMovement(mi);
+ }
+
+ // The computed velocity be zero, as we stopped at the end of the gesture. In
+ // particular, it should not be negative, as all movement was in the positive
+ // direction.
+ state_restricted.ComputeCurrentVelocity(1000, 20000);
+ EXPECT_EQ(0, state_restricted.GetXVelocity(0));
+ EXPECT_EQ(0, state_restricted.GetYVelocity(0));
+
+ // This does not hold for the unrestricted LSQ2 strategy.
+ state_unrestricted.ComputeCurrentVelocity(1000, 20000);
+ EXPECT_EQ(0, state_unrestricted.GetXVelocity(0));
+ // Y velocity is negative, despite the fact that the finger only moved in the
+ // positive y direction.
+ EXPECT_GT(0, state_unrestricted.GetYVelocity(0));
+}
+
} // namespace ui