OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/web_contents/web_contents_view_aura.h" | 5 #include "content/browser/web_contents/web_contents_view_aura.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
13 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
14 #endif | 14 #endif |
15 #include "content/browser/frame_host/navigation_controller_impl.h" | 15 #include "content/browser/frame_host/navigation_controller_impl.h" |
16 #include "content/browser/frame_host/navigation_entry_impl.h" | 16 #include "content/browser/frame_host/navigation_entry_impl.h" |
17 #include "content/browser/frame_host/navigation_entry_screenshot_manager.h" | 17 #include "content/browser/frame_host/navigation_entry_screenshot_manager.h" |
18 #include "content/browser/renderer_host/render_widget_host_view_aura.h" | 18 #include "content/browser/renderer_host/render_widget_host_view_aura.h" |
19 #include "content/browser/web_contents/web_contents_impl.h" | 19 #include "content/browser/web_contents/web_contents_impl.h" |
20 #include "content/browser/web_contents/web_contents_view.h" | 20 #include "content/browser/web_contents/web_contents_view.h" |
21 #include "content/common/input/synthetic_web_input_event_builders.h" | |
22 #include "content/common/input_messages.h" | |
21 #include "content/common/view_messages.h" | 23 #include "content/common/view_messages.h" |
24 #include "content/public/browser/browser_message_filter.h" | |
22 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
23 #include "content/public/browser/web_contents_observer.h" | 26 #include "content/public/browser/web_contents_observer.h" |
24 #include "content/public/common/content_switches.h" | 27 #include "content/public/common/content_switches.h" |
25 #include "content/public/test/browser_test_utils.h" | 28 #include "content/public/test/browser_test_utils.h" |
26 #include "content/public/test/content_browser_test.h" | 29 #include "content/public/test/content_browser_test.h" |
27 #include "content/public/test/content_browser_test_utils.h" | 30 #include "content/public/test/content_browser_test_utils.h" |
28 #include "content/public/test/test_renderer_host.h" | 31 #include "content/public/test/test_renderer_host.h" |
29 #include "content/public/test/test_utils.h" | 32 #include "content/public/test/test_utils.h" |
30 #include "content/shell/browser/shell.h" | 33 #include "content/shell/browser/shell.h" |
31 #include "ui/aura/window.h" | 34 #include "ui/aura/window.h" |
32 #include "ui/aura/window_tree_host.h" | 35 #include "ui/aura/window_tree_host.h" |
33 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 36 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
34 #include "ui/events/event_processor.h" | 37 #include "ui/events/event_processor.h" |
38 #include "ui/events/event_switches.h" | |
35 #include "ui/events/event_utils.h" | 39 #include "ui/events/event_utils.h" |
36 #include "ui/events/test/event_generator.h" | 40 #include "ui/events/test/event_generator.h" |
37 | 41 |
42 namespace { | |
43 | |
44 void GiveItSomeTime() { | |
45 base::RunLoop run_loop; | |
46 base::MessageLoop::current()->PostDelayedTask( | |
47 FROM_HERE, | |
48 run_loop.QuitClosure(), | |
49 base::TimeDelta::FromMillisecondsD(0.001)); | |
tdresser
2014/08/15 23:10:32
0.001ms is obviously a terrible value, I'm only us
| |
50 run_loop.Run(); | |
51 } | |
52 | |
53 } //namespace | |
54 | |
55 | |
38 namespace content { | 56 namespace content { |
39 | 57 |
40 // This class keeps track of the RenderViewHost whose screenshot was captured. | 58 // This class keeps track of the RenderViewHost whose screenshot was captured. |
41 class ScreenshotTracker : public NavigationEntryScreenshotManager { | 59 class ScreenshotTracker : public NavigationEntryScreenshotManager { |
42 public: | 60 public: |
43 explicit ScreenshotTracker(NavigationControllerImpl* controller) | 61 explicit ScreenshotTracker(NavigationControllerImpl* controller) |
44 : NavigationEntryScreenshotManager(controller), | 62 : NavigationEntryScreenshotManager(controller), |
45 screenshot_taken_for_(NULL), | 63 screenshot_taken_for_(NULL), |
46 waiting_for_screenshots_(0) { | 64 waiting_for_screenshots_(0) { |
47 } | 65 } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 if (should_quit_loop_) | 138 if (should_quit_loop_) |
121 base::MessageLoop::current()->Quit(); | 139 base::MessageLoop::current()->Quit(); |
122 } | 140 } |
123 | 141 |
124 bool navigated_; | 142 bool navigated_; |
125 bool should_quit_loop_; | 143 bool should_quit_loop_; |
126 | 144 |
127 DISALLOW_COPY_AND_ASSIGN(NavigationWatcher); | 145 DISALLOW_COPY_AND_ASSIGN(NavigationWatcher); |
128 }; | 146 }; |
129 | 147 |
148 class InputEventMessageFilterWaitsForAcks : public BrowserMessageFilter { | |
149 public: | |
150 InputEventMessageFilterWaitsForAcks() | |
151 : BrowserMessageFilter(InputMsgStart), | |
152 type_(blink::WebInputEvent::Undefined), | |
153 state_(INPUT_EVENT_ACK_STATE_UNKNOWN) {} | |
154 | |
155 void WaitForAck(blink::WebInputEvent::Type type) { | |
156 base::RunLoop run_loop; | |
157 base::AutoReset<base::Closure> reset_quit(&quit_, run_loop.QuitClosure()); | |
158 base::AutoReset<blink::WebInputEvent::Type> reset_type(&type_, type); | |
159 run_loop.Run(); | |
160 } | |
161 | |
162 InputEventAckState last_ack_state() const { return state_; } | |
163 | |
164 protected: | |
165 virtual ~InputEventMessageFilterWaitsForAcks() {} | |
166 | |
167 private: | |
168 void ReceivedEventAck(blink::WebInputEvent::Type type, | |
169 InputEventAckState state) { | |
170 if (type_ == type) { | |
171 state_ = state; | |
172 quit_.Run(); | |
173 } | |
174 } | |
175 | |
176 // BrowserMessageFilter: | |
177 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | |
178 if (message.type() == InputHostMsg_HandleInputEvent_ACK::ID) { | |
179 InputHostMsg_HandleInputEvent_ACK::Param params; | |
180 InputHostMsg_HandleInputEvent_ACK::Read(&message, ¶ms); | |
181 blink::WebInputEvent::Type type = params.a.type; | |
182 InputEventAckState ack = params.a.state; | |
183 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
184 base::Bind(&InputEventMessageFilterWaitsForAcks::ReceivedEventAck, | |
185 this, type, ack)); | |
186 } | |
187 return false; | |
188 } | |
189 | |
190 base::Closure quit_; | |
191 blink::WebInputEvent::Type type_; | |
192 InputEventAckState state_; | |
193 | |
194 DISALLOW_COPY_AND_ASSIGN(InputEventMessageFilterWaitsForAcks); | |
195 }; | |
196 | |
130 class WebContentsViewAuraTest : public ContentBrowserTest { | 197 class WebContentsViewAuraTest : public ContentBrowserTest { |
131 public: | 198 public: |
132 WebContentsViewAuraTest() | 199 WebContentsViewAuraTest() |
133 : screenshot_manager_(NULL) { | 200 : screenshot_manager_(NULL) { |
134 } | 201 } |
135 | 202 |
136 // Executes the javascript synchronously and makes sure the returned value is | 203 // Executes the javascript synchronously and makes sure the returned value is |
137 // freed properly. | 204 // freed properly. |
138 void ExecuteSyncJSFunction(RenderFrameHost* rfh, const std::string& jscript) { | 205 void ExecuteSyncJSFunction(RenderFrameHost* rfh, const std::string& jscript) { |
139 scoped_ptr<base::Value> value = | 206 scoped_ptr<base::Value> value = |
140 content::ExecuteScriptAndGetValue(rfh, jscript); | 207 content::ExecuteScriptAndGetValue(rfh, jscript); |
141 } | 208 } |
142 | 209 |
143 // Starts the test server and navigates to the given url. Sets a large enough | 210 // Starts the test server and navigates to the given url. Sets a large enough |
144 // size to the root window. Returns after the navigation to the url is | 211 // size to the root window. Returns after the navigation to the url is |
145 // complete. | 212 // complete. |
146 void StartTestWithPage(const std::string& url) { | 213 void StartTestWithPage(const std::string& url) { |
147 ASSERT_TRUE(test_server()->Start()); | 214 ASSERT_TRUE(test_server()->Start()); |
148 GURL test_url(test_server()->GetURL(url)); | 215 GURL test_url(test_server()->GetURL(url)); |
149 NavigateToURL(shell(), test_url); | 216 NavigateToURL(shell(), test_url); |
150 | 217 |
151 WebContentsImpl* web_contents = | 218 WebContentsImpl* web_contents = |
152 static_cast<WebContentsImpl*>(shell()->web_contents()); | 219 static_cast<WebContentsImpl*>(shell()->web_contents()); |
153 NavigationControllerImpl* controller = &web_contents->GetController(); | 220 NavigationControllerImpl* controller = &web_contents->GetController(); |
154 | 221 |
155 screenshot_manager_ = new ScreenshotTracker(controller); | 222 screenshot_manager_ = new ScreenshotTracker(controller); |
156 controller->SetScreenshotManager(screenshot_manager_); | 223 controller->SetScreenshotManager(screenshot_manager_); |
157 } | 224 } |
158 | 225 |
226 virtual void SetUpCommandLine(CommandLine* cmd) OVERRIDE { | |
227 cmd->AppendSwitchASCII(switches::kTouchEvents, | |
228 switches::kTouchEventsEnabled); | |
229 } | |
230 | |
159 void TestOverscrollNavigation(bool touch_handler) { | 231 void TestOverscrollNavigation(bool touch_handler) { |
160 ASSERT_NO_FATAL_FAILURE( | 232 ASSERT_NO_FATAL_FAILURE( |
161 StartTestWithPage("files/overscroll_navigation.html")); | 233 StartTestWithPage("files/overscroll_navigation.html")); |
162 WebContentsImpl* web_contents = | 234 WebContentsImpl* web_contents = |
163 static_cast<WebContentsImpl*>(shell()->web_contents()); | 235 static_cast<WebContentsImpl*>(shell()->web_contents()); |
164 NavigationController& controller = web_contents->GetController(); | 236 NavigationController& controller = web_contents->GetController(); |
165 RenderFrameHost* main_frame = web_contents->GetMainFrame(); | 237 RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
166 | 238 |
167 EXPECT_FALSE(controller.CanGoBack()); | 239 EXPECT_FALSE(controller.CanGoBack()); |
168 EXPECT_FALSE(controller.CanGoForward()); | 240 EXPECT_FALSE(controller.CanGoForward()); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 static_cast<WebContentsImpl*>(shell()->web_contents()); | 321 static_cast<WebContentsImpl*>(shell()->web_contents()); |
250 RenderFrameHost* main_frame = web_contents->GetMainFrame(); | 322 RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
251 int index = -1; | 323 int index = -1; |
252 scoped_ptr<base::Value> value; | 324 scoped_ptr<base::Value> value; |
253 value = content::ExecuteScriptAndGetValue(main_frame, "get_current()"); | 325 value = content::ExecuteScriptAndGetValue(main_frame, "get_current()"); |
254 if (!value->GetAsInteger(&index)) | 326 if (!value->GetAsInteger(&index)) |
255 index = -1; | 327 index = -1; |
256 return index; | 328 return index; |
257 } | 329 } |
258 | 330 |
331 int ExecuteScriptAndExtractInt(const std::string& script) { | |
332 int value = 0; | |
333 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( | |
334 shell()->web_contents(), | |
335 "domAutomationController.send(" + script + ")", | |
336 &value)); | |
337 return value; | |
338 } | |
339 | |
340 RenderViewHost* GetRenderViewHost() const { | |
341 RenderViewHost* const rvh = shell()->web_contents()->GetRenderViewHost(); | |
342 CHECK(rvh); | |
343 return rvh; | |
344 } | |
345 | |
346 RenderWidgetHostImpl* GetRenderWidgetHost() const { | |
347 RenderWidgetHostImpl* const rwh = | |
348 RenderWidgetHostImpl::From(shell() | |
349 ->web_contents() | |
350 ->GetRenderWidgetHostView() | |
351 ->GetRenderWidgetHost()); | |
352 CHECK(rwh); | |
353 return rwh; | |
354 } | |
355 | |
356 RenderWidgetHostViewBase* GetRenderWidgetHostView() const { | |
357 return static_cast<RenderWidgetHostViewBase*>( | |
358 GetRenderViewHost()->GetView()); | |
359 } | |
360 | |
361 InputEventMessageFilterWaitsForAcks* filter() { | |
362 return filter_.get(); | |
363 } | |
364 | |
365 void WaitAFrame() { | |
366 uint32 frame = GetRenderWidgetHostView()->RendererFrameNumber(); | |
367 while (!GetRenderWidgetHost()->ScheduleComposite()) | |
368 GiveItSomeTime(); | |
369 while (GetRenderWidgetHostView()->RendererFrameNumber() == frame) | |
370 GiveItSomeTime(); | |
371 } | |
372 | |
259 protected: | 373 protected: |
260 ScreenshotTracker* screenshot_manager() { return screenshot_manager_; } | 374 ScreenshotTracker* screenshot_manager() { return screenshot_manager_; } |
261 void set_min_screenshot_interval(int interval_ms) { | 375 void set_min_screenshot_interval(int interval_ms) { |
262 screenshot_manager_->SetScreenshotInterval(interval_ms); | 376 screenshot_manager_->SetScreenshotInterval(interval_ms); |
263 } | 377 } |
264 | 378 |
379 void AddInputEventMessageFilter() { | |
380 filter_ = new InputEventMessageFilterWaitsForAcks(); | |
381 GetRenderWidgetHost()->GetProcess()->AddFilter(filter_); | |
382 } | |
383 | |
265 private: | 384 private: |
266 ScreenshotTracker* screenshot_manager_; | 385 ScreenshotTracker* screenshot_manager_; |
386 scoped_refptr<InputEventMessageFilterWaitsForAcks> filter_; | |
267 | 387 |
268 DISALLOW_COPY_AND_ASSIGN(WebContentsViewAuraTest); | 388 DISALLOW_COPY_AND_ASSIGN(WebContentsViewAuraTest); |
269 }; | 389 }; |
270 | 390 |
271 // Flaky on Windows: http://crbug.com/305722 | 391 // Flaky on Windows: http://crbug.com/305722 |
272 #if defined(OS_WIN) | 392 #if defined(OS_WIN) |
273 #define MAYBE_OverscrollNavigation DISABLED_OverscrollNavigation | 393 #define MAYBE_OverscrollNavigation DISABLED_OverscrollNavigation |
274 #else | 394 #else |
275 #define MAYBE_OverscrollNavigation OverscrollNavigation | 395 #define MAYBE_OverscrollNavigation OverscrollNavigation |
276 #endif | 396 #endif |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
740 | 860 |
741 RenderWidgetHostViewAura* rwhva = | 861 RenderWidgetHostViewAura* rwhva = |
742 static_cast<RenderWidgetHostViewAura*>( | 862 static_cast<RenderWidgetHostViewAura*>( |
743 shell()->web_contents()->GetRenderWidgetHostView()); | 863 shell()->web_contents()->GetRenderWidgetHostView()); |
744 rwhva->ResetHasSnappedToBoundary(); | 864 rwhva->ResetHasSnappedToBoundary(); |
745 EXPECT_FALSE(rwhva->has_snapped_to_boundary()); | 865 EXPECT_FALSE(rwhva->has_snapped_to_boundary()); |
746 window->AddChild(shell()->web_contents()->GetNativeView()); | 866 window->AddChild(shell()->web_contents()->GetNativeView()); |
747 EXPECT_TRUE(rwhva->has_snapped_to_boundary()); | 867 EXPECT_TRUE(rwhva->has_snapped_to_boundary()); |
748 } | 868 } |
749 | 869 |
870 // Tests that touch moves are not throttled when performing a scroll gesture on | |
871 // a non-scrollable area, except during gesture-nav. | |
872 IN_PROC_BROWSER_TEST_F(WebContentsViewAuraTest, | |
873 OverscrollNavigationTouchThrottling) { | |
874 ASSERT_NO_FATAL_FAILURE( | |
875 StartTestWithPage("files/overscroll_navigation.html")); | |
876 | |
877 AddInputEventMessageFilter(); | |
878 | |
879 WebContentsImpl* web_contents = | |
880 static_cast<WebContentsImpl*>(shell()->web_contents()); | |
881 aura::Window* content = web_contents->GetContentNativeView(); | |
882 gfx::Rect bounds = content->GetBoundsInRootWindow(); | |
883 const int dx = 20; | |
884 | |
885 ExecuteSyncJSFunction(web_contents->GetMainFrame(), | |
886 "install_touchmove_handler()"); | |
887 | |
888 WaitAFrame(); | |
tdresser
2014/08/15 23:10:32
Dana, these WaitAFrame() calls are obviously terri
| |
889 | |
890 for (int navigated = 0; navigated <= 1; ++navigated) { | |
891 if (navigated) { | |
892 ExecuteSyncJSFunction(web_contents->GetMainFrame(), "navigate_next()"); | |
893 ExecuteSyncJSFunction(web_contents->GetMainFrame(), | |
894 "reset_touchmove_count()"); | |
895 } | |
896 // Send touch press. | |
897 SyntheticWebTouchEvent touch; | |
898 touch.PressPoint(bounds.x() + 2, bounds.y() + 10); | |
899 GetRenderWidgetHost()->ForwardTouchEventWithLatencyInfo(touch, | |
900 ui::LatencyInfo()); | |
901 filter()->WaitForAck(blink::WebInputEvent::TouchStart); | |
902 // Assert on the ack, because we'll end up waiting for acks that will never | |
903 // come if this is not true. | |
904 ASSERT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, filter()->last_ack_state()); | |
905 | |
906 // Send first touch move, and then a scroll begin. | |
907 touch.MovePoint(0, bounds.x() + 20 + 1 * dx, bounds.y() + 100); | |
908 GetRenderWidgetHost()->ForwardTouchEventWithLatencyInfo(touch, | |
909 ui::LatencyInfo()); | |
910 filter()->WaitForAck(blink::WebInputEvent::TouchMove); | |
911 ASSERT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, filter()->last_ack_state()); | |
912 | |
913 blink::WebGestureEvent scroll_begin = | |
914 SyntheticWebGestureEventBuilder::BuildScrollBegin(1, 1); | |
915 GetRenderWidgetHost()->ForwardGestureEventWithLatencyInfo( | |
916 scroll_begin, ui::LatencyInfo()); | |
917 // Scroll begin ignores ack disposition, so don't wait for the ack. | |
918 // GiveItSomeTime(); | |
919 WaitAFrame(); | |
920 | |
921 // First touchmove already sent, start at 2. | |
922 for (int i = 2; i <= 10; ++i) { | |
923 // Send a touch move, followed by a scroll update | |
924 touch.MovePoint(0, bounds.x() + 20 + i * dx, bounds.y() + 100); | |
925 GetRenderWidgetHost()->ForwardTouchEventWithLatencyInfo( | |
926 touch, ui::LatencyInfo()); | |
927 WaitAFrame(); | |
928 | |
929 blink::WebGestureEvent scroll_update = | |
930 SyntheticWebGestureEventBuilder::BuildScrollUpdate(dx, 5, 0); | |
931 | |
932 GetRenderWidgetHost()->ForwardGestureEventWithLatencyInfo( | |
933 scroll_update, ui::LatencyInfo()); | |
934 | |
935 WaitAFrame(); | |
936 } | |
937 | |
938 touch.ReleasePoint(0); | |
939 GetRenderWidgetHost()->ForwardTouchEventWithLatencyInfo(touch, | |
940 ui::LatencyInfo()); | |
941 WaitAFrame(); | |
942 | |
943 blink::WebGestureEvent scroll_end; | |
944 scroll_end.type = blink::WebInputEvent::GestureScrollEnd; | |
945 GetRenderWidgetHost()->ForwardGestureEventWithLatencyInfo( | |
946 scroll_end, ui::LatencyInfo()); | |
947 WaitAFrame(); | |
948 | |
949 if (!navigated) | |
950 EXPECT_EQ(10, ExecuteScriptAndExtractInt("touchmoveCount")); | |
951 else | |
952 EXPECT_GT(10, ExecuteScriptAndExtractInt("touchmoveCount")); | |
953 } | |
954 } | |
955 | |
750 } // namespace content | 956 } // namespace content |
OLD | NEW |