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 WebKit::WebDeviceMotionData
;
30 using WebKit::WebDeviceOrientationData
;
31 using WebKit::WebGamepads
;
32 using WebKit::WebRect
;
33 using WebKit::WebSize
;
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 RenderFrameImpl
* CreateWebFrameTestProxy(
57 RenderViewImpl
* render_view
,
59 typedef WebTestProxy
<RenderViewImpl
, RenderViewImplParams
*> ViewProxy
;
60 typedef WebFrameTestProxy
<RenderFrameImpl
, RenderViewImpl
*, int32
> FrameProxy
;
62 ViewProxy
* render_view_proxy
= static_cast<ViewProxy
*>(render_view
);
63 WebTestProxyBase
* base
= static_cast<WebTestProxyBase
*>(render_view_proxy
);
64 FrameProxy
* render_frame_proxy
= new FrameProxy(render_view
, routing_id
);
65 render_frame_proxy
->setBaseProxy(base
);
66 render_frame_proxy
->setVersion(3);
68 return render_frame_proxy
;
74 void EnableWebTestProxyCreation(
75 const base::Callback
<void(RenderView
*, WebTestProxyBase
*)>& callback
) {
76 g_callback
.Get() = callback
;
77 RenderViewImpl::InstallCreateHook(CreateWebTestProxy
);
78 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy
);
81 void SetMockGamepads(const WebGamepads
& pads
) {
82 RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(pads
);
85 void SetMockDeviceMotionData(const WebDeviceMotionData
& data
) {
86 RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data
);
89 void SetMockDeviceOrientationData(const WebDeviceOrientationData
& data
) {
90 RendererWebKitPlatformSupportImpl::
91 SetMockDeviceOrientationDataForTesting(data
);
94 void EnableRendererLayoutTestMode() {
95 RenderThreadImpl::current()->set_layout_test_mode(true);
98 void EnableBrowserLayoutTestMode() {
99 #if defined(OS_MACOSX)
100 ImageTransportSurface::SetAllowOSMesaForTesting(true);
101 PopupMenuHelper::DontShowPopupMenuForTesting();
102 #elif defined(OS_WIN) && !defined(USE_AURA)
103 WebContentsDragWin::DisableDragDropForTesting();
105 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
108 int GetLocalSessionHistoryLength(RenderView
* render_view
) {
109 return static_cast<RenderViewImpl
*>(render_view
)
110 ->GetLocalSessionHistoryLengthForTesting();
113 void SyncNavigationState(RenderView
* render_view
) {
114 static_cast<RenderViewImpl
*>(render_view
)->SyncNavigationState();
117 void SetFocusAndActivate(RenderView
* render_view
, bool enable
) {
118 static_cast<RenderViewImpl
*>(render_view
)
119 ->SetFocusAndActivateForTesting(enable
);
122 void ForceResizeRenderView(RenderView
* render_view
,
123 const WebSize
& new_size
) {
124 RenderViewImpl
* render_view_impl
= static_cast<RenderViewImpl
*>(render_view
);
125 render_view_impl
->setWindowRect(WebRect(render_view_impl
->rootWindowRect().x
,
126 render_view_impl
->rootWindowRect().y
,
131 void SetDeviceScaleFactor(RenderView
* render_view
, float factor
) {
132 static_cast<RenderViewImpl
*>(render_view
)
133 ->SetDeviceScaleFactorForTesting(factor
);
136 void UseSynchronousResizeMode(RenderView
* render_view
, bool enable
) {
137 static_cast<RenderViewImpl
*>(render_view
)
138 ->UseSynchronousResizeModeForTesting(enable
);
141 void EnableAutoResizeMode(RenderView
* render_view
,
142 const WebSize
& min_size
,
143 const WebSize
& max_size
) {
144 static_cast<RenderViewImpl
*>(render_view
)
145 ->EnableAutoResizeForTesting(min_size
, max_size
);
148 void DisableAutoResizeMode(RenderView
* render_view
, const WebSize
& new_size
) {
149 static_cast<RenderViewImpl
*>(render_view
)
150 ->DisableAutoResizeForTesting(new_size
);
153 void UseMockMediaStreams(RenderView
* render_view
) {
154 RenderViewImpl
* render_view_impl
= static_cast<RenderViewImpl
*>(render_view
);
155 render_view_impl
->SetMediaStreamClientForTesting(
156 new TestMediaStreamClient(render_view_impl
));
159 } // namespace content