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 "cc/blink/web_layer_impl.h"
10 #include "content/browser/renderer_host/render_widget_host_impl.h"
11 #include "content/child/bluetooth/web_bluetooth_impl.h"
12 #include "content/child/geofencing/web_geofencing_provider_impl.h"
13 #include "content/common/gpu/image_transport_surface.h"
14 #include "content/public/common/page_state.h"
15 #include "content/public/renderer/renderer_gamepad_provider.h"
16 #include "content/renderer/fetchers/manifest_fetcher.h"
17 #include "content/renderer/history_entry.h"
18 #include "content/renderer/history_serialization.h"
19 #include "content/renderer/render_frame_impl.h"
20 #include "content/renderer/render_thread_impl.h"
21 #include "content/renderer/render_view_impl.h"
22 #include "content/renderer/renderer_blink_platform_impl.h"
23 #include "content/shell/renderer/test_runner/test_common.h"
24 #include "content/shell/renderer/test_runner/web_frame_test_proxy.h"
25 #include "content/shell/renderer/test_runner/web_test_proxy.h"
26 #include "third_party/WebKit/public/platform/WebBatteryStatus.h"
27 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
28 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
29 #include "third_party/WebKit/public/platform/WebGamepads.h"
30 #include "third_party/WebKit/public/web/WebHistoryItem.h"
31 #include "third_party/WebKit/public/web/WebView.h"
33 #if defined(OS_MACOSX)
34 #include "content/browser/frame_host/popup_menu_helper_mac.h"
37 using blink::WebBatteryStatus
;
38 using blink::WebDeviceMotionData
;
39 using blink::WebDeviceOrientationData
;
40 using blink::WebGamepad
;
41 using blink::WebGamepads
;
49 base::LazyInstance
<base::Callback
<void(RenderView
*, WebTestProxyBase
*)> >::Leaky
50 g_callback
= LAZY_INSTANCE_INITIALIZER
;
52 RenderViewImpl
* CreateWebTestProxy(const ViewMsg_New_Params
& params
) {
53 typedef WebTestProxy
<RenderViewImpl
, const ViewMsg_New_Params
&> ProxyType
;
54 ProxyType
* render_view_proxy
= new ProxyType(params
);
56 return render_view_proxy
;
57 g_callback
.Get().Run(render_view_proxy
, render_view_proxy
);
58 return render_view_proxy
;
61 WebTestProxyBase
* GetWebTestProxyBase(RenderViewImpl
* render_view
) {
62 typedef WebTestProxy
<RenderViewImpl
, ViewMsg_New_Params
*> ViewProxy
;
64 ViewProxy
* render_view_proxy
= static_cast<ViewProxy
*>(render_view
);
65 return static_cast<WebTestProxyBase
*>(render_view_proxy
);
68 RenderFrameImpl
* CreateWebFrameTestProxy(
69 RenderViewImpl
* render_view
,
71 typedef WebFrameTestProxy
<RenderFrameImpl
, RenderViewImpl
*, int32
> FrameProxy
;
73 FrameProxy
* render_frame_proxy
= new FrameProxy(render_view
, routing_id
);
74 render_frame_proxy
->set_base_proxy(GetWebTestProxyBase(render_view
));
76 return render_frame_proxy
;
82 void EnableWebTestProxyCreation(
83 const base::Callback
<void(RenderView
*, WebTestProxyBase
*)>& callback
) {
84 g_callback
.Get() = callback
;
85 RenderViewImpl::InstallCreateHook(CreateWebTestProxy
);
86 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy
);
89 void FetchManifestDoneCallback(
90 scoped_ptr
<ManifestFetcher
> fetcher
,
91 const FetchManifestCallback
& callback
,
92 const blink::WebURLResponse
& response
,
93 const std::string
& data
) {
94 // |fetcher| will be autodeleted here as it is going out of scope.
95 callback
.Run(response
, data
);
98 void FetchManifest(blink::WebView
* view
, const GURL
& url
,
99 const FetchManifestCallback
& callback
) {
100 ManifestFetcher
* fetcher
= new ManifestFetcher(url
);
101 scoped_ptr
<ManifestFetcher
> autodeleter(fetcher
);
103 // Start is called on fetcher which is also bound to the callback.
104 // A raw pointer is used instead of a scoped_ptr as base::Passes passes
105 // ownership and thus nulls the scoped_ptr. On MSVS this happens before
106 // the call to Start, resulting in a crash.
107 fetcher
->Start(view
->mainFrame(),
108 base::Bind(&FetchManifestDoneCallback
,
109 base::Passed(&autodeleter
),
113 void SetMockGamepadProvider(scoped_ptr
<RendererGamepadProvider
> provider
) {
114 RenderThreadImpl::current()
115 ->blink_platform_impl()
116 ->SetPlatformEventObserverForTesting(
117 blink::WebPlatformEventGamepad
,
121 void SetMockDeviceLightData(const double data
) {
122 RendererBlinkPlatformImpl::SetMockDeviceLightDataForTesting(data
);
125 void SetMockDeviceMotionData(const WebDeviceMotionData
& data
) {
126 RendererBlinkPlatformImpl::SetMockDeviceMotionDataForTesting(data
);
129 void SetMockDeviceOrientationData(const WebDeviceOrientationData
& data
) {
130 RendererBlinkPlatformImpl::SetMockDeviceOrientationDataForTesting(data
);
133 void MockBatteryStatusChanged(const WebBatteryStatus
& status
) {
134 RenderThreadImpl::current()
135 ->blink_platform_impl()
136 ->MockBatteryStatusChangedForTesting(status
);
139 void EnableRendererLayoutTestMode() {
140 RenderThreadImpl::current()->set_layout_test_mode(true);
143 void EnableBrowserLayoutTestMode() {
144 #if defined(OS_MACOSX)
145 ImageTransportSurface::SetAllowOSMesaForTesting(true);
146 PopupMenuHelper::DontShowPopupMenuForTesting();
148 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
151 int GetLocalSessionHistoryLength(RenderView
* render_view
) {
152 return static_cast<RenderViewImpl
*>(render_view
)->
153 GetLocalSessionHistoryLengthForTesting();
156 void SyncNavigationState(RenderView
* render_view
) {
157 static_cast<RenderViewImpl
*>(render_view
)->SyncNavigationState();
160 void SetFocusAndActivate(RenderView
* render_view
, bool enable
) {
161 static_cast<RenderViewImpl
*>(render_view
)->
162 SetFocusAndActivateForTesting(enable
);
165 void ForceResizeRenderView(RenderView
* render_view
,
166 const WebSize
& new_size
) {
167 RenderViewImpl
* render_view_impl
= static_cast<RenderViewImpl
*>(render_view
);
168 render_view_impl
->ForceResizeForTesting(new_size
);
171 void SetDeviceScaleFactor(RenderView
* render_view
, float factor
) {
172 static_cast<RenderViewImpl
*>(render_view
)->
173 SetDeviceScaleFactorForTesting(factor
);
176 void SetDeviceColorProfile(RenderView
* render_view
, const std::string
& name
) {
177 if (name
== "reset") {
178 static_cast<RenderViewImpl
*>(render_view
)->
179 ResetDeviceColorProfileForTesting();
183 std::vector
<char> color_profile
;
185 struct TestColorProfile
{
187 static unsigned char color_profile_data
[] = {
188 0x00,0x00,0x01,0xea,0x54,0x45,0x53,0x54,0x00,0x00,0x00,0x00,
189 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
190 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
191 0x61,0x63,0x73,0x70,0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,
192 0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
193 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
194 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x74,0x65,0x73,0x74,
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,0x09,
199 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x0d,
200 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x8c,
201 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x8c,0x00,0x00,0x00,0x14,
202 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xa0,0x00,0x00,0x00,0x14,
203 0x67,0x58,0x59,0x5a,0x00,0x00,0x01,0xb4,0x00,0x00,0x00,0x14,
204 0x62,0x58,0x59,0x5a,0x00,0x00,0x01,0xc8,0x00,0x00,0x00,0x14,
205 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
206 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
207 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
208 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
209 0x00,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
210 0x00,0x00,0x00,0x10,0x77,0x68,0x61,0x63,0x6b,0x65,0x64,0x2e,
211 0x69,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
212 0x00,0x00,0x00,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
213 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
214 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
215 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
216 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
217 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
218 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
219 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
220 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
221 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0x52,
222 0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,0x58,0x59,0x5a,0x20,
223 0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,
224 0x00,0x00,0x0f,0x95,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
225 0x00,0x00,0x26,0x31,0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9b,
226 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,
227 0x00,0x00,0x4f,0xa5,0x00,0x00,0x04,0xfc,0x63,0x75,0x72,0x76,
228 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33
231 return reinterpret_cast<char*>(color_profile_data
);
235 const size_t kTestColorProfileSizeInBytes
= 490u;
236 return kTestColorProfileSizeInBytes
;
240 struct AdobeRGBColorProfile
{
242 static unsigned char color_profile_data
[] = {
243 0x00,0x00,0x02,0x30,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
244 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
245 0x07,0xd0,0x00,0x08,0x00,0x0b,0x00,0x13,0x00,0x33,0x00,0x3b,
246 0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4c,0x00,0x00,0x00,0x00,
247 0x6e,0x6f,0x6e,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
248 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
249 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x41,0x44,0x42,0x45,
250 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
251 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
252 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
253 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,
254 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xfc,0x00,0x00,0x00,0x32,
255 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x30,0x00,0x00,0x00,0x6b,
256 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x9c,0x00,0x00,0x00,0x14,
257 0x62,0x6b,0x70,0x74,0x00,0x00,0x01,0xb0,0x00,0x00,0x00,0x14,
258 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xc4,0x00,0x00,0x00,0x0e,
259 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xd4,0x00,0x00,0x00,0x0e,
260 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xe4,0x00,0x00,0x00,0x0e,
261 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xf4,0x00,0x00,0x00,0x14,
262 0x67,0x58,0x59,0x5a,0x00,0x00,0x02,0x08,0x00,0x00,0x00,0x14,
263 0x62,0x58,0x59,0x5a,0x00,0x00,0x02,0x1c,0x00,0x00,0x00,0x14,
264 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x43,0x6f,0x70,0x79,
265 0x72,0x69,0x67,0x68,0x74,0x20,0x32,0x30,0x30,0x30,0x20,0x41,
266 0x64,0x6f,0x62,0x65,0x20,0x53,0x79,0x73,0x74,0x65,0x6d,0x73,
267 0x20,0x49,0x6e,0x63,0x6f,0x72,0x70,0x6f,0x72,0x61,0x74,0x65,
268 0x64,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
269 0x00,0x00,0x00,0x11,0x41,0x64,0x6f,0x62,0x65,0x20,0x52,0x47,
270 0x42,0x20,0x28,0x31,0x39,0x39,0x38,0x29,0x00,0x00,0x00,0x00,
271 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
272 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
273 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
274 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
275 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
276 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
277 0x00,0x00,0x00,0x00,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
278 0x00,0x00,0xf3,0x51,0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,
279 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
280 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x75,0x72,0x76,
281 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,
282 0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
283 0x02,0x33,0x00,0x00,0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,
284 0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,0x58,0x59,0x5a,0x20,
285 0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,0x00,0x00,0x4f,0xa5,
286 0x00,0x00,0x04,0xfc,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
287 0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,0x00,0x00,0x0f,0x95,
288 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0x31,
289 0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9c
292 return reinterpret_cast<char*>(color_profile_data
);
296 const size_t kAdobeRGBColorProfileSizeInBytes
= 560u;
297 return kAdobeRGBColorProfileSizeInBytes
;
301 if (name
== "sRGB") {
302 color_profile
.assign(name
.data(), name
.data() + name
.size());
303 } else if (name
== "test") {
304 TestColorProfile test
;
305 color_profile
.assign(test
.data(), test
.data() + test
.size());
306 } else if (name
== "adobeRGB") {
307 AdobeRGBColorProfile test
;
308 color_profile
.assign(test
.data(), test
.data() + test
.size());
311 static_cast<RenderViewImpl
*>(render_view
)->
312 SetDeviceColorProfileForTesting(color_profile
);
315 void SetBluetoothMockDataSetForTesting(const std::string
& name
) {
316 RenderThreadImpl::current()
317 ->blink_platform_impl()
318 ->BluetoothImplForTesting()
319 ->SetBluetoothMockDataSetForTesting(name
);
322 void SetGeofencingMockProvider(bool service_available
) {
323 static_cast<WebGeofencingProviderImpl
*>(
324 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
325 ->SetMockProvider(service_available
);
328 void ClearGeofencingMockProvider() {
329 static_cast<WebGeofencingProviderImpl
*>(
330 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
331 ->ClearMockProvider();
334 void SetGeofencingMockPosition(double latitude
, double longitude
) {
335 static_cast<WebGeofencingProviderImpl
*>(
336 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
337 ->SetMockPosition(latitude
, longitude
);
340 void UseSynchronousResizeMode(RenderView
* render_view
, bool enable
) {
341 static_cast<RenderViewImpl
*>(render_view
)->
342 UseSynchronousResizeModeForTesting(enable
);
345 void EnableAutoResizeMode(RenderView
* render_view
,
346 const WebSize
& min_size
,
347 const WebSize
& max_size
) {
348 static_cast<RenderViewImpl
*>(render_view
)->
349 EnableAutoResizeForTesting(min_size
, max_size
);
352 void DisableAutoResizeMode(RenderView
* render_view
, const WebSize
& new_size
) {
353 static_cast<RenderViewImpl
*>(render_view
)->
354 DisableAutoResizeForTesting(new_size
);
358 base::char16
operator()(base::char16 c
) { return tolower(c
); }
361 // Returns True if node1 < node2.
362 bool HistoryEntryCompareLess(HistoryEntry::HistoryNode
* node1
,
363 HistoryEntry::HistoryNode
* node2
) {
364 base::string16 target1
= node1
->item().target();
365 base::string16 target2
= node2
->item().target();
366 std::transform(target1
.begin(), target1
.end(), target1
.begin(), ToLower());
367 std::transform(target2
.begin(), target2
.end(), target2
.begin(), ToLower());
368 return target1
< target2
;
371 std::string
DumpHistoryItem(HistoryEntry::HistoryNode
* node
,
373 bool is_current_index
) {
376 const blink::WebHistoryItem
& item
= node
->item();
377 if (is_current_index
) {
378 result
.append("curr->");
379 result
.append(indent
- 6, ' '); // 6 == "curr->".length()
381 result
.append(indent
, ' ');
384 std::string url
= NormalizeLayoutTestURL(item
.urlString().utf8());
386 if (!item
.target().isEmpty()) {
387 result
.append(" (in frame \"");
388 result
.append(item
.target().utf8());
389 result
.append("\")");
393 std::vector
<HistoryEntry::HistoryNode
*> children
= node
->children();
394 if (!children
.empty()) {
395 std::sort(children
.begin(), children
.end(), HistoryEntryCompareLess
);
396 for (size_t i
= 0; i
< children
.size(); ++i
)
397 result
+= DumpHistoryItem(children
[i
], indent
+ 4, false);
403 std::string
DumpBackForwardList(std::vector
<PageState
>& page_state
,
404 size_t current_index
) {
406 result
.append("\n============== Back Forward List ==============\n");
407 for (size_t index
= 0; index
< page_state
.size(); ++index
) {
408 scoped_ptr
<HistoryEntry
> entry(
409 PageStateToHistoryEntry(page_state
[index
]));
411 DumpHistoryItem(entry
->root_history_node(),
413 index
== current_index
));
415 result
.append("===============================================\n");
419 blink::WebLayer
* InstantiateWebLayer(scoped_refptr
<cc::TextureLayer
> layer
) {
420 return new cc_blink::WebLayerImpl(layer
);
423 } // namespace content