forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfast_ink_points_unittest.cc
256 lines (215 loc) · 9.3 KB
/
fast_ink_points_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2016 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 "ash/fast_ink/fast_ink_points.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/test/event_generator.h"
namespace fast_ink {
namespace {
const int kTestPointsLifetimeSeconds = 5;
class FastInkPointsTest : public testing::Test {
public:
FastInkPointsTest()
: points_(base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds)),
predicted_(base::TimeDelta::FromSeconds(kTestPointsLifetimeSeconds)),
event_time_(base::TimeTicks()),
screen_size_(1000, 1000) {}
~FastInkPointsTest() override = default;
protected:
FastInkPoints points_;
FastInkPoints predicted_;
base::TimeTicks event_time_;
const gfx::Size screen_size_;
base::TimeDelta prediction_duration_;
void AddPoint(const gfx::PointF& point, base::TimeDelta interval) {
event_time_ += interval;
points_.AddPoint(point, event_time_);
predicted_.Predict(points_, event_time_, prediction_duration_,
screen_size_);
const base::TimeTicks presentation_time =
event_time_ + prediction_duration_;
points_.MoveForwardToTime(presentation_time);
predicted_.MoveForwardToTime(presentation_time);
}
void AddStroke(int points,
base::TimeDelta interval,
const gfx::PointF& position,
const gfx::Vector2dF& velocity,
const gfx::Vector2dF& acceleration) {
points_.Clear();
gfx::PointF p = position;
gfx::Vector2dF v = velocity;
for (int i = 0; i < points; ++i) {
AddPoint(p, interval);
p += v;
v += acceleration;
}
}
void Diff(std::vector<gfx::Vector2dF>& dst,
const std::vector<gfx::Vector2dF>& src) {
dst.clear();
if (src.size() < 2)
return;
for (size_t i = 1; i < src.size(); ++i)
dst.push_back(src[i] - src[i - 1]);
}
void ComputeDeltas(std::vector<gfx::Vector2dF>& velocity,
std::vector<gfx::Vector2dF>& acceleration) {
std::vector<gfx::Vector2dF> position;
for (auto p : points_.points())
position.push_back(p.location.OffsetFromOrigin());
for (auto p : predicted_.points())
position.push_back(p.location.OffsetFromOrigin());
Diff(velocity, position);
Diff(acceleration, velocity);
}
private:
DISALLOW_COPY_AND_ASSIGN(FastInkPointsTest);
};
} // namespace
// Tests that the fast ink points internal collection handles receiving points
// and that the functions are returning the expected output.
TEST_F(FastInkPointsTest, FastInkPointsInternalCollection) {
EXPECT_TRUE(points_.IsEmpty());
EXPECT_EQ(gfx::Rect(), points_.GetBoundingBox());
const gfx::PointF left(1, 1);
const gfx::PointF bottom(1, 9);
const gfx::PointF top_right(30, 0);
const gfx::PointF last(2, 2);
points_.AddPoint(left, base::TimeTicks());
EXPECT_EQ(gfx::Rect(1, 1, 0, 0), points_.GetBoundingBox());
// Should be the new bottom of the bounding box.
points_.AddPoint(bottom, base::TimeTicks());
EXPECT_EQ(gfx::Rect(1, 1, 0, bottom.y() - 1), points_.GetBoundingBox());
// Should be the new top and right of the bounding box.
points_.AddPoint(top_right, base::TimeTicks());
EXPECT_EQ(3, points_.GetNumberOfPoints());
EXPECT_FALSE(points_.IsEmpty());
EXPECT_EQ(gfx::Rect(left.x(), top_right.y(), top_right.x() - left.x(),
bottom.y() - top_right.y()),
points_.GetBoundingBox());
// Should not expand bounding box.
points_.AddPoint(last, base::TimeTicks());
EXPECT_EQ(gfx::Rect(left.x(), top_right.y(), top_right.x() - left.x(),
bottom.y() - top_right.y()),
points_.GetBoundingBox());
// Points should be sorted in the order they are added.
EXPECT_EQ(left, points_.GetOldest().location);
EXPECT_EQ(last, points_.GetNewest().location);
// Add a new point which will expand the bounding box.
gfx::PointF new_left_bottom(0, 40);
points_.AddPoint(new_left_bottom, base::TimeTicks());
EXPECT_EQ(5, points_.GetNumberOfPoints());
EXPECT_EQ(gfx::Rect(new_left_bottom.x(), top_right.y(),
top_right.x() - new_left_bottom.x(),
new_left_bottom.y() - top_right.y()),
points_.GetBoundingBox());
// Verify clearing works.
points_.Clear();
EXPECT_TRUE(points_.IsEmpty());
}
// Test the fast ink points collection to verify that old points are
// removed.
TEST_F(FastInkPointsTest, FastInkPointsInternalCollectionDeletion) {
EXPECT_EQ(1, prediction_duration_.is_zero());
// When a point older than kTestPointsLifetimeSeconds (5 sec) is added, it
// should get removed. The age of the point is a number between 0.0 and 1.0,
// with 0.0 specifying a newly added point and 1.0 specifying the age of a
// point added |kTestPointsLifetimeSeconds| ago.
AddPoint(gfx::PointF(), base::TimeDelta::FromSeconds(1));
EXPECT_EQ(1, points_.GetNumberOfPoints());
EXPECT_FLOAT_EQ(0.0, points_.GetFadeoutFactor(0));
// Verify when we move forward in time by one second, the age of the last
// point, added one second ago is 1 / |kTestPointsLifetimeSeconds|.
AddPoint(gfx::PointF(), base::TimeDelta::FromSeconds(1));
EXPECT_EQ(2, points_.GetNumberOfPoints());
EXPECT_FLOAT_EQ(0.2, points_.GetFadeoutFactor(0));
EXPECT_FLOAT_EQ(0.0, points_.GetFadeoutFactor(1));
// Verify adding a point 10 seconds later will clear all other points, since
// they are older than 5 seconds.
AddPoint(gfx::PointF(), base::TimeDelta::FromSeconds(10));
EXPECT_EQ(1, points_.GetNumberOfPoints());
// Verify adding 3 points one second apart each will add 3 points to the
// collection, since all 4 points are younger than 5 seconds. All 4 points are
// added 1 second apart so their age should be 0.2 apart.
AddPoint(gfx::PointF(), base::TimeDelta::FromSeconds(1));
AddPoint(gfx::PointF(), base::TimeDelta::FromSeconds(1));
AddPoint(gfx::PointF(), base::TimeDelta::FromSeconds(1));
EXPECT_EQ(4, points_.GetNumberOfPoints());
EXPECT_FLOAT_EQ(0.6, points_.GetFadeoutFactor(0));
EXPECT_FLOAT_EQ(0.4, points_.GetFadeoutFactor(1));
EXPECT_FLOAT_EQ(0.2, points_.GetFadeoutFactor(2));
EXPECT_FLOAT_EQ(0.0, points_.GetFadeoutFactor(3));
// Verify adding 1 point three seconds later will remove 2 points which are
// older than 5 seconds.
AddPoint(gfx::PointF(), base::TimeDelta::FromSeconds(3));
EXPECT_EQ(3, points_.GetNumberOfPoints());
}
// Test the fast ink prediction.
TEST_F(FastInkPointsTest, FastInkPointsPrediction) {
prediction_duration_ = base::TimeDelta::FromMilliseconds(18);
const base::TimeDelta kTraceInterval = base::TimeDelta::FromMilliseconds(5);
const int kExpectedPredictionDepth = 3;
// Using fairly generous error margin to allow for the accumulation
// of rounding errors.
const float kMaxPredictionError = 1e-4;
std::vector<gfx::Vector2dF> computed_velocity;
std::vector<gfx::Vector2dF> computed_acceleration;
const gfx::Vector2dF zero;
const gfx::PointF position(0, 0);
// 0 points, no prediction.
AddStroke(0, kTraceInterval, position, zero, zero);
EXPECT_EQ(0, predicted_.GetNumberOfPoints());
// 1 point, no prediction.
AddStroke(1, kTraceInterval, position, zero, zero);
EXPECT_EQ(0, predicted_.GetNumberOfPoints());
// Fixed position, no prediction.
for (int points = 2; points <= 4; ++points) {
SCOPED_TRACE(points);
AddStroke(points, kTraceInterval, position, zero, zero);
EXPECT_EQ(0, predicted_.GetNumberOfPoints());
}
// Constant velocity, the predicted trajectory should maintain it.
const gfx::Vector2dF velocity(10, 5);
for (int points = 2; points <= 4; ++points) {
SCOPED_TRACE(points);
AddStroke(points, kTraceInterval, position, velocity, zero);
EXPECT_EQ(kExpectedPredictionDepth, predicted_.GetNumberOfPoints());
ComputeDeltas(computed_velocity, computed_acceleration);
for (auto v : computed_velocity) {
EXPECT_GT(kMaxPredictionError, (velocity - v).Length());
}
}
// Constant acceleration, the predicted trajectory should maintain it.
const gfx::Vector2dF acceleration(4, 2);
for (int points = 3; points <= 4; ++points) {
SCOPED_TRACE(points);
AddStroke(points, kTraceInterval, position, velocity, acceleration);
EXPECT_EQ(kExpectedPredictionDepth, predicted_.GetNumberOfPoints());
ComputeDeltas(computed_velocity, computed_acceleration);
for (auto a : computed_acceleration) {
EXPECT_GT(kMaxPredictionError, (acceleration - a).Length());
}
}
// Not testing with non-zero jerk, as the current prediction implementation
// is not maintaining constant jerk on purpose.
}
// Test the interrupted stroke support.
TEST_F(FastInkPointsTest, AddGap) {
points_.AddPoint(gfx::PointF(0, 0), base::TimeTicks());
points_.AddPoint(gfx::PointF(1, 1), base::TimeTicks());
points_.AddGap();
points_.AddPoint(gfx::PointF(2, 2), base::TimeTicks());
points_.AddPoint(gfx::PointF(3, 3), base::TimeTicks());
points_.AddPoint(gfx::PointF(4, 4), base::TimeTicks());
points_.AddGap();
points_.AddPoint(gfx::PointF(5, 5), base::TimeTicks());
auto points = points_.points();
EXPECT_FALSE(points[0].gap_after);
EXPECT_TRUE(points[1].gap_after);
EXPECT_FALSE(points[2].gap_after);
EXPECT_FALSE(points[3].gap_after);
EXPECT_TRUE(points[4].gap_after);
EXPECT_FALSE(points[5].gap_after);
}
} // namespace fast_ink