[Android] Use the platform SwipeRefreshLayout
[chromium-blink-merge.git] / content / browser / android / overscroll_refresh_unittest.cc
blob70ed0dbd510d1f15e8b2a04f91b0ee386b21a99f
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 "cc/layers/layer.h"
6 #include "content/browser/android/overscroll_refresh.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace content {
11 class OverscrollRefreshTest : public OverscrollRefreshHandler,
12 public testing::Test {
13 public:
14 OverscrollRefreshTest() {}
16 // OverscrollRefreshHandler implementation.
17 bool PullStart() override {
18 started_ = true;
19 return true;
22 void PullUpdate(float delta) override { delta_ += delta; }
24 void PullRelease(bool allow_refresh) override {
25 released_ = true;
26 refresh_allowed_ = allow_refresh;
29 void PullReset() override { reset_ = true; }
31 bool GetAndResetPullStarted() {
32 bool result = started_;
33 started_ = false;
34 return result;
37 float GetAndResetPullDelta() {
38 float result = delta_;
39 delta_ = 0;
40 return result;
43 bool GetAndResetPullReleased() {
44 bool result = released_;
45 released_ = false;
46 return result;
49 bool GetAndResetRefreshAllowed() {
50 bool result = refresh_allowed_;
51 refresh_allowed_ = false;
52 return result;
55 bool GetAndResetPullReset() {
56 bool result = reset_;
57 reset_ = false;
58 return result;
61 private:
62 float delta_ = 0;
63 bool started_ = false;
64 bool released_ = false;
65 bool reset_ = false;
66 bool refresh_allowed_ = false;
69 TEST_F(OverscrollRefreshTest, Basic) {
70 OverscrollRefresh effect(this);
72 EXPECT_FALSE(effect.IsActive());
73 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
75 effect.OnScrollBegin();
76 EXPECT_FALSE(effect.IsActive());
77 EXPECT_TRUE(effect.IsAwaitingScrollUpdateAck());
79 // The initial scroll should not be consumed, as it should first be offered
80 // to content.
81 gfx::Vector2dF scroll_up(0, 10);
82 EXPECT_FALSE(effect.WillHandleScrollUpdate(scroll_up));
83 EXPECT_FALSE(effect.IsActive());
84 EXPECT_TRUE(effect.IsAwaitingScrollUpdateAck());
86 // The unconsumed, overscrolling scroll will trigger the effect.
87 effect.OnScrollUpdateAck(false);
88 EXPECT_TRUE(effect.IsActive());
89 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
90 EXPECT_TRUE(GetAndResetPullStarted());
92 // Further scrolls will be consumed.
93 EXPECT_TRUE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 50)));
94 EXPECT_EQ(50.f, GetAndResetPullDelta());
95 EXPECT_TRUE(effect.IsActive());
97 // Even scrolls in the down direction should be consumed.
98 EXPECT_TRUE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, -50)));
99 EXPECT_EQ(-50.f, GetAndResetPullDelta());
100 EXPECT_TRUE(effect.IsActive());
102 // Ending the scroll while beyond the threshold should trigger a refresh.
103 gfx::Vector2dF zero_velocity;
104 EXPECT_FALSE(GetAndResetPullReleased());
105 effect.OnScrollEnd(zero_velocity);
106 EXPECT_FALSE(effect.IsActive());
107 EXPECT_TRUE(GetAndResetPullReleased());
108 EXPECT_TRUE(GetAndResetRefreshAllowed());
111 TEST_F(OverscrollRefreshTest, NotTriggeredIfInitialYOffsetIsNotZero) {
112 OverscrollRefresh effect(this);
114 // A positive y scroll offset at the start of scroll will prevent activation,
115 // even if the subsequent scroll overscrolls upward.
116 gfx::Vector2dF nonzero_offset(0, 10);
117 bool overflow_y_hidden = false;
118 effect.OnFrameUpdated(nonzero_offset, overflow_y_hidden);
119 effect.OnScrollBegin();
121 effect.OnFrameUpdated(gfx::Vector2dF(), overflow_y_hidden);
122 ASSERT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 10)));
123 EXPECT_FALSE(effect.IsActive());
124 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
125 effect.OnScrollUpdateAck(false);
126 EXPECT_FALSE(effect.IsActive());
127 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
128 EXPECT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 500)));
129 effect.OnScrollEnd(gfx::Vector2dF());
130 EXPECT_FALSE(GetAndResetPullStarted());
131 EXPECT_FALSE(GetAndResetPullReleased());
134 TEST_F(OverscrollRefreshTest, NotTriggeredIfOverflowYHidden) {
135 OverscrollRefresh effect(this);
137 // overflow-y:hidden at the start of scroll will prevent activation.
138 gfx::Vector2dF zero_offset;
139 bool overflow_y_hidden = true;
140 effect.OnFrameUpdated(zero_offset, overflow_y_hidden);
141 effect.OnScrollBegin();
143 ASSERT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 10)));
144 EXPECT_FALSE(effect.IsActive());
145 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
146 effect.OnScrollUpdateAck(false);
147 EXPECT_FALSE(effect.IsActive());
148 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
149 EXPECT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 500)));
150 effect.OnScrollEnd(gfx::Vector2dF());
151 EXPECT_FALSE(GetAndResetPullStarted());
152 EXPECT_FALSE(GetAndResetPullReleased());
155 TEST_F(OverscrollRefreshTest, NotTriggeredIfInitialScrollDownward) {
156 OverscrollRefresh effect(this);
157 effect.OnScrollBegin();
159 // A downward initial scroll will prevent activation, even if the subsequent
160 // scroll overscrolls upward.
161 ASSERT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, -10)));
162 EXPECT_FALSE(effect.IsActive());
163 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
165 effect.OnScrollUpdateAck(false);
166 EXPECT_FALSE(effect.IsActive());
167 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
168 EXPECT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 500)));
169 effect.OnScrollEnd(gfx::Vector2dF());
170 EXPECT_FALSE(GetAndResetPullReleased());
173 TEST_F(OverscrollRefreshTest, NotTriggeredIfInitialScrollOrTouchConsumed) {
174 OverscrollRefresh effect(this);
175 effect.OnScrollBegin();
176 ASSERT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 10)));
177 ASSERT_TRUE(effect.IsAwaitingScrollUpdateAck());
179 // Consumption of the initial touchmove or scroll should prevent future
180 // activation.
181 effect.OnScrollUpdateAck(true);
182 EXPECT_FALSE(effect.IsActive());
183 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
184 EXPECT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 500)));
185 effect.OnScrollUpdateAck(false);
186 EXPECT_FALSE(effect.IsActive());
187 EXPECT_FALSE(effect.IsAwaitingScrollUpdateAck());
188 EXPECT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 500)));
189 effect.OnScrollEnd(gfx::Vector2dF());
190 EXPECT_FALSE(GetAndResetPullStarted());
191 EXPECT_FALSE(GetAndResetPullReleased());
194 TEST_F(OverscrollRefreshTest, NotTriggeredIfFlungDownward) {
195 OverscrollRefresh effect(this);
196 effect.OnScrollBegin();
197 ASSERT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 10)));
198 ASSERT_TRUE(effect.IsAwaitingScrollUpdateAck());
199 effect.OnScrollUpdateAck(false);
200 ASSERT_TRUE(effect.IsActive());
201 EXPECT_TRUE(GetAndResetPullStarted());
203 // Terminating the pull with a down-directed fling should prevent triggering.
204 effect.OnScrollEnd(gfx::Vector2dF(0, -1000));
205 EXPECT_TRUE(GetAndResetPullReleased());
206 EXPECT_FALSE(GetAndResetRefreshAllowed());
209 TEST_F(OverscrollRefreshTest, NotTriggeredIfReleasedWithoutActivation) {
210 OverscrollRefresh effect(this);
211 effect.OnScrollBegin();
212 ASSERT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 10)));
213 ASSERT_TRUE(effect.IsAwaitingScrollUpdateAck());
214 effect.OnScrollUpdateAck(false);
215 ASSERT_TRUE(effect.IsActive());
216 EXPECT_TRUE(GetAndResetPullStarted());
218 // An early release should prevent the refresh action from firing.
219 effect.ReleaseWithoutActivation();
220 effect.OnScrollEnd(gfx::Vector2dF());
221 EXPECT_TRUE(GetAndResetPullReleased());
222 EXPECT_FALSE(GetAndResetRefreshAllowed());
225 TEST_F(OverscrollRefreshTest, NotTriggeredIfReset) {
226 OverscrollRefresh effect(this);
227 effect.OnScrollBegin();
228 ASSERT_FALSE(effect.WillHandleScrollUpdate(gfx::Vector2dF(0, 10)));
229 ASSERT_TRUE(effect.IsAwaitingScrollUpdateAck());
230 effect.OnScrollUpdateAck(false);
231 ASSERT_TRUE(effect.IsActive());
232 EXPECT_TRUE(GetAndResetPullStarted());
234 // An early reset should prevent the refresh action from firing.
235 effect.Reset();
236 EXPECT_TRUE(GetAndResetPullReset());
237 effect.OnScrollEnd(gfx::Vector2dF());
238 EXPECT_FALSE(GetAndResetPullReleased());
241 } // namespace content