Add ICU message format support
[chromium-blink-merge.git] / content / browser / renderer_host / input / touch_action_browsertest.cc
blobd1c5e462ef4c76f3379386398c5683b047c43e70
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"
6 #include "base/bind.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;
35 namespace {
37 const char kTouchActionDataURL[] =
38 "data:text/html;charset=utf-8,"
39 "<!DOCTYPE html>"
40 "<meta name='viewport' content='width=device-width'/>"
41 "<style>"
42 "html, body {"
43 " margin: 0;"
44 "}"
45 ".box {"
46 " height: 96px;"
47 " width: 96px;"
48 " border: 2px solid blue;"
49 "}"
50 ".spacer { height: 1000px; }"
51 ".ta-none { touch-action: none; }"
52 "</style>"
53 "<div class=box></div>"
54 "<div class='box ta-none'></div>"
55 "<div class=spacer></div>"
56 "<script>"
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); "
62 " }"
63 " document.title='ready';"
64 "</script>";
66 } // namespace
68 namespace content {
71 class TouchActionBrowserTest : public ContentBrowserTest {
72 public:
73 TouchActionBrowserTest() {}
74 ~TouchActionBrowserTest() override {}
76 RenderWidgetHostImpl* GetWidgetHost() {
77 return RenderWidgetHostImpl::From(shell()->web_contents()->
78 GetRenderViewHost());
81 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) {
82 EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result);
83 runner_->Quit();
86 protected:
87 void LoadURL() {
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
103 // regions.
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) {
117 int value = 0;
118 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
119 shell()->web_contents(),
120 "domAutomationController.send(" + script + ")",
121 &value));
122 return value;
125 int GetScrollTop() {
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(
149 gesture.Pass(),
150 base::Bind(&TouchActionBrowserTest::OnSyntheticGestureCompleted,
151 base::Unretained(this)));
153 // Runs until we get the OnSyntheticGestureCompleted callback
154 runner_->Run();
155 runner_ = NULL;
157 // Check the scroll offset
158 int scrollTop = GetScrollTop();
159 if (scrollTop == 0)
160 return false;
162 EXPECT_EQ(distance.y(), scrollTop);
163 return true;
166 private:
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
181 #else
182 #define MAYBE_DefaultAuto DefaultAuto
183 #endif
184 IN_PROC_BROWSER_TEST_F(TouchActionBrowserTest, MAYBE_DefaultAuto) {
185 LoadURL();
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
202 #else
203 #define MAYBE_TouchActionNone TouchActionNone
204 #endif
205 IN_PROC_BROWSER_TEST_F(TouchActionBrowserTest, MAYBE_TouchActionNone) {
206 LoadURL();
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