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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
ash / public / cpp / pagination / pagination_model_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/pagination/pagination_model.h"
#include <string>
#include "ash/public/cpp/pagination/pagination_model_observer.h"
#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "ui/views/test/widget_test.h"
namespace ash {
namespace test {
class TestPaginationModelObserver : public PaginationModelObserver {
public:
TestPaginationModelObserver() = default;
TestPaginationModelObserver(const TestPaginationModelObserver&) = delete;
TestPaginationModelObserver& operator=(const TestPaginationModelObserver&) =
delete;
~TestPaginationModelObserver() override = default;
void Reset() {
selection_count_ = 0;
transition_start_count_ = 0;
transition_end_count_ = 0;
selected_pages_.clear();
transition_start_call_count_ = 0;
transition_ended_call_count_ = 0;
wait_loop_ = nullptr;
}
void set_model(PaginationModel* model) { model_ = model; }
void set_wait_loop(base::RunLoop* wait_loop) { wait_loop_ = wait_loop; }
void set_expected_page_selection(int expected_page_selection) {
expected_page_selection_ = expected_page_selection;
}
void set_expected_transition_start(int expected_transition_start) {
expected_transition_start_ = expected_transition_start;
}
void set_expected_transition_end(int expected_transition_end) {
expected_transition_end_ = expected_transition_end;
}
void set_transition_page(int page) { transition_page_ = page; }
const std::string& selected_pages() const { return selected_pages_; }
int selection_count() const { return selection_count_; }
int transition_start_count() const { return transition_start_count_; }
int transition_end_count() const { return transition_end_count_; }
int transition_start_call_count() const {
return transition_start_call_count_;
}
int transition_end_call_count() const { return transition_ended_call_count_; }
private:
void AppendSelectedPage(int page) {
if (selected_pages_.length())
selected_pages_.append(std::string(" "));
selected_pages_.append(base::NumberToString(page));
}
// PaginationModelObserver overrides:
void TotalPagesChanged(int previous_page_count, int new_page_count) override {
}
void SelectedPageChanged(int old_selected, int new_selected) override {
AppendSelectedPage(new_selected);
++selection_count_;
if (wait_loop_ && expected_page_selection_ &&
selection_count_ == expected_page_selection_) {
wait_loop_->Quit();
}
}
void TransitionStarted() override { ++transition_start_call_count_; }
void TransitionChanged() override {
if (transition_page_ == -1 ||
model_->transition().target_page == transition_page_) {
if (model_->transition().progress == 0)
++transition_start_count_;
if (model_->transition().progress == 1)
++transition_end_count_;
}
if (!wait_loop_)
return;
if ((expected_transition_start_ &&
transition_start_count_ == expected_transition_start_) ||
(expected_transition_end_ &&
transition_end_count_ == expected_transition_end_)) {
wait_loop_->Quit();
}
}
void TransitionEnded() override { ++transition_ended_call_count_; }
raw_ptr<PaginationModel, DanglingUntriaged> model_ = nullptr;
int expected_page_selection_ = 0;
int expected_transition_start_ = 0;
int expected_transition_end_ = 0;
int selection_count_ = 0;
int transition_start_count_ = 0;
int transition_end_count_ = 0;
// Indicate which page index should be counted for |transition_start_count_|
// and |transition_end_count_|. -1 means all the pages should be counted.
int transition_page_ = -1;
std::string selected_pages_;
int transition_start_call_count_ = 0;
int transition_ended_call_count_ = 0;
raw_ptr<base::RunLoop, DanglingUntriaged> wait_loop_ = nullptr;
};
class PaginationModelTest : public views::test::WidgetTest {
public:
PaginationModelTest() = default;
PaginationModelTest(const PaginationModelTest&) = delete;
PaginationModelTest& operator=(const PaginationModelTest&) = delete;
~PaginationModelTest() override = default;
// testing::Test overrides:
void SetUp() override {
views::test::WidgetTest::SetUp();
widget_.reset(CreateTopLevelPlatformWidget());
pagination_ = std::make_unique<PaginationModel>(widget_->GetContentsView());
pagination_->SetTotalPages(5);
pagination_->SetTransitionDurations(base::Milliseconds(1),
base::Milliseconds(1));
observer_.set_model(pagination_.get());
pagination_->AddObserver(&observer_);
}
void TearDown() override {
pagination_->RemoveObserver(&observer_);
observer_.set_model(NULL);
widget_.reset();
views::test::WidgetTest::TearDown();
}
protected:
void SetStartPageAndExpects(int start_page,
int expected_selection,
int expected_transition_start,
int expected_transition_end) {
observer_.set_wait_loop(nullptr);
pagination_->SelectPage(start_page, /*animate=*/false);
observer_.Reset();
paging_animation_wait_loop_ = std::make_unique<base::RunLoop>();
observer_.set_wait_loop(paging_animation_wait_loop_.get());
observer_.set_expected_page_selection(expected_selection);
observer_.set_expected_transition_start(expected_transition_start);
observer_.set_expected_transition_end(expected_transition_end);
}
void WaitForPagingAnimation() {
ASSERT_TRUE(paging_animation_wait_loop_);
paging_animation_wait_loop_->Run();
}
void WaitForRevertAnimation() {
while (pagination()->IsRevertingCurrentTransition()) {
base::RunLoop run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(100));
run_loop.Run();
}
}
PaginationModel* pagination() { return pagination_.get(); }
TestPaginationModelObserver observer_;
private:
WidgetAutoclosePtr widget_;
std::unique_ptr<PaginationModel> pagination_;
std::unique_ptr<base::RunLoop> paging_animation_wait_loop_;
};
TEST_F(PaginationModelTest, SelectPage) {
pagination()->SelectPage(2, /*animate=*/false);
pagination()->SelectPage(4, /*animate=*/false);
pagination()->SelectPage(3, /*animate=*/false);
pagination()->SelectPage(1, /*animate=*/false);
EXPECT_EQ(0, observer_.transition_start_count());
EXPECT_EQ(0, observer_.transition_end_count());
EXPECT_EQ(4, observer_.selection_count());
EXPECT_EQ(std::string("2 4 3 1"), observer_.selected_pages());
// Nothing happens if select the same page.
pagination()->SelectPage(1, /*animate=*/false);
EXPECT_EQ(4, observer_.selection_count());
EXPECT_EQ(std::string("2 4 3 1"), observer_.selected_pages());
}
TEST_F(PaginationModelTest, IsValidPageRelative) {
pagination()->SelectPage(0, false /*animate*/);
ASSERT_FALSE(pagination()->IsValidPageRelative(-1));
ASSERT_FALSE(pagination()->IsValidPageRelative(5));
ASSERT_TRUE(pagination()->IsValidPageRelative(1));
ASSERT_TRUE(pagination()->IsValidPageRelative(4));
}
TEST_F(PaginationModelTest, SelectPageAnimated) {
const int kStartPage = 0;
// One transition.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->SelectPage(1, /*animate=*/true);
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.transition_start_count());
EXPECT_EQ(1, observer_.transition_end_count());
EXPECT_EQ(1, observer_.selection_count());
EXPECT_EQ(std::string("1"), observer_.selected_pages());
// Two transitions in a row.
SetStartPageAndExpects(kStartPage, 2, 0, 0);
pagination()->SelectPage(1, /*animate=*/true);
pagination()->SelectPage(3, /*animate=*/true);
WaitForPagingAnimation();
EXPECT_EQ(2, observer_.transition_start_count());
EXPECT_EQ(2, observer_.transition_end_count());
EXPECT_EQ(2, observer_.selection_count());
EXPECT_EQ(std::string("1 3"), observer_.selected_pages());
// Transition to same page twice and only one should happen.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->SelectPage(1, /*animate=*/true);
pagination()->SelectPage(1, /*animate=*/true); // Ignored.
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.transition_start_count());
EXPECT_EQ(1, observer_.transition_end_count());
EXPECT_EQ(1, observer_.selection_count());
EXPECT_EQ(std::string("1"), observer_.selected_pages());
// More than two transitions and only the first and last would happen.
SetStartPageAndExpects(kStartPage, 2, 0, 0);
pagination()->SelectPage(1, /*animate=*/true);
pagination()->SelectPage(3, /*animate=*/true); // Ignored
pagination()->SelectPage(4, /*animate=*/true); // Ignored
pagination()->SelectPage(2, /*animate=*/true);
WaitForPagingAnimation();
EXPECT_EQ(2, observer_.transition_start_count());
EXPECT_EQ(2, observer_.transition_end_count());
EXPECT_EQ(2, observer_.selection_count());
EXPECT_EQ(std::string("1 2"), observer_.selected_pages());
// Multiple transitions with one transition that goes back to the original
// and followed by a new transition. Two transitions would happen. The first
// one will be reversed by the kStart transition and the second one will be
// finished.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->SelectPage(1, /*animate=*/true);
pagination()->SelectPage(2, /*animate=*/true); // Ignored
pagination()->SelectPage(kStartPage, /*animate=*/true);
pagination()->SelectPage(3, /*animate=*/true);
WaitForPagingAnimation();
EXPECT_EQ(std::string("3"), observer_.selected_pages());
}
TEST_F(PaginationModelTest, SimpleScroll) {
const int kStartPage = 2;
// Scroll to the next page (negative delta) and finish it.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
pagination()->EndScroll(false); // Finish transition
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.selection_count());
// Scroll to the previous page (positive delta) and finish it.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->StartScroll();
pagination()->UpdateScroll(0.1);
EXPECT_EQ(kStartPage - 1, pagination()->transition().target_page);
pagination()->EndScroll(false); // Finish transition
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.selection_count());
// Scroll to the next page (negative delta) and cancel it.
SetStartPageAndExpects(kStartPage, 0, 1, 0);
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
pagination()->EndScroll(true); // Cancel transition
WaitForPagingAnimation();
EXPECT_EQ(0, observer_.selection_count());
// Scroll to the previous page (position delta) and cancel it.
SetStartPageAndExpects(kStartPage, 0, 1, 0);
pagination()->StartScroll();
pagination()->UpdateScroll(0.1);
EXPECT_EQ(kStartPage - 1, pagination()->transition().target_page);
pagination()->EndScroll(true); // Cancel transition
WaitForPagingAnimation();
EXPECT_EQ(0, observer_.selection_count());
}
TEST_F(PaginationModelTest, ScrollWithTransition) {
const int kStartPage = 2;
// Scroll to the next page (negative delta) with a transition in the same
// direction.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage + 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
EXPECT_EQ(0.6, pagination()->transition().progress);
pagination()->EndScroll(false);
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.selection_count());
// Scroll to the next page (negative delta) with a transition in a different
// direction.
SetStartPageAndExpects(kStartPage, 0, 1, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage - 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
EXPECT_EQ(kStartPage - 1, pagination()->transition().target_page);
EXPECT_EQ(0.4, pagination()->transition().progress);
pagination()->EndScroll(true);
// Scroll to the previous page (positive delta) with a transition in the same
// direction.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage - 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(0.1);
EXPECT_EQ(kStartPage - 1, pagination()->transition().target_page);
EXPECT_EQ(0.6, pagination()->transition().progress);
pagination()->EndScroll(false);
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.selection_count());
// Scroll to the previous page (positive delta) with a transition in a
// different direction.
SetStartPageAndExpects(kStartPage, 0, 1, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage + 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(0.1);
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
EXPECT_EQ(0.4, pagination()->transition().progress);
pagination()->EndScroll(true);
}
TEST_F(PaginationModelTest, LongScroll) {
const int kStartPage = 2;
// Scroll to the next page (negative delta) with a transition in the same
// direction. And scroll enough to change page twice.
SetStartPageAndExpects(kStartPage, 2, 0, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage + 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
EXPECT_EQ(0.6, pagination()->transition().progress);
pagination()->UpdateScroll(-0.5);
EXPECT_EQ(1, observer_.selection_count());
pagination()->UpdateScroll(-0.5);
EXPECT_EQ(kStartPage + 2, pagination()->transition().target_page);
pagination()->EndScroll(false);
WaitForPagingAnimation();
EXPECT_EQ(2, observer_.selection_count());
// Scroll to the next page (negative delta) with a transition in a different
// direction. And scroll enough to revert it and switch page once.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage - 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
EXPECT_EQ(kStartPage - 1, pagination()->transition().target_page);
EXPECT_EQ(0.4, pagination()->transition().progress);
pagination()->UpdateScroll(-0.5); // This clears the transition.
pagination()->UpdateScroll(-0.5); // This starts a new transition.
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
pagination()->EndScroll(false);
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.selection_count());
// Similar cases as above but in the opposite direction.
// Scroll to the previous page (positive delta) with a transition in the same
// direction. And scroll enough to change page twice.
SetStartPageAndExpects(kStartPage, 2, 0, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage - 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(0.1);
EXPECT_EQ(kStartPage - 1, pagination()->transition().target_page);
EXPECT_EQ(0.6, pagination()->transition().progress);
pagination()->UpdateScroll(0.5);
EXPECT_EQ(1, observer_.selection_count());
pagination()->UpdateScroll(0.5);
EXPECT_EQ(kStartPage - 2, pagination()->transition().target_page);
pagination()->EndScroll(false);
WaitForPagingAnimation();
EXPECT_EQ(2, observer_.selection_count());
// Scroll to the previous page (positive delta) with a transition in a
// different direction. And scroll enough to revert it and switch page once.
SetStartPageAndExpects(kStartPage, 1, 0, 0);
pagination()->SetTransition(PaginationModel::Transition(kStartPage + 1, 0.5));
pagination()->StartScroll();
pagination()->UpdateScroll(0.1);
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
EXPECT_EQ(0.4, pagination()->transition().progress);
pagination()->UpdateScroll(0.5); // This clears the transition.
pagination()->UpdateScroll(0.5); // This starts a new transition.
EXPECT_EQ(kStartPage - 1, pagination()->transition().target_page);
pagination()->EndScroll(false);
WaitForPagingAnimation();
EXPECT_EQ(1, observer_.selection_count());
}
TEST_F(PaginationModelTest, FireTransitionZero) {
const int kStartPage = 2;
// Scroll to next page then revert the scroll and make sure transition
// progress 0 is fired when previous scroll is cleared.
SetStartPageAndExpects(kStartPage, 0, 0, 0);
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
int target_page = kStartPage + 1;
EXPECT_EQ(target_page, pagination()->transition().target_page);
observer_.set_transition_page(target_page);
pagination()->UpdateScroll(0.2); // This clears the transition.
EXPECT_EQ(1, observer_.transition_start_count());
pagination()->EndScroll(true); // Cancel transition.
// Similar to above but in the other direction.
SetStartPageAndExpects(kStartPage, 0, 0, 0);
pagination()->StartScroll();
pagination()->UpdateScroll(0.1);
target_page = kStartPage - 1;
EXPECT_EQ(target_page, pagination()->transition().target_page);
observer_.set_transition_page(target_page);
pagination()->UpdateScroll(-0.2); // This clears the transition.
EXPECT_EQ(1, observer_.transition_start_count());
pagination()->EndScroll(true); // Cancel transition.
}
TEST_F(PaginationModelTest, SelectedPageIsLost) {
pagination()->SetTotalPages(2);
// The selected page is set to 0 once the total number of pages are set.
EXPECT_EQ(0, pagination()->selected_page());
pagination()->SelectPage(1, /*animate=*/false);
EXPECT_EQ(1, pagination()->selected_page());
// The selected page is unchanged if it's still valid.
pagination()->SetTotalPages(3);
EXPECT_EQ(1, pagination()->selected_page());
pagination()->SetTotalPages(2);
EXPECT_EQ(1, pagination()->selected_page());
// But if the currently selected_page exceeds the total number of pages,
// it automatically switches to the last page.
pagination()->SetTotalPages(1);
EXPECT_EQ(0, pagination()->selected_page());
}
TEST_F(PaginationModelTest, SelectPageRelativeBeginning) {
// Test starts with 5 pages. Select Page 1.
pagination()->SelectPage(1, false);
pagination()->SelectPageRelative(-1, false);
EXPECT_EQ(0, pagination()->selected_page());
}
TEST_F(PaginationModelTest, SelectPageRelativeMiddle) {
// Test starts with 5 pages. Select page 2.
pagination()->SelectPage(2, false);
pagination()->SelectPageRelative(-1, false);
EXPECT_EQ(1, pagination()->selected_page());
pagination()->SelectPageRelative(1, false);
EXPECT_EQ(2, pagination()->selected_page());
}
// Tests that only one TransitionEnd is called for the invalid page selection
// and no TransitionEnd happens for the reverse animation of the invalid page
// selection..
TEST_F(PaginationModelTest, NoTransitionEndForRevertingAnimation) {
// Attempts to go beyond the first page.
SetStartPageAndExpects(0, 0, 0, 1);
pagination()->SelectPageRelative(-1, /*animate=*/true);
WaitForPagingAnimation();
WaitForRevertAnimation();
EXPECT_EQ(1, observer_.transition_start_call_count());
EXPECT_EQ(1, observer_.transition_end_call_count());
// Attempts to go beyond the last page.
SetStartPageAndExpects(pagination()->total_pages() - 1, 0, 0, 1);
pagination()->SelectPageRelative(1, /*animate=*/true);
WaitForPagingAnimation();
WaitForRevertAnimation();
EXPECT_EQ(1, observer_.transition_start_call_count());
EXPECT_EQ(1, observer_.transition_end_call_count());
}
// Tests that a canceled scroll will call both TransitionStart and
// TransitionEnd.
TEST_F(PaginationModelTest, CancelAnimationHasOneTransitionEnd) {
const int kStartPage = 2;
// Scroll to the next page (negative delta) and cancel it.
SetStartPageAndExpects(kStartPage, 0, 1, 0);
pagination()->StartScroll();
pagination()->UpdateScroll(-0.1);
EXPECT_EQ(kStartPage + 1, pagination()->transition().target_page);
pagination()->EndScroll(true); // Cancel transition
WaitForPagingAnimation();
EXPECT_EQ(0, observer_.selection_count());
EXPECT_EQ(1, observer_.transition_start_call_count());
EXPECT_EQ(1, observer_.transition_end_call_count());
}
} // namespace test
} // namespace ash