Index: ui/events/gesture_detection/velocity_tracker_unittest.cc |
diff --git a/ui/events/gesture_detection/velocity_tracker_unittest.cc b/ui/events/gesture_detection/velocity_tracker_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..09a9b25b2b292069eba91ae64bfb7e5ca102c80b |
--- /dev/null |
+++ b/ui/events/gesture_detection/velocity_tracker_unittest.cc |
@@ -0,0 +1,113 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/time/time.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/events/gesture_detection/mock_motion_event.h" |
+#include "ui/events/gesture_detection/velocity_tracker_state.h" |
+#include "ui/gfx/geometry/point_f.h" |
+#include "ui/gfx/geometry/vector2d_f.h" |
+ |
+using base::TimeDelta; |
+using base::TimeTicks; |
+ |
+namespace ui { |
+namespace { |
+ |
+const TimeDelta kTenMillis = TimeDelta::FromMilliseconds(10); |
+const TimeDelta kOneSecond = TimeDelta::FromSeconds(1); |
+const float kEpsilson = .01f; |
+ |
+} // namespace |
+ |
+class VelocityTrackerTest : public testing::Test { |
+ public: |
+ VelocityTrackerTest() {} |
+ virtual ~VelocityTrackerTest() {} |
+ |
+ protected: |
+ static MockMotionEvent Sample(MotionEvent::Action action, |
+ gfx::PointF p0, |
+ TimeTicks t0, |
+ gfx::Vector2dF v, |
+ TimeDelta dt) { |
+ const gfx::PointF p = p0 + ScaleVector2d(v, dt.InSecondsF()); |
+ return MockMotionEvent(action, t0 + dt, p.x(), p.y()); |
+ } |
+ |
+ static void ApplyMovement(VelocityTrackerState* state, |
+ gfx::PointF p0, |
+ gfx::Vector2dF v, |
+ TimeTicks t0, |
+ TimeDelta t, |
+ size_t samples) { |
+ EXPECT_TRUE(!!samples); |
+ if (!samples) |
+ return; |
+ const base::TimeDelta dt = t / samples; |
+ state->AddMovement(Sample(MotionEvent::ACTION_DOWN, p0, t0, v, dt * 0)); |
+ for (size_t i = 0; i < samples; ++i) |
+ state->AddMovement(Sample(MotionEvent::ACTION_MOVE, p0, t0, v, dt * i)); |
+ state->AddMovement(Sample(MotionEvent::ACTION_UP, p0, t0, v, t)); |
+ } |
+}; |
+ |
+TEST_F(VelocityTrackerTest, Basic) { |
+ const gfx::PointF p0(0, 0); |
+ const gfx::Vector2dF v(0, 500); |
+ const size_t samples = 60; |
+ |
+ for (int i = 0; i <= VelocityTracker::STRATEGY_MAX; ++i) { |
+ VelocityTracker::Strategy strategy = |
+ static_cast<VelocityTracker::Strategy>(i); |
+ |
+ SCOPED_TRACE(strategy); |
+ VelocityTrackerState state(strategy); |
+ |
+ // Default state should report zero velocity. |
+ EXPECT_EQ(0, state.GetXVelocity(0)); |
+ EXPECT_EQ(0, state.GetYVelocity(0)); |
+ |
+ // Sample a constant velocity sequence. |
+ ApplyMovement(&state, p0, v, TimeTicks::Now(), kOneSecond, samples); |
+ |
+ // The computed velocity should match that of the input. |
+ state.ComputeCurrentVelocity(1000, 20000); |
+ EXPECT_NEAR(v.x(), state.GetXVelocity(0), kEpsilson * v.x()); |
+ EXPECT_NEAR(v.y(), state.GetYVelocity(0), kEpsilson * v.y()); |
+ |
+ // A pointer ID of -1 should report the velocity of the active pointer. |
+ EXPECT_NEAR(v.x(), state.GetXVelocity(-1), kEpsilson * v.x()); |
+ EXPECT_NEAR(v.y(), state.GetYVelocity(-1), kEpsilson * v.y()); |
+ |
+ // Invalid pointer ID's should report zero velocity. |
+ EXPECT_EQ(0, state.GetXVelocity(1)); |
+ EXPECT_EQ(0, state.GetYVelocity(1)); |
+ EXPECT_EQ(0, state.GetXVelocity(7)); |
+ EXPECT_EQ(0, state.GetYVelocity(7)); |
+ } |
+} |
+ |
+TEST_F(VelocityTrackerTest, MaxVelocity) { |
+ const gfx::PointF p0(0, 0); |
+ const gfx::Vector2dF v(-50000, 50000); |
+ const size_t samples = 3; |
+ |
+ VelocityTrackerState state; |
+ ApplyMovement(&state, p0, v, TimeTicks::Now(), kTenMillis * 2, samples); |
+ |
+ // The computed velocity should be restricted to the provided maximum. |
+ state.ComputeCurrentVelocity(1000, 100); |
+ EXPECT_NEAR(-100, state.GetXVelocity(0), kEpsilson); |
+ EXPECT_NEAR(100, state.GetYVelocity(0), kEpsilson); |
+ |
+ state.ComputeCurrentVelocity(1000, 1000); |
+ EXPECT_NEAR(-1000, state.GetXVelocity(0), kEpsilson); |
+ EXPECT_NEAR(1000, state.GetYVelocity(0), kEpsilson); |
+} |
+ |
+} // namespace ui |