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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
content / browser / navigation_transitions / physics_model_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/navigation_transitions/physics_model.h"
#include <memory>
#include "base/numerics/ranges.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
static constexpr float kScreenWidthForTesting = 1080.f;
// Test input and output for finger drag curve.
struct FingerDragCurveConfig {
std::tuple<float, base::TimeTicks> movement_timestamp;
PhysicsModel::Result expected;
};
// Test input and output for the spring models.
struct SpringConfig {
base::TimeTicks timestamp;
PhysicsModel::Result expected;
};
struct TestConfig {
std::vector<FingerDragCurveConfig> gesture_progressed;
std::vector<SpringConfig> commit_stop;
std::vector<SpringConfig> cancel;
std::vector<SpringConfig> invoke;
};
class PhysicsModelUnittest : public ::testing::Test {
public:
PhysicsModelUnittest() = default;
~PhysicsModelUnittest() override = default;
void SetUp() override {
// Simulate a Pixel6/7. The commit-stop position is 918px.
physics_model_ = std::make_unique<PhysicsModel>(
/*screen_width=*/static_cast<int>(kScreenWidthForTesting),
/*device_scale_factor=*/2.625);
}
// Nine gestures: simulate the finger moves from 0px to 900px, before the
// commit-stop 918px.
// Every 100px finger move -> every 85px foreground layer move -> every 25px
// background layer move.
std::vector<FingerDragCurveConfig> NineGestureProgressed(
base::TimeDelta increment) {
return {
FingerDragCurveConfig{
.movement_timestamp = {100.f,
NextTimeTickAfter(base::Milliseconds(0))},
.expected = PhysicsModel::Result{.foreground_offset_physical = 85,
.background_offset_physical = -245,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 170,
.background_offset_physical = -220,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 255,
.background_offset_physical = -195,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 340,
.background_offset_physical = -170,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 425,
.background_offset_physical = -145,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 510,
.background_offset_physical = -120,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 595,
.background_offset_physical = -95,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 680,
.background_offset_physical = -70,
.done = false}},
FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 765,
.background_offset_physical = -45,
.done = false}},
};
}
// Ten gestures: simulate the finger moves from 0px to 1000px, which is after
// the commit-stop position.
std::vector<FingerDragCurveConfig> TenGestureProgressed(
base::TimeDelta increment) {
auto nine = NineGestureProgressed(increment);
nine.push_back(FingerDragCurveConfig{
.movement_timestamp = {100.f, NextTimeTickAfter(increment)},
.expected = PhysicsModel::Result{.foreground_offset_physical = 850.f,
.background_offset_physical = -20.f,
.done = false}});
return nine;
}
base::TimeTicks NextTimeTickAfter(base::TimeDelta delta) {
start_ += delta;
return start_;
}
PhysicsModel* physics_model() const { return physics_model_.get(); }
private:
std::unique_ptr<PhysicsModel> physics_model_;
base::TimeTicks start_ = base::TimeTicks::Now();
};
} // namespace
// Better EXPECT_EQ output.
std::ostream& operator<<(std::ostream& os, const PhysicsModel::Result& r) {
os << "foreground offset: " << r.foreground_offset_physical
<< " background offset: " << r.background_offset_physical
<< " done: " << (r.done ? "true" : "false");
return os;
}
bool operator==(const PhysicsModel::Result& lhs,
const PhysicsModel::Result& rhs) {
return lhs.done == rhs.done &&
base::IsApproximatelyEqual(lhs.background_offset_physical,
rhs.background_offset_physical, 0.01f) &&
base::IsApproximatelyEqual(lhs.foreground_offset_physical,
rhs.foreground_offset_physical, 0.01f);
}
// Exercise the finger drag curve and the invoke spring, and skip the
// commit-stop spring completely. The finger lifts from the screen BEFORE the
// commit-stop position.
TEST_F(PhysicsModelUnittest, ProgressInvoke_LiftBeforeCommitStop) {
const TestConfig config{
.gesture_progressed = NineGestureProgressed(base::Milliseconds(100)),
.commit_stop = {},
.cancel = {},
.invoke =
{
// Same positional result. With the drag curve we don't store the
// timestamp in the physics model, so the first requested frame
// will have a `raf_since_start`=0 calculated from the wallclock,
// which gives us the same position result as the end of the drag
// curve. This won't be a problem in real life because we will
// just be drawing one more frame at the start of the animation.
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 765,
.background_offset_physical = -45,
.done = false}},
// The foreground has reached the commit-stop point. From this
// point on the background will have offset=0 - it will not
// bounce.
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1042.11,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1078.67,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical =
kScreenWidthForTesting,
.background_offset_physical = 0,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical =
kScreenWidthForTesting,
.background_offset_physical = 0,
.done = true}},
},
};
for (const auto& gesture_progress : config.gesture_progressed) {
float movement = std::get<0>(gesture_progress.movement_timestamp);
base::TimeTicks timestamp =
std::get<1>(gesture_progress.movement_timestamp);
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
// This simulates a busy browser UI thread where `PhysicsModel::OnAnimate()`
// isn't even called once after the user lifts the finger.
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureInvoked);
physics_model()->OnNavigationFinished(/*navigation_committed=*/true);
for (const auto& invoke : config.invoke) {
PhysicsModel::Result r = physics_model()->OnAnimate(invoke.timestamp);
EXPECT_EQ(r, invoke.expected);
}
}
// Exercise the finger drag curve and the invoke spring, and skipping the
// commit-stop spring completely. The finger lifts from the screen AFTER the
// commit-stop position.
TEST_F(PhysicsModelUnittest, ProgressInvoke_LiftAfterCommitStop) {
const TestConfig config{
.gesture_progressed = TenGestureProgressed(base::Milliseconds(100)),
.commit_stop = {},
.cancel = {},
.invoke =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 850,
.background_offset_physical = -20,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1055.37,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1079.2,
.background_offset_physical = 0,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical =
kScreenWidthForTesting,
.background_offset_physical = 0,
.done = true}},
},
};
for (const auto& gesture_progress : config.gesture_progressed) {
// [float, base::TimeTicks]
auto [movement, timestamp] = gesture_progress.movement_timestamp;
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureInvoked);
physics_model()->OnNavigationFinished(/*navigation_committed=*/true);
for (const auto& invoke : config.invoke) {
PhysicsModel::Result r = physics_model()->OnAnimate(invoke.timestamp);
EXPECT_EQ(r, invoke.expected);
}
}
// Exercise the finger drag curve, the commit-stop and the invoke springs. The
// finger lifts from the screen BEFORE the commit-stop position.
TEST_F(PhysicsModelUnittest, ProgressCommitStopInvoke_LiftBeforeCommitStop) {
const TestConfig config{
.gesture_progressed = NineGestureProgressed(base::Milliseconds(100)),
.commit_stop =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 765,
.background_offset_physical = -45,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 867.43,
.background_offset_physical = -14.87,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 924.33,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 951.18,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 959.68,
.background_offset_physical = 0,
.done = false}},
// The commit-stop spring is bouncing back (towards the left).
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 958.07,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 951.75,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 944.00,
.background_offset_physical = 0,
.done = false}},
},
.cancel = {},
.invoke =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1060.61,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1079.26,
.background_offset_physical = 0,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical =
kScreenWidthForTesting,
.background_offset_physical = 0,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical =
kScreenWidthForTesting,
.background_offset_physical = 0,
.done = true}},
},
};
for (const auto& gesture_progress : config.gesture_progressed) {
// [float, base::TimeTicks]
auto [movement, timestamp] = gesture_progress.movement_timestamp;
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureInvoked);
for (const auto& commit_stop : config.commit_stop) {
PhysicsModel::Result r = physics_model()->OnAnimate(commit_stop.timestamp);
EXPECT_EQ(r, commit_stop.expected);
}
physics_model()->OnNavigationFinished(/*navigation_committed=*/true);
for (const auto& invoke : config.invoke) {
PhysicsModel::Result r = physics_model()->OnAnimate(invoke.timestamp);
EXPECT_EQ(r, invoke.expected);
}
}
// Exercise the finger drag curve, the commit-stop and the invoke springs. The
// finger lifts from the screen AFTER the commit-stop position.
TEST_F(PhysicsModelUnittest, ProgressCommitStopInvoke_LiftAfterCommitStop) {
const TestConfig config{
// Ten gestures: simulate the finger moves from 0px to 1000px.
.gesture_progressed = TenGestureProgressed(base::Milliseconds(100)),
.commit_stop =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 850,
.background_offset_physical = -20,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 945.85,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 988.71,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 999.83,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 993.94,
.background_offset_physical = 0,
.done = false}},
// The commit-stop spring is bouncing back (towards the left).
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 980.58,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 965.43,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 951.49,
.background_offset_physical = 0,
.done = false}},
},
.cancel = {},
.invoke =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1060.75,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 1079.25,
.background_offset_physical = 0,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical =
kScreenWidthForTesting,
.background_offset_physical = 0,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical =
kScreenWidthForTesting,
.background_offset_physical = 0,
.done = true}},
},
};
for (const auto& gesture_progress : config.gesture_progressed) {
// [float, base::TimeTicks]
auto [movement, timestamp] = gesture_progress.movement_timestamp;
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureInvoked);
for (const auto& commit_stop : config.commit_stop) {
PhysicsModel::Result r = physics_model()->OnAnimate(commit_stop.timestamp);
EXPECT_EQ(r, commit_stop.expected);
}
physics_model()->OnNavigationFinished(/*navigation_committed=*/true);
for (const auto& invoke : config.invoke) {
PhysicsModel::Result r = physics_model()->OnAnimate(invoke.timestamp);
EXPECT_EQ(r, invoke.expected);
}
}
// Exercise the finger drag curve and the cancel springs. The finger lifts from
// the screen BEFORE the commit-stop position.
TEST_F(PhysicsModelUnittest, ProgressCancel_LiftBeforeCommitStop) {
const TestConfig config{
.gesture_progressed = NineGestureProgressed(base::Milliseconds(100)),
.commit_stop = {},
.cancel =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 765,
.background_offset_physical = -45,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 111.73,
.background_offset_physical = -237.14,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 0,
.background_offset_physical = -270,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 0,
.background_offset_physical = -270,
.done = true}},
},
.invoke = {},
};
for (const auto& gesture_progress : config.gesture_progressed) {
// [float, base::TimeTicks]
auto [movement, timestamp] = gesture_progress.movement_timestamp;
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureCancelled);
for (const auto& cancel : config.cancel) {
PhysicsModel::Result r = physics_model()->OnAnimate(cancel.timestamp);
EXPECT_EQ(r, cancel.expected);
}
}
// Exercise the finger drag curve and the cancel springs. The finger lifts from
// the screen AFTER the commit-stop.
TEST_F(PhysicsModelUnittest, ProgressCancel_LiftAfterCommitStop) {
const TestConfig config{
.gesture_progressed = TenGestureProgressed(base::Milliseconds(100)),
.commit_stop = {},
.cancel =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 850,
.background_offset_physical = -20,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 122.91,
.background_offset_physical = -233.85,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 0,
.background_offset_physical = -270,
.done = true}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 0,
.background_offset_physical = -270,
.done = true}},
},
.invoke = {},
};
for (const auto& gesture_progress : config.gesture_progressed) {
// [float, base::TimeTicks]
auto [movement, timestamp] = gesture_progress.movement_timestamp;
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureCancelled);
for (const auto& cancel : config.cancel) {
PhysicsModel::Result r = physics_model()->OnAnimate(cancel.timestamp);
EXPECT_EQ(r, cancel.expected);
}
}
// Exercise the finger drag curve and the cancel springs, as if the user has
// signal the start of the navigation and the navigation gets cancelled so fast
// that the commit-pending spring hasn't played a single frame.
TEST_F(PhysicsModelUnittest, ProgressAndCancelNav) {
const TestConfig config{
.gesture_progressed = NineGestureProgressed(base::Milliseconds(100)),
.commit_stop = {},
.cancel =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 765,
.background_offset_physical = -45,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 416.61,
.background_offset_physical = -147.47,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 111.73,
.background_offset_physical = -237.14,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 17.45,
.background_offset_physical = -264.87,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 0,
.background_offset_physical = -270,
.done = true}},
},
.invoke = {},
};
for (const auto& gesture_progress : config.gesture_progressed) {
// [float, base::TimeTicks]
auto [movement, timestamp] = gesture_progress.movement_timestamp;
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureInvoked);
physics_model()->OnNavigationFinished(/*navigation_committed=*/false);
for (const auto& cancel : config.cancel) {
PhysicsModel::Result r = physics_model()->OnAnimate(cancel.timestamp);
EXPECT_EQ(r, cancel.expected);
}
}
// Exercise the finger drag curve, commit pending springs, and the cancel
// springs. This simulates the user has signal the start of the navigation, but
// the navigation gets cancelled, for which we must bring the outgoing live page
// back.
TEST_F(PhysicsModelUnittest, ProgressCommitPendingAndCancelNav) {
const TestConfig config{
.gesture_progressed = NineGestureProgressed(base::Milliseconds(100)),
.commit_stop =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 765,
.background_offset_physical = -45,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 924.33,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 959.68,
.background_offset_physical = 0,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(100)),
.expected = {.foreground_offset_physical = 951.75,
.background_offset_physical = 0,
.done = false}},
},
.cancel =
{
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 511.11,
.background_offset_physical = -119.67,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 136.29,
.background_offset_physical = -229.91,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 21.12,
.background_offset_physical = -263.79,
.done = false}},
SpringConfig{
.timestamp = NextTimeTickAfter(base::Milliseconds(50)),
.expected = {.foreground_offset_physical = 0,
.background_offset_physical = -270,
.done = true}},
},
.invoke = {},
};
for (const auto& gesture_progress : config.gesture_progressed) {
// [float, base::TimeTicks]
auto [movement, timestamp] = gesture_progress.movement_timestamp;
PhysicsModel::Result r =
physics_model()->OnGestureProgressed(movement, timestamp);
EXPECT_EQ(r, gesture_progress.expected);
}
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureInvoked);
for (const auto& commit_stop : config.commit_stop) {
PhysicsModel::Result r = physics_model()->OnAnimate(commit_stop.timestamp);
EXPECT_EQ(r, commit_stop.expected);
}
physics_model()->OnNavigationFinished(/*navigation_committed=*/false);
for (const auto& cancel : config.cancel) {
PhysicsModel::Result r = physics_model()->OnAnimate(cancel.timestamp);
EXPECT_EQ(r, cancel.expected);
}
}
// Regression test for https://crbug.com/326850774: The CommitPending spring
// shouldn't overshoot the left edge neither.
TEST_F(PhysicsModelUnittest, CommitPendingSpringOvershootLeftEdge) {
// Simulating a fling from 1000px to 0px.
physics_model()->OnGestureProgressed(
1000, NextTimeTickAfter(base::Milliseconds(0)));
// Ten data points so we can evict the first gesture (0px to 1000px). Makes
// sure that this sequence carries enough speed.
for (int i = 0; i < 10; ++i) {
physics_model()->OnGestureProgressed(
-100, NextTimeTickAfter(base::Milliseconds(1)));
}
// Lift the finger. The physics model will switch to the commit-pending
// spring. The spring will have initial position at the left edge, and with
// the initial velocity towards the left. Without the clampping, the spring
// will keep moving to the left, which is incorrect.
physics_model()->SwitchSpringForReason(
PhysicsModel::SwitchSpringReason::kGestureInvoked);
PhysicsModel::Result first_frame =
physics_model()->OnAnimate(NextTimeTickAfter(base::Milliseconds(100)));
EXPECT_GE(first_frame.foreground_offset_physical, 0.f);
EXPECT_LE(first_frame.foreground_offset_physical, kScreenWidthForTesting);
PhysicsModel::Result second_frame =
physics_model()->OnAnimate(NextTimeTickAfter(base::Milliseconds(100)));
EXPECT_GE(second_frame.foreground_offset_physical, 0.f);
EXPECT_LE(second_frame.foreground_offset_physical, kScreenWidthForTesting);
PhysicsModel::Result third_frame =
physics_model()->OnAnimate(NextTimeTickAfter(base::Milliseconds(100)));
EXPECT_GE(third_frame.foreground_offset_physical, 0.f);
EXPECT_LE(third_frame.foreground_offset_physical, kScreenWidthForTesting);
}
} // namespace content