1 // Copyright 2015 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 "content/browser/web_contents/aura/overscroll_window_delegate.h"
7 #include "content/browser/renderer_host/overscroll_controller_delegate.h"
8 #include "content/public/browser/overscroll_configuration.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/aura/window.h"
12 #include "ui/events/gesture_detection/gesture_configuration.h"
13 #include "ui/events/test/event_generator.h"
18 const int kTestWindowWidth
= 600;
21 class OverscrollWindowDelegateTest
: public aura::test::AuraTestBase
,
22 public OverscrollControllerDelegate
{
24 OverscrollWindowDelegateTest()
26 overscroll_complete_(false),
27 overscroll_started_(false),
29 current_mode_(OVERSCROLL_NONE
),
30 touch_start_threshold_(content::GetOverscrollConfig(
31 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN
)),
32 touch_complete_threshold_(content::GetOverscrollConfig(
33 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE
)) {
36 ~OverscrollWindowDelegateTest() override
{}
39 overscroll_complete_
= false;
40 overscroll_started_
= false;
41 mode_changed_
= false;
42 current_mode_
= OVERSCROLL_NONE
;
43 window_
.reset(CreateNormalWindow(
44 0, root_window(), new OverscrollWindowDelegate(this, gfx::Image())));
45 window_
->SetBounds(gfx::Rect(0, 0, kTestWindowWidth
, kTestWindowWidth
));
49 aura::Window
* window() { return window_
.get(); }
51 bool overscroll_complete() { return overscroll_complete_
; }
52 bool overscroll_started() { return overscroll_started_
; }
53 bool mode_changed() { return mode_changed_
; }
55 OverscrollMode
current_mode() { return current_mode_
; }
57 const float touch_start_threshold() {
58 return touch_start_threshold_
;
61 const float touch_complete_threshold() {
62 return kTestWindowWidth
* touch_complete_threshold_
;
66 // aura::test::AuraTestBase:
67 void SetUp() override
{
68 aura::test::AuraTestBase::SetUp();
70 ui::GestureConfiguration::GetInstance()
71 ->set_max_touch_move_in_pixels_for_click(0);
74 void TearDown() override
{
76 aura::test::AuraTestBase::TearDown();
80 // OverscrollControllerDelegate:
81 gfx::Rect
GetVisibleBounds() const override
{
82 return gfx::Rect(kTestWindowWidth
, kTestWindowWidth
);
85 bool OnOverscrollUpdate(float delta_x
, float delta_y
) override
{
89 void OnOverscrollComplete(OverscrollMode overscroll_mode
) override
{
90 overscroll_complete_
= true;
93 void OnOverscrollModeChange(OverscrollMode old_mode
,
94 OverscrollMode new_mode
) override
{
96 current_mode_
= new_mode
;
97 if (current_mode_
!= OVERSCROLL_NONE
)
98 overscroll_started_
= true;
101 // Window in which the overscroll window delegate is installed.
102 scoped_ptr
<aura::Window
> window_
;
105 bool overscroll_complete_
;
106 bool overscroll_started_
;
108 OverscrollMode current_mode_
;
110 // Config defined constants.
111 const float touch_start_threshold_
;
112 const float touch_complete_threshold_
;
114 DISALLOW_COPY_AND_ASSIGN(OverscrollWindowDelegateTest
);
117 // Tests that the basic overscroll gesture works and sends updates to the
119 TEST_F(OverscrollWindowDelegateTest
, BasicOverscroll
) {
120 ui::test::EventGenerator
generator(root_window());
122 // Start an OVERSCROLL_EAST gesture.
123 generator
.GestureScrollSequence(
124 gfx::Point(0, 0), gfx::Point(touch_complete_threshold() + 1, 10),
125 base::TimeDelta::FromMilliseconds(10), 10);
126 EXPECT_TRUE(overscroll_started());
127 EXPECT_EQ(current_mode(), OVERSCROLL_EAST
);
128 EXPECT_TRUE(overscroll_complete());
131 // Start an OVERSCROLL_WEST gesture.
132 generator
.GestureScrollSequence(gfx::Point(touch_complete_threshold() + 1, 0),
134 base::TimeDelta::FromMilliseconds(10), 10);
135 EXPECT_TRUE(overscroll_started());
136 EXPECT_EQ(current_mode(), OVERSCROLL_WEST
);
137 EXPECT_TRUE(overscroll_complete());
140 // Verifies that the OverscrollWindowDelegate direction is set correctly during
142 TEST_F(OverscrollWindowDelegateTest
, BasicOverscrollModes
) {
143 ui::test::EventGenerator
generator(root_window());
144 OverscrollWindowDelegate
* delegate
=
145 static_cast<OverscrollWindowDelegate
*>(window()->delegate());
147 // Start pressing a touch, but do not start the gesture yet.
148 generator
.MoveTouch(gfx::Point(0, 0));
149 generator
.PressTouch();
150 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
152 // Slide the touch to the right.
153 generator
.MoveTouch(gfx::Point(touch_complete_threshold() + 1, 0));
154 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_EAST
);
156 // Complete the gesture.
157 generator
.ReleaseTouch();
158 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
159 EXPECT_TRUE(overscroll_complete());
161 // Start another overscroll.
162 generator
.MoveTouch(gfx::Point(touch_complete_threshold() + 1, 0));
163 generator
.PressTouch();
164 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
166 // Slide the touch to the left.
167 generator
.MoveTouch(gfx::Point(0, 0));
168 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_WEST
);
170 // Complete the gesture.
171 generator
.ReleaseTouch();
172 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
173 EXPECT_TRUE(overscroll_complete());
175 // Generate a mouse events which normally cancel the overscroll. Confirm
176 // that superfluous mode changed events are not dispatched.
178 generator
.PressLeftButton();
179 generator
.MoveMouseTo(gfx::Point(10, 10));
180 EXPECT_FALSE(mode_changed());
183 // Tests that the overscroll does not start until the gesture gets past a
184 // particular threshold.
185 TEST_F(OverscrollWindowDelegateTest
, OverscrollThreshold
) {
186 ui::test::EventGenerator
generator(root_window());
188 // Start an OVERSCROLL_EAST gesture.
189 generator
.GestureScrollSequence(gfx::Point(0, 0),
190 gfx::Point(touch_start_threshold(), 0),
191 base::TimeDelta::FromMilliseconds(10), 10);
192 EXPECT_FALSE(overscroll_started());
193 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
194 EXPECT_FALSE(overscroll_complete());
197 // Start an OVERSCROLL_WEST gesture.
198 generator
.GestureScrollSequence(gfx::Point(touch_start_threshold(), 0),
200 base::TimeDelta::FromMilliseconds(10), 10);
201 EXPECT_FALSE(overscroll_started());
202 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
203 EXPECT_FALSE(overscroll_complete());
206 // Tests that the overscroll is aborted if the gesture does not get past the
207 // completion threshold.
208 TEST_F(OverscrollWindowDelegateTest
, AbortOverscrollThreshold
) {
209 ui::test::EventGenerator
generator(root_window());
211 // Start an OVERSCROLL_EAST gesture.
212 generator
.GestureScrollSequence(gfx::Point(0, 0),
213 gfx::Point(touch_start_threshold() + 1, 0),
214 base::TimeDelta::FromMilliseconds(10), 10);
215 EXPECT_TRUE(overscroll_started());
216 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
217 EXPECT_FALSE(overscroll_complete());
220 // Start an OVERSCROLL_WEST gesture.
221 generator
.GestureScrollSequence(gfx::Point(touch_start_threshold() + 1, 0),
223 base::TimeDelta::FromMilliseconds(10), 10);
224 EXPECT_TRUE(overscroll_started());
225 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
226 EXPECT_FALSE(overscroll_complete());
229 // Tests that the overscroll is aborted if the delegate receives some other
231 TEST_F(OverscrollWindowDelegateTest
, EventAbortsOverscroll
) {
232 ui::test::EventGenerator
generator(root_window());
233 // Start an OVERSCROLL_EAST gesture, without releasing touch.
234 generator
.set_current_location(gfx::Point(0, 0));
235 generator
.PressTouch();
236 int touch_x
= touch_start_threshold() + 1;
237 generator
.MoveTouch(gfx::Point(touch_x
, 0));
238 EXPECT_TRUE(overscroll_started());
239 EXPECT_EQ(current_mode(), OVERSCROLL_EAST
);
240 EXPECT_FALSE(overscroll_complete());
242 // Dispatch a mouse event, the overscroll should be cancelled.
243 generator
.PressLeftButton();
244 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
245 EXPECT_FALSE(overscroll_complete());
247 // We should be able to restart the overscroll without lifting the finger.
248 generator
.MoveTouch(gfx::Point(touch_x
+ touch_start_threshold() + 1, 0));
249 EXPECT_EQ(current_mode(), OVERSCROLL_EAST
);
250 EXPECT_FALSE(overscroll_complete());
253 } // namespace content