Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / test / layouttest_support.cc
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/shell/renderer/test_runner/WebFrameTestProxy.h"
15 #include "content/shell/renderer/test_runner/WebTestProxy.h"
16 #include "content/test/test_media_stream_client.h"
17 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
18 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
19 #include "third_party/WebKit/public/platform/WebGamepads.h"
20
21 #if defined(OS_MACOSX)
22 #include "content/browser/renderer_host/popup_menu_helper_mac.h"
23 #endif
24
25 using blink::WebDeviceMotionData;
26 using blink::WebDeviceOrientationData;
27 using blink::WebGamepads;
28 using blink::WebRect;
29 using blink::WebSize;
30 using WebTestRunner::WebFrameTestProxy;
31 using WebTestRunner::WebTestProxy;
32 using WebTestRunner::WebTestProxyBase;
33
34 namespace content {
35
36 namespace {
37
38 base::LazyInstance<base::Callback<void(RenderView*, WebTestProxyBase*)> >::Leaky
39     g_callback = LAZY_INSTANCE_INITIALIZER;
40
41 RenderViewImpl* CreateWebTestProxy(RenderViewImplParams* params) {
42   typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ProxyType;
43   ProxyType* render_view_proxy = new ProxyType(
44       reinterpret_cast<RenderViewImplParams*>(params));
45   if (g_callback == 0)
46     return render_view_proxy;
47   g_callback.Get().Run(
48       static_cast<RenderView*>(render_view_proxy), render_view_proxy);
49   return render_view_proxy;
50 }
51
52 WebTestProxyBase* GetWebTestProxyBase(RenderViewImpl* render_view) {
53   typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ViewProxy;
54
55   ViewProxy* render_view_proxy = static_cast<ViewProxy*>(render_view);
56   return static_cast<WebTestProxyBase*>(render_view_proxy);
57 }
58
59 RenderFrameImpl* CreateWebFrameTestProxy(
60     RenderViewImpl* render_view,
61     int32 routing_id) {
62   typedef WebFrameTestProxy<RenderFrameImpl, RenderViewImpl*, int32> FrameProxy;
63
64   FrameProxy* render_frame_proxy = new FrameProxy(render_view, routing_id);
65   render_frame_proxy->setBaseProxy(GetWebTestProxyBase(render_view));
66   render_frame_proxy->setVersion(3);
67
68   return render_frame_proxy;
69 }
70
71 }  // namespace
72
73
74 void EnableWebTestProxyCreation(
75     const base::Callback<void(RenderView*, WebTestProxyBase*)>& callback) {
76   g_callback.Get() = callback;
77   RenderViewImpl::InstallCreateHook(CreateWebTestProxy);
78   RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy);
79 }
80
81 void SetMockGamepads(const WebGamepads& pads) {
82   RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(pads);
83 }
84
85 void SetMockDeviceMotionData(const WebDeviceMotionData& data) {
86   RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data);
87 }
88
89 void SetMockDeviceOrientationData(const WebDeviceOrientationData& data) {
90   RendererWebKitPlatformSupportImpl::
91       SetMockDeviceOrientationDataForTesting(data);
92 }
93
94 void EnableRendererLayoutTestMode() {
95   RenderThreadImpl::current()->set_layout_test_mode(true);
96 }
97
98 void EnableBrowserLayoutTestMode() {
99 #if defined(OS_MACOSX)
100   ImageTransportSurface::SetAllowOSMesaForTesting(true);
101   PopupMenuHelper::DontShowPopupMenuForTesting();
102 #endif
103   RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
104 }
105
106 int GetLocalSessionHistoryLength(RenderView* render_view) {
107   return static_cast<RenderViewImpl*>(render_view)->
108       GetLocalSessionHistoryLengthForTesting();
109 }
110
111 void SyncNavigationState(RenderView* render_view) {
112   static_cast<RenderViewImpl*>(render_view)->SyncNavigationState();
113 }
114
115 void SetFocusAndActivate(RenderView* render_view, bool enable) {
116   static_cast<RenderViewImpl*>(render_view)->
117       SetFocusAndActivateForTesting(enable);
118 }
119
120 void ForceResizeRenderView(RenderView* render_view,
121                            const WebSize& new_size) {
122   RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view);
123   render_view_impl->ForceResizeForTesting(new_size);
124   GetWebTestProxyBase(render_view_impl)->didForceResize();
125 }
126
127 void SetDeviceScaleFactor(RenderView* render_view, float factor) {
128   static_cast<RenderViewImpl*>(render_view)->
129       SetDeviceScaleFactorForTesting(factor);
130 }
131
132 void UseSynchronousResizeMode(RenderView* render_view, bool enable) {
133   static_cast<RenderViewImpl*>(render_view)->
134       UseSynchronousResizeModeForTesting(enable);
135 }
136
137 void EnableAutoResizeMode(RenderView* render_view,
138                           const WebSize& min_size,
139                           const WebSize& max_size) {
140   static_cast<RenderViewImpl*>(render_view)->
141       EnableAutoResizeForTesting(min_size, max_size);
142 }
143
144 void DisableAutoResizeMode(RenderView* render_view, const WebSize& new_size) {
145   static_cast<RenderViewImpl*>(render_view)->
146       DisableAutoResizeForTesting(new_size);
147 }
148
149 void UseMockMediaStreams(RenderView* render_view) {
150   RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view);
151   render_view_impl->SetMediaStreamClientForTesting(
152       new TestMediaStreamClient(render_view_impl));
153 }
154
155 }  // namespace content