OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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" |
| 9 |
| 10 using base::TimeDelta; |
| 11 using base::TimeTicks; |
| 12 using ui::test::MockMotionEvent; |
| 13 |
| 14 namespace ui { |
| 15 namespace { |
| 16 |
| 17 const float kSnapBound = 8.f; |
| 18 |
| 19 gfx::SizeF GetDisplayBounds() { |
| 20 return gfx::SizeF(640, 480); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 TEST(SnapScrollControllerTest, Basic) { |
| 26 SnapScrollController controller(kSnapBound, GetDisplayBounds()); |
| 27 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 28 EXPECT_FALSE(controller.IsSnapHorizontal()); |
| 29 EXPECT_FALSE(controller.IsSnapVertical()); |
| 30 |
| 31 // Test basic horizontal snapping. |
| 32 MockMotionEvent event; |
| 33 event.PressPoint(0, 0); |
| 34 controller.SetSnapScrollMode(event, false); |
| 35 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 36 |
| 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()); |
| 42 |
| 43 event.ReleasePoint(); |
| 44 controller.SetSnapScrollMode(event, false); |
| 45 |
| 46 // Test basic vertical snapping. |
| 47 event.PressPoint(0, 0); |
| 48 controller.SetSnapScrollMode(event, false); |
| 49 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 50 |
| 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()); |
| 56 } |
| 57 |
| 58 TEST(SnapScrollControllerTest, VerticalScroll) { |
| 59 SnapScrollController controller(kSnapBound, GetDisplayBounds()); |
| 60 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 61 |
| 62 MockMotionEvent event; |
| 63 event.PressPoint(0, 0); |
| 64 controller.SetSnapScrollMode(event, false); |
| 65 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 66 |
| 67 event.MovePoint(0, 0.f, -kSnapBound / 2.f); |
| 68 controller.SetSnapScrollMode(event, false); |
| 69 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 70 |
| 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()); |
| 75 |
| 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()); |
| 82 |
| 83 // Subsequent scrolling should be snapped as long as it's within the rails. |
| 84 delta_x = 5; |
| 85 delta_y = 10; |
| 86 controller.UpdateSnapScrollMode(delta_x, delta_y); |
| 87 EXPECT_TRUE(controller.IsSnapVertical()); |
| 88 EXPECT_FALSE(controller.IsSnapHorizontal()); |
| 89 |
| 90 // Large horizontal movement should end snapping. |
| 91 delta_x = 100; |
| 92 delta_y = 10; |
| 93 controller.UpdateSnapScrollMode(delta_x, delta_y); |
| 94 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 95 } |
| 96 |
| 97 TEST(SnapScrollControllerTest, HorizontalScroll) { |
| 98 SnapScrollController controller(kSnapBound, GetDisplayBounds()); |
| 99 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 100 |
| 101 MockMotionEvent event; |
| 102 event.PressPoint(0, 0); |
| 103 controller.SetSnapScrollMode(event, false); |
| 104 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 105 |
| 106 event.MovePoint(0, -kSnapBound / 2.f, 0.f); |
| 107 controller.SetSnapScrollMode(event, false); |
| 108 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 109 |
| 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()); |
| 114 |
| 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()); |
| 121 |
| 122 // Subsequent scrolling should be snapped as long as it's within the rails. |
| 123 delta_x = 10; |
| 124 delta_y = 5; |
| 125 controller.UpdateSnapScrollMode(delta_x, delta_y); |
| 126 EXPECT_TRUE(controller.IsSnapHorizontal()); |
| 127 EXPECT_FALSE(controller.IsSnapVertical()); |
| 128 |
| 129 // Large vertical movement should end snapping. |
| 130 delta_x = 10; |
| 131 delta_y = 100; |
| 132 controller.UpdateSnapScrollMode(delta_x, delta_y); |
| 133 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 134 } |
| 135 |
| 136 TEST(SnapScrollControllerTest, Diagonal) { |
| 137 SnapScrollController controller(kSnapBound, GetDisplayBounds()); |
| 138 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 139 |
| 140 MockMotionEvent event; |
| 141 event.PressPoint(0, 0); |
| 142 controller.SetSnapScrollMode(event, false); |
| 143 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 144 |
| 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()); |
| 149 |
| 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()); |
| 154 |
| 155 event.MovePoint(0, 0, 0); |
| 156 controller.SetSnapScrollMode(event, false); |
| 157 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 158 |
| 159 event.MovePoint(0, kSnapBound * 5, 0); |
| 160 controller.SetSnapScrollMode(event, false); |
| 161 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 162 |
| 163 event.MovePoint(0, 0, -kSnapBound * 5); |
| 164 controller.SetSnapScrollMode(event, false); |
| 165 EXPECT_FALSE(controller.IsSnappingScrolls()); |
| 166 } |
| 167 |
| 168 } // namespace ui |
OLD | NEW |