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 "config.h" | |
6 #include <gtest/gtest.h> | |
7 | |
8 #include "platform/geometry/IntPoint.h" | |
9 #include "platform/geometry/IntSize.h" | |
10 #include "platform/mac/ScrollElasticityController.h" | |
11 #include "platform/PlatformWheelEvent.h" | |
12 | |
13 namespace blink { | |
14 | |
15 class MockScrollElasticityControllerClient : public ScrollElasticityControllerCl
ient { | |
16 public: | |
17 MockScrollElasticityControllerClient() : m_pinned(true), m_stretchX(0.0f) {} | |
18 | |
19 virtual bool allowsHorizontalStretching() override { return true; } | |
20 virtual bool allowsVerticalStretching() override { return true; } | |
21 // The amount that the view is stretched past the normal allowable bounds. | |
22 // The "overhang" amount. | |
23 virtual IntSize stretchAmount() override { return IntSize(m_stretchX, 0); } | |
24 virtual bool pinnedInDirection(const FloatSize&) override { return m_pinned;
} | |
25 virtual bool canScrollHorizontally() override { return true; } | |
26 virtual bool canScrollVertically() override { return true; } | |
27 | |
28 // Return the absolute scroll position, not relative to the scroll origin. | |
29 virtual IntPoint absoluteScrollPosition() override { return IntPoint(m_stret
chX, 0); } | |
30 | |
31 virtual void immediateScrollBy(const FloatSize& size) override { m_stretchX
+= size.width(); } | |
32 virtual void immediateScrollByWithoutContentEdgeConstraints(const FloatSize&
size) override { m_stretchX += size.width(); } | |
33 virtual void startSnapRubberbandTimer() override {} | |
34 virtual void stopSnapRubberbandTimer() override {} | |
35 virtual void adjustScrollPositionToBoundsIfNecessary() override {} | |
36 | |
37 void reset() { m_stretchX = 0; } | |
38 | |
39 bool m_pinned; | |
40 | |
41 private: | |
42 float m_stretchX; | |
43 }; | |
44 | |
45 class MockPlatformWheelEvent : public PlatformWheelEvent { | |
46 public: | |
47 MockPlatformWheelEvent(PlatformWheelEventPhase phase, float deltaX, bool can
RubberbandLeft) | |
48 { | |
49 m_phase = phase; | |
50 m_deltaX = deltaX; | |
51 m_canRubberbandLeft = canRubberbandLeft; | |
52 } | |
53 }; | |
54 | |
55 enum RubberbandPermission { | |
56 DisallowRubberband, | |
57 AllowRubberband | |
58 }; | |
59 | |
60 enum ScrollPermission { | |
61 DisallowScroll, | |
62 AllowScroll | |
63 }; | |
64 | |
65 const float deltaLeft = 1.0f; | |
66 const float deltaNone = 0.0f; | |
67 | |
68 class ScrollElasticityControllerTest : public testing::Test { | |
69 public: | |
70 ScrollElasticityControllerTest() : m_controller(&m_client) {} | |
71 | |
72 // Makes a wheel event with the given parameters, and forwards the event to
the | |
73 // controller. | |
74 bool handleWheelEvent(RubberbandPermission canRubberband, ScrollPermission c
anScroll, PlatformWheelEventPhase phase, float delta) | |
75 { | |
76 m_client.m_pinned = !canScroll; | |
77 MockPlatformWheelEvent event(phase, delta, canRubberband); | |
78 return m_controller.handleWheelEvent(event); | |
79 } | |
80 | |
81 void SetUp() | |
82 { | |
83 m_client.reset(); | |
84 } | |
85 | |
86 private: | |
87 MockScrollElasticityControllerClient m_client; | |
88 ScrollElasticityController m_controller; | |
89 }; | |
90 | |
91 // The client cannot scroll, but the event allows rubber banding. | |
92 TEST_F(ScrollElasticityControllerTest, canRubberband) | |
93 { | |
94 // The PlatformWheelEventPhaseMayBegin event should never be handled. | |
95 EXPECT_FALSE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheel
EventPhaseMayBegin, deltaNone)); | |
96 | |
97 // All other events should be handled. | |
98 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseBegan, deltaLeft)); | |
99 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseChanged, deltaLeft)); | |
100 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseEnded, deltaNone)); | |
101 } | |
102 | |
103 // The client cannot scroll, and the event disallows rubber banding. | |
104 TEST_F(ScrollElasticityControllerTest, cannotRubberband) | |
105 { | |
106 // The PlatformWheelEventPhaseMayBegin event should never be handled. | |
107 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWh
eelEventPhaseMayBegin, deltaNone)); | |
108 | |
109 // Rubber-banding is disabled, and the client is pinned. | |
110 // Ignore all events. | |
111 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWh
eelEventPhaseBegan, deltaLeft)); | |
112 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWh
eelEventPhaseChanged, deltaLeft)); | |
113 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWh
eelEventPhaseEnded, deltaNone)); | |
114 } | |
115 | |
116 // The client can scroll, and the event disallows rubber banding. | |
117 TEST_F(ScrollElasticityControllerTest, cannotRubberbandCanScroll) | |
118 { | |
119 // The PlatformWheelEventPhaseMayBegin event should never be handled. | |
120 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, AllowScroll, PlatformWheel
EventPhaseMayBegin, deltaNone)); | |
121 | |
122 // Rubber-banding is disabled, but the client is not pinned. | |
123 // Handle all events. | |
124 EXPECT_TRUE(handleWheelEvent(DisallowRubberband, AllowScroll, PlatformWheelE
ventPhaseBegan, deltaLeft)); | |
125 EXPECT_TRUE(handleWheelEvent(DisallowRubberband, AllowScroll, PlatformWheelE
ventPhaseChanged, deltaLeft)); | |
126 EXPECT_TRUE(handleWheelEvent(DisallowRubberband, AllowScroll, PlatformWheelE
ventPhaseEnded, deltaNone)); | |
127 } | |
128 | |
129 // The client cannot scroll. The initial events allow rubber banding, but the | |
130 // later events disallow it. | |
131 TEST_F(ScrollElasticityControllerTest, canRubberbandBecomesFalse) | |
132 { | |
133 // The PlatformWheelEventPhaseMayBegin event should never be handled. | |
134 EXPECT_FALSE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheel
EventPhaseMayBegin, deltaNone)); | |
135 | |
136 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseBegan, deltaLeft)); | |
137 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseChanged, deltaLeft)); | |
138 | |
139 // Rubber-banding is no longer allowed, but its already started. | |
140 // Events should still be handled. | |
141 EXPECT_TRUE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWhe
elEventPhaseChanged, deltaLeft)); | |
142 EXPECT_TRUE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWhe
elEventPhaseEnded, deltaNone)); | |
143 } | |
144 | |
145 // The client cannot scroll. The initial events disallow rubber banding, but the | |
146 // later events allow it. | |
147 TEST_F(ScrollElasticityControllerTest, canRubberbandBecomesTrue) | |
148 { | |
149 // The PlatformWheelEventPhaseMayBegin event should never be handled. | |
150 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWh
eelEventPhaseMayBegin, deltaNone)); | |
151 | |
152 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWh
eelEventPhaseBegan, deltaLeft)); | |
153 EXPECT_FALSE(handleWheelEvent(DisallowRubberband, DisallowScroll, PlatformWh
eelEventPhaseChanged, deltaLeft)); | |
154 | |
155 // Rubber-banding is now allowed | |
156 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseChanged, deltaLeft)); | |
157 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseEnded, deltaNone)); | |
158 } | |
159 | |
160 // Events with no delta should not cause scrolling. | |
161 TEST_F(ScrollElasticityControllerTest, zeroDeltaEventsIgnored) | |
162 { | |
163 // The PlatformWheelEventPhaseMayBegin event should never be handled. | |
164 EXPECT_FALSE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheel
EventPhaseMayBegin, deltaNone)); | |
165 | |
166 // TODO(erikchen): This logic is incorrect. The zero delta event should not
have been handled. crbug.com/375512 | |
167 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseBegan, deltaNone)); | |
168 EXPECT_FALSE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheel
EventPhaseChanged, deltaNone)); | |
169 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseChanged, deltaLeft)); | |
170 EXPECT_TRUE(handleWheelEvent(AllowRubberband, DisallowScroll, PlatformWheelE
ventPhaseEnded, deltaNone)); | |
171 } | |
172 | |
173 } // namespace blink | |
OLD | NEW |