Chromium Code Reviews| Index: ui/aura/gestures/velocity_calculator_unittest.cc |
| diff --git a/ui/aura/gestures/velocity_calculator_unittest.cc b/ui/aura/gestures/velocity_calculator_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..82a492e84d8a979c799ea6d5f1add1adbdf22e49 |
| --- /dev/null |
| +++ b/ui/aura/gestures/velocity_calculator_unittest.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/aura/gestures/velocity_calculator.h" |
| +#include "ui/aura/test/aura_test_base.h" |
| + |
| +namespace aura { |
| +namespace test { |
| + |
| +namespace { |
| +static void AddPoints(VelocityCalculator* velocityCalculator, |
|
sadrul
2012/02/02 22:04:06
newlines after lines 12 and 28
|
| + float x_increment, |
| + float y_increment, |
| + float time_increment_seconds, |
| + int num_points) { |
| + float x = 0; |
| + float y = 0; |
| + double time = 0; |
| + |
| + for (int i = 0; i < num_points; ++i) { |
| + velocityCalculator->PointSeen(x, y, time); |
| + x += x_increment; |
| + y += y_increment; |
| + time += time_increment_seconds * 1000000; |
| + } |
| +} |
| +} // namespace |
| + |
| +typedef AuraTestBase VelocityCalculatorTest; |
| + |
| +// Test that the velocity returned is reasonable |
| +TEST_F(VelocityCalculatorTest, ReturnsReasonableVelocity) { |
| + VelocityCalculator velocityCalculator(5); |
| + AddPoints(&velocityCalculator, 10, -10, 1, 7); |
| + |
| + EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| + EXPECT_LT(velocityCalculator.XVelocity(), 10.1); |
| + EXPECT_GT(velocityCalculator.YVelocity(), -10.1); |
| + EXPECT_LT(velocityCalculator.YVelocity(), -9.9); |
| + |
| + velocityCalculator.PointSeen(9, -11, 5500000); |
| + velocityCalculator.PointSeen(21, -19, 6000000); |
| + velocityCalculator.PointSeen(30, -32, 6500000); |
| + velocityCalculator.PointSeen(38, -40, 7000000); |
| + velocityCalculator.PointSeen(50, -51, 7500000); |
| + |
| + EXPECT_GT(velocityCalculator.XVelocity(), 19); |
| + EXPECT_LT(velocityCalculator.XVelocity(), 21); |
| + EXPECT_GT(velocityCalculator.YVelocity(), -21); |
| + EXPECT_LT(velocityCalculator.YVelocity(), -19); |
| + |
| + // Significantly larger difference in position |
| + velocityCalculator.PointSeen(70, -70, 8000000); |
| + |
| + EXPECT_GT(velocityCalculator.XVelocity(), 20); |
| + EXPECT_LT(velocityCalculator.XVelocity(), 25); |
| + EXPECT_GT(velocityCalculator.YVelocity(), -25); |
| + EXPECT_LT(velocityCalculator.YVelocity(), -20); |
| +} |
| + |
| +// Check that the velocity returned is 0 if the velocity calculator |
| +// doesn't have enough data |
| +TEST_F(VelocityCalculatorTest, RequiresEnoughData) { |
| + VelocityCalculator velocityCalculator(5); |
| + EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| + EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| + |
| + AddPoints(&velocityCalculator, 10, 10, 1, 4); |
| + |
| + // We've only seen 4 points, the buffer size is 5 |
| + // Since the buffer isn't full, return 0 |
| + EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| + EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| + |
| + AddPoints(&velocityCalculator, 10, 10, 1, 1); |
| + |
| + EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| + EXPECT_GT(velocityCalculator.YVelocity(), 9.9); |
| +} |
| + |
| +// Ensures ClearHistory behaves correctly |
| +TEST_F(VelocityCalculatorTest, ClearsHistory) { |
| + VelocityCalculator velocityCalculator(5); |
| + AddPoints(&velocityCalculator, 10, -10, 1, 7); |
| + |
| + EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| + EXPECT_LT(velocityCalculator.XVelocity(), 10.1); |
| + EXPECT_GT(velocityCalculator.YVelocity(), -10.1); |
| + EXPECT_LT(velocityCalculator.YVelocity(), -9.9); |
| + |
| + velocityCalculator.ClearHistory(); |
| + |
| + EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| + EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| +} |
| + |
| +// Ensure data older than the buffer size is ignored |
| +TEST_F(VelocityCalculatorTest, IgnoresOldData) { |
| + VelocityCalculator velocityCalculator(5); |
| + AddPoints(&velocityCalculator, 10, -10, 1, 7); |
| + |
| + EXPECT_GT(velocityCalculator.XVelocity(), 9.9); |
| + EXPECT_LT(velocityCalculator.XVelocity(), 10.1); |
| + EXPECT_GT(velocityCalculator.YVelocity(), -10.1); |
| + EXPECT_LT(velocityCalculator.YVelocity(), -9.9); |
| + |
| + AddPoints(&velocityCalculator, 0, 0, 1, 5); |
| + |
| + EXPECT_EQ(velocityCalculator.XVelocity(), 0); |
| + EXPECT_EQ(velocityCalculator.YVelocity(), 0); |
| +} |
| + |
| +} // namespace test |
| +} // namespace aura |