| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h" |
| 6 #include "ui/aura/gestures/velocity_calculator.h" |
| 7 #include "ui/aura/test/aura_test_base.h" |
| 8 |
| 9 namespace aura { |
| 10 namespace test { |
| 11 |
| 12 namespace { |
| 13 |
| 14 static void AddPoints(VelocityCalculator* velocityCalculator, |
| 15 float x_increment, |
| 16 float y_increment, |
| 17 float time_increment_seconds, |
| 18 int num_points) { |
| 19 float x = 0; |
| 20 float y = 0; |
| 21 double time = 0; |
| 22 |
| 23 for (int i = 0; i < num_points; ++i) { |
| 24 velocityCalculator->PointSeen(x, y, time); |
| 25 x += x_increment; |
| 26 y += y_increment; |
| 27 time += time_increment_seconds * 1000000; |
| 28 } |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 typedef AuraTestBase VelocityCalculatorTest; |
| 34 |
| 35 // Test that the velocity returned is reasonable |
| 36 TEST_F(VelocityCalculatorTest, ReturnsReasonableVelocity) { |
| 37 VelocityCalculator velocityCalculator(5); |
| 38 AddPoints(&velocityCalculator, 10, -10, 1, 7); |
| 39 |
| 40 EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| 41 EXPECT_LT(velocityCalculator.XVelocity(), 10.1); |
| 42 EXPECT_GT(velocityCalculator.YVelocity(), -10.1); |
| 43 EXPECT_LT(velocityCalculator.YVelocity(), -9.9); |
| 44 |
| 45 velocityCalculator.PointSeen(9, -11, 5500000); |
| 46 velocityCalculator.PointSeen(21, -19, 6000000); |
| 47 velocityCalculator.PointSeen(30, -32, 6500000); |
| 48 velocityCalculator.PointSeen(38, -40, 7000000); |
| 49 velocityCalculator.PointSeen(50, -51, 7500000); |
| 50 |
| 51 EXPECT_GT(velocityCalculator.XVelocity(), 19); |
| 52 EXPECT_LT(velocityCalculator.XVelocity(), 21); |
| 53 EXPECT_GT(velocityCalculator.YVelocity(), -21); |
| 54 EXPECT_LT(velocityCalculator.YVelocity(), -19); |
| 55 |
| 56 // Significantly larger difference in position |
| 57 velocityCalculator.PointSeen(70, -70, 8000000); |
| 58 |
| 59 EXPECT_GT(velocityCalculator.XVelocity(), 20); |
| 60 EXPECT_LT(velocityCalculator.XVelocity(), 25); |
| 61 EXPECT_GT(velocityCalculator.YVelocity(), -25); |
| 62 EXPECT_LT(velocityCalculator.YVelocity(), -20); |
| 63 } |
| 64 |
| 65 // Check that the velocity returned is 0 if the velocity calculator |
| 66 // doesn't have enough data |
| 67 TEST_F(VelocityCalculatorTest, RequiresEnoughData) { |
| 68 VelocityCalculator velocityCalculator(5); |
| 69 EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| 70 EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| 71 |
| 72 AddPoints(&velocityCalculator, 10, 10, 1, 4); |
| 73 |
| 74 // We've only seen 4 points, the buffer size is 5 |
| 75 // Since the buffer isn't full, return 0 |
| 76 EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| 77 EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| 78 |
| 79 AddPoints(&velocityCalculator, 10, 10, 1, 1); |
| 80 |
| 81 EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| 82 EXPECT_GT(velocityCalculator.YVelocity(), 9.9); |
| 83 } |
| 84 |
| 85 // Ensures ClearHistory behaves correctly |
| 86 TEST_F(VelocityCalculatorTest, ClearsHistory) { |
| 87 VelocityCalculator velocityCalculator(5); |
| 88 AddPoints(&velocityCalculator, 10, -10, 1, 7); |
| 89 |
| 90 EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| 91 EXPECT_LT(velocityCalculator.XVelocity(), 10.1); |
| 92 EXPECT_GT(velocityCalculator.YVelocity(), -10.1); |
| 93 EXPECT_LT(velocityCalculator.YVelocity(), -9.9); |
| 94 |
| 95 velocityCalculator.ClearHistory(); |
| 96 |
| 97 EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| 98 EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| 99 } |
| 100 |
| 101 // Ensure data older than the buffer size is ignored |
| 102 TEST_F(VelocityCalculatorTest, IgnoresOldData) { |
| 103 VelocityCalculator velocityCalculator(5); |
| 104 AddPoints(&velocityCalculator, 10, -10, 1, 7); |
| 105 |
| 106 EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| 107 EXPECT_LT(velocityCalculator.XVelocity(), 10.1); |
| 108 EXPECT_GT(velocityCalculator.YVelocity(), -10.1); |
| 109 EXPECT_LT(velocityCalculator.YVelocity(), -9.9); |
| 110 |
| 111 AddPoints(&velocityCalculator, 0, 0, 1, 5); |
| 112 |
| 113 EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| 114 EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| 115 } |
| 116 |
| 117 } // namespace test |
| 118 } // namespace aura |
| OLD | NEW |