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.
5 #include "content/public/test/layouttest_support.h"
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/WebDeviceOrientationData.h"
17 #include "third_party/WebKit/public/platform/WebGamepads.h"
18 #include "third_party/WebKit/public/testing/WebFrameTestProxy.h"
19 #include "third_party/WebKit/public/testing/WebTestProxy.h"
21 #if defined(OS_WIN) && !defined(USE_AURA)
22 #include "content/browser/web_contents/web_contents_drag_win.h"
25 #if defined(OS_MACOSX)
26 #include "content/browser/renderer_host/popup_menu_helper_mac.h"
29 using blink::WebDeviceMotionData
;
30 using blink::WebDeviceOrientationData
;
31 using blink::WebGamepads
;
34 using WebTestRunner::WebFrameTestProxy
;
35 using WebTestRunner::WebTestProxy
;
36 using WebTestRunner::WebTestProxyBase
;
42 base::LazyInstance
<base::Callback
<void(RenderView
*, WebTestProxyBase
*)> >::Leaky
43 g_callback
= LAZY_INSTANCE_INITIALIZER
;
45 RenderViewImpl
* CreateWebTestProxy(RenderViewImplParams
* params
) {
46 typedef WebTestProxy
<RenderViewImpl
, RenderViewImplParams
*> ProxyType
;
47 ProxyType
* render_view_proxy
= new ProxyType(
48 reinterpret_cast<RenderViewImplParams
*>(params
));
50 return render_view_proxy
;
52 static_cast<RenderView
*>(render_view_proxy
), render_view_proxy
);
53 return render_view_proxy
;
56 WebTestProxyBase
* GetWebTestProxyBase(RenderViewImpl
* render_view
) {
57 typedef WebTestProxy
<RenderViewImpl
, RenderViewImplParams
*> ViewProxy
;
59 ViewProxy
* render_view_proxy
= static_cast<ViewProxy
*>(render_view
);
60 return static_cast<WebTestProxyBase
*>(render_view_proxy
);
63 RenderFrameImpl
* CreateWebFrameTestProxy(
64 RenderViewImpl
* render_view
,
66 typedef WebFrameTestProxy
<RenderFrameImpl
, RenderViewImpl
*, int32
> FrameProxy
;
68 FrameProxy
* render_frame_proxy
= new FrameProxy(render_view
, routing_id
);
69 render_frame_proxy
->setBaseProxy(GetWebTestProxyBase(render_view
));
70 render_frame_proxy
->setVersion(3);
72 return render_frame_proxy
;
78 void EnableWebTestProxyCreation(
79 const base::Callback
<void(RenderView
*, WebTestProxyBase
*)>& callback
) {
80 g_callback
.Get() = callback
;
81 RenderViewImpl::InstallCreateHook(CreateWebTestProxy
);
82 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy
);
85 void SetMockGamepads(const WebGamepads
& pads
) {
86 RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(pads
);
89 void SetMockDeviceMotionData(const WebDeviceMotionData
& data
) {
90 RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data
);
93 void SetMockDeviceOrientationData(const WebDeviceOrientationData
& data
) {
94 RendererWebKitPlatformSupportImpl::
95 SetMockDeviceOrientationDataForTesting(data
);
98 void EnableRendererLayoutTestMode() {
99 RenderThreadImpl::current()->set_layout_test_mode(true);
102 void EnableBrowserLayoutTestMode() {
103 #if defined(OS_MACOSX)
104 ImageTransportSurface::SetAllowOSMesaForTesting(true);
105 PopupMenuHelper::DontShowPopupMenuForTesting();
106 #elif defined(OS_WIN) && !defined(USE_AURA)
107 WebContentsDragWin::DisableDragDropForTesting();
109 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
112 int GetLocalSessionHistoryLength(RenderView
* render_view
) {
113 return static_cast<RenderViewImpl
*>(render_view
)->
114 GetLocalSessionHistoryLengthForTesting();
117 void SyncNavigationState(RenderView
* render_view
) {
118 static_cast<RenderViewImpl
*>(render_view
)->SyncNavigationState();
121 void SetFocusAndActivate(RenderView
* render_view
, bool enable
) {
122 static_cast<RenderViewImpl
*>(render_view
)->
123 SetFocusAndActivateForTesting(enable
);
126 void ForceResizeRenderView(RenderView
* render_view
,
127 const WebSize
& new_size
) {
128 RenderViewImpl
* render_view_impl
= static_cast<RenderViewImpl
*>(render_view
);
129 render_view_impl
->ForceResizeForTesting(new_size
);
130 GetWebTestProxyBase(render_view_impl
)->didForceResize();
133 void SetDeviceScaleFactor(RenderView
* render_view
, float factor
) {
134 static_cast<RenderViewImpl
*>(render_view
)->
135 SetDeviceScaleFactorForTesting(factor
);
138 void UseSynchronousResizeMode(RenderView
* render_view
, bool enable
) {
139 static_cast<RenderViewImpl
*>(render_view
)->
140 UseSynchronousResizeModeForTesting(enable
);
143 void EnableAutoResizeMode(RenderView
* render_view
,
144 const WebSize
& min_size
,
145 const WebSize
& max_size
) {
146 static_cast<RenderViewImpl
*>(render_view
)->
147 EnableAutoResizeForTesting(min_size
, max_size
);
150 void DisableAutoResizeMode(RenderView
* render_view
, const WebSize
& new_size
) {
151 static_cast<RenderViewImpl
*>(render_view
)->
152 DisableAutoResizeForTesting(new_size
);
155 void UseMockMediaStreams(RenderView
* render_view
) {
156 RenderViewImpl
* render_view_impl
= static_cast<RenderViewImpl
*>(render_view
);
157 render_view_impl
->SetMediaStreamClientForTesting(
158 new TestMediaStreamClient(render_view_impl
));
161 } // namespace content