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