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/common/gpu/image_transport_surface.h"
12 #include "content/public/common/page_state.h"
13 #include "content/public/renderer/renderer_gamepad_provider.h"
14 #include "content/renderer/fetchers/manifest_fetcher.h"
15 #include "content/renderer/history_entry.h"
16 #include "content/renderer/history_serialization.h"
17 #include "content/renderer/render_frame_impl.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "content/renderer/render_view_impl.h"
20 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
21 #include "content/shell/renderer/test_runner/test_common.h"
22 #include "content/shell/renderer/test_runner/web_frame_test_proxy.h"
23 #include "content/shell/renderer/test_runner/web_test_proxy.h"
24 #include "third_party/WebKit/public/platform/WebBatteryStatus.h"
25 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
26 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
27 #include "third_party/WebKit/public/platform/WebGamepads.h"
28 #include "third_party/WebKit/public/web/WebHistoryItem.h"
29 #include "third_party/WebKit/public/web/WebView.h"
31 #if defined(OS_MACOSX)
32 #include "content/browser/frame_host/popup_menu_helper_mac.h"
35 using blink::WebBatteryStatus;
36 using blink::WebDeviceMotionData;
37 using blink::WebDeviceOrientationData;
38 using blink::WebGamepad;
39 using blink::WebGamepads;
47 base::LazyInstance<base::Callback<void(RenderView*, WebTestProxyBase*)> >::Leaky
48 g_callback = LAZY_INSTANCE_INITIALIZER;
50 RenderViewImpl* CreateWebTestProxy(RenderViewImplParams* params) {
51 typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ProxyType;
52 ProxyType* render_view_proxy = new ProxyType(params);
54 return render_view_proxy;
55 g_callback.Get().Run(render_view_proxy, render_view_proxy);
56 return render_view_proxy;
59 WebTestProxyBase* GetWebTestProxyBase(RenderViewImpl* render_view) {
60 typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ViewProxy;
62 ViewProxy* render_view_proxy = static_cast<ViewProxy*>(render_view);
63 return static_cast<WebTestProxyBase*>(render_view_proxy);
66 RenderFrameImpl* CreateWebFrameTestProxy(
67 RenderViewImpl* render_view,
69 typedef WebFrameTestProxy<RenderFrameImpl, RenderViewImpl*, int32> FrameProxy;
71 FrameProxy* render_frame_proxy = new FrameProxy(render_view, routing_id);
72 render_frame_proxy->set_base_proxy(GetWebTestProxyBase(render_view));
74 return render_frame_proxy;
80 void EnableWebTestProxyCreation(
81 const base::Callback<void(RenderView*, WebTestProxyBase*)>& callback) {
82 g_callback.Get() = callback;
83 RenderViewImpl::InstallCreateHook(CreateWebTestProxy);
84 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy);
87 void FetchManifestDoneCallback(
88 scoped_ptr<ManifestFetcher> fetcher,
89 const FetchManifestCallback& callback,
90 const blink::WebURLResponse& response,
91 const std::string& data) {
92 // |fetcher| will be autodeleted here as it is going out of scope.
93 callback.Run(response, data);
96 void FetchManifest(blink::WebView* view, const GURL& url,
97 const FetchManifestCallback& callback) {
98 scoped_ptr<ManifestFetcher> fetcher(new ManifestFetcher(url));
100 fetcher->Start(view->mainFrame(),
101 base::Bind(&FetchManifestDoneCallback,
102 base::Passed(&fetcher),
106 void SetMockGamepadProvider(scoped_ptr<RendererGamepadProvider> provider) {
107 RenderThreadImpl::current()->webkit_platform_support()->
108 SetPlatformEventObserverForTesting(
109 blink::WebPlatformEventGamepad,
110 provider.PassAs<PlatformEventObserverBase>());
113 void SetMockDeviceLightData(const double data) {
114 RendererWebKitPlatformSupportImpl::SetMockDeviceLightDataForTesting(data);
117 void SetMockDeviceMotionData(const WebDeviceMotionData& data) {
118 RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data);
121 void SetMockDeviceOrientationData(const WebDeviceOrientationData& data) {
122 RendererWebKitPlatformSupportImpl::
123 SetMockDeviceOrientationDataForTesting(data);
126 void MockBatteryStatusChanged(const WebBatteryStatus& status) {
127 RenderThreadImpl::current()->webkit_platform_support()->
128 MockBatteryStatusChangedForTesting(status);
131 void EnableRendererLayoutTestMode() {
132 RenderThreadImpl::current()->set_layout_test_mode(true);
135 void EnableBrowserLayoutTestMode() {
136 #if defined(OS_MACOSX)
137 ImageTransportSurface::SetAllowOSMesaForTesting(true);
138 PopupMenuHelper::DontShowPopupMenuForTesting();
140 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
143 int GetLocalSessionHistoryLength(RenderView* render_view) {
144 return static_cast<RenderViewImpl*>(render_view)->
145 GetLocalSessionHistoryLengthForTesting();
148 void SyncNavigationState(RenderView* render_view) {
149 static_cast<RenderViewImpl*>(render_view)->SyncNavigationState();
152 void SetFocusAndActivate(RenderView* render_view, bool enable) {
153 static_cast<RenderViewImpl*>(render_view)->
154 SetFocusAndActivateForTesting(enable);
157 void ForceResizeRenderView(RenderView* render_view,
158 const WebSize& new_size) {
159 RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view);
160 render_view_impl->ForceResizeForTesting(new_size);
163 void SetDeviceScaleFactor(RenderView* render_view, float factor) {
164 static_cast<RenderViewImpl*>(render_view)->
165 SetDeviceScaleFactorForTesting(factor);
168 void SetDeviceColorProfile(RenderView* render_view, const std::string& name) {
169 if (name == "reset") {
170 static_cast<RenderViewImpl*>(render_view)->
171 ResetDeviceColorProfileForTesting();
175 std::vector<char> color_profile;
177 struct TestColorProfile {
179 static unsigned char color_profile_data[] = {
180 0x00,0x00,0x01,0xea,0x54,0x45,0x53,0x54,0x00,0x00,0x00,0x00,
181 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
182 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
183 0x61,0x63,0x73,0x70,0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,
184 0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
185 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
186 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x74,0x65,0x73,0x74,
187 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
188 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
189 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
190 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,
191 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x0d,
192 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x8c,
193 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x8c,0x00,0x00,0x00,0x14,
194 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xa0,0x00,0x00,0x00,0x14,
195 0x67,0x58,0x59,0x5a,0x00,0x00,0x01,0xb4,0x00,0x00,0x00,0x14,
196 0x62,0x58,0x59,0x5a,0x00,0x00,0x01,0xc8,0x00,0x00,0x00,0x14,
197 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
198 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
199 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
200 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
201 0x00,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
202 0x00,0x00,0x00,0x10,0x77,0x68,0x61,0x63,0x6b,0x65,0x64,0x2e,
203 0x69,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
204 0x00,0x00,0x00,0x11,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,0x00,
207 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
208 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
209 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
210 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
211 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
212 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
213 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0x52,
214 0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,0x58,0x59,0x5a,0x20,
215 0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,
216 0x00,0x00,0x0f,0x95,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
217 0x00,0x00,0x26,0x31,0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9b,
218 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,
219 0x00,0x00,0x4f,0xa5,0x00,0x00,0x04,0xfc,0x63,0x75,0x72,0x76,
220 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33
223 return reinterpret_cast<char*>(color_profile_data);
227 const size_t kTestColorProfileSizeInBytes = 490u;
228 return kTestColorProfileSizeInBytes;
232 struct AdobeRGBColorProfile {
234 static unsigned char color_profile_data[] = {
235 0x00,0x00,0x02,0x30,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
236 0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
237 0x07,0xd0,0x00,0x08,0x00,0x0b,0x00,0x13,0x00,0x33,0x00,0x3b,
238 0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4c,0x00,0x00,0x00,0x00,
239 0x6e,0x6f,0x6e,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
240 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
241 0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x41,0x44,0x42,0x45,
242 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
243 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
244 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
245 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,
246 0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xfc,0x00,0x00,0x00,0x32,
247 0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x30,0x00,0x00,0x00,0x6b,
248 0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x9c,0x00,0x00,0x00,0x14,
249 0x62,0x6b,0x70,0x74,0x00,0x00,0x01,0xb0,0x00,0x00,0x00,0x14,
250 0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xc4,0x00,0x00,0x00,0x0e,
251 0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xd4,0x00,0x00,0x00,0x0e,
252 0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xe4,0x00,0x00,0x00,0x0e,
253 0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xf4,0x00,0x00,0x00,0x14,
254 0x67,0x58,0x59,0x5a,0x00,0x00,0x02,0x08,0x00,0x00,0x00,0x14,
255 0x62,0x58,0x59,0x5a,0x00,0x00,0x02,0x1c,0x00,0x00,0x00,0x14,
256 0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x43,0x6f,0x70,0x79,
257 0x72,0x69,0x67,0x68,0x74,0x20,0x32,0x30,0x30,0x30,0x20,0x41,
258 0x64,0x6f,0x62,0x65,0x20,0x53,0x79,0x73,0x74,0x65,0x6d,0x73,
259 0x20,0x49,0x6e,0x63,0x6f,0x72,0x70,0x6f,0x72,0x61,0x74,0x65,
260 0x64,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
261 0x00,0x00,0x00,0x11,0x41,0x64,0x6f,0x62,0x65,0x20,0x52,0x47,
262 0x42,0x20,0x28,0x31,0x39,0x39,0x38,0x29,0x00,0x00,0x00,0x00,
263 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
264 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
265 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
266 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
267 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
268 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
269 0x00,0x00,0x00,0x00,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
270 0x00,0x00,0xf3,0x51,0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,
271 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
272 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x75,0x72,0x76,
273 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,
274 0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
275 0x02,0x33,0x00,0x00,0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,
276 0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,0x58,0x59,0x5a,0x20,
277 0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,0x00,0x00,0x4f,0xa5,
278 0x00,0x00,0x04,0xfc,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
279 0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,0x00,0x00,0x0f,0x95,
280 0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0x31,
281 0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9c
284 return reinterpret_cast<char*>(color_profile_data);
288 const size_t kAdobeRGBColorProfileSizeInBytes = 560u;
289 return kAdobeRGBColorProfileSizeInBytes;
293 if (name == "sRGB") {
294 color_profile.assign(name.data(), name.data() + name.size());
295 } else if (name == "test") {
296 TestColorProfile test;
297 color_profile.assign(test.data(), test.data() + test.size());
298 } else if (name == "adobeRGB") {
299 AdobeRGBColorProfile test;
300 color_profile.assign(test.data(), test.data() + test.size());
303 static_cast<RenderViewImpl*>(render_view)->
304 SetDeviceColorProfileForTesting(color_profile);
307 void UseSynchronousResizeMode(RenderView* render_view, bool enable) {
308 static_cast<RenderViewImpl*>(render_view)->
309 UseSynchronousResizeModeForTesting(enable);
312 void EnableAutoResizeMode(RenderView* render_view,
313 const WebSize& min_size,
314 const WebSize& max_size) {
315 static_cast<RenderViewImpl*>(render_view)->
316 EnableAutoResizeForTesting(min_size, max_size);
319 void DisableAutoResizeMode(RenderView* render_view, const WebSize& new_size) {
320 static_cast<RenderViewImpl*>(render_view)->
321 DisableAutoResizeForTesting(new_size);
325 base::char16 operator()(base::char16 c) { return tolower(c); }
328 // Returns True if node1 < node2.
329 bool HistoryEntryCompareLess(HistoryEntry::HistoryNode* node1,
330 HistoryEntry::HistoryNode* node2) {
331 base::string16 target1 = node1->item().target();
332 base::string16 target2 = node2->item().target();
333 std::transform(target1.begin(), target1.end(), target1.begin(), ToLower());
334 std::transform(target2.begin(), target2.end(), target2.begin(), ToLower());
335 return target1 < target2;
338 std::string DumpHistoryItem(HistoryEntry::HistoryNode* node,
340 bool is_current_index) {
343 const blink::WebHistoryItem& item = node->item();
344 if (is_current_index) {
345 result.append("curr->");
346 result.append(indent - 6, ' '); // 6 == "curr->".length()
348 result.append(indent, ' ');
351 std::string url = NormalizeLayoutTestURL(item.urlString().utf8());
353 if (!item.target().isEmpty()) {
354 result.append(" (in frame \"");
355 result.append(item.target().utf8());
356 result.append("\")");
360 std::vector<HistoryEntry::HistoryNode*> children = node->children();
361 if (!children.empty()) {
362 std::sort(children.begin(), children.end(), HistoryEntryCompareLess);
363 for (size_t i = 0; i < children.size(); ++i)
364 result += DumpHistoryItem(children[i], indent + 4, false);
370 std::string DumpBackForwardList(std::vector<PageState>& page_state,
371 size_t current_index) {
373 result.append("\n============== Back Forward List ==============\n");
374 for (size_t index = 0; index < page_state.size(); ++index) {
375 scoped_ptr<HistoryEntry> entry(
376 PageStateToHistoryEntry(page_state[index]));
378 DumpHistoryItem(entry->root_history_node(),
380 index == current_index));
382 result.append("===============================================\n");
386 blink::WebLayer* InstantiateWebLayer(scoped_refptr<cc::TextureLayer> layer) {
387 return new cc_blink::WebLayerImpl(layer);
390 } // namespace content