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