1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
content / browser / web_contents / aura / gesture_nav_simple_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/web_contents/aura/gesture_nav_simple.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/site_instance.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "ui/aura/window.h"
#include <memory>
namespace content {
namespace {
// A subclass of TestWebContents that offers a fake content window.
class GestureNavTestWebContents : public TestWebContents {
public:
explicit GestureNavTestWebContents(
BrowserContext* browser_context,
std::unique_ptr<aura::Window> fake_native_view,
std::unique_ptr<aura::Window> fake_contents_window)
: TestWebContents(browser_context),
fake_native_view_(std::move(fake_native_view)),
fake_contents_window_(std::move(fake_contents_window)) {}
~GestureNavTestWebContents() override {}
static std::unique_ptr<GestureNavTestWebContents> Create(
BrowserContext* browser_context,
scoped_refptr<SiteInstance> instance,
std::unique_ptr<aura::Window> fake_native_view,
std::unique_ptr<aura::Window> fake_contents_window) {
std::unique_ptr<GestureNavTestWebContents> web_contents =
std::make_unique<GestureNavTestWebContents>(
browser_context, std::move(fake_native_view),
std::move(fake_contents_window));
web_contents->Init(
WebContents::CreateParams(browser_context, std::move(instance)),
blink::FramePolicy());
return web_contents;
}
// Overriden from TestWebContents:
gfx::NativeView GetNativeView() override { return fake_native_view_.get(); }
gfx::NativeView GetContentNativeView() override {
return fake_contents_window_.get();
}
private:
std::unique_ptr<aura::Window> fake_native_view_;
std::unique_ptr<aura::Window> fake_contents_window_;
};
} // namespace
class GestureNavSimpleTest : public RenderViewHostImplTestHarness {
public:
GestureNavSimpleTest()
: first_("https://www.google.com"), second_("http://www.chromium.org") {}
~GestureNavSimpleTest() override = default;
GestureNavSimpleTest(const GestureNavSimpleTest&) = delete;
GestureNavSimpleTest& operator=(const GestureNavSimpleTest&) = delete;
protected:
// RenderViewHostImplTestHarness:
void SetUp() override {
RenderViewHostImplTestHarness::SetUp();
// Set up the fake web contents native view.
auto fake_native_view = std::make_unique<aura::Window>(nullptr);
fake_native_view->Init(ui::LAYER_SOLID_COLOR);
root_window()->AddChild(fake_native_view.get());
fake_native_view->SetBounds(gfx::Rect(root_window()->bounds().size()));
// Set up the fake contents window.
auto fake_contents_window = std::make_unique<aura::Window>(nullptr);
fake_contents_window->Init(ui::LAYER_SOLID_COLOR);
root_window()->AddChild(fake_contents_window.get());
fake_contents_window->SetBounds(gfx::Rect(root_window()->bounds().size()));
// Replace the default test web contents with our custom class.
SetContents(GestureNavTestWebContents::Create(
browser_context(), SiteInstance::Create(browser_context()),
std::move(fake_native_view), std::move(fake_contents_window)));
// Add two pages to navigation history.
contents()->NavigateAndCommit(first_);
EXPECT_TRUE(controller().GetVisibleEntry());
EXPECT_FALSE(controller().CanGoBack());
contents()->NavigateAndCommit(second_);
EXPECT_TRUE(controller().CanGoBack());
EXPECT_FALSE(controller().CanGoForward());
gesture_nav_simple_ = std::make_unique<GestureNavSimple>(contents());
}
void TearDown() override {
gesture_nav_simple_ = nullptr;
RenderViewHostImplTestHarness::TearDown();
}
const GURL first() const { return first_; }
const GURL second() const { return second_; }
void OnOverscrollModeChange(OverscrollMode old_mode,
OverscrollMode new_mode,
OverscrollSource source,
cc::OverscrollBehavior behavior) {
gesture_nav_simple_->OnOverscrollModeChange(old_mode, new_mode, source,
behavior);
}
void OnOverscrollComplete(OverscrollMode overscroll_mode) {
gesture_nav_simple_->OnOverscrollComplete(overscroll_mode);
}
OverscrollMode mode() const { return gesture_nav_simple_->mode_; }
OverscrollSource source() const { return gesture_nav_simple_->source_; }
private:
// Test URLs.
const GURL first_;
const GURL second_;
std::unique_ptr<GestureNavSimple> gesture_nav_simple_;
};
// Tests that setting 'overscroll-behavior-x' to 'auto' allows gesture-nav.
TEST_F(GestureNavSimpleTest, OverscrollBehaviorXAutoAllowsGestureNav) {
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
cc::OverscrollBehavior behavior_x_auto;
behavior_x_auto.x = cc::OverscrollBehavior::Type::kAuto;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_EAST,
OverscrollSource::TOUCHSCREEN, behavior_x_auto);
EXPECT_EQ(OverscrollMode::OVERSCROLL_EAST, mode());
EXPECT_EQ(OverscrollSource::TOUCHSCREEN, source());
}
// Tests that setting 'overscroll-behavior-x' to 'contain' prevents gesture-nav.
TEST_F(GestureNavSimpleTest, OverscrollBehaviorXContainPreventsGestureNav) {
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
cc::OverscrollBehavior behavior_x_contain;
behavior_x_contain.x = cc::OverscrollBehavior::Type::kContain;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_EAST,
OverscrollSource::TOUCHSCREEN, behavior_x_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
}
// Tests that setting 'overscroll-behavior-x' to 'none' prevents gesture-nav.
TEST_F(GestureNavSimpleTest, OverscrollBehaviorXNonePreventsGestureNav) {
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
cc::OverscrollBehavior behavior_x_none;
behavior_x_none.x = cc::OverscrollBehavior::Type::kNone;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_EAST,
OverscrollSource::TOUCHSCREEN, behavior_x_none);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
}
// Tests that setting 'overscroll-behavior-y' to 'auto' allows pull-to-refresh.
TEST_F(GestureNavSimpleTest, OverscrollBehaviorYAutoAllowsPullToRefresh) {
cc::OverscrollBehavior behavior_y_auto;
behavior_y_auto.y = cc::OverscrollBehavior::Type::kAuto;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_SOUTH,
OverscrollSource::TOUCHSCREEN, behavior_y_auto);
EXPECT_EQ(OverscrollMode::OVERSCROLL_SOUTH, mode());
EXPECT_EQ(OverscrollSource::TOUCHSCREEN, source());
}
// Tests that setting 'overscroll-behavior-y' to 'contain' prevents
// pull-to-refresh.
TEST_F(GestureNavSimpleTest, OverscrollBehaviorYContainPreventsPullToRefresh) {
cc::OverscrollBehavior behavior_y_contain;
behavior_y_contain.y = cc::OverscrollBehavior::Type::kContain;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_SOUTH,
OverscrollSource::TOUCHSCREEN, behavior_y_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
}
// Tests that setting 'overscroll-behavior-y' to 'none' prevents
// pull-to-refresh.
TEST_F(GestureNavSimpleTest, OverscrollBehaviorYNonePreventsPullToRefresh) {
cc::OverscrollBehavior behavior_y_none;
behavior_y_none.y = cc::OverscrollBehavior::Type::kNone;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_SOUTH,
OverscrollSource::TOUCHSCREEN, behavior_y_none);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
}
// Tests that setting 'overscroll-behavior-x' to a value that prevents
// gesture-nav after it has started does not affect aborting it.
TEST_F(GestureNavSimpleTest, PreventGestureNavBeforeAbort) {
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
cc::OverscrollBehavior behavior_x_auto;
behavior_x_auto.x = cc::OverscrollBehavior::Type::kAuto;
cc::OverscrollBehavior behavior_x_contain;
behavior_x_contain.x = cc::OverscrollBehavior::Type::kContain;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_EAST,
OverscrollSource::TOUCHSCREEN, behavior_x_auto);
EXPECT_EQ(OverscrollMode::OVERSCROLL_EAST, mode());
EXPECT_EQ(OverscrollSource::TOUCHSCREEN, source());
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_EAST,
OverscrollMode::OVERSCROLL_NONE,
OverscrollSource::TOUCHSCREEN, behavior_x_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
}
// Tests that after gesture-nav was prevented due to 'overscroll-behavior-x',
// setting it to 'auto' does not affect aborting overscroll.
TEST_F(GestureNavSimpleTest, AllowGestureNavBeforeAbort) {
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
cc::OverscrollBehavior behavior_x_contain;
behavior_x_contain.x = cc::OverscrollBehavior::Type::kContain;
cc::OverscrollBehavior behavior_x_auto;
behavior_x_auto.x = cc::OverscrollBehavior::Type::kAuto;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_EAST,
OverscrollSource::TOUCHSCREEN, behavior_x_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_EAST,
OverscrollMode::OVERSCROLL_NONE,
OverscrollSource::TOUCHSCREEN, behavior_x_auto);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
}
// Tests that preventing gesture-nav using 'overscroll-behavior-x' does not
// affect completing overscroll.
TEST_F(GestureNavSimpleTest, CompletePreventedGestureNav) {
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
cc::OverscrollBehavior behavior_x_contain;
behavior_x_contain.x = cc::OverscrollBehavior::Type::kContain;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_EAST,
OverscrollSource::TOUCHSCREEN, behavior_x_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
OnOverscrollComplete(OverscrollMode::OVERSCROLL_EAST);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
EXPECT_EQ(second(), contents()->GetLastCommittedURL());
}
// Tests that setting 'overscroll-behavior-y' to a value that prevents
// pull-to-refresh after it has started does not affect aborting it.
TEST_F(GestureNavSimpleTest, PreventPullToRefreshBeforeAbort) {
cc::OverscrollBehavior behavior_y_auto;
behavior_y_auto.y = cc::OverscrollBehavior::Type::kAuto;
cc::OverscrollBehavior behavior_y_contain;
behavior_y_contain.y = cc::OverscrollBehavior::Type::kContain;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_SOUTH,
OverscrollSource::TOUCHSCREEN, behavior_y_auto);
EXPECT_EQ(OverscrollMode::OVERSCROLL_SOUTH, mode());
EXPECT_EQ(OverscrollSource::TOUCHSCREEN, source());
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_SOUTH,
OverscrollMode::OVERSCROLL_NONE,
OverscrollSource::TOUCHSCREEN, behavior_y_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
}
// Tests that after pull-to-refresh was prevented due to
// 'overscroll-behavior-y', setting it to 'auto' does not affect aborting
// overscroll.
TEST_F(GestureNavSimpleTest, AllowPullToRefreshBeforeAbort) {
cc::OverscrollBehavior behavior_y_contain;
behavior_y_contain.y = cc::OverscrollBehavior::Type::kContain;
cc::OverscrollBehavior behavior_y_auto;
behavior_y_auto.y = cc::OverscrollBehavior::Type::kAuto;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_SOUTH,
OverscrollSource::TOUCHSCREEN, behavior_y_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_SOUTH,
OverscrollMode::OVERSCROLL_NONE,
OverscrollSource::TOUCHSCREEN, behavior_y_auto);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
}
// Tests that preventing pull-to-refresh using 'overscroll-behavior-y' does not
// affect completing overscroll.
TEST_F(GestureNavSimpleTest, CompletePreventedPullToRefresh) {
cc::OverscrollBehavior behavior_y_contain;
behavior_y_contain.y = cc::OverscrollBehavior::Type::kContain;
OnOverscrollModeChange(OverscrollMode::OVERSCROLL_NONE,
OverscrollMode::OVERSCROLL_SOUTH,
OverscrollSource::TOUCHSCREEN, behavior_y_contain);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
OnOverscrollComplete(OverscrollMode::OVERSCROLL_SOUTH);
EXPECT_EQ(OverscrollMode::OVERSCROLL_NONE, mode());
EXPECT_EQ(OverscrollSource::NONE, source());
}
} // namespace content