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 "base/basictypes.h" | |
6 #include "testing/gtest/include/gtest/gtest.h" | |
7 #include "ui/events/gestures/velocity_calculator.h" | |
8 | |
9 namespace ui { | |
10 namespace test { | |
11 | |
12 namespace { | |
13 | |
14 static void AddPoints(VelocityCalculator* velocity_calculator, | |
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 velocity_calculator->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 // Test that the velocity returned is reasonable | |
34 TEST(VelocityCalculatorTest, ReturnsReasonableVelocity) { | |
35 VelocityCalculator velocity_calculator(5); | |
36 AddPoints(&velocity_calculator, 10, -10, 1, 7); | |
37 | |
38 EXPECT_GT(velocity_calculator.XVelocity(), 9.9); | |
39 EXPECT_LT(velocity_calculator.XVelocity(), 10.1); | |
40 EXPECT_GT(velocity_calculator.YVelocity(), -10.1); | |
41 EXPECT_LT(velocity_calculator.YVelocity(), -9.9); | |
42 | |
43 velocity_calculator.PointSeen(9, -11, 5500000); | |
44 velocity_calculator.PointSeen(21, -19, 6000000); | |
45 velocity_calculator.PointSeen(30, -32, 6500000); | |
46 velocity_calculator.PointSeen(38, -40, 7000000); | |
47 velocity_calculator.PointSeen(50, -51, 7500000); | |
48 | |
49 EXPECT_GT(velocity_calculator.XVelocity(), 19); | |
50 EXPECT_LT(velocity_calculator.XVelocity(), 21); | |
51 EXPECT_GT(velocity_calculator.YVelocity(), -21); | |
52 EXPECT_LT(velocity_calculator.YVelocity(), -19); | |
53 | |
54 // Significantly larger difference in position | |
55 velocity_calculator.PointSeen(70, -70, 8000000); | |
56 | |
57 EXPECT_GT(velocity_calculator.XVelocity(), 20); | |
58 EXPECT_LT(velocity_calculator.XVelocity(), 25); | |
59 EXPECT_GT(velocity_calculator.YVelocity(), -25); | |
60 EXPECT_LT(velocity_calculator.YVelocity(), -20); | |
61 } | |
62 | |
63 TEST(VelocityCalculatorTest, IsAccurateWithLargeTimes) { | |
64 VelocityCalculator velocity_calculator(5); | |
65 int64 start_time = 0; | |
66 velocity_calculator.PointSeen(9, -11, start_time); | |
67 velocity_calculator.PointSeen(21, -19, start_time + 8); | |
68 velocity_calculator.PointSeen(30, -32, start_time + 16); | |
69 velocity_calculator.PointSeen(38, -40, start_time + 24); | |
70 velocity_calculator.PointSeen(50, -51, start_time + 32); | |
71 | |
72 EXPECT_GT(velocity_calculator.XVelocity(), 1230000); | |
73 EXPECT_LT(velocity_calculator.XVelocity(), 1260000); | |
74 EXPECT_GT(velocity_calculator.YVelocity(), -1270000); | |
75 EXPECT_LT(velocity_calculator.YVelocity(), -1240000); | |
76 | |
77 start_time = 1223372036800000000LL; | |
78 velocity_calculator.PointSeen(9, -11, start_time); | |
79 velocity_calculator.PointSeen(21, -19, start_time + 8); | |
80 velocity_calculator.PointSeen(30, -32, start_time + 16); | |
81 velocity_calculator.PointSeen(38, -40, start_time + 24); | |
82 velocity_calculator.PointSeen(50, -51, start_time + 32); | |
83 | |
84 EXPECT_GT(velocity_calculator.XVelocity(), 1230000); | |
85 EXPECT_LT(velocity_calculator.XVelocity(), 1260000); | |
86 EXPECT_GT(velocity_calculator.YVelocity(), -1270000); | |
87 EXPECT_LT(velocity_calculator.YVelocity(), -124000); | |
88 } | |
89 | |
90 // Check that the right values are returned for insufficient data. | |
91 TEST(VelocityCalculatorTest, RequiresEnoughData) { | |
92 VelocityCalculator velocity_calculator(3); | |
93 int64 start_time = 0; | |
94 | |
95 // Zero points is zero velocity. | |
96 EXPECT_EQ(velocity_calculator.XVelocity(), 0); | |
97 EXPECT_EQ(velocity_calculator.YVelocity(), 0); | |
98 | |
99 // 1point is still zero velocity. | |
100 velocity_calculator.PointSeen(10, 10, start_time); | |
101 EXPECT_EQ(velocity_calculator.XVelocity(), 0); | |
102 EXPECT_EQ(velocity_calculator.YVelocity(), 0); | |
103 | |
104 // 2 points has non-zero velocity. | |
105 velocity_calculator.PointSeen(20, 20, start_time + 5); | |
106 EXPECT_FLOAT_EQ(velocity_calculator.XVelocity(), 1923077.f); | |
107 EXPECT_FLOAT_EQ(velocity_calculator.YVelocity(), 1923077.f); | |
108 | |
109 velocity_calculator.PointSeen(30, 30, start_time + 10); | |
110 velocity_calculator.PointSeen(40, 40, start_time + 15); | |
111 EXPECT_FLOAT_EQ(velocity_calculator.XVelocity(), 2000000.f); | |
112 EXPECT_FLOAT_EQ(velocity_calculator.YVelocity(), 2000000.f); | |
113 | |
114 velocity_calculator.PointSeen(50, 50, start_time + 20); | |
115 EXPECT_FLOAT_EQ(velocity_calculator.XVelocity(), 2000000.f); | |
116 EXPECT_FLOAT_EQ(velocity_calculator.YVelocity(), 2000000.f); | |
117 } | |
118 | |
119 // Ensures ClearHistory behaves correctly | |
120 TEST(VelocityCalculatorTest, ClearsHistory) { | |
121 VelocityCalculator velocity_calculator(5); | |
122 AddPoints(&velocity_calculator, 10, -10, 1, 7); | |
123 | |
124 EXPECT_FLOAT_EQ(velocity_calculator.XVelocity(), 10.f); | |
125 EXPECT_FLOAT_EQ(velocity_calculator.YVelocity(), -10.f); | |
126 | |
127 velocity_calculator.ClearHistory(); | |
128 | |
129 EXPECT_EQ(velocity_calculator.XVelocity(), 0); | |
130 EXPECT_EQ(velocity_calculator.YVelocity(), 0); | |
131 } | |
132 | |
133 // Ensure data older than the buffer size is ignored | |
134 TEST(VelocityCalculatorTest, IgnoresOldData) { | |
135 VelocityCalculator velocity_calculator(5); | |
136 AddPoints(&velocity_calculator, 10, -10, 1, 7); | |
137 | |
138 EXPECT_FLOAT_EQ(velocity_calculator.XVelocity(), 10.f); | |
139 EXPECT_FLOAT_EQ(velocity_calculator.YVelocity(), -10.f); | |
140 | |
141 AddPoints(&velocity_calculator, 0, 0, 1, 5); | |
142 | |
143 EXPECT_FLOAT_EQ(velocity_calculator.XVelocity(), 0); | |
144 EXPECT_FLOAT_EQ(velocity_calculator.YVelocity(), 0); | |
145 } | |
146 | |
147 } // namespace test | |
148 } // namespace ui | |
OLD | NEW |