1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/events/gesture_detection/mock_motion_event.h"
11 #include "ui/events/gesture_detection/velocity_tracker_state.h"
12 #include "ui/gfx/geometry/point_f.h"
13 #include "ui/gfx/geometry/vector2d_f.h"
15 using base::TimeDelta;
16 using base::TimeTicks;
21 const TimeDelta kTenMillis = TimeDelta::FromMilliseconds(10);
22 const TimeDelta kOneSecond = TimeDelta::FromSeconds(1);
23 const float kEpsilson = .01f;
25 const char* GetStrategyName(VelocityTracker::Strategy strategy) {
27 case VelocityTracker::LSQ1: return "LSQ1";
28 case VelocityTracker::LSQ2: return "LSQ2";
29 case VelocityTracker::LSQ3: return "LSQ3";
30 case VelocityTracker::WLSQ2_DELTA: return "WLSQ2_DELTA";
31 case VelocityTracker::WLSQ2_CENTRAL: return "WLSQ2_CENTRAL";
32 case VelocityTracker::WLSQ2_RECENT: return "WLSQ2_RECENT";
33 case VelocityTracker::INT1: return "INT1";
34 case VelocityTracker::INT2: return "INT2";
36 NOTREACHED() << "Invalid strategy";
42 class VelocityTrackerTest : public testing::Test {
44 VelocityTrackerTest() {}
45 virtual ~VelocityTrackerTest() {}
48 static MockMotionEvent Sample(MotionEvent::Action action,
53 const gfx::PointF p = p0 + ScaleVector2d(v, dt.InSecondsF());
54 return MockMotionEvent(action, t0 + dt, p.x(), p.y());
57 static void ApplyMovementSequence(VelocityTrackerState* state,
63 EXPECT_TRUE(!!samples);
66 const base::TimeDelta dt = t / samples;
67 state->AddMovement(Sample(MotionEvent::ACTION_DOWN, p0, t0, v, dt * 0));
68 ApplyMovement(state, p0, v, t0, t, samples);
69 state->AddMovement(Sample(MotionEvent::ACTION_UP, p0, t0, v, t));
72 static void ApplyMovement(VelocityTrackerState* state,
78 EXPECT_TRUE(!!samples);
81 const base::TimeDelta dt = t / samples;
82 for (size_t i = 0; i < samples; ++i)
83 state->AddMovement(Sample(MotionEvent::ACTION_MOVE, p0, t0, v, dt * i));
87 TEST_F(VelocityTrackerTest, Basic) {
88 const gfx::PointF p0(0, 0);
89 const gfx::Vector2dF v(0, 500);
90 const size_t samples = 60;
92 for (int i = 0; i <= VelocityTracker::STRATEGY_MAX; ++i) {
93 VelocityTracker::Strategy strategy =
94 static_cast<VelocityTracker::Strategy>(i);
96 SCOPED_TRACE(GetStrategyName(strategy));
97 VelocityTrackerState state(strategy);
99 // Default state should report zero velocity.
100 EXPECT_EQ(0, state.GetXVelocity(0));
101 EXPECT_EQ(0, state.GetYVelocity(0));
103 // Sample a constant velocity sequence.
104 ApplyMovementSequence(&state, p0, v, TimeTicks::Now(), kOneSecond, samples);
106 // The computed velocity should match that of the input.
107 state.ComputeCurrentVelocity(1000, 20000);
108 EXPECT_NEAR(v.x(), state.GetXVelocity(0), kEpsilson * v.x());
109 EXPECT_NEAR(v.y(), state.GetYVelocity(0), kEpsilson * v.y());
111 // A pointer ID of -1 should report the velocity of the active pointer.
112 EXPECT_NEAR(v.x(), state.GetXVelocity(-1), kEpsilson * v.x());
113 EXPECT_NEAR(v.y(), state.GetYVelocity(-1), kEpsilson * v.y());
115 // Invalid pointer ID's should report zero velocity.
116 EXPECT_EQ(0, state.GetXVelocity(1));
117 EXPECT_EQ(0, state.GetYVelocity(1));
118 EXPECT_EQ(0, state.GetXVelocity(7));
119 EXPECT_EQ(0, state.GetYVelocity(7));
123 TEST_F(VelocityTrackerTest, MaxVelocity) {
124 const gfx::PointF p0(0, 0);
125 const gfx::Vector2dF v(-50000, 50000);
126 const size_t samples = 3;
127 const base::TimeDelta dt = kTenMillis * 2;
129 VelocityTrackerState state;
130 ApplyMovementSequence(&state, p0, v, TimeTicks::Now(), dt, samples);
132 // The computed velocity should be restricted to the provided maximum.
133 state.ComputeCurrentVelocity(1000, 100);
134 EXPECT_NEAR(-100, state.GetXVelocity(0), kEpsilson);
135 EXPECT_NEAR(100, state.GetYVelocity(0), kEpsilson);
137 state.ComputeCurrentVelocity(1000, 1000);
138 EXPECT_NEAR(-1000, state.GetXVelocity(0), kEpsilson);
139 EXPECT_NEAR(1000, state.GetYVelocity(0), kEpsilson);
142 TEST_F(VelocityTrackerTest, VaryingVelocity) {
143 const gfx::PointF p0(0, 0);
144 const gfx::Vector2dF vFast(0, 500);
145 const gfx::Vector2dF vSlow = ScaleVector2d(vFast, 0.5f);
146 const size_t samples = 12;
148 for (int i = 0; i <= VelocityTracker::STRATEGY_MAX; ++i) {
149 VelocityTracker::Strategy strategy =
150 static_cast<VelocityTracker::Strategy>(i);
152 SCOPED_TRACE(GetStrategyName(strategy));
153 VelocityTrackerState state(strategy);
155 base::TimeTicks t0 = base::TimeTicks::Now();
156 base::TimeDelta dt = kTenMillis * 10;
158 Sample(MotionEvent::ACTION_DOWN, p0, t0, vFast, base::TimeDelta()));
160 // Apply some fast movement and compute the velocity.
161 gfx::PointF pCurr = p0;
162 base::TimeTicks tCurr = t0;
163 ApplyMovement(&state, pCurr, vFast, tCurr, dt, samples);
164 state.ComputeCurrentVelocity(1000, 20000);
165 float vOldY = state.GetYVelocity(0);
167 // Apply some slow movement.
168 pCurr += ScaleVector2d(vFast, dt.InSecondsF());
170 ApplyMovement(&state, pCurr, vSlow, tCurr, dt, samples);
172 // The computed velocity should have decreased.
173 state.ComputeCurrentVelocity(1000, 20000);
174 float vCurrentY = state.GetYVelocity(0);
175 EXPECT_GT(vFast.y(), vCurrentY);
176 EXPECT_GT(vOldY, vCurrentY);
179 // Apply some additional fast movement.
180 pCurr += ScaleVector2d(vSlow, dt.InSecondsF());
182 ApplyMovement(&state, pCurr, vFast, tCurr, dt, samples);
184 // The computed velocity should have increased.
185 state.ComputeCurrentVelocity(1000, 20000);
186 vCurrentY = state.GetYVelocity(0);
187 EXPECT_LT(vSlow.y(), vCurrentY);
188 EXPECT_LT(vOldY, vCurrentY);