| 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..113324a03e7b5ab141988e3a642c0a3ab74b1fbf
|
| --- /dev/null
|
| +++ b/ui/aura/gestures/velocity_calculator_unittest.cc
|
| @@ -0,0 +1,129 @@
|
| +// 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,
|
| + float x_increment,
|
| + float y_increment,
|
| + float time_increment,
|
| + 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;
|
| + }
|
| +}
|
| +} // namespace
|
| +
|
| +typedef AuraTestBase VelocityCalculatorTest;
|
| +
|
| +// Test that the velocity returned is reasonable
|
| +TEST_F(VelocityCalculatorTest, ReturnsReasonableVelocity) {
|
| + float vx;
|
| + float vy;
|
| +
|
| + VelocityCalculator velocityCalculator(5);
|
| + AddPoints(&velocityCalculator, 10, -10, 1, 7);
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_GT(vx, 9.9);
|
| + EXPECT_LT(vx, 10.1);
|
| + EXPECT_GT(vy, -10.1);
|
| + EXPECT_LT(vy, -9.9);
|
| +
|
| + velocityCalculator.PointSeen(9, -11, 5.5);
|
| + velocityCalculator.PointSeen(21, -19, 6);
|
| + velocityCalculator.PointSeen(30, -32, 6.5);
|
| + velocityCalculator.PointSeen(38, -40, 7);
|
| + velocityCalculator.PointSeen(50, -51, 7.5);
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_GT(vx, 19);
|
| + EXPECT_LT(vx, 21);
|
| + EXPECT_GT(vy, -21);
|
| + EXPECT_LT(vy, -19);
|
| +
|
| + // Significantly larger difference in position
|
| + velocityCalculator.PointSeen(70, -70, 8);
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_GT(vx, 20);
|
| + EXPECT_LT(vx, 25);
|
| + EXPECT_GT(vy, -25);
|
| + EXPECT_LT(vy, -20);
|
| +}
|
| +
|
| +// Check that the velocity returned is 0 if the velocity calculator
|
| +// doesn't have enough data
|
| +TEST_F(VelocityCalculatorTest, RequiresEnoughData) {
|
| + float vx = 5;
|
| + float vy = 5;
|
| + VelocityCalculator velocityCalculator(5);
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_EQ(vx, 0);
|
| + EXPECT_EQ(vy, 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
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_EQ(vx, 0);
|
| + EXPECT_EQ(vy, 0);
|
| +
|
| + AddPoints(&velocityCalculator, 10, 10, 1, 1);
|
| +
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_GT(vx, 9.9);
|
| + EXPECT_GT(vy, 9.9);
|
| +}
|
| +
|
| +// Ensures ClearHistory behaves correctly
|
| +TEST_F(VelocityCalculatorTest, ClearsHistory) {
|
| + float vx;
|
| + float vy;
|
| +
|
| + VelocityCalculator velocityCalculator(5);
|
| + AddPoints(&velocityCalculator, 10, -10, 1, 7);
|
| +
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_GT(vx, 9.9);
|
| + EXPECT_LT(vy, -9.9);
|
| +
|
| + velocityCalculator.ClearHistory();
|
| +
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_EQ(vx, 0);
|
| + EXPECT_EQ(vy, 0);
|
| +}
|
| +
|
| +// Ensure data older than the buffer size is ignored
|
| +TEST_F(VelocityCalculatorTest, IgnoresOldData) {
|
| + float vx;
|
| + float vy;
|
| +
|
| + VelocityCalculator velocityCalculator(5);
|
| + AddPoints(&velocityCalculator, 10, -10, 1, 7);
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_GT(vx, 9.9);
|
| + EXPECT_LT(vy, -9.9);
|
| +
|
| + AddPoints(&velocityCalculator, 0, 0, 1, 5);
|
| +
|
| + velocityCalculator.Velocity(&vx, &vy);
|
| + EXPECT_EQ(vx, 0);
|
| + EXPECT_EQ(vy, 0);
|
| +}
|
| +
|
| +} // namespace test
|
| +} // namespace aura
|
|
|