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 "base/auto_reset.h"
7 #include "base/command_line.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/browser/renderer_host/input/synthetic_gesture.h"
11 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h"
12 #include "content/browser/renderer_host/input/synthetic_gesture_target.h"
13 #include "content/browser/renderer_host/input/synthetic_smooth_scroll_gesture.h"
14 #include "content/browser/renderer_host/input/touch_event_queue.h"
15 #include "content/browser/renderer_host/render_widget_host_impl.h"
16 #include "content/browser/renderer_host/render_widget_host_view_base.h"
17 #include "content/browser/web_contents/web_contents_impl.h"
18 #include "content/common/input/synthetic_gesture_params.h"
19 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
20 #include "content/common/input_messages.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/render_widget_host_view.h"
23 #include "content/public/common/content_switches.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "content/public/test/content_browser_test.h"
26 #include "content/public/test/content_browser_test_utils.h"
27 #include "content/public/test/test_utils.h"
28 #include "content/shell/browser/shell.h"
29 #include "third_party/WebKit/public/web/WebInputEvent.h"
30 #include "ui/events/event_switches.h"
31 #include "ui/events/latency_info.h"
33 using blink::WebInputEvent
;
37 const char kTouchActionDataURL
[] =
38 "data:text/html;charset=utf-8,"
40 "<meta name='viewport' content='width=device-width'/>"
48 " border: 2px solid blue;"
50 ".spacer { height: 1000px; }"
51 ".ta-none { touch-action: none; }"
53 "<div class=box></div>"
54 "<div class='box ta-none'></div>"
55 "<div class=spacer></div>"
57 " window.eventCounts = "
58 " {touchstart:0, touchmove:0, touchend: 0, touchcancel:0};"
59 " function countEvent(e) { eventCounts[e.type]++; }"
60 " for (var evt in eventCounts) { "
61 " document.addEventListener(evt, countEvent); "
63 " document.title='ready';"
71 class TouchActionBrowserTest
: public ContentBrowserTest
{
73 TouchActionBrowserTest() {}
74 ~TouchActionBrowserTest() override
{}
76 RenderWidgetHostImpl
* GetWidgetHost() {
77 return RenderWidgetHostImpl::From(shell()->web_contents()->
81 void OnSyntheticGestureCompleted(SyntheticGesture::Result result
) {
82 EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED
, result
);
88 const GURL
data_url(kTouchActionDataURL
);
89 NavigateToURL(shell(), data_url
);
91 RenderWidgetHostImpl
* host
= GetWidgetHost();
92 scoped_refptr
<FrameWatcher
> frame_watcher(new FrameWatcher());
93 host
->GetProcess()->AddFilter(frame_watcher
.get());
94 host
->GetView()->SetSize(gfx::Size(400, 400));
96 base::string16
ready_title(base::ASCIIToUTF16("ready"));
97 TitleWatcher
watcher(shell()->web_contents(), ready_title
);
98 ignore_result(watcher
.WaitAndGetTitle());
100 // We need to wait until at least one frame has been composited
101 // otherwise the injection of the synthetic gestures may get
102 // dropped because of MainThread/Impl thread sync of touch event
104 frame_watcher
->WaitFrames(1);
107 // ContentBrowserTest:
108 void SetUpCommandLine(base::CommandLine
* cmd
) override
{
109 cmd
->AppendSwitchASCII(switches::kTouchEvents
,
110 switches::kTouchEventsEnabled
);
111 // TODO(rbyers): Remove this switch once touch-action ships.
112 // http://crbug.com/241964
113 cmd
->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures
);
116 int ExecuteScriptAndExtractInt(const std::string
& script
) {
118 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
119 shell()->web_contents(),
120 "domAutomationController.send(" + script
+ ")",
126 return ExecuteScriptAndExtractInt("document.scrollingElement.scrollTop");
129 // Generate touch events for a synthetic scroll from |point| for |distance|.
130 // Returns true if the page scrolled by the desired amount, and false if
131 // it didn't scroll at all.
132 bool DoTouchScroll(const gfx::Point
& point
, const gfx::Vector2d
& distance
) {
133 EXPECT_EQ(0, GetScrollTop());
135 int scrollHeight
= ExecuteScriptAndExtractInt(
136 "document.documentElement.scrollHeight");
137 EXPECT_EQ(1200, scrollHeight
);
139 SyntheticSmoothScrollGestureParams params
;
140 params
.gesture_source_type
= SyntheticGestureParams::TOUCH_INPUT
;
141 params
.anchor
= point
;
142 params
.distances
.push_back(-distance
);
144 runner_
= new MessageLoopRunner();
146 scoped_ptr
<SyntheticSmoothScrollGesture
> gesture(
147 new SyntheticSmoothScrollGesture(params
));
148 GetWidgetHost()->QueueSyntheticGesture(
150 base::Bind(&TouchActionBrowserTest::OnSyntheticGestureCompleted
,
151 base::Unretained(this)));
153 // Runs until we get the OnSyntheticGestureCompleted callback
157 // Check the scroll offset
158 int scrollTop
= GetScrollTop();
162 EXPECT_EQ(distance
.y(), scrollTop
);
167 scoped_refptr
<MessageLoopRunner
> runner_
;
169 DISALLOW_COPY_AND_ASSIGN(TouchActionBrowserTest
);
172 // Mac doesn't yet have a gesture recognizer, so can't support turning touch
173 // events into scroll gestures.
174 // Will be fixed with http://crbug.com/337142
175 // Flaky on Android, see https://crbug.com/376668.
177 // Verify the test infrastructure works - we can touch-scroll the page and get a
178 // touchcancel as expected.
179 #if defined(OS_MACOSX) || defined(OS_ANDROID)
180 #define MAYBE_DefaultAuto DISABLED_DefaultAuto
182 #define MAYBE_DefaultAuto DefaultAuto
184 IN_PROC_BROWSER_TEST_F(TouchActionBrowserTest
, MAYBE_DefaultAuto
) {
187 bool scrolled
= DoTouchScroll(gfx::Point(50, 50), gfx::Vector2d(0, 45));
188 EXPECT_TRUE(scrolled
);
190 EXPECT_EQ(1, ExecuteScriptAndExtractInt("eventCounts.touchstart"));
191 EXPECT_GE(ExecuteScriptAndExtractInt("eventCounts.touchmove"), 1);
192 EXPECT_EQ(1, ExecuteScriptAndExtractInt("eventCounts.touchend"));
193 EXPECT_EQ(0, ExecuteScriptAndExtractInt("eventCounts.touchcancel"));
196 // Verify that touching a touch-action: none region disables scrolling and
197 // enables all touch events to be sent.
198 // Disabled on MacOS because it doesn't support touch input.
199 // Disabled on Android due to flakiness, see https://crbug.com/376668.
200 #if defined(OS_MACOSX) || defined(OS_ANDROID)
201 #define MAYBE_TouchActionNone DISABLED_TouchActionNone
203 #define MAYBE_TouchActionNone TouchActionNone
205 IN_PROC_BROWSER_TEST_F(TouchActionBrowserTest
, MAYBE_TouchActionNone
) {
208 bool scrolled
= DoTouchScroll(gfx::Point(50, 150), gfx::Vector2d(0, 45));
209 EXPECT_FALSE(scrolled
);
211 EXPECT_EQ(1, ExecuteScriptAndExtractInt("eventCounts.touchstart"));
212 EXPECT_GE(ExecuteScriptAndExtractInt("eventCounts.touchmove"), 1);
213 EXPECT_EQ(1, ExecuteScriptAndExtractInt("eventCounts.touchend"));
214 EXPECT_EQ(0, ExecuteScriptAndExtractInt("eventCounts.touchcancel"));
217 } // namespace content