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/public/common/page_state.h"
12 #include "content/renderer/history_entry.h"
13 #include "content/renderer/history_serialization.h"
14 #include "content/renderer/render_frame_impl.h"
15 #include "content/renderer/render_thread_impl.h"
16 #include "content/renderer/render_view_impl.h"
17 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
18 #include "content/shell/renderer/test_runner/TestCommon.h"
19 #include "content/shell/renderer/test_runner/web_frame_test_proxy.h"
20 #include "content/shell/renderer/test_runner/web_test_proxy.h"
21 #include "third_party/WebKit/public/platform/WebBatteryStatus.h"
22 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
23 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
24 #include "third_party/WebKit/public/platform/WebGamepads.h"
25 #include "third_party/WebKit/public/web/WebHistoryItem.h"
27 #if defined(OS_MACOSX)
28 #include "content/browser/renderer_host/popup_menu_helper_mac.h"
31 using blink::WebBatteryStatus
;
32 using blink::WebDeviceMotionData
;
33 using blink::WebDeviceOrientationData
;
34 using blink::WebGamepad
;
35 using blink::WebGamepads
;
43 base::LazyInstance
<base::Callback
<void(RenderView
*, WebTestProxyBase
*)> >::Leaky
44 g_callback
= LAZY_INSTANCE_INITIALIZER
;
46 RenderViewImpl
* CreateWebTestProxy(RenderViewImplParams
* params
) {
47 typedef WebTestProxy
<RenderViewImpl
, RenderViewImplParams
*> ProxyType
;
48 ProxyType
* render_view_proxy
= new ProxyType(params
);
50 return render_view_proxy
;
51 g_callback
.Get().Run(render_view_proxy
, render_view_proxy
);
52 return render_view_proxy
;
55 WebTestProxyBase
* GetWebTestProxyBase(RenderViewImpl
* render_view
) {
56 typedef WebTestProxy
<RenderViewImpl
, RenderViewImplParams
*> ViewProxy
;
58 ViewProxy
* render_view_proxy
= static_cast<ViewProxy
*>(render_view
);
59 return static_cast<WebTestProxyBase
*>(render_view_proxy
);
62 RenderFrameImpl
* CreateWebFrameTestProxy(
63 RenderViewImpl
* render_view
,
65 typedef WebFrameTestProxy
<RenderFrameImpl
, RenderViewImpl
*, int32
> FrameProxy
;
67 FrameProxy
* render_frame_proxy
= new FrameProxy(render_view
, routing_id
);
68 render_frame_proxy
->set_base_proxy(GetWebTestProxyBase(render_view
));
70 return render_frame_proxy
;
76 void EnableWebTestProxyCreation(
77 const base::Callback
<void(RenderView
*, WebTestProxyBase
*)>& callback
) {
78 g_callback
.Get() = callback
;
79 RenderViewImpl::InstallCreateHook(CreateWebTestProxy
);
80 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy
);
83 void SetMockGamepads(const WebGamepads
& pads
) {
84 RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(pads
);
87 void MockGamepadConnected(int index
, const WebGamepad
& pad
) {
88 RendererWebKitPlatformSupportImpl::MockGamepadConnected(index
, pad
);
91 void MockGamepadDisconnected(int index
, const WebGamepad
& pad
) {
92 RendererWebKitPlatformSupportImpl::MockGamepadDisconnected(index
, pad
);
95 void SetMockDeviceMotionData(const WebDeviceMotionData
& data
) {
96 RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data
);
99 void SetMockDeviceOrientationData(const WebDeviceOrientationData
& data
) {
100 RendererWebKitPlatformSupportImpl::
101 SetMockDeviceOrientationDataForTesting(data
);
104 void SetMockScreenOrientation(
105 RenderView
* render_view
,
106 const blink::WebScreenOrientationType
& orientation
) {
107 static_cast<RenderViewImpl
*>(render_view
)
108 ->SetScreenOrientationForTesting(orientation
);
109 // FIXME(ostap): Remove this when blink side gets updated.
110 RendererWebKitPlatformSupportImpl::
111 SetMockScreenOrientationForTesting(orientation
);
114 void ResetMockScreenOrientation()
116 RendererWebKitPlatformSupportImpl::ResetMockScreenOrientationForTesting();
119 void MockBatteryStatusChanged(const WebBatteryStatus
& status
) {
120 RendererWebKitPlatformSupportImpl::MockBatteryStatusChangedForTesting(status
);
123 void EnableRendererLayoutTestMode() {
124 RenderThreadImpl::current()->set_layout_test_mode(true);
127 void EnableBrowserLayoutTestMode() {
128 #if defined(OS_MACOSX)
129 ImageTransportSurface::SetAllowOSMesaForTesting(true);
130 PopupMenuHelper::DontShowPopupMenuForTesting();
132 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
135 int GetLocalSessionHistoryLength(RenderView
* render_view
) {
136 return static_cast<RenderViewImpl
*>(render_view
)->
137 GetLocalSessionHistoryLengthForTesting();
140 void SyncNavigationState(RenderView
* render_view
) {
141 static_cast<RenderViewImpl
*>(render_view
)->SyncNavigationState();
144 void SetFocusAndActivate(RenderView
* render_view
, bool enable
) {
145 static_cast<RenderViewImpl
*>(render_view
)->
146 SetFocusAndActivateForTesting(enable
);
149 void ForceResizeRenderView(RenderView
* render_view
,
150 const WebSize
& new_size
) {
151 RenderViewImpl
* render_view_impl
= static_cast<RenderViewImpl
*>(render_view
);
152 render_view_impl
->ForceResizeForTesting(new_size
);
155 void SetDeviceScaleFactor(RenderView
* render_view
, float factor
) {
156 static_cast<RenderViewImpl
*>(render_view
)->
157 SetDeviceScaleFactorForTesting(factor
);
160 void SetDeviceColorProfile(RenderView
* render_view
, const std::string
& name
) {
161 std::vector
<char> color_profile
;
163 struct TestColorProfile
{
165 static unsigned char color_profile_data
[] = {
166 0x00,0x00,0x01,0xea,0x54,0x45,0x53,0x54,0x00,0x00,0x00,0x00,
167 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
168 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
169 0x61,0x63,0x73,0x70,0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,
170 0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
171 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
172 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x74,0x65,0x73,0x74,
173 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
174 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
175 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
176 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,
177 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x0d,
178 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x8c,
179 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x8c,0x00,0x00,0x00,0x14,
180 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xa0,0x00,0x00,0x00,0x14,
181 0x67,0x58,0x59,0x5a,0x00,0x00,0x01,0xb4,0x00,0x00,0x00,0x14,
182 0x62,0x58,0x59,0x5a,0x00,0x00,0x01,0xc8,0x00,0x00,0x00,0x14,
183 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
184 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
185 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
186 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
187 0x00,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
188 0x00,0x00,0x00,0x10,0x77,0x68,0x61,0x63,0x6b,0x65,0x64,0x2e,
189 0x69,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
190 0x00,0x00,0x00,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
191 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
192 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
193 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
194 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
195 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
196 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
197 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
198 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
199 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0x52,
200 0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,0x58,0x59,0x5a,0x20,
201 0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,
202 0x00,0x00,0x0f,0x95,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
203 0x00,0x00,0x26,0x31,0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9b,
204 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,
205 0x00,0x00,0x4f,0xa5,0x00,0x00,0x04,0xfc,0x63,0x75,0x72,0x76,
206 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33
209 return reinterpret_cast<char*>(color_profile_data
);
213 const size_t kColorProfileSizeInBytes
= 490u;
214 return kColorProfileSizeInBytes
;
218 if (name
== "sRGB") {
219 color_profile
.assign(name
.data(), name
.data() + name
.size());
220 } else if (name
== "test") {
221 TestColorProfile test
;
222 color_profile
.assign(test
.data(), test
.data() + test
.size());
225 static_cast<RenderViewImpl
*>(render_view
)->
226 SetDeviceColorProfileForTesting(color_profile
);
229 void UseSynchronousResizeMode(RenderView
* render_view
, bool enable
) {
230 static_cast<RenderViewImpl
*>(render_view
)->
231 UseSynchronousResizeModeForTesting(enable
);
234 void EnableAutoResizeMode(RenderView
* render_view
,
235 const WebSize
& min_size
,
236 const WebSize
& max_size
) {
237 static_cast<RenderViewImpl
*>(render_view
)->
238 EnableAutoResizeForTesting(min_size
, max_size
);
241 void DisableAutoResizeMode(RenderView
* render_view
, const WebSize
& new_size
) {
242 static_cast<RenderViewImpl
*>(render_view
)->
243 DisableAutoResizeForTesting(new_size
);
247 base::char16
operator()(base::char16 c
) { return tolower(c
); }
250 // Returns True if node1 < node2.
251 bool HistoryEntryCompareLess(HistoryEntry::HistoryNode
* node1
,
252 HistoryEntry::HistoryNode
* node2
) {
253 base::string16 target1
= node1
->item().target();
254 base::string16 target2
= node2
->item().target();
255 std::transform(target1
.begin(), target1
.end(), target1
.begin(), ToLower());
256 std::transform(target2
.begin(), target2
.end(), target2
.begin(), ToLower());
257 return target1
< target2
;
260 std::string
DumpHistoryItem(HistoryEntry::HistoryNode
* node
,
262 bool is_current_index
) {
265 const blink::WebHistoryItem
& item
= node
->item();
266 if (is_current_index
) {
267 result
.append("curr->");
268 result
.append(indent
- 6, ' '); // 6 == "curr->".length()
270 result
.append(indent
, ' ');
273 std::string url
= normalizeLayoutTestURL(item
.urlString().utf8());
275 if (!item
.target().isEmpty()) {
276 result
.append(" (in frame \"");
277 result
.append(item
.target().utf8());
278 result
.append("\")");
282 std::vector
<HistoryEntry::HistoryNode
*> children
= node
->children();
283 if (!children
.empty()) {
284 std::sort(children
.begin(), children
.end(), HistoryEntryCompareLess
);
285 for (size_t i
= 0; i
< children
.size(); ++i
)
286 result
+= DumpHistoryItem(children
[i
], indent
+ 4, false);
292 std::string
DumpBackForwardList(std::vector
<PageState
>& page_state
,
293 size_t current_index
) {
295 result
.append("\n============== Back Forward List ==============\n");
296 for (size_t index
= 0; index
< page_state
.size(); ++index
) {
297 scoped_ptr
<HistoryEntry
> entry(
298 PageStateToHistoryEntry(page_state
[index
]));
300 DumpHistoryItem(entry
->root_history_node(),
302 index
== current_index
));
304 result
.append("===============================================\n");
308 } // namespace content