1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/events/gesture_detection/snap_scroll_controller.h"
8 #include "ui/events/test/motion_event_test_utils.h"
10 using base::TimeDelta
;
11 using base::TimeTicks
;
12 using ui::test::MockMotionEvent
;
17 const float kSnapBound
= 8.f
;
19 gfx::SizeF
GetDisplayBounds() {
20 return gfx::SizeF(640, 480);
25 TEST(SnapScrollControllerTest
, Basic
) {
26 SnapScrollController
controller(kSnapBound
, GetDisplayBounds());
27 EXPECT_FALSE(controller
.IsSnappingScrolls());
28 EXPECT_FALSE(controller
.IsSnapHorizontal());
29 EXPECT_FALSE(controller
.IsSnapVertical());
31 // Test basic horizontal snapping.
32 MockMotionEvent event
;
33 event
.PressPoint(0, 0);
34 controller
.SetSnapScrollMode(event
, false);
35 EXPECT_FALSE(controller
.IsSnappingScrolls());
37 event
.MovePoint(0, kSnapBound
* 2, 0.f
);
38 controller
.SetSnapScrollMode(event
, false);
39 EXPECT_TRUE(controller
.IsSnappingScrolls());
40 EXPECT_TRUE(controller
.IsSnapHorizontal());
41 EXPECT_FALSE(controller
.IsSnapVertical());
44 controller
.SetSnapScrollMode(event
, false);
46 // Test basic vertical snapping.
47 event
.PressPoint(0, 0);
48 controller
.SetSnapScrollMode(event
, false);
49 EXPECT_FALSE(controller
.IsSnappingScrolls());
51 event
.MovePoint(0, 0.f
, kSnapBound
* 2);
52 controller
.SetSnapScrollMode(event
, false);
53 EXPECT_TRUE(controller
.IsSnappingScrolls());
54 EXPECT_TRUE(controller
.IsSnapVertical());
55 EXPECT_FALSE(controller
.IsSnapHorizontal());
58 TEST(SnapScrollControllerTest
, VerticalScroll
) {
59 SnapScrollController
controller(kSnapBound
, GetDisplayBounds());
60 EXPECT_FALSE(controller
.IsSnappingScrolls());
62 MockMotionEvent event
;
63 event
.PressPoint(0, 0);
64 controller
.SetSnapScrollMode(event
, false);
65 EXPECT_FALSE(controller
.IsSnappingScrolls());
67 event
.MovePoint(0, 0.f
, -kSnapBound
/ 2.f
);
68 controller
.SetSnapScrollMode(event
, false);
69 EXPECT_FALSE(controller
.IsSnappingScrolls());
71 event
.MovePoint(0, kSnapBound
/ 2.f
, -kSnapBound
* 2.f
);
72 controller
.SetSnapScrollMode(event
, false);
73 EXPECT_TRUE(controller
.IsSnapVertical());
74 EXPECT_FALSE(controller
.IsSnapHorizontal());
76 // Initial scrolling should be snapped.
77 float delta_x
= event
.GetX(0);
78 float delta_y
= event
.GetY(0);
79 controller
.UpdateSnapScrollMode(delta_x
, delta_y
);
80 EXPECT_TRUE(controller
.IsSnapVertical());
81 EXPECT_FALSE(controller
.IsSnapHorizontal());
83 // Subsequent scrolling should be snapped as long as it's within the rails.
86 controller
.UpdateSnapScrollMode(delta_x
, delta_y
);
87 EXPECT_TRUE(controller
.IsSnapVertical());
88 EXPECT_FALSE(controller
.IsSnapHorizontal());
90 // Large horizontal movement should end snapping.
93 controller
.UpdateSnapScrollMode(delta_x
, delta_y
);
94 EXPECT_FALSE(controller
.IsSnappingScrolls());
97 TEST(SnapScrollControllerTest
, HorizontalScroll
) {
98 SnapScrollController
controller(kSnapBound
, GetDisplayBounds());
99 EXPECT_FALSE(controller
.IsSnappingScrolls());
101 MockMotionEvent event
;
102 event
.PressPoint(0, 0);
103 controller
.SetSnapScrollMode(event
, false);
104 EXPECT_FALSE(controller
.IsSnappingScrolls());
106 event
.MovePoint(0, -kSnapBound
/ 2.f
, 0.f
);
107 controller
.SetSnapScrollMode(event
, false);
108 EXPECT_FALSE(controller
.IsSnappingScrolls());
110 event
.MovePoint(0, kSnapBound
* 2.f
, kSnapBound
/ 2.f
);
111 controller
.SetSnapScrollMode(event
, false);
112 EXPECT_TRUE(controller
.IsSnapHorizontal());
113 EXPECT_FALSE(controller
.IsSnapVertical());
115 // Initial scrolling should be snapped.
116 float delta_x
= event
.GetX(0);
117 float delta_y
= event
.GetY(0);
118 controller
.UpdateSnapScrollMode(delta_x
, delta_y
);
119 EXPECT_TRUE(controller
.IsSnapHorizontal());
120 EXPECT_FALSE(controller
.IsSnapVertical());
122 // Subsequent scrolling should be snapped as long as it's within the rails.
125 controller
.UpdateSnapScrollMode(delta_x
, delta_y
);
126 EXPECT_TRUE(controller
.IsSnapHorizontal());
127 EXPECT_FALSE(controller
.IsSnapVertical());
129 // Large vertical movement should end snapping.
132 controller
.UpdateSnapScrollMode(delta_x
, delta_y
);
133 EXPECT_FALSE(controller
.IsSnappingScrolls());
136 TEST(SnapScrollControllerTest
, Diagonal
) {
137 SnapScrollController
controller(kSnapBound
, GetDisplayBounds());
138 EXPECT_FALSE(controller
.IsSnappingScrolls());
140 MockMotionEvent event
;
141 event
.PressPoint(0, 0);
142 controller
.SetSnapScrollMode(event
, false);
143 EXPECT_FALSE(controller
.IsSnappingScrolls());
145 // Sufficient initial diagonal motion will prevent any future snapping.
146 event
.MovePoint(0, kSnapBound
* 3.f
, -kSnapBound
* 3.f
);
147 controller
.SetSnapScrollMode(event
, false);
148 EXPECT_FALSE(controller
.IsSnappingScrolls());
150 float delta_x
= event
.GetX(0);
151 float delta_y
= event
.GetY(0);
152 controller
.UpdateSnapScrollMode(delta_x
, delta_y
);
153 EXPECT_FALSE(controller
.IsSnappingScrolls());
155 event
.MovePoint(0, 0, 0);
156 controller
.SetSnapScrollMode(event
, false);
157 EXPECT_FALSE(controller
.IsSnappingScrolls());
159 event
.MovePoint(0, kSnapBound
* 5, 0);
160 controller
.SetSnapScrollMode(event
, false);
161 EXPECT_FALSE(controller
.IsSnappingScrolls());
163 event
.MovePoint(0, 0, -kSnapBound
* 5);
164 controller
.SetSnapScrollMode(event
, false);
165 EXPECT_FALSE(controller
.IsSnappingScrolls());