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 3 // found in the LICENSE file. 4 5 #include "content/public/test/layouttest_support.h" 6 7 #include "base/callback.h" 8 #include "base/lazy_instance.h" 9 #include "content/browser/renderer_host/render_widget_host_impl.h" 10 #include "content/common/gpu/image_transport_surface.h" 11 #include "content/renderer/render_thread_impl.h" 12 #include "content/renderer/render_view_impl.h" 13 #include "content/renderer/renderer_webkitplatformsupport_impl.h" 14 #include "content/test/test_media_stream_client.h" 15 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h" 16 #include "third_party/WebKit/public/platform/WebGamepads.h" 17 #include "third_party/WebKit/public/testing/WebFrameTestProxy.h" 18 #include "third_party/WebKit/public/testing/WebTestProxy.h" 19 20 #if defined(OS_WIN) && !defined(USE_AURA) 21 #include "content/browser/web_contents/web_contents_drag_win.h" 22 #endif 23 24 #if defined(OS_MACOSX) 25 #include "content/browser/renderer_host/popup_menu_helper_mac.h" 26 #endif 27 28 using WebKit::WebDeviceMotionData; 29 using WebKit::WebGamepads; 30 using WebKit::WebRect; 31 using WebKit::WebSize; 32 using WebTestRunner::WebFrameTestProxy; 33 using WebTestRunner::WebTestProxy; 34 using WebTestRunner::WebTestProxyBase; 35 36 namespace content { 37 38 namespace { 39 40 base::LazyInstance<base::Callback<void(RenderView*, WebTestProxyBase*)> >::Leaky 41 g_callback = LAZY_INSTANCE_INITIALIZER; 42 43 RenderViewImpl* CreateWebTestProxy(RenderViewImplParams* params) { 44 typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ProxyType; 45 ProxyType* render_view_proxy = new ProxyType( 46 reinterpret_cast<RenderViewImplParams*>(params)); 47 if (g_callback == 0) 48 return render_view_proxy; 49 g_callback.Get().Run( 50 static_cast<RenderView*>(render_view_proxy), render_view_proxy); 51 return render_view_proxy; 52 } 53 54 RenderFrameImpl* CreateWebFrameTestProxy( 55 RenderViewImpl* render_view, 56 int32 routing_id) { 57 typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ViewProxy; 58 typedef WebFrameTestProxy<RenderFrameImpl, RenderViewImpl*, int32> FrameProxy; 59 60 ViewProxy* render_view_proxy = static_cast<ViewProxy*>(render_view); 61 WebTestProxyBase* base = static_cast<WebTestProxyBase*>(render_view_proxy); 62 FrameProxy* render_frame_proxy = new FrameProxy(render_view, routing_id); 63 render_frame_proxy->setBaseProxy(base); 64 render_frame_proxy->setVersion(2); 65 66 return render_frame_proxy; 67 } 68 69 } // namespace 70 71 72 void EnableWebTestProxyCreation( 73 const base::Callback<void(RenderView*, WebTestProxyBase*)>& callback) { 74 g_callback.Get() = callback; 75 RenderViewImpl::InstallCreateHook(CreateWebTestProxy); 76 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy); 77 } 78 79 void SetMockGamepads(const WebGamepads& pads) { 80 RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(pads); 81 } 82 83 void SetMockDeviceMotionData(const WebDeviceMotionData& data) { 84 RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data); 85 } 86 87 void EnableRendererLayoutTestMode() { 88 RenderThreadImpl::current()->set_layout_test_mode(true); 89 } 90 91 void EnableBrowserLayoutTestMode() { 92 #if defined(OS_MACOSX) 93 ImageTransportSurface::SetAllowOSMesaForTesting(true); 94 PopupMenuHelper::DontShowPopupMenuForTesting(); 95 #elif defined(OS_WIN) && !defined(USE_AURA) 96 WebContentsDragWin::DisableDragDropForTesting(); 97 #endif 98 RenderWidgetHostImpl::DisableResizeAckCheckForTesting(); 99 } 100 101 int GetLocalSessionHistoryLength(RenderView* render_view) { 102 return static_cast<RenderViewImpl*>(render_view) 103 ->GetLocalSessionHistoryLengthForTesting(); 104 } 105 106 void SyncNavigationState(RenderView* render_view) { 107 static_cast<RenderViewImpl*>(render_view)->SyncNavigationState(); 108 } 109 110 void SetFocusAndActivate(RenderView* render_view, bool enable) { 111 static_cast<RenderViewImpl*>(render_view) 112 ->SetFocusAndActivateForTesting(enable); 113 } 114 115 void ForceResizeRenderView(RenderView* render_view, 116 const WebSize& new_size) { 117 RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view); 118 render_view_impl->setWindowRect(WebRect(render_view_impl->rootWindowRect().x, 119 render_view_impl->rootWindowRect().y, 120 new_size.width, 121 new_size.height)); 122 } 123 124 void SetDeviceScaleFactor(RenderView* render_view, float factor) { 125 static_cast<RenderViewImpl*>(render_view) 126 ->SetDeviceScaleFactorForTesting(factor); 127 } 128 129 void EnableAutoResizeMode(RenderView* render_view, 130 const WebSize& min_size, 131 const WebSize& max_size) { 132 static_cast<RenderViewImpl*>(render_view) 133 ->EnableAutoResizeForTesting(min_size, max_size); 134 } 135 136 void DisableAutoResizeMode(RenderView* render_view, const WebSize& new_size) { 137 static_cast<RenderViewImpl*>(render_view) 138 ->DisableAutoResizeForTesting(new_size); 139 } 140 141 void UseMockMediaStreams(RenderView* render_view) { 142 RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view); 143 render_view_impl->SetMediaStreamClientForTesting( 144 new TestMediaStreamClient(render_view_impl)); 145 } 146 147 } // namespace content 148