File: snap_scroll_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 5,686 bytes parent folder | download | duplicates (9)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/events/gesture_detection/snap_scroll_controller.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/test/motion_event_test_utils.h"

using base::TimeTicks;
using ui::test::MockMotionEvent;

namespace ui {
namespace {

const float kSnapBound = 8.f;

gfx::SizeF GetDisplayBounds() {
  return gfx::SizeF(640, 480);
}

}  // namespace

TEST(SnapScrollControllerTest, Basic) {
  SnapScrollController controller(GetDisplayBounds());
  EXPECT_FALSE(controller.IsSnappingScrolls());
  EXPECT_FALSE(controller.IsSnapHorizontal());
  EXPECT_FALSE(controller.IsSnapVertical());

  // Test basic horizontal snapping.
  MockMotionEvent event;
  event.PressPoint(0, 0);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, kSnapBound * 2, 0.f);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_TRUE(controller.IsSnappingScrolls());
  EXPECT_TRUE(controller.IsSnapHorizontal());
  EXPECT_FALSE(controller.IsSnapVertical());

  event.ReleasePoint();
  controller.SetSnapScrollMode(event, false, kSnapBound);

  // Test basic vertical snapping.
  event.PressPoint(0, 0);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, 0.f, kSnapBound * 2);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_TRUE(controller.IsSnappingScrolls());
  EXPECT_TRUE(controller.IsSnapVertical());
  EXPECT_FALSE(controller.IsSnapHorizontal());
}

TEST(SnapScrollControllerTest, VerticalScroll) {
  SnapScrollController controller(GetDisplayBounds());
  EXPECT_FALSE(controller.IsSnappingScrolls());

  MockMotionEvent event;
  event.PressPoint(0, 0);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, 0.f, -kSnapBound / 2.f);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, kSnapBound / 2.f, -kSnapBound * 2.f);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_TRUE(controller.IsSnapVertical());
  EXPECT_FALSE(controller.IsSnapHorizontal());

  // Initial scrolling should be snapped.
  float delta_x = event.GetX(0);
  float delta_y = event.GetY(0);
  controller.UpdateSnapScrollMode(delta_x, delta_y, kSnapBound);
  EXPECT_TRUE(controller.IsSnapVertical());
  EXPECT_FALSE(controller.IsSnapHorizontal());

  // Subsequent scrolling should be snapped as long as it's within the rails.
  delta_x = 5;
  delta_y = 10;
  controller.UpdateSnapScrollMode(delta_x, delta_y, kSnapBound);
  EXPECT_TRUE(controller.IsSnapVertical());
  EXPECT_FALSE(controller.IsSnapHorizontal());

  // Large horizontal movement should end snapping.
  delta_x = 100;
  delta_y = 10;
  controller.UpdateSnapScrollMode(delta_x, delta_y, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());
}

TEST(SnapScrollControllerTest, HorizontalScroll) {
  SnapScrollController controller(GetDisplayBounds());
  EXPECT_FALSE(controller.IsSnappingScrolls());

  MockMotionEvent event;
  event.PressPoint(0, 0);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, -kSnapBound / 2.f, 0.f);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, kSnapBound * 2.f, kSnapBound / 2.f);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_TRUE(controller.IsSnapHorizontal());
  EXPECT_FALSE(controller.IsSnapVertical());

  // Initial scrolling should be snapped.
  float delta_x = event.GetX(0);
  float delta_y = event.GetY(0);
  controller.UpdateSnapScrollMode(delta_x, delta_y, kSnapBound);
  EXPECT_TRUE(controller.IsSnapHorizontal());
  EXPECT_FALSE(controller.IsSnapVertical());

  // Subsequent scrolling should be snapped as long as it's within the rails.
  delta_x = 10;
  delta_y = 5;
  controller.UpdateSnapScrollMode(delta_x, delta_y, kSnapBound);
  EXPECT_TRUE(controller.IsSnapHorizontal());
  EXPECT_FALSE(controller.IsSnapVertical());

  // Large vertical movement should end snapping.
  delta_x = 10;
  delta_y = 100;
  controller.UpdateSnapScrollMode(delta_x, delta_y, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());
}

TEST(SnapScrollControllerTest, Diagonal) {
  SnapScrollController controller(GetDisplayBounds());
  EXPECT_FALSE(controller.IsSnappingScrolls());

  MockMotionEvent event;
  event.PressPoint(0, 0);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  // Sufficient initial diagonal motion will prevent any future snapping.
  event.MovePoint(0, kSnapBound * 3.f, -kSnapBound * 3.f);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  float delta_x = event.GetX(0);
  float delta_y = event.GetY(0);
  controller.UpdateSnapScrollMode(delta_x, delta_y, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, 0, 0);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, kSnapBound * 5, 0);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());

  event.MovePoint(0, 0, -kSnapBound * 5);
  controller.SetSnapScrollMode(event, false, kSnapBound);
  EXPECT_FALSE(controller.IsSnappingScrolls());
}

}  // namespace ui