OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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" |
| 14 |
| 15 using base::TimeDelta; |
| 16 using base::TimeTicks; |
| 17 |
| 18 namespace ui { |
| 19 namespace { |
| 20 |
| 21 const TimeDelta kTenMillis = TimeDelta::FromMilliseconds(10); |
| 22 const TimeDelta kOneSecond = TimeDelta::FromSeconds(1); |
| 23 const float kEpsilson = .01f; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 class VelocityTrackerTest : public testing::Test { |
| 28 public: |
| 29 VelocityTrackerTest() {} |
| 30 virtual ~VelocityTrackerTest() {} |
| 31 |
| 32 protected: |
| 33 static MockMotionEvent Sample(MotionEvent::Action action, |
| 34 gfx::PointF p0, |
| 35 TimeTicks t0, |
| 36 gfx::Vector2dF v, |
| 37 TimeDelta dt) { |
| 38 const gfx::PointF p = p0 + ScaleVector2d(v, dt.InSecondsF()); |
| 39 return MockMotionEvent(action, t0 + dt, p.x(), p.y()); |
| 40 } |
| 41 |
| 42 static void ApplyMovement(VelocityTrackerState* state, |
| 43 gfx::PointF p0, |
| 44 gfx::Vector2dF v, |
| 45 TimeTicks t0, |
| 46 TimeDelta t, |
| 47 size_t samples) { |
| 48 EXPECT_TRUE(!!samples); |
| 49 if (!samples) |
| 50 return; |
| 51 const base::TimeDelta dt = t / samples; |
| 52 state->AddMovement(Sample(MotionEvent::ACTION_DOWN, p0, t0, v, dt * 0)); |
| 53 for (size_t i = 0; i < samples; ++i) |
| 54 state->AddMovement(Sample(MotionEvent::ACTION_MOVE, p0, t0, v, dt * i)); |
| 55 state->AddMovement(Sample(MotionEvent::ACTION_UP, p0, t0, v, t)); |
| 56 } |
| 57 }; |
| 58 |
| 59 TEST_F(VelocityTrackerTest, Basic) { |
| 60 const gfx::PointF p0(0, 0); |
| 61 const gfx::Vector2dF v(0, 500); |
| 62 const size_t samples = 60; |
| 63 |
| 64 for (int i = 0; i <= VelocityTracker::STRATEGY_MAX; ++i) { |
| 65 VelocityTracker::Strategy strategy = |
| 66 static_cast<VelocityTracker::Strategy>(i); |
| 67 |
| 68 SCOPED_TRACE(strategy); |
| 69 VelocityTrackerState state(strategy); |
| 70 |
| 71 // Default state should report zero velocity. |
| 72 EXPECT_EQ(0, state.GetXVelocity(0)); |
| 73 EXPECT_EQ(0, state.GetYVelocity(0)); |
| 74 |
| 75 // Sample a constant velocity sequence. |
| 76 ApplyMovement(&state, p0, v, TimeTicks::Now(), kOneSecond, samples); |
| 77 |
| 78 // The computed velocity should match that of the input. |
| 79 state.ComputeCurrentVelocity(1000, 20000); |
| 80 EXPECT_NEAR(v.x(), state.GetXVelocity(0), kEpsilson * v.x()); |
| 81 EXPECT_NEAR(v.y(), state.GetYVelocity(0), kEpsilson * v.y()); |
| 82 |
| 83 // A pointer ID of -1 should report the velocity of the active pointer. |
| 84 EXPECT_NEAR(v.x(), state.GetXVelocity(-1), kEpsilson * v.x()); |
| 85 EXPECT_NEAR(v.y(), state.GetYVelocity(-1), kEpsilson * v.y()); |
| 86 |
| 87 // Invalid pointer ID's should report zero velocity. |
| 88 EXPECT_EQ(0, state.GetXVelocity(1)); |
| 89 EXPECT_EQ(0, state.GetYVelocity(1)); |
| 90 EXPECT_EQ(0, state.GetXVelocity(7)); |
| 91 EXPECT_EQ(0, state.GetYVelocity(7)); |
| 92 } |
| 93 } |
| 94 |
| 95 TEST_F(VelocityTrackerTest, MaxVelocity) { |
| 96 const gfx::PointF p0(0, 0); |
| 97 const gfx::Vector2dF v(-50000, 50000); |
| 98 const size_t samples = 3; |
| 99 |
| 100 VelocityTrackerState state; |
| 101 ApplyMovement(&state, p0, v, TimeTicks::Now(), kTenMillis * 2, samples); |
| 102 |
| 103 // The computed velocity should be restricted to the provided maximum. |
| 104 state.ComputeCurrentVelocity(1000, 100); |
| 105 EXPECT_NEAR(-100, state.GetXVelocity(0), kEpsilson); |
| 106 EXPECT_NEAR(100, state.GetYVelocity(0), kEpsilson); |
| 107 |
| 108 state.ComputeCurrentVelocity(1000, 1000); |
| 109 EXPECT_NEAR(-1000, state.GetXVelocity(0), kEpsilson); |
| 110 EXPECT_NEAR(1000, state.GetYVelocity(0), kEpsilson); |
| 111 } |
| 112 |
| 113 } // namespace ui |
OLD | NEW |