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