forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll_snap_data_unittest.cc
406 lines (361 loc) · 18.4 KB
/
scroll_snap_data_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2018 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 "cc/input/scroll_snap_data.h"
#include <memory>
#include "cc/input/snap_selection_strategy.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cc {
class ScrollSnapDataTest : public testing::Test {};
TEST_F(ScrollSnapDataTest, StartAlignmentCalculation) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(10, 10, 200, 300), gfx::ScrollOffset(600, 800));
SnapAreaData area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(100, 150, 100, 100), false);
container.AddSnapAreaData(area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(0, 0), true,
true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(90, snap_position.x());
EXPECT_EQ(140, snap_position.y());
}
TEST_F(ScrollSnapDataTest, CenterAlignmentCalculation) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(10, 10, 200, 300), gfx::ScrollOffset(600, 800));
SnapAreaData area(ScrollSnapAlign(SnapAlignment::kCenter),
gfx::RectF(100, 150, 100, 100), false);
container.AddSnapAreaData(area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(0, 0), true,
true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(40, snap_position.x());
EXPECT_EQ(40, snap_position.y());
}
TEST_F(ScrollSnapDataTest, EndAlignmentCalculation) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(10, 10, 200, 200), gfx::ScrollOffset(600, 800));
SnapAreaData area(ScrollSnapAlign(SnapAlignment::kEnd),
gfx::RectF(150, 200, 100, 100), false);
container.AddSnapAreaData(area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(0, 0), true,
true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(40, snap_position.x());
EXPECT_EQ(90, snap_position.y());
}
TEST_F(ScrollSnapDataTest, UnreachableSnapPositionCalculation) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(100, 100));
SnapAreaData area(ScrollSnapAlign(SnapAlignment::kEnd, SnapAlignment::kStart),
gfx::RectF(200, 0, 100, 100), false);
container.AddSnapAreaData(area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(50, 50),
true, true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
// Aligning to start on x would lead the scroll offset larger than max, and
// aligning to end on y would lead the scroll offset smaller than zero. So
// we expect these are clamped.
EXPECT_EQ(100, snap_position.x());
EXPECT_EQ(0, snap_position.y());
}
TEST_F(ScrollSnapDataTest, FindsClosestSnapPositionIndependently) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(600, 800));
SnapAreaData snap_x_only(
ScrollSnapAlign(SnapAlignment::kNone, SnapAlignment::kStart),
gfx::RectF(80, 0, 150, 150), false);
SnapAreaData snap_y_only(
ScrollSnapAlign(SnapAlignment::kStart, SnapAlignment::kNone),
gfx::RectF(0, 70, 150, 150), false);
SnapAreaData snap_on_both(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(50, 150, 150, 150), false);
container.AddSnapAreaData(snap_x_only);
container.AddSnapAreaData(snap_y_only);
container.AddSnapAreaData(snap_on_both);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(100, 100),
true, true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(80, snap_position.x());
EXPECT_EQ(70, snap_position.y());
}
TEST_F(ScrollSnapDataTest, FindsClosestSnapPositionOnAxisValueBoth) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(600, 800));
SnapAreaData snap_x_only(
ScrollSnapAlign(SnapAlignment::kNone, SnapAlignment::kStart),
gfx::RectF(80, 0, 150, 150), false);
SnapAreaData snap_y_only(
ScrollSnapAlign(SnapAlignment::kStart, SnapAlignment::kNone),
gfx::RectF(0, 70, 150, 150), false);
SnapAreaData snap_on_both(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(50, 150, 150, 150), false);
container.AddSnapAreaData(snap_x_only);
container.AddSnapAreaData(snap_y_only);
container.AddSnapAreaData(snap_on_both);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(40, 120),
true, true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(50, snap_position.x());
EXPECT_EQ(150, snap_position.y());
}
TEST_F(ScrollSnapDataTest, DoesNotSnapOnNonScrolledAxis) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(600, 800));
SnapAreaData snap_x_only(
ScrollSnapAlign(SnapAlignment::kNone, SnapAlignment::kStart),
gfx::RectF(80, 0, 150, 150), false);
SnapAreaData snap_y_only(
ScrollSnapAlign(SnapAlignment::kStart, SnapAlignment::kNone),
gfx::RectF(0, 70, 150, 150), false);
container.AddSnapAreaData(snap_x_only);
container.AddSnapAreaData(snap_y_only);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(100, 100),
true, false);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(80, snap_position.x());
EXPECT_EQ(100, snap_position.y());
}
TEST_F(ScrollSnapDataTest, DoesNotSnapOnNonVisibleAreas) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(600, 800));
SnapAreaData snap_x_only(
ScrollSnapAlign(SnapAlignment::kNone, SnapAlignment::kStart),
gfx::RectF(300, 400, 100, 100), false);
SnapAreaData snap_y_only(
ScrollSnapAlign(SnapAlignment::kStart, SnapAlignment::kNone),
gfx::RectF(400, 300, 100, 100), false);
container.AddSnapAreaData(snap_x_only);
container.AddSnapAreaData(snap_y_only);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(0, 0), true,
true);
EXPECT_FALSE(container.FindSnapPosition(*strategy, &snap_position));
}
TEST_F(ScrollSnapDataTest, SnapOnClosestAxisFirstIfVisibilityConflicts) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(600, 800));
// Both the areas are currently visible.
// However, if we snap to them on x and y independently, none is visible after
// snapping. So we only snap on the axis that has a closer snap point first.
// After that, we look for another snap point on y axis which does not
// conflict with the snap point on x.
SnapAreaData snap_x(
ScrollSnapAlign(SnapAlignment::kNone, SnapAlignment::kStart),
gfx::RectF(150, 0, 100, 100), false);
SnapAreaData snap_y1(
ScrollSnapAlign(SnapAlignment::kStart, SnapAlignment::kNone),
gfx::RectF(0, 180, 100, 100), false);
SnapAreaData snap_y2(
ScrollSnapAlign(SnapAlignment::kStart, SnapAlignment::kNone),
gfx::RectF(250, 80, 100, 100), false);
container.AddSnapAreaData(snap_x);
container.AddSnapAreaData(snap_y1);
container.AddSnapAreaData(snap_y2);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(0, 0), true,
true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(150, snap_position.x());
EXPECT_EQ(80, snap_position.y());
}
TEST_F(ScrollSnapDataTest, DoesNotSnapToPositionsOutsideProximityRange) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(600, 800));
container.set_proximity_range(gfx::ScrollOffset(50, 50));
SnapAreaData area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(80, 160, 100, 100), false);
container.AddSnapAreaData(area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndPosition(gfx::ScrollOffset(100, 100),
true, true);
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
// The snap position on x, 80, is within the proximity range of [50, 150].
// However, the snap position on y, 160, is outside the proximity range of
// [50, 150], so we should only snap on x.
EXPECT_EQ(80, snap_position.x());
EXPECT_EQ(100, snap_position.y());
}
TEST_F(ScrollSnapDataTest, MandatoryReturnsToCurrentIfNoValidAreaForward) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(2000, 2000));
SnapAreaData area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(600, 0, 100, 100), false);
container.AddSnapAreaData(area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> direction_strategy =
SnapSelectionStrategy::CreateForDirection(gfx::ScrollOffset(600, 0),
gfx::ScrollOffset(5, 0));
EXPECT_TRUE(container.FindSnapPosition(*direction_strategy, &snap_position));
// The snap direction is right. However, there is no valid snap position on
// that direction. So we have to stay at the current snap position of 600 as
// the snap type is mandatory.
EXPECT_EQ(600, snap_position.x());
EXPECT_EQ(0, snap_position.y());
std::unique_ptr<SnapSelectionStrategy> end_direction_strategy =
SnapSelectionStrategy::CreateForEndAndDirection(
gfx::ScrollOffset(600, 0), gfx::ScrollOffset(15, 15));
EXPECT_TRUE(
container.FindSnapPosition(*end_direction_strategy, &snap_position));
// The snap direction is down and right. However, there is no valid snap
// position on that direction. So we have to stay at the current snap position
// of (600, 0) as the snap type is mandatory.
EXPECT_EQ(600, snap_position.x());
EXPECT_EQ(0, snap_position.y());
// If the scroll-snap-type is proximity, we wouldn't consider the current
// snap area valid even if there is no snap area forward.
container.set_scroll_snap_type(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kProximity));
EXPECT_FALSE(container.FindSnapPosition(*direction_strategy, &snap_position));
EXPECT_FALSE(
container.FindSnapPosition(*end_direction_strategy, &snap_position));
}
TEST_F(ScrollSnapDataTest, MandatorySnapsBackwardIfNoValidAreaForward) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(2000, 2000));
SnapAreaData area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(600, 0, 100, 100), false);
container.AddSnapAreaData(area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> direction_strategy =
SnapSelectionStrategy::CreateForDirection(gfx::ScrollOffset(650, 0),
gfx::ScrollOffset(5, 0));
EXPECT_TRUE(container.FindSnapPosition(*direction_strategy, &snap_position));
// The snap direction is right. However, there is no valid snap position on
// that direction. So we have to scroll back to the snap position of 600 as
// the snap type is mandatory.
EXPECT_EQ(600, snap_position.x());
EXPECT_EQ(0, snap_position.y());
std::unique_ptr<SnapSelectionStrategy> end_direction_strategy =
SnapSelectionStrategy::CreateForEndAndDirection(
gfx::ScrollOffset(650, 10), gfx::ScrollOffset(15, 15));
EXPECT_TRUE(
container.FindSnapPosition(*end_direction_strategy, &snap_position));
// The snap direction is down and right. However, there is no valid snap
// position on that direction. So we have to scroll back to the snap position
// of (600, 0) as the snap type is mandatory.
EXPECT_EQ(600, snap_position.x());
EXPECT_EQ(0, snap_position.y());
// If the scroll-snap-type is proximity, we wouldn't consider the backward
// snap area valid even if there is no snap area forward.
container.set_scroll_snap_type(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kProximity));
EXPECT_FALSE(container.FindSnapPosition(*direction_strategy, &snap_position));
EXPECT_FALSE(
container.FindSnapPosition(*end_direction_strategy, &snap_position));
}
TEST_F(ScrollSnapDataTest, ShouldNotPassScrollSnapStopAlwaysElement) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(2000, 2000));
SnapAreaData must_snap_1(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(200, 0, 100, 100), true);
SnapAreaData must_snap_2(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(400, 0, 100, 100), true);
SnapAreaData closer_to_target(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(600, 0, 100, 100), false);
container.AddSnapAreaData(must_snap_1);
container.AddSnapAreaData(must_snap_2);
container.AddSnapAreaData(closer_to_target);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> end_direction_strategy =
SnapSelectionStrategy::CreateForEndAndDirection(
gfx::ScrollOffset(0, 0), gfx::ScrollOffset(600, 0));
EXPECT_TRUE(
container.FindSnapPosition(*end_direction_strategy, &snap_position));
// Even though closer_to_target and must_snap_2 are closer to the target
// position of the scroll, the must_snap_1 which is closer to the start
// shouldn't be passed.
EXPECT_EQ(200, snap_position.x());
EXPECT_EQ(0, snap_position.y());
}
TEST_F(ScrollSnapDataTest, SnapStopAlwaysOverridesCoveringSnapArea) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 200), gfx::ScrollOffset(600, 800));
SnapAreaData stop_area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(100, 0, 100, 100), true);
SnapAreaData covering_area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(250, 0, 600, 600), false);
container.AddSnapAreaData(stop_area);
container.AddSnapAreaData(covering_area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndAndDirection(
gfx::ScrollOffset(0, 0), gfx::ScrollOffset(300, 0));
// The fling is from (0, 0) to (300, 0), and the destination would make
// the |covering_area| perfectly cover the snapport. However, another area
// with snap-stop:always precedes this |covering_area| so we snap at
// (100, 100).
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(100, snap_position.x());
EXPECT_EQ(0, snap_position.y());
}
TEST_F(ScrollSnapDataTest, SnapStopAlwaysInReverseDirection) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 300), gfx::ScrollOffset(600, 800));
SnapAreaData stop_area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(100, 0, 100, 100), true);
container.AddSnapAreaData(stop_area);
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> strategy =
SnapSelectionStrategy::CreateForEndAndDirection(
gfx::ScrollOffset(150, 0), gfx::ScrollOffset(200, 0));
// The fling is from (150, 0) to (350, 0), but the snap position is in the
// reverse direction at (100, 0). Since the container has mandatory for
// snapstrictness, we should go back to snap at (100, 0).
EXPECT_TRUE(container.FindSnapPosition(*strategy, &snap_position));
EXPECT_EQ(100, snap_position.x());
EXPECT_EQ(0, snap_position.y());
}
TEST_F(ScrollSnapDataTest, SnapStopAlwaysNotInterferingWithDirectionStrategy) {
SnapContainerData container(
ScrollSnapType(false, SnapAxis::kBoth, SnapStrictness::kMandatory),
gfx::RectF(0, 0, 200, 300), gfx::ScrollOffset(600, 800));
SnapAreaData closer_area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(100, 0, 1, 1), false);
SnapAreaData stop_area(ScrollSnapAlign(SnapAlignment::kStart),
gfx::RectF(120, 0, 1, 1), true);
container.AddSnapAreaData(closer_area);
container.AddSnapAreaData(stop_area);
// The DirectionStrategy should always choose the first snap position
// regardless its scroll-snap-stop value.
gfx::ScrollOffset snap_position;
std::unique_ptr<SnapSelectionStrategy> direction_strategy =
SnapSelectionStrategy::CreateForDirection(gfx::ScrollOffset(90, 0),
gfx::ScrollOffset(50, 0));
snap_position = gfx::ScrollOffset();
EXPECT_TRUE(container.FindSnapPosition(*direction_strategy, &snap_position));
EXPECT_EQ(100, snap_position.x());
EXPECT_EQ(0, snap_position.y());
}
} // namespace cc