Move more string_util functions to base namespace.
[chromium-blink-merge.git] / content / test / layouttest_support.cc
blob2551f0c023a7e441764ffdc02f22945f1e2b0c39
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 "components/test_runner/test_common.h"
11 #include "components/test_runner/web_frame_test_proxy.h"
12 #include "components/test_runner/web_test_proxy.h"
13 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h"
14 #include "content/browser/renderer_host/render_process_host_impl.h"
15 #include "content/browser/renderer_host/render_widget_host_impl.h"
16 #include "content/child/geofencing/web_geofencing_provider_impl.h"
17 #include "content/common/gpu/image_transport_surface.h"
18 #include "content/public/common/page_state.h"
19 #include "content/public/renderer/renderer_gamepad_provider.h"
20 #include "content/renderer/fetchers/manifest_fetcher.h"
21 #include "content/renderer/history_entry.h"
22 #include "content/renderer/history_serialization.h"
23 #include "content/renderer/render_frame_impl.h"
24 #include "content/renderer/render_thread_impl.h"
25 #include "content/renderer/render_view_impl.h"
26 #include "content/renderer/renderer_blink_platform_impl.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<
52 base::Callback<void(RenderView*, test_runner::WebTestProxyBase*)>>::Leaky
53 g_callback = LAZY_INSTANCE_INITIALIZER;
55 RenderViewImpl* CreateWebTestProxy(const ViewMsg_New_Params& params) {
56 typedef test_runner::WebTestProxy<RenderViewImpl, const ViewMsg_New_Params&>
57 ProxyType;
58 ProxyType* render_view_proxy = new ProxyType(params);
59 if (g_callback == 0)
60 return render_view_proxy;
61 g_callback.Get().Run(render_view_proxy, render_view_proxy);
62 return render_view_proxy;
65 test_runner::WebTestProxyBase* GetWebTestProxyBase(
66 RenderViewImpl* render_view) {
67 typedef test_runner::WebTestProxy<RenderViewImpl, ViewMsg_New_Params*>
68 ViewProxy;
70 ViewProxy* render_view_proxy = static_cast<ViewProxy*>(render_view);
71 return static_cast<test_runner::WebTestProxyBase*>(render_view_proxy);
74 RenderFrameImpl* CreateWebFrameTestProxy(
75 RenderViewImpl* render_view,
76 int32 routing_id) {
77 typedef test_runner::WebFrameTestProxy<RenderFrameImpl, RenderViewImpl*,
78 int32> FrameProxy;
80 FrameProxy* render_frame_proxy = new FrameProxy(render_view, routing_id);
81 render_frame_proxy->set_base_proxy(GetWebTestProxyBase(render_view));
83 return render_frame_proxy;
86 } // namespace
88 void EnableWebTestProxyCreation(
89 const base::Callback<void(RenderView*, test_runner::WebTestProxyBase*)>&
90 callback) {
91 g_callback.Get() = callback;
92 RenderViewImpl::InstallCreateHook(CreateWebTestProxy);
93 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy);
96 void FetchManifestDoneCallback(
97 scoped_ptr<ManifestFetcher> fetcher,
98 const FetchManifestCallback& callback,
99 const blink::WebURLResponse& response,
100 const std::string& data) {
101 // |fetcher| will be autodeleted here as it is going out of scope.
102 callback.Run(response, data);
105 void FetchManifest(blink::WebView* view, const GURL& url,
106 const FetchManifestCallback& callback) {
107 ManifestFetcher* fetcher = new ManifestFetcher(url);
108 scoped_ptr<ManifestFetcher> autodeleter(fetcher);
110 // Start is called on fetcher which is also bound to the callback.
111 // A raw pointer is used instead of a scoped_ptr as base::Passes passes
112 // ownership and thus nulls the scoped_ptr. On MSVS this happens before
113 // the call to Start, resulting in a crash.
114 fetcher->Start(view->mainFrame(),
115 false,
116 base::Bind(&FetchManifestDoneCallback,
117 base::Passed(&autodeleter),
118 callback));
121 void SetMockGamepadProvider(scoped_ptr<RendererGamepadProvider> provider) {
122 RenderThreadImpl::current()
123 ->blink_platform_impl()
124 ->SetPlatformEventObserverForTesting(
125 blink::WebPlatformEventGamepad,
126 provider.Pass());
129 void SetMockDeviceLightData(const double data) {
130 RendererBlinkPlatformImpl::SetMockDeviceLightDataForTesting(data);
133 void SetMockDeviceMotionData(const WebDeviceMotionData& data) {
134 RendererBlinkPlatformImpl::SetMockDeviceMotionDataForTesting(data);
137 void SetMockDeviceOrientationData(const WebDeviceOrientationData& data) {
138 RendererBlinkPlatformImpl::SetMockDeviceOrientationDataForTesting(data);
141 void MockBatteryStatusChanged(const WebBatteryStatus& status) {
142 RenderThreadImpl::current()
143 ->blink_platform_impl()
144 ->MockBatteryStatusChangedForTesting(status);
147 void EnableRendererLayoutTestMode() {
148 RenderThreadImpl::current()->set_layout_test_mode(true);
151 void EnableBrowserLayoutTestMode() {
152 #if defined(OS_MACOSX)
153 ImageTransportSurface::SetAllowOSMesaForTesting(true);
154 PopupMenuHelper::DontShowPopupMenuForTesting();
155 #endif
156 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
159 int GetLocalSessionHistoryLength(RenderView* render_view) {
160 return static_cast<RenderViewImpl*>(render_view)->
161 GetLocalSessionHistoryLengthForTesting();
164 void SyncNavigationState(RenderView* render_view) {
165 static_cast<RenderViewImpl*>(render_view)->SyncNavigationState();
168 void SetFocusAndActivate(RenderView* render_view, bool enable) {
169 static_cast<RenderViewImpl*>(render_view)->
170 SetFocusAndActivateForTesting(enable);
173 void ForceResizeRenderView(RenderView* render_view,
174 const WebSize& new_size) {
175 RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view);
176 render_view_impl->ForceResizeForTesting(new_size);
179 void SetDeviceScaleFactor(RenderView* render_view, float factor) {
180 static_cast<RenderViewImpl*>(render_view)->
181 SetDeviceScaleFactorForTesting(factor);
184 void SetDeviceColorProfile(RenderView* render_view, const std::string& name) {
185 if (name == "reset") {
186 static_cast<RenderViewImpl*>(render_view)->
187 ResetDeviceColorProfileForTesting();
188 return;
191 std::vector<char> color_profile;
193 struct TestColorProfile {
194 char* data() {
195 static unsigned char color_profile_data[] = {
196 0x00,0x00,0x01,0xea,0x54,0x45,0x53,0x54,0x00,0x00,0x00,0x00,
197 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
198 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
199 0x61,0x63,0x73,0x70,0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,
200 0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
201 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
202 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x74,0x65,0x73,0x74,
203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
204 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
205 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
206 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,
207 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x0d,
208 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x8c,
209 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x8c,0x00,0x00,0x00,0x14,
210 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xa0,0x00,0x00,0x00,0x14,
211 0x67,0x58,0x59,0x5a,0x00,0x00,0x01,0xb4,0x00,0x00,0x00,0x14,
212 0x62,0x58,0x59,0x5a,0x00,0x00,0x01,0xc8,0x00,0x00,0x00,0x14,
213 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
214 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
215 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
216 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
217 0x00,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
218 0x00,0x00,0x00,0x10,0x77,0x68,0x61,0x63,0x6b,0x65,0x64,0x2e,
219 0x69,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
220 0x00,0x00,0x00,0x11,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 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
225 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
226 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
227 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
228 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
229 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0x52,
230 0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,0x58,0x59,0x5a,0x20,
231 0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,
232 0x00,0x00,0x0f,0x95,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
233 0x00,0x00,0x26,0x31,0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9b,
234 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,
235 0x00,0x00,0x4f,0xa5,0x00,0x00,0x04,0xfc,0x63,0x75,0x72,0x76,
236 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33
239 return reinterpret_cast<char*>(color_profile_data);
242 size_t size() {
243 const size_t kTestColorProfileSizeInBytes = 490u;
244 return kTestColorProfileSizeInBytes;
248 struct AdobeRGBColorProfile {
249 char* data() {
250 static unsigned char color_profile_data[] = {
251 0x00,0x00,0x02,0x30,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
252 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
253 0x07,0xd0,0x00,0x08,0x00,0x0b,0x00,0x13,0x00,0x33,0x00,0x3b,
254 0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4c,0x00,0x00,0x00,0x00,
255 0x6e,0x6f,0x6e,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
256 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
257 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x41,0x44,0x42,0x45,
258 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
259 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
260 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
261 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,
262 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xfc,0x00,0x00,0x00,0x32,
263 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x30,0x00,0x00,0x00,0x6b,
264 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x9c,0x00,0x00,0x00,0x14,
265 0x62,0x6b,0x70,0x74,0x00,0x00,0x01,0xb0,0x00,0x00,0x00,0x14,
266 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xc4,0x00,0x00,0x00,0x0e,
267 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xd4,0x00,0x00,0x00,0x0e,
268 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xe4,0x00,0x00,0x00,0x0e,
269 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xf4,0x00,0x00,0x00,0x14,
270 0x67,0x58,0x59,0x5a,0x00,0x00,0x02,0x08,0x00,0x00,0x00,0x14,
271 0x62,0x58,0x59,0x5a,0x00,0x00,0x02,0x1c,0x00,0x00,0x00,0x14,
272 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x43,0x6f,0x70,0x79,
273 0x72,0x69,0x67,0x68,0x74,0x20,0x32,0x30,0x30,0x30,0x20,0x41,
274 0x64,0x6f,0x62,0x65,0x20,0x53,0x79,0x73,0x74,0x65,0x6d,0x73,
275 0x20,0x49,0x6e,0x63,0x6f,0x72,0x70,0x6f,0x72,0x61,0x74,0x65,
276 0x64,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
277 0x00,0x00,0x00,0x11,0x41,0x64,0x6f,0x62,0x65,0x20,0x52,0x47,
278 0x42,0x20,0x28,0x31,0x39,0x39,0x38,0x29,0x00,0x00,0x00,0x00,
279 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
280 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
281 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
282 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
283 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
284 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
285 0x00,0x00,0x00,0x00,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
286 0x00,0x00,0xf3,0x51,0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,
287 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
288 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x75,0x72,0x76,
289 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,
290 0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
291 0x02,0x33,0x00,0x00,0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,
292 0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,0x58,0x59,0x5a,0x20,
293 0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,0x00,0x00,0x4f,0xa5,
294 0x00,0x00,0x04,0xfc,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
295 0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,0x00,0x00,0x0f,0x95,
296 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0x31,
297 0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9c
300 return reinterpret_cast<char*>(color_profile_data);
303 size_t size() {
304 const size_t kAdobeRGBColorProfileSizeInBytes = 560u;
305 return kAdobeRGBColorProfileSizeInBytes;
309 if (name == "sRGB") {
310 color_profile.assign(name.data(), name.data() + name.size());
311 } else if (name == "test") {
312 TestColorProfile test;
313 color_profile.assign(test.data(), test.data() + test.size());
314 } else if (name == "adobeRGB") {
315 AdobeRGBColorProfile test;
316 color_profile.assign(test.data(), test.data() + test.size());
319 static_cast<RenderViewImpl*>(render_view)->
320 SetDeviceColorProfileForTesting(color_profile);
323 void SetBluetoothAdapter(int render_process_id,
324 scoped_refptr<device::BluetoothAdapter> adapter) {
325 RenderProcessHostImpl* render_process_host_impl =
326 static_cast<RenderProcessHostImpl*>(
327 RenderProcessHost::FromID(render_process_id));
329 BluetoothDispatcherHost* dispatcher_host =
330 render_process_host_impl->GetBluetoothDispatcherHost();
332 if (dispatcher_host != NULL)
333 dispatcher_host->SetBluetoothAdapterForTesting(adapter.Pass());
336 void SetGeofencingMockProvider(bool service_available) {
337 static_cast<WebGeofencingProviderImpl*>(
338 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
339 ->SetMockProvider(service_available);
342 void ClearGeofencingMockProvider() {
343 static_cast<WebGeofencingProviderImpl*>(
344 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
345 ->ClearMockProvider();
348 void SetGeofencingMockPosition(double latitude, double longitude) {
349 static_cast<WebGeofencingProviderImpl*>(
350 RenderThreadImpl::current()->blink_platform_impl()->geofencingProvider())
351 ->SetMockPosition(latitude, longitude);
354 void UseSynchronousResizeMode(RenderView* render_view, bool enable) {
355 static_cast<RenderViewImpl*>(render_view)->
356 UseSynchronousResizeModeForTesting(enable);
359 void EnableAutoResizeMode(RenderView* render_view,
360 const WebSize& min_size,
361 const WebSize& max_size) {
362 static_cast<RenderViewImpl*>(render_view)->
363 EnableAutoResizeForTesting(min_size, max_size);
366 void DisableAutoResizeMode(RenderView* render_view, const WebSize& new_size) {
367 static_cast<RenderViewImpl*>(render_view)->
368 DisableAutoResizeForTesting(new_size);
371 struct ToLower {
372 base::char16 operator()(base::char16 c) { return tolower(c); }
375 // Returns True if node1 < node2.
376 bool HistoryEntryCompareLess(HistoryEntry::HistoryNode* node1,
377 HistoryEntry::HistoryNode* node2) {
378 base::string16 target1 = node1->item().target();
379 base::string16 target2 = node2->item().target();
380 std::transform(target1.begin(), target1.end(), target1.begin(), ToLower());
381 std::transform(target2.begin(), target2.end(), target2.begin(), ToLower());
382 return target1 < target2;
385 std::string DumpHistoryItem(HistoryEntry::HistoryNode* node,
386 int indent,
387 bool is_current_index) {
388 std::string result;
390 const blink::WebHistoryItem& item = node->item();
391 if (is_current_index) {
392 result.append("curr->");
393 result.append(indent - 6, ' '); // 6 == "curr->".length()
394 } else {
395 result.append(indent, ' ');
398 std::string url =
399 test_runner::NormalizeLayoutTestURL(item.urlString().utf8());
400 result.append(url);
401 if (!item.target().isEmpty()) {
402 result.append(" (in frame \"");
403 result.append(item.target().utf8());
404 result.append("\")");
406 result.append("\n");
408 std::vector<HistoryEntry::HistoryNode*> children = node->children();
409 if (!children.empty()) {
410 std::sort(children.begin(), children.end(), HistoryEntryCompareLess);
411 for (size_t i = 0; i < children.size(); ++i)
412 result += DumpHistoryItem(children[i], indent + 4, false);
415 return result;
418 std::string DumpBackForwardList(std::vector<PageState>& page_state,
419 size_t current_index) {
420 std::string result;
421 result.append("\n============== Back Forward List ==============\n");
422 for (size_t index = 0; index < page_state.size(); ++index) {
423 scoped_ptr<HistoryEntry> entry(
424 PageStateToHistoryEntry(page_state[index]));
425 result.append(
426 DumpHistoryItem(entry->root_history_node(),
428 index == current_index));
430 result.append("===============================================\n");
431 return result;
434 scoped_refptr<cc::TextureLayer> CreateTextureLayerForMailbox(
435 cc::TextureLayerClient* client) {
436 return cc::TextureLayer::CreateForMailbox(
437 cc_blink::WebLayerImpl::LayerSettings(), client);
440 blink::WebLayer* InstantiateWebLayer(scoped_refptr<cc::TextureLayer> layer) {
441 return new cc_blink::WebLayerImpl(layer);
444 } // namespace content