Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / test / layouttest_support.cc
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.
4
5 #include "content/public/test/layouttest_support.h"
6
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/compositor_bindings/web_layer_impl.h"
13 #include "content/renderer/history_entry.h"
14 #include "content/renderer/history_serialization.h"
15 #include "content/renderer/render_frame_impl.h"
16 #include "content/renderer/render_thread_impl.h"
17 #include "content/renderer/render_view_impl.h"
18 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
19 #include "content/shell/renderer/test_runner/test_common.h"
20 #include "content/shell/renderer/test_runner/web_frame_test_proxy.h"
21 #include "content/shell/renderer/test_runner/web_test_proxy.h"
22 #include "third_party/WebKit/public/platform/WebBatteryStatus.h"
23 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
24 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
25 #include "third_party/WebKit/public/platform/WebGamepads.h"
26 #include "third_party/WebKit/public/web/WebHistoryItem.h"
27
28 #if defined(OS_MACOSX)
29 #include "content/browser/renderer_host/popup_menu_helper_mac.h"
30 #endif
31
32 using blink::WebBatteryStatus;
33 using blink::WebDeviceMotionData;
34 using blink::WebDeviceOrientationData;
35 using blink::WebGamepad;
36 using blink::WebGamepads;
37 using blink::WebRect;
38 using blink::WebSize;
39
40 namespace content {
41
42 namespace {
43
44 base::LazyInstance<base::Callback<void(RenderView*, WebTestProxyBase*)> >::Leaky
45     g_callback = LAZY_INSTANCE_INITIALIZER;
46
47 RenderViewImpl* CreateWebTestProxy(RenderViewImplParams* params) {
48   typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ProxyType;
49   ProxyType* render_view_proxy = new ProxyType(params);
50   if (g_callback == 0)
51     return render_view_proxy;
52   g_callback.Get().Run(render_view_proxy, render_view_proxy);
53   return render_view_proxy;
54 }
55
56 WebTestProxyBase* GetWebTestProxyBase(RenderViewImpl* render_view) {
57   typedef WebTestProxy<RenderViewImpl, RenderViewImplParams*> ViewProxy;
58
59   ViewProxy* render_view_proxy = static_cast<ViewProxy*>(render_view);
60   return static_cast<WebTestProxyBase*>(render_view_proxy);
61 }
62
63 RenderFrameImpl* CreateWebFrameTestProxy(
64     RenderViewImpl* render_view,
65     int32 routing_id) {
66   typedef WebFrameTestProxy<RenderFrameImpl, RenderViewImpl*, int32> FrameProxy;
67
68   FrameProxy* render_frame_proxy = new FrameProxy(render_view, routing_id);
69   render_frame_proxy->set_base_proxy(GetWebTestProxyBase(render_view));
70
71   return render_frame_proxy;
72 }
73
74 }  // namespace
75
76
77 void EnableWebTestProxyCreation(
78     const base::Callback<void(RenderView*, WebTestProxyBase*)>& callback) {
79   g_callback.Get() = callback;
80   RenderViewImpl::InstallCreateHook(CreateWebTestProxy);
81   RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy);
82 }
83
84 void SetMockGamepadProvider(RendererGamepadProvider* provider) {
85   RenderThreadImpl::current()->webkit_platform_support()->
86       set_gamepad_provider(provider);
87 }
88
89 void SetMockDeviceLightData(const double data) {
90   RendererWebKitPlatformSupportImpl::SetMockDeviceLightDataForTesting(data);
91 }
92
93 void SetMockDeviceMotionData(const WebDeviceMotionData& data) {
94   RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data);
95 }
96
97 void SetMockDeviceOrientationData(const WebDeviceOrientationData& data) {
98   RendererWebKitPlatformSupportImpl::
99       SetMockDeviceOrientationDataForTesting(data);
100 }
101
102 void MockBatteryStatusChanged(const WebBatteryStatus& status) {
103   RendererWebKitPlatformSupportImpl::MockBatteryStatusChangedForTesting(status);
104 }
105
106 void EnableRendererLayoutTestMode() {
107   RenderThreadImpl::current()->set_layout_test_mode(true);
108 }
109
110 void EnableBrowserLayoutTestMode() {
111 #if defined(OS_MACOSX)
112   ImageTransportSurface::SetAllowOSMesaForTesting(true);
113   PopupMenuHelper::DontShowPopupMenuForTesting();
114 #endif
115   RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
116 }
117
118 int GetLocalSessionHistoryLength(RenderView* render_view) {
119   return static_cast<RenderViewImpl*>(render_view)->
120       GetLocalSessionHistoryLengthForTesting();
121 }
122
123 void SyncNavigationState(RenderView* render_view) {
124   static_cast<RenderViewImpl*>(render_view)->SyncNavigationState();
125 }
126
127 void SetFocusAndActivate(RenderView* render_view, bool enable) {
128   static_cast<RenderViewImpl*>(render_view)->
129       SetFocusAndActivateForTesting(enable);
130 }
131
132 void ForceResizeRenderView(RenderView* render_view,
133                            const WebSize& new_size) {
134   RenderViewImpl* render_view_impl = static_cast<RenderViewImpl*>(render_view);
135   render_view_impl->ForceResizeForTesting(new_size);
136 }
137
138 void SetDeviceScaleFactor(RenderView* render_view, float factor) {
139   static_cast<RenderViewImpl*>(render_view)->
140       SetDeviceScaleFactorForTesting(factor);
141 }
142
143 void SetDeviceColorProfile(RenderView* render_view, const std::string& name) {
144   std::vector<char> color_profile;
145
146   struct TestColorProfile {
147     char* data() {
148       static unsigned char color_profile_data[] = {
149         0x00,0x00,0x01,0xea,0x54,0x45,0x53,0x54,0x00,0x00,0x00,0x00,
150         0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
151         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
152         0x61,0x63,0x73,0x70,0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,
153         0x74,0x65,0x73,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
154         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
155         0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x74,0x65,0x73,0x74,
156         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
157         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
158         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
159         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,
160         0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x0d,
161         0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x8c,
162         0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x8c,0x00,0x00,0x00,0x14,
163         0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xa0,0x00,0x00,0x00,0x14,
164         0x67,0x58,0x59,0x5a,0x00,0x00,0x01,0xb4,0x00,0x00,0x00,0x14,
165         0x62,0x58,0x59,0x5a,0x00,0x00,0x01,0xc8,0x00,0x00,0x00,0x14,
166         0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
167         0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
168         0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xdc,0x00,0x00,0x00,0x0e,
169         0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
170         0x00,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
171         0x00,0x00,0x00,0x10,0x77,0x68,0x61,0x63,0x6b,0x65,0x64,0x2e,
172         0x69,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
173         0x00,0x00,0x00,0x11,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,0x00,
177         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
178         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
179         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
180         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
181         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
182         0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0x52,
183         0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,0x58,0x59,0x5a,0x20,
184         0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,
185         0x00,0x00,0x0f,0x95,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
186         0x00,0x00,0x26,0x31,0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9b,
187         0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,
188         0x00,0x00,0x4f,0xa5,0x00,0x00,0x04,0xfc,0x63,0x75,0x72,0x76,
189         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33
190       };
191
192       return reinterpret_cast<char*>(color_profile_data);
193     }
194
195     size_t size() {
196       const size_t kTestColorProfileSizeInBytes = 490u;
197       return kTestColorProfileSizeInBytes;
198     }
199   };
200
201   struct AdobeRGBColorProfile {
202     char* data() {
203       static unsigned char color_profile_data[] = {
204         0x00,0x00,0x02,0x30,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
205         0x6d,0x6e,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5a,0x20,
206         0x07,0xd0,0x00,0x08,0x00,0x0b,0x00,0x13,0x00,0x33,0x00,0x3b,
207         0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4c,0x00,0x00,0x00,0x00,
208         0x6e,0x6f,0x6e,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
209         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf6,0xd6,
210         0x00,0x01,0x00,0x00,0x00,0x00,0xd3,0x2d,0x41,0x44,0x42,0x45,
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         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
214         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,
215         0x63,0x70,0x72,0x74,0x00,0x00,0x00,0xfc,0x00,0x00,0x00,0x32,
216         0x64,0x65,0x73,0x63,0x00,0x00,0x01,0x30,0x00,0x00,0x00,0x6b,
217         0x77,0x74,0x70,0x74,0x00,0x00,0x01,0x9c,0x00,0x00,0x00,0x14,
218         0x62,0x6b,0x70,0x74,0x00,0x00,0x01,0xb0,0x00,0x00,0x00,0x14,
219         0x72,0x54,0x52,0x43,0x00,0x00,0x01,0xc4,0x00,0x00,0x00,0x0e,
220         0x67,0x54,0x52,0x43,0x00,0x00,0x01,0xd4,0x00,0x00,0x00,0x0e,
221         0x62,0x54,0x52,0x43,0x00,0x00,0x01,0xe4,0x00,0x00,0x00,0x0e,
222         0x72,0x58,0x59,0x5a,0x00,0x00,0x01,0xf4,0x00,0x00,0x00,0x14,
223         0x67,0x58,0x59,0x5a,0x00,0x00,0x02,0x08,0x00,0x00,0x00,0x14,
224         0x62,0x58,0x59,0x5a,0x00,0x00,0x02,0x1c,0x00,0x00,0x00,0x14,
225         0x74,0x65,0x78,0x74,0x00,0x00,0x00,0x00,0x43,0x6f,0x70,0x79,
226         0x72,0x69,0x67,0x68,0x74,0x20,0x32,0x30,0x30,0x30,0x20,0x41,
227         0x64,0x6f,0x62,0x65,0x20,0x53,0x79,0x73,0x74,0x65,0x6d,0x73,
228         0x20,0x49,0x6e,0x63,0x6f,0x72,0x70,0x6f,0x72,0x61,0x74,0x65,
229         0x64,0x00,0x00,0x00,0x64,0x65,0x73,0x63,0x00,0x00,0x00,0x00,
230         0x00,0x00,0x00,0x11,0x41,0x64,0x6f,0x62,0x65,0x20,0x52,0x47,
231         0x42,0x20,0x28,0x31,0x39,0x39,0x38,0x29,0x00,0x00,0x00,0x00,
232         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
233         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
234         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
235         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
236         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
237         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
238         0x00,0x00,0x00,0x00,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
239         0x00,0x00,0xf3,0x51,0x00,0x01,0x00,0x00,0x00,0x01,0x16,0xcc,
240         0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
241         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x75,0x72,0x76,
242         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,
243         0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
244         0x02,0x33,0x00,0x00,0x63,0x75,0x72,0x76,0x00,0x00,0x00,0x00,
245         0x00,0x00,0x00,0x01,0x02,0x33,0x00,0x00,0x58,0x59,0x5a,0x20,
246         0x00,0x00,0x00,0x00,0x00,0x00,0x9c,0x18,0x00,0x00,0x4f,0xa5,
247         0x00,0x00,0x04,0xfc,0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,
248         0x00,0x00,0x34,0x8d,0x00,0x00,0xa0,0x2c,0x00,0x00,0x0f,0x95,
249         0x58,0x59,0x5a,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0x31,
250         0x00,0x00,0x10,0x2f,0x00,0x00,0xbe,0x9c
251       };
252
253       return reinterpret_cast<char*>(color_profile_data);
254     }
255
256     size_t size() {
257       const size_t kAdobeRGBColorProfileSizeInBytes = 560u;
258       return kAdobeRGBColorProfileSizeInBytes;
259     }
260   };
261
262   if (name == "sRGB") {
263     color_profile.assign(name.data(), name.data() + name.size());
264   } else if (name == "test") {
265     TestColorProfile test;
266     color_profile.assign(test.data(), test.data() + test.size());
267   } else if (name == "adobeRGB") {
268     AdobeRGBColorProfile test;
269     color_profile.assign(test.data(), test.data() + test.size());
270   }
271
272   static_cast<RenderViewImpl*>(render_view)->
273       SetDeviceColorProfileForTesting(color_profile);
274 }
275
276 void UseSynchronousResizeMode(RenderView* render_view, bool enable) {
277   static_cast<RenderViewImpl*>(render_view)->
278       UseSynchronousResizeModeForTesting(enable);
279 }
280
281 void EnableAutoResizeMode(RenderView* render_view,
282                           const WebSize& min_size,
283                           const WebSize& max_size) {
284   static_cast<RenderViewImpl*>(render_view)->
285       EnableAutoResizeForTesting(min_size, max_size);
286 }
287
288 void DisableAutoResizeMode(RenderView* render_view, const WebSize& new_size) {
289   static_cast<RenderViewImpl*>(render_view)->
290       DisableAutoResizeForTesting(new_size);
291 }
292
293 struct ToLower {
294   base::char16 operator()(base::char16 c) { return tolower(c); }
295 };
296
297 // Returns True if node1 < node2.
298 bool HistoryEntryCompareLess(HistoryEntry::HistoryNode* node1,
299                              HistoryEntry::HistoryNode* node2) {
300   base::string16 target1 = node1->item().target();
301   base::string16 target2 = node2->item().target();
302   std::transform(target1.begin(), target1.end(), target1.begin(), ToLower());
303   std::transform(target2.begin(), target2.end(), target2.begin(), ToLower());
304   return target1 < target2;
305 }
306
307 std::string DumpHistoryItem(HistoryEntry::HistoryNode* node,
308                             int indent,
309                             bool is_current_index) {
310   std::string result;
311
312   const blink::WebHistoryItem& item = node->item();
313   if (is_current_index) {
314     result.append("curr->");
315     result.append(indent - 6, ' '); // 6 == "curr->".length()
316   } else {
317     result.append(indent, ' ');
318   }
319
320   std::string url = NormalizeLayoutTestURL(item.urlString().utf8());
321   result.append(url);
322   if (!item.target().isEmpty()) {
323     result.append(" (in frame \"");
324     result.append(item.target().utf8());
325     result.append("\")");
326   }
327   result.append("\n");
328
329   std::vector<HistoryEntry::HistoryNode*> children = node->children();
330   if (!children.empty()) {
331     std::sort(children.begin(), children.end(), HistoryEntryCompareLess);
332     for (size_t i = 0; i < children.size(); ++i)
333       result += DumpHistoryItem(children[i], indent + 4, false);
334   }
335
336   return result;
337 }
338
339 std::string DumpBackForwardList(std::vector<PageState>& page_state,
340                                 size_t current_index) {
341   std::string result;
342   result.append("\n============== Back Forward List ==============\n");
343   for (size_t index = 0; index < page_state.size(); ++index) {
344     scoped_ptr<HistoryEntry> entry(
345         PageStateToHistoryEntry(page_state[index]));
346     result.append(
347         DumpHistoryItem(entry->root_history_node(),
348                         8,
349                         index == current_index));
350   }
351   result.append("===============================================\n");
352   return result;
353 }
354
355 blink::WebLayer* InstantiateWebLayer(scoped_refptr<cc::TextureLayer> layer) {
356   return new WebLayerImpl(layer);
357 }
358
359 }  // namespace content