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