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),
28 current_mode_(OVERSCROLL_NONE
),
29 touch_start_threshold_(content::GetOverscrollConfig(
30 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN
)),
31 touch_complete_threshold_(content::GetOverscrollConfig(
32 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE
)) {
35 ~OverscrollWindowDelegateTest() override
{}
38 overscroll_complete_
= false;
39 overscroll_started_
= false;
40 current_mode_
= OVERSCROLL_NONE
;
41 window_
.reset(CreateNormalWindow(
42 0, root_window(), new OverscrollWindowDelegate(this, gfx::Image())));
43 window_
->SetBounds(gfx::Rect(0, 0, kTestWindowWidth
, kTestWindowWidth
));
47 aura::Window
* window() { return window_
.get(); }
49 bool overscroll_complete() { return overscroll_complete_
; }
50 bool overscroll_started() { return overscroll_started_
; }
52 OverscrollMode
current_mode() { return current_mode_
; }
54 const float touch_start_threshold() {
55 return touch_start_threshold_
;
58 const float touch_complete_threshold() {
59 return kTestWindowWidth
* touch_complete_threshold_
;
63 // aura::test::AuraTestBase:
64 void SetUp() override
{
65 aura::test::AuraTestBase::SetUp();
67 ui::GestureConfiguration::GetInstance()
68 ->set_max_touch_move_in_pixels_for_click(0);
71 void TearDown() override
{
73 aura::test::AuraTestBase::TearDown();
77 // OverscrollControllerDelegate:
78 gfx::Rect
GetVisibleBounds() const override
{
79 return gfx::Rect(kTestWindowWidth
, kTestWindowWidth
);
82 bool OnOverscrollUpdate(float delta_x
, float delta_y
) override
{
86 void OnOverscrollComplete(OverscrollMode overscroll_mode
) override
{
87 overscroll_complete_
= true;
90 void OnOverscrollModeChange(OverscrollMode old_mode
,
91 OverscrollMode new_mode
) override
{
92 current_mode_
= new_mode
;
93 if (current_mode_
!= OVERSCROLL_NONE
)
94 overscroll_started_
= true;
97 // Window in which the overscroll window delegate is installed.
98 scoped_ptr
<aura::Window
> window_
;
101 bool overscroll_complete_
;
102 bool overscroll_started_
;
104 OverscrollMode current_mode_
;
106 // Config defined constants.
107 const float touch_start_threshold_
;
108 const float touch_complete_threshold_
;
110 DISALLOW_COPY_AND_ASSIGN(OverscrollWindowDelegateTest
);
113 // Tests that the basic overscroll gesture works and sends updates to the
115 TEST_F(OverscrollWindowDelegateTest
, BasicOverscroll
) {
116 ui::test::EventGenerator
generator(root_window());
118 // Start an OVERSCROLL_EAST gesture.
119 generator
.GestureScrollSequence(
120 gfx::Point(0, 0), gfx::Point(touch_complete_threshold() + 1, 10),
121 base::TimeDelta::FromMilliseconds(10), 10);
122 EXPECT_TRUE(overscroll_started());
123 EXPECT_EQ(current_mode(), OVERSCROLL_EAST
);
124 EXPECT_TRUE(overscroll_complete());
127 // Start an OVERSCROLL_WEST gesture.
128 generator
.GestureScrollSequence(gfx::Point(touch_complete_threshold() + 1, 0),
130 base::TimeDelta::FromMilliseconds(10), 10);
131 EXPECT_TRUE(overscroll_started());
132 EXPECT_EQ(current_mode(), OVERSCROLL_WEST
);
133 EXPECT_TRUE(overscroll_complete());
136 // Verifies that the OverscrollWindowDelegate direction is set correctly during
138 TEST_F(OverscrollWindowDelegateTest
, BasicOverscrollModes
) {
139 ui::test::EventGenerator
generator(root_window());
140 OverscrollWindowDelegate
* delegate
=
141 static_cast<OverscrollWindowDelegate
*>(window()->delegate());
143 // Start pressing a touch, but do not start the gesture yet.
144 generator
.MoveTouch(gfx::Point(0, 0));
145 generator
.PressTouch();
146 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
148 // Slide the touch to the right.
149 generator
.MoveTouch(gfx::Point(touch_complete_threshold() + 1, 0));
150 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_EAST
);
152 // Complete the gesture.
153 generator
.ReleaseTouch();
154 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
155 EXPECT_TRUE(overscroll_complete());
157 // Start another overscroll.
158 generator
.MoveTouch(gfx::Point(touch_complete_threshold() + 1, 0));
159 generator
.PressTouch();
160 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
162 // Slide the touch to the left.
163 generator
.MoveTouch(gfx::Point(0, 0));
164 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_WEST
);
166 // Complete the gesture.
167 generator
.ReleaseTouch();
168 EXPECT_EQ(delegate
->overscroll_mode_
, OVERSCROLL_NONE
);
169 EXPECT_TRUE(overscroll_complete());
172 // Tests that the overscroll does not start until the gesture gets past a
173 // particular threshold.
174 TEST_F(OverscrollWindowDelegateTest
, OverscrollThreshold
) {
175 ui::test::EventGenerator
generator(root_window());
177 // Start an OVERSCROLL_EAST gesture.
178 generator
.GestureScrollSequence(gfx::Point(0, 0),
179 gfx::Point(touch_start_threshold(), 0),
180 base::TimeDelta::FromMilliseconds(10), 10);
181 EXPECT_FALSE(overscroll_started());
182 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
183 EXPECT_FALSE(overscroll_complete());
186 // Start an OVERSCROLL_WEST gesture.
187 generator
.GestureScrollSequence(gfx::Point(touch_start_threshold(), 0),
189 base::TimeDelta::FromMilliseconds(10), 10);
190 EXPECT_FALSE(overscroll_started());
191 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
192 EXPECT_FALSE(overscroll_complete());
195 // Tests that the overscroll is aborted if the gesture does not get past the
196 // completion threshold.
197 TEST_F(OverscrollWindowDelegateTest
, AbortOverscrollThreshold
) {
198 ui::test::EventGenerator
generator(root_window());
200 // Start an OVERSCROLL_EAST gesture.
201 generator
.GestureScrollSequence(gfx::Point(0, 0),
202 gfx::Point(touch_start_threshold() + 1, 0),
203 base::TimeDelta::FromMilliseconds(10), 10);
204 EXPECT_TRUE(overscroll_started());
205 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
206 EXPECT_FALSE(overscroll_complete());
209 // Start an OVERSCROLL_WEST gesture.
210 generator
.GestureScrollSequence(gfx::Point(touch_start_threshold() + 1, 0),
212 base::TimeDelta::FromMilliseconds(10), 10);
213 EXPECT_TRUE(overscroll_started());
214 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
215 EXPECT_FALSE(overscroll_complete());
218 // Tests that the overscroll is aborted if the delegate receives some other
220 TEST_F(OverscrollWindowDelegateTest
, EventAbortsOverscroll
) {
221 ui::test::EventGenerator
generator(root_window());
222 // Start an OVERSCROLL_EAST gesture, without releasing touch.
223 generator
.set_current_location(gfx::Point(0, 0));
224 generator
.PressTouch();
225 int touch_x
= touch_start_threshold() + 1;
226 generator
.MoveTouch(gfx::Point(touch_x
, 0));
227 EXPECT_TRUE(overscroll_started());
228 EXPECT_EQ(current_mode(), OVERSCROLL_EAST
);
229 EXPECT_FALSE(overscroll_complete());
231 // Dispatch a mouse event, the overscroll should be cancelled.
232 generator
.PressLeftButton();
233 EXPECT_EQ(current_mode(), OVERSCROLL_NONE
);
234 EXPECT_FALSE(overscroll_complete());
236 // We should be able to restart the overscroll without lifting the finger.
237 generator
.MoveTouch(gfx::Point(touch_x
+ touch_start_threshold() + 1, 0));
238 EXPECT_EQ(current_mode(), OVERSCROLL_EAST
);
239 EXPECT_FALSE(overscroll_complete());
242 } // namespace content