Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / content / test / layouttest_support.cc
blobf6a488eccc9b1389bcbed3283d34b29cbafb5531
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/bluetooth/bluetooth_dispatcher_host.h"
11 #include "content/browser/renderer_host/render_process_host_impl.h"
12 #include "content/browser/renderer_host/render_widget_host_impl.h"
13 #include "content/child/geofencing/web_geofencing_provider_impl.h"
14 #include "content/common/gpu/image_transport_surface.h"
15 #include "content/public/common/page_state.h"
16 #include "content/public/renderer/renderer_gamepad_provider.h"
17 #include "content/renderer/fetchers/manifest_fetcher.h"
18 #include "content/renderer/history_entry.h"
19 #include "content/renderer/history_serialization.h"
20 #include "content/renderer/render_frame_impl.h"
21 #include "content/renderer/render_thread_impl.h"
22 #include "content/renderer/render_view_impl.h"
23 #include "content/renderer/renderer_blink_platform_impl.h"
24 #include "content/shell/renderer/test_runner/test_common.h"
25 #include "content/shell/renderer/test_runner/web_frame_test_proxy.h"
26 #include "content/shell/renderer/test_runner/web_test_proxy.h"
27 #include "device/bluetooth/bluetooth_adapter.h"
28 #include "third_party/WebKit/public/platform/WebBatteryStatus.h"
29 #include "third_party/WebKit/public/platform/WebGamepads.h"
30 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionData.h"
31 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceOrientationData.h"
32 #include "third_party/WebKit/public/web/WebHistoryItem.h"
33 #include "third_party/WebKit/public/web/WebView.h"
35 #if defined(OS_MACOSX)
36 #include "content/browser/frame_host/popup_menu_helper_mac.h"
37 #endif
39 using blink::WebBatteryStatus;
40 using blink::WebDeviceMotionData;
41 using blink::WebDeviceOrientationData;
42 using blink::WebGamepad;
43 using blink::WebGamepads;
44 using blink::WebRect;
45 using blink::WebSize;
47 namespace content {
49 namespace {
51 base::LazyInstance<base::Callback<void(RenderView*, WebTestProxyBase*)> >::Leaky
52 g_callback = LAZY_INSTANCE_INITIALIZER;
54 RenderViewImpl* CreateWebTestProxy(const ViewMsg_New_Params& params) {
55 typedef WebTestProxy<RenderViewImpl, const ViewMsg_New_Params&> ProxyType;
56 ProxyType* render_view_proxy = new ProxyType(params);
57 if (g_callback == 0)
58 return render_view_proxy;
59 g_callback.Get().Run(render_view_proxy, render_view_proxy);
60 return render_view_proxy;
63 WebTestProxyBase* GetWebTestProxyBase(RenderViewImpl* render_view) {
64 typedef WebTestProxy<RenderViewImpl, ViewMsg_New_Params*> ViewProxy;
66 ViewProxy* render_view_proxy = static_cast<ViewProxy*>(render_view);
67 return static_cast<WebTestProxyBase*>(render_view_proxy);
70 RenderFrameImpl* CreateWebFrameTestProxy(
71 RenderViewImpl* render_view,
72 int32 routing_id) {
73 typedef WebFrameTestProxy<RenderFrameImpl, RenderViewImpl*, int32> FrameProxy;
75 FrameProxy* render_frame_proxy = new FrameProxy(render_view, routing_id);
76 render_frame_proxy->set_base_proxy(GetWebTestProxyBase(render_view));
78 return render_frame_proxy;
81 } // namespace
84 void EnableWebTestProxyCreation(
85 const base::Callback<void(RenderView*, WebTestProxyBase*)>& callback) {
86 g_callback.Get() = callback;
87 RenderViewImpl::InstallCreateHook(CreateWebTestProxy);
88 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy);
91 void FetchManifestDoneCallback(
92 scoped_ptr<ManifestFetcher> fetcher,
93 const FetchManifestCallback& callback,
94 const blink::WebURLResponse& response,
95 const std::string& data) {
96 // |fetcher| will be autodeleted here as it is going out of scope.
97 callback.Run(response, data);
100 void FetchManifest(blink::WebView* view, const GURL& url,
101 const FetchManifestCallback& callback) {
102 ManifestFetcher* fetcher = new ManifestFetcher(url);
103 scoped_ptr<ManifestFetcher> autodeleter(fetcher);
105 // Start is called on fetcher which is also bound to the callback.
106 // A raw pointer is used instead of a scoped_ptr as base::Passes passes
107 // ownership and thus nulls the scoped_ptr. On MSVS this happens before
108 // the call to Start, resulting in a crash.
109 fetcher->Start(view->mainFrame(),
110 false,
111 base::Bind(&FetchManifestDoneCallback,
112 base::Passed(&autodeleter),
113 callback));
116 void SetMockGamepadProvider(scoped_ptr<RendererGamepadProvider> provider) {
117 RenderThreadImpl::current()
118 ->blink_platform_impl()
119 ->SetPlatformEventObserverForTesting(
120 blink::WebPlatformEventGamepad,
121 provider.Pass());
124 void SetMockDeviceLightData(const double data) {
125 RendererBlinkPlatformImpl::SetMockDeviceLightDataForTesting(data);
128 void SetMockDeviceMotionData(const WebDeviceMotionData& data) {
129 RendererBlinkPlatformImpl::SetMockDeviceMotionDataForTesting(data);
132 void SetMockDeviceOrientationData(const WebDeviceOrientationData& data) {
133 RendererBlinkPlatformImpl::SetMockDeviceOrientationDataForTesting(data);
136 void MockBatteryStatusChanged(const WebBatteryStatus& status) {
137 RenderThreadImpl::current()
138 ->blink_platform_impl()
139 ->MockBatteryStatusChangedForTesting(status);
142 void EnableRendererLayoutTestMode() {
143 RenderThreadImpl::current()->set_layout_test_mode(true);
146 void EnableBrowserLayoutTestMode() {
147 #if defined(OS_MACOSX)
148 ImageTransportSurface::SetAllowOSMesaForTesting(true);
149 PopupMenuHelper::DontShowPopupMenuForTesting();
150 #endif
151 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
154 int GetLocalSessionHistoryLength(RenderView* render_view) {
155 return static_cast<RenderViewImpl*>(render_view)->
156 GetLocalSessionHistoryLengthForTesting();
159 void SyncNavigationState(RenderView* render_view) {
160 static_cast<RenderViewImpl*>(render_view)->SyncNavigationState();
163 void SetFocusAndActivate(RenderView* render_view, bool enable) {
164 static_cast<RenderViewImpl*>(render_view)->
165 SetFocusAndActivateForTesting(enable);
168 void ForceResizeRenderView(RenderView* render_view,
169 const WebSize& new_size) {
170 RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view);
171 render_view_impl->ForceResizeForTesting(new_size);
174 void SetDeviceScaleFactor(RenderView* render_view, float factor) {
175 static_cast<RenderViewImpl*>(render_view)->
176 SetDeviceScaleFactorForTesting(factor);
179 void SetDeviceColorProfile(RenderView* render_view, const std::string& name) {
180 if (name == "reset") {
181 static_cast<RenderViewImpl*>(render_view)->
182 ResetDeviceColorProfileForTesting();
183 return;
186 std::vector<char> color_profile;
188 struct TestColorProfile {
189 char* data() {
190 static unsigned char color_profile_data[] = {
191 0x00,0x00,0x01,0xea,0x54,0x45,0x53,0x54,0x00,0x00,0x00,0x00,
192 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
193 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
194 0x61,0x63,0x73,0x70,0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,
195 0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
196 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
197 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x74,0x65,0x73,0x74,
198 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
199 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
200 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
201 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,
202 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x0d,
203 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x8c,
204 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x8c,0x00,0x00,0x00,0x14,
205 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xa0,0x00,0x00,0x00,0x14,
206 0x67,0x58,0x59,0x5a,0x00,0x00,0x01,0xb4,0x00,0x00,0x00,0x14,
207 0x62,0x58,0x59,0x5a,0x00,0x00,0x01,0xc8,0x00,0x00,0x00,0x14,
208 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
209 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
210 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
211 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
212 0x00,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
213 0x00,0x00,0x00,0x10,0x77,0x68,0x61,0x63,0x6b,0x65,0x64,0x2e,
214 0x69,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
215 0x00,0x00,0x00,0x11,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 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
222 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
223 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
224 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0x52,
225 0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,0x58,0x59,0x5a,0x20,
226 0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,
227 0x00,0x00,0x0f,0x95,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
228 0x00,0x00,0x26,0x31,0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9b,
229 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,
230 0x00,0x00,0x4f,0xa5,0x00,0x00,0x04,0xfc,0x63,0x75,0x72,0x76,
231 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33
234 return reinterpret_cast<char*>(color_profile_data);
237 size_t size() {
238 const size_t kTestColorProfileSizeInBytes = 490u;
239 return kTestColorProfileSizeInBytes;
243 struct AdobeRGBColorProfile {
244 char* data() {
245 static unsigned char color_profile_data[] = {
246 0x00,0x00,0x02,0x30,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
247 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
248 0x07,0xd0,0x00,0x08,0x00,0x0b,0x00,0x13,0x00,0x33,0x00,0x3b,
249 0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4c,0x00,0x00,0x00,0x00,
250 0x6e,0x6f,0x6e,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
251 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
252 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x41,0x44,0x42,0x45,
253 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
254 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
255 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
256 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,
257 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xfc,0x00,0x00,0x00,0x32,
258 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x30,0x00,0x00,0x00,0x6b,
259 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x9c,0x00,0x00,0x00,0x14,
260 0x62,0x6b,0x70,0x74,0x00,0x00,0x01,0xb0,0x00,0x00,0x00,0x14,
261 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xc4,0x00,0x00,0x00,0x0e,
262 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xd4,0x00,0x00,0x00,0x0e,
263 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xe4,0x00,0x00,0x00,0x0e,
264 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xf4,0x00,0x00,0x00,0x14,
265 0x67,0x58,0x59,0x5a,0x00,0x00,0x02,0x08,0x00,0x00,0x00,0x14,
266 0x62,0x58,0x59,0x5a,0x00,0x00,0x02,0x1c,0x00,0x00,0x00,0x14,
267 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x43,0x6f,0x70,0x79,
268 0x72,0x69,0x67,0x68,0x74,0x20,0x32,0x30,0x30,0x30,0x20,0x41,
269 0x64,0x6f,0x62,0x65,0x20,0x53,0x79,0x73,0x74,0x65,0x6d,0x73,
270 0x20,0x49,0x6e,0x63,0x6f,0x72,0x70,0x6f,0x72,0x61,0x74,0x65,
271 0x64,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
272 0x00,0x00,0x00,0x11,0x41,0x64,0x6f,0x62,0x65,0x20,0x52,0x47,
273 0x42,0x20,0x28,0x31,0x39,0x39,0x38,0x29,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
278 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
279 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
280 0x00,0x00,0x00,0x00,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
281 0x00,0x00,0xf3,0x51,0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,
282 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
283 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x75,0x72,0x76,
284 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,
285 0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
286 0x02,0x33,0x00,0x00,0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,
287 0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,0x58,0x59,0x5a,0x20,
288 0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,0x00,0x00,0x4f,0xa5,
289 0x00,0x00,0x04,0xfc,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
290 0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,0x00,0x00,0x0f,0x95,
291 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0x31,
292 0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9c
295 return reinterpret_cast<char*>(color_profile_data);
298 size_t size() {
299 const size_t kAdobeRGBColorProfileSizeInBytes = 560u;
300 return kAdobeRGBColorProfileSizeInBytes;
304 if (name == "sRGB") {
305 color_profile.assign(name.data(), name.data() + name.size());
306 } else if (name == "test") {
307 TestColorProfile test;
308 color_profile.assign(test.data(), test.data() + test.size());
309 } else if (name == "adobeRGB") {
310 AdobeRGBColorProfile test;
311 color_profile.assign(test.data(), test.data() + test.size());
314 static_cast<RenderViewImpl*>(render_view)->
315 SetDeviceColorProfileForTesting(color_profile);
318 void SetBluetoothAdapter(int render_process_id,
319 scoped_refptr<device::BluetoothAdapter> adapter) {
320 RenderProcessHostImpl* render_process_host_impl =
321 static_cast<RenderProcessHostImpl*>(
322 RenderProcessHost::FromID(render_process_id));
324 BluetoothDispatcherHost* dispatcher_host =
325 render_process_host_impl->GetBluetoothDispatcherHost();
327 if (dispatcher_host != NULL)
328 dispatcher_host->SetBluetoothAdapterForTesting(adapter.Pass());
331 void SetGeofencingMockProvider(bool service_available) {
332 static_cast<WebGeofencingProviderImpl*>(
333 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
334 ->SetMockProvider(service_available);
337 void ClearGeofencingMockProvider() {
338 static_cast<WebGeofencingProviderImpl*>(
339 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
340 ->ClearMockProvider();
343 void SetGeofencingMockPosition(double latitude, double longitude) {
344 static_cast<WebGeofencingProviderImpl*>(
345 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
346 ->SetMockPosition(latitude, longitude);
349 void UseSynchronousResizeMode(RenderView* render_view, bool enable) {
350 static_cast<RenderViewImpl*>(render_view)->
351 UseSynchronousResizeModeForTesting(enable);
354 void EnableAutoResizeMode(RenderView* render_view,
355 const WebSize& min_size,
356 const WebSize& max_size) {
357 static_cast<RenderViewImpl*>(render_view)->
358 EnableAutoResizeForTesting(min_size, max_size);
361 void DisableAutoResizeMode(RenderView* render_view, const WebSize& new_size) {
362 static_cast<RenderViewImpl*>(render_view)->
363 DisableAutoResizeForTesting(new_size);
366 struct ToLower {
367 base::char16 operator()(base::char16 c) { return tolower(c); }
370 // Returns True if node1 < node2.
371 bool HistoryEntryCompareLess(HistoryEntry::HistoryNode* node1,
372 HistoryEntry::HistoryNode* node2) {
373 base::string16 target1 = node1->item().target();
374 base::string16 target2 = node2->item().target();
375 std::transform(target1.begin(), target1.end(), target1.begin(), ToLower());
376 std::transform(target2.begin(), target2.end(), target2.begin(), ToLower());
377 return target1 < target2;
380 std::string DumpHistoryItem(HistoryEntry::HistoryNode* node,
381 int indent,
382 bool is_current_index) {
383 std::string result;
385 const blink::WebHistoryItem& item = node->item();
386 if (is_current_index) {
387 result.append("curr->");
388 result.append(indent - 6, ' '); // 6 == "curr->".length()
389 } else {
390 result.append(indent, ' ');
393 std::string url = NormalizeLayoutTestURL(item.urlString().utf8());
394 result.append(url);
395 if (!item.target().isEmpty()) {
396 result.append(" (in frame \"");
397 result.append(item.target().utf8());
398 result.append("\")");
400 result.append("\n");
402 std::vector<HistoryEntry::HistoryNode*> children = node->children();
403 if (!children.empty()) {
404 std::sort(children.begin(), children.end(), HistoryEntryCompareLess);
405 for (size_t i = 0; i < children.size(); ++i)
406 result += DumpHistoryItem(children[i], indent + 4, false);
409 return result;
412 std::string DumpBackForwardList(std::vector<PageState>& page_state,
413 size_t current_index) {
414 std::string result;
415 result.append("\n============== Back Forward List ==============\n");
416 for (size_t index = 0; index < page_state.size(); ++index) {
417 scoped_ptr<HistoryEntry> entry(
418 PageStateToHistoryEntry(page_state[index]));
419 result.append(
420 DumpHistoryItem(entry->root_history_node(),
422 index == current_index));
424 result.append("===============================================\n");
425 return result;
428 scoped_refptr<cc::TextureLayer> CreateTextureLayerForMailbox(
429 cc::TextureLayerClient* client) {
430 return cc::TextureLayer::CreateForMailbox(
431 cc_blink::WebLayerImpl::LayerSettings(), client);
434 blink::WebLayer* InstantiateWebLayer(scoped_refptr<cc::TextureLayer> layer) {
435 return new cc_blink::WebLayerImpl(layer);
438 } // namespace content