blob: a63e0cd6bcb9fb474c2a28fbad1b208efd2881ad [file] [log] [blame]
[email protected]31d71b02012-01-26 03:42:311// 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
[email protected]9b4c6cd2012-08-20 10:47:455#include "content/shell/webkit_test_runner.h"
[email protected]31d71b02012-01-26 03:42:316
[email protected]0c5f2222013-02-20 11:26:197#include <algorithm>
[email protected]833b700d2013-02-18 19:43:008#include <clocale>
[email protected]b9fad24c2012-11-15 08:22:109#include <cmath>
10
[email protected]09ec9f92012-12-17 09:46:3611#include "base/base64.h"
[email protected]29e82702013-02-14 12:56:4012#include "base/debug/debugger.h"
[email protected]0799da02012-06-27 10:58:5113#include "base/md5.h"
[email protected]e225b922012-08-18 01:43:0414#include "base/memory/scoped_ptr.h"
[email protected]b2324b092012-11-01 10:34:1115#include "base/message_loop.h"
[email protected]0c5f2222013-02-20 11:26:1916#include "base/string_util.h"
[email protected]c272c5b2012-06-06 09:01:0617#include "base/stringprintf.h"
[email protected]0d2dfb92d2012-11-05 10:26:4518#include "base/sys_string_conversions.h"
[email protected]b2324b092012-11-01 10:34:1119#include "base/time.h"
[email protected]0d2dfb92d2012-11-05 10:26:4520#include "base/utf_string_conversions.h"
[email protected]efb5f572012-01-29 10:57:3321#include "content/public/renderer/render_view.h"
[email protected]e5196922012-12-11 22:18:2522#include "content/public/test/layouttest_support.h"
[email protected]efb5f572012-01-29 10:57:3323#include "content/shell/shell_messages.h"
[email protected]b2324b092012-11-01 10:34:1124#include "content/shell/shell_render_process_observer.h"
[email protected]55915a72012-12-18 11:55:2525#include "content/shell/webkit_test_helpers.h"
[email protected]9d9ec982013-01-10 18:53:3926#include "net/base/net_errors.h"
[email protected]0d2dfb92d2012-11-05 10:26:4527#include "net/base/net_util.h"
[email protected]0799da02012-06-27 10:58:5128#include "skia/ext/platform_canvas.h"
[email protected]2a58eb12012-12-18 07:56:4029#include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
[email protected]87884c42013-01-10 01:56:2730#include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h"
31#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
32#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
33#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
34#include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
[email protected]9d9ec982013-01-10 18:53:3935#include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h"
[email protected]0ac1a032013-02-12 12:53:3736#include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
[email protected]893fc6362013-02-18 13:24:0537#include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBufferView.h"
[email protected]b2324b092012-11-01 10:34:1138#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
[email protected]0ac1a032013-02-12 12:53:3739#include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
[email protected]001841c92012-12-11 17:00:1340#include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h"
[email protected]29e82702013-02-14 12:56:4041#include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientation.h"
[email protected]efb5f572012-01-29 10:57:3342#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
43#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
44#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
[email protected]8f722692013-02-23 14:14:0645#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h"
[email protected]b2324b092012-11-01 10:34:1146#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]efb5f572012-01-29 10:57:3347#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b2324b092012-11-01 10:34:1148#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]5f20df362013-02-15 19:48:1949#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestInterfaces.h"
[email protected]b9fad24c2012-11-15 08:22:1050#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]5f20df362013-02-15 19:48:1951#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestRunner.h"
[email protected]c24b40222013-02-20 11:45:4252#include "ui/gfx/rect.h"
[email protected]984a5f72012-12-13 21:41:5053#include "webkit/base/file_path_string_conversions.h"
[email protected]8f722692013-02-23 14:14:0654#include "webkit/glue/glue_serialize.h"
[email protected]0799da02012-06-27 10:58:5155#include "webkit/glue/webkit_glue.h"
[email protected]324825d2012-11-30 12:37:1556#include "webkit/glue/webpreferences.h"
[email protected]efb5f572012-01-29 10:57:3357
[email protected]2a58eb12012-12-18 07:56:4058using WebKit::Platform;
[email protected]893fc6362013-02-18 13:24:0559using WebKit::WebArrayBufferView;
[email protected]b2324b092012-11-01 10:34:1160using WebKit::WebContextMenuData;
[email protected]001841c92012-12-11 17:00:1361using WebKit::WebDevToolsAgent;
[email protected]29e82702013-02-14 12:56:4062using WebKit::WebDeviceOrientation;
[email protected]efb5f572012-01-29 10:57:3363using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1164using WebKit::WebFrame;
65using WebKit::WebGamepads;
[email protected]8f722692013-02-23 14:14:0666using WebKit::WebHistoryItem;
[email protected]0799da02012-06-27 10:58:5167using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0668using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1169using WebKit::WebString;
[email protected]09ec9f92012-12-17 09:46:3670using WebKit::WebURL;
[email protected]9d9ec982013-01-10 18:53:3971using WebKit::WebURLError;
[email protected]b2324b092012-11-01 10:34:1172using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5173using WebKit::WebView;
[email protected]b3dbcb5e2012-12-17 00:16:3774using WebTestRunner::WebPreferences;
[email protected]b2324b092012-11-01 10:34:1175using WebTestRunner::WebTask;
[email protected]5f20df362013-02-15 19:48:1976using WebTestRunner::WebTestInterfaces;
[email protected]9063bf62013-02-20 19:42:0177using WebTestRunner::WebTestProxyBase;
[email protected]efb5f572012-01-29 10:57:3378
[email protected]31d71b02012-01-26 03:42:3179namespace content {
80
[email protected]efb5f572012-01-29 10:57:3381namespace {
82
[email protected]b2324b092012-11-01 10:34:1183void InvokeTaskHelper(void* context) {
84 WebTask* task = reinterpret_cast<WebTask*>(context);
85 task->run();
86 delete task;
87}
88
[email protected]0799da02012-06-27 10:58:5189#if !defined(OS_MACOSX)
90void MakeBitmapOpaque(SkBitmap* bitmap) {
91 SkAutoLockPixels lock(*bitmap);
92 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
93 for (int y = 0; y < bitmap->height(); ++y) {
94 uint32_t* row = bitmap->getAddr32(0, y);
95 for (int x = 0; x < bitmap->width(); ++x)
96 row[x] |= 0xFF000000; // Set alpha bits to 1.
97 }
98}
99#endif
100
[email protected]b9fad24c2012-11-15 08:22:10101void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56102 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51103 const SkBitmap& bitmap = device->accessBitmap(false);
104 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
105
106#if !defined(OS_MACOSX)
107 // Only the expected PNGs for Mac have a valid alpha channel.
108 MakeBitmapOpaque(snapshot);
109#endif
110
111}
112
[email protected]efb5f572012-01-29 10:57:33113} // namespace
[email protected]cccb5cf2012-06-06 22:20:04114
[email protected]9b4c6cd2012-08-20 10:47:45115WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]5f20df362013-02-15 19:48:19116 : RenderViewObserver(render_view),
[email protected]2025a2f42013-02-18 07:25:41117 RenderViewObserverTracker<WebKitTestRunner>(render_view),
[email protected]5f20df362013-02-15 19:48:19118 is_main_window_(false) {
[email protected]31d71b02012-01-26 03:42:31119}
120
[email protected]9b4c6cd2012-08-20 10:47:45121WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31122}
123
[email protected]b2324b092012-11-01 10:34:11124// WebTestDelegate -----------------------------------------------------------
125
[email protected]b2324b092012-11-01 10:34:11126void WebKitTestRunner::clearEditCommand() {
127 render_view()->ClearEditCommands();
128}
129
130void WebKitTestRunner::setEditCommand(const std::string& name,
131 const std::string& value) {
132 render_view()->SetEditCommandForNextKeyEvent(name, value);
133}
134
[email protected]b2324b092012-11-01 10:34:11135void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25136 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11137}
138
139void WebKitTestRunner::printMessage(const std::string& message) {
140 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
141}
142
143void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40144 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11145}
146
147void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
148 MessageLoop::current()->PostDelayedTask(
149 FROM_HERE,
150 base::Bind(&WebTask::run, base::Owned(task)),
151 base::TimeDelta::FromMilliseconds(ms));
152}
153
154WebString WebKitTestRunner::registerIsolatedFileSystem(
155 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]d30a36f2013-02-07 04:16:26156 std::vector<base::FilePath> files;
[email protected]984a5f72012-12-13 21:41:50157 for (size_t i = 0; i < absolute_filenames.size(); ++i)
158 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
159 std::string filesystem_id;
160 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
161 routing_id(), files, &filesystem_id));
162 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11163}
164
165long long WebKitTestRunner::getCurrentTimeInMillisecond() {
166 return base::TimeTicks::Now().ToInternalValue() /
167 base::Time::kMicrosecondsPerMillisecond;
168}
169
170WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
171 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45172#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26173 base::FilePath path(UTF8ToWide(utf8_path));
[email protected]0d2dfb92d2012-11-05 10:26:45174#else
[email protected]d30a36f2013-02-07 04:16:26175 base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
[email protected]0d2dfb92d2012-11-05 10:26:45176#endif
177 if (!path.IsAbsolute()) {
178 GURL base_url =
179 net::FilePathToFileURL(current_working_directory_.Append(
180 FILE_PATH_LITERAL("foo")));
181 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
182 }
[email protected]984a5f72012-12-13 21:41:50183 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11184}
185
[email protected]09ec9f92012-12-17 09:46:36186WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
[email protected]d30a36f2013-02-07 04:16:26187 base::FilePath local_path;
[email protected]09ec9f92012-12-17 09:46:36188 if (!net::FileURLToFilePath(file_url, &local_path))
189 return WebURL();
190
191 std::string contents;
192 Send(new ShellViewHostMsg_ReadFileToString(
193 routing_id(), local_path, &contents));
194
195 std::string contents_base64;
196 if (!base::Base64Encode(contents, &contents_base64))
197 return WebURL();
198
199 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
200 return WebURL(GURL(data_url_prefix + contents_base64));
201}
202
203WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
204 const char kPrefix[] = "file:///tmp/LayoutTests/";
205 const int kPrefixLen = arraysize(kPrefix) - 1;
206
207 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
208 return WebURL(GURL(utf8_url));
209
[email protected]d30a36f2013-02-07 04:16:26210 base::FilePath replace_path =
[email protected]09ec9f92012-12-17 09:46:36211 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
212 FILE_PATH_LITERAL("LayoutTests/"));
213#if defined(OS_WIN)
214 std::string utf8_path = WideToUTF8(replace_path.value());
215#else
216 std::string utf8_path =
217 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
218#endif
219 std::string new_url =
220 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
221 return WebURL(GURL(new_url));
222}
223
[email protected]b3dbcb5e2012-12-17 00:16:37224WebPreferences* WebKitTestRunner::preferences() {
225 return &prefs_;
226}
227
228void WebKitTestRunner::applyPreferences() {
229 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47230 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37231 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25232 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37233}
234
[email protected]9d9ec982013-01-10 18:53:39235std::string WebKitTestRunner::makeURLErrorDescription(
236 const WebURLError& error) {
237 std::string domain = error.domain.utf8();
238 int code = error.reason;
239
240 if (domain == net::kErrorDomain) {
241 domain = "NSURLErrorDomain";
242 switch (error.reason) {
243 case net::ERR_ABORTED:
244 code = -999; // NSURLErrorCancelled
245 break;
246 case net::ERR_UNSAFE_PORT:
247 // Our unsafe port checking happens at the network stack level, but we
248 // make this translation here to match the behavior of stock WebKit.
249 domain = "WebKitErrorDomain";
250 code = 103;
251 break;
252 case net::ERR_ADDRESS_INVALID:
253 case net::ERR_ADDRESS_UNREACHABLE:
254 case net::ERR_NETWORK_ACCESS_DENIED:
255 code = -1004; // NSURLErrorCannotConnectToHost
256 break;
257 }
258 } else {
259 DLOG(WARNING) << "Unknown error domain";
260 }
261
262 return base::StringPrintf("<NSError domain %s, code %d, failing URL \"%s\">",
263 domain.c_str(), code, error.unreachableURL.spec().data());
264}
265
[email protected]29e82702013-02-14 12:56:40266void WebKitTestRunner::setClientWindowRect(const WebRect& rect) {
[email protected]c24b40222013-02-20 11:45:42267 Send(new ShellViewHostMsg_SetClientWindowRect(
268 routing_id(), gfx::Rect(rect)));
[email protected]29e82702013-02-14 12:56:40269}
270
271void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06272 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40273}
274
275void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06276 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40277}
278
279void WebKitTestRunner::evaluateInWebInspector(long call_id,
280 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06281 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
282 if (agent)
283 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40284}
285
286void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00287 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40288}
289
290void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00291 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40292}
293
294void WebKitTestRunner::setDeviceScaleFactor(float factor) {
[email protected]db6769c2013-02-24 14:59:14295 render_view()->GetWebView()->setDeviceScaleFactor(factor);
[email protected]29e82702013-02-14 12:56:40296}
297
298void WebKitTestRunner::setFocus(bool focus) {
[email protected]c24b40222013-02-20 11:45:42299 Send(new ShellViewHostMsg_SetFocus(routing_id(), focus));
[email protected]29e82702013-02-14 12:56:40300}
301
302void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00303 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40304}
305
306std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19307#if defined(OS_WIN)
308 if (resource.find("/tmp/") == 0) {
309 // We want a temp file.
310 GURL base_url = net::FilePathToFileURL(temp_path_);
311 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
312 }
313#endif
314
315 // Some layout tests use file://// which we resolve as a UNC path. Normalize
316 // them to just file:///.
317 std::string result = resource;
318 while (StringToLowerASCII(result).find("file:////") == 0) {
319 result = result.substr(0, strlen("file:///")) +
320 result.substr(strlen("file:////"));
321 }
322 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40323}
324
325void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00326 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40327}
328
[email protected]29e82702013-02-14 12:56:40329void WebKitTestRunner::testFinished() {
[email protected]5f20df362013-02-15 19:48:19330 if (!is_main_window_)
331 return;
332 WebTestInterfaces* interfaces =
333 ShellRenderProcessObserver::GetInstance()->test_interfaces();
334 interfaces->setTestIsRunning(false);
[email protected]8f722692013-02-23 14:14:06335 if (interfaces->testRunner()->shouldDumpBackForwardList()) {
336 Send(new ShellViewHostMsg_CaptureSessionHistory(routing_id()));
337 } else {
338 CaptureDump();
339 }
[email protected]29e82702013-02-14 12:56:40340}
341
342void WebKitTestRunner::testTimedOut() {
[email protected]5f20df362013-02-15 19:48:19343 if (!is_main_window_)
344 return;
345 WebTestInterfaces* interfaces =
346 ShellRenderProcessObserver::GetInstance()->test_interfaces();
347 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40348 Send(new ShellViewHostMsg_TestFinished(routing_id(), true));
349}
350
351bool WebKitTestRunner::isBeingDebugged() {
352 return base::debug::BeingDebugged();
353}
354
355int WebKitTestRunner::layoutTestTimeout() {
356 return layout_test_timeout_;
357}
358
359void WebKitTestRunner::closeRemainingWindows() {
[email protected]b17e3f92013-02-20 14:05:35360 // We currently always close all remaining windows at the end of each test.
361 // TODO(jochen): Reuse the renderer across tests instead of closing all
362 // windows. http://crbug.com/171128
[email protected]29e82702013-02-14 12:56:40363}
364
365int WebKitTestRunner::navigationEntryCount() {
[email protected]b283d292013-02-21 08:40:34366 return GetLocalSessionHistoryLength(render_view());
[email protected]29e82702013-02-14 12:56:40367}
368
[email protected]29e82702013-02-14 12:56:40369void WebKitTestRunner::goToOffset(int offset) {
[email protected]d2494ff2013-02-20 08:22:37370 Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset));
[email protected]29e82702013-02-14 12:56:40371}
372
373void WebKitTestRunner::reload() {
[email protected]d2494ff2013-02-20 08:22:37374 Send(new ShellViewHostMsg_Reload(routing_id()));
[email protected]29e82702013-02-14 12:56:40375}
376
377void WebKitTestRunner::loadURLForFrame(const WebURL& url,
378 const std::string& frame_name) {
[email protected]d2494ff2013-02-20 08:22:37379 Send(new ShellViewHostMsg_LoadURLForFrame(
380 routing_id(), url, frame_name));
[email protected]29e82702013-02-14 12:56:40381}
382
383bool WebKitTestRunner::allowExternalPages() {
384 return allow_external_pages_;
385}
386
387void WebKitTestRunner::captureHistoryForWindow(
[email protected]9063bf62013-02-20 19:42:01388 WebTestProxyBase* proxy,
[email protected]29e82702013-02-14 12:56:40389 WebVector<WebKit::WebHistoryItem>* history,
390 size_t* currentEntryIndex) {
[email protected]8f722692013-02-23 14:14:06391 size_t pos = 0;
392 std::vector<int>::iterator id;
393 for (id = routing_ids_.begin(); id != routing_ids_.end(); ++id, ++pos) {
394 RenderView* render_view = RenderView::FromRoutingID(*id);
395 if (!render_view) {
396 NOTREACHED();
397 continue;
398 }
399 if (WebKitTestRunner::Get(render_view)->proxy() == proxy)
400 break;
401 }
402
403 if (id == routing_ids_.end()) {
404 NOTREACHED();
405 return;
406 }
407 size_t num_entries = session_histories_[pos].size();
408 *currentEntryIndex = current_entry_indexes_[pos];
409 WebVector<WebHistoryItem> result(num_entries);
410 for (size_t entry = 0; entry < num_entries; ++entry) {
411 result[entry] =
412 webkit_glue::HistoryItemFromString(session_histories_[pos][entry]);
413 }
414 history->swap(result);
[email protected]29e82702013-02-14 12:56:40415}
416
[email protected]b2324b092012-11-01 10:34:11417// RenderViewObserver --------------------------------------------------------
418
[email protected]9b4c6cd2012-08-20 10:47:45419void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11420 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56421}
422
[email protected]9b4c6cd2012-08-20 10:47:45423bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33424 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45425 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37426 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
427 OnSetTestConfiguration)
[email protected]8f722692013-02-23 14:14:06428 IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory)
[email protected]efb5f572012-01-29 10:57:33429 IPC_MESSAGE_UNHANDLED(handled = false)
430 IPC_END_MESSAGE_MAP()
431
432 return handled;
433}
434
[email protected]b9fad24c2012-11-15 08:22:10435// Public methods - -----------------------------------------------------------
436
[email protected]324825d2012-11-30 12:37:15437void WebKitTestRunner::Reset() {
[email protected]b3dbcb5e2012-12-17 00:16:37438 prefs_.reset();
[email protected]324825d2012-11-30 12:37:15439 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47440 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]324825d2012-11-30 12:37:15441 render_view()->SetWebkitPreferences(prefs);
[email protected]0ac1a032013-02-12 12:53:37442 enable_pixel_dumping_ = true;
443 layout_test_timeout_ = 30 * 1000;
444 allow_external_pages_ = false;
445 expected_pixel_hash_ = std::string();
[email protected]8f722692013-02-23 14:14:06446 routing_ids_.clear();
447 session_histories_.clear();
448 current_entry_indexes_.clear();
[email protected]324825d2012-11-30 12:37:15449}
450
[email protected]b2324b092012-11-01 10:34:11451// Private methods -----------------------------------------------------------
452
[email protected]0ac1a032013-02-12 12:53:37453void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19454 WebTestInterfaces* interfaces =
455 ShellRenderProcessObserver::GetInstance()->test_interfaces();
456
[email protected]893fc6362013-02-18 13:24:05457 if (interfaces->testRunner()->shouldDumpAsAudio()) {
458 const WebArrayBufferView* audio_data =
459 interfaces->testRunner()->audioData();
460 std::vector<unsigned char> vector_data(
461 static_cast<const unsigned char*>(audio_data->baseAddress()),
462 static_cast<const unsigned char*>(audio_data->baseAddress()) +
463 audio_data->byteLength());
464 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
465 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
466 return;
467 }
468
[email protected]5f20df362013-02-15 19:48:19469 Send(
470 new ShellViewHostMsg_TextDump(routing_id(), proxy()->captureTree(false)));
471
472 if (interfaces->testRunner()->shouldGeneratePixelResults()) {
473 SkBitmap snapshot;
474 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
475
476 SkAutoLockPixels snapshot_lock(snapshot);
477 base::MD5Digest digest;
478#if defined(OS_ANDROID)
479 // On Android, pixel layout is RGBA, however, other Chrome platforms use
480 // BGRA.
481 const uint8_t* raw_pixels =
482 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
483 size_t snapshot_size = snapshot.getSize();
484 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
485 for (size_t i = 0; i < snapshot_size; i += 4) {
486 reordered_pixels[i] = raw_pixels[i + 2];
487 reordered_pixels[i + 1] = raw_pixels[i + 1];
488 reordered_pixels[i + 2] = raw_pixels[i];
489 reordered_pixels[i + 3] = raw_pixels[i + 3];
490 }
491 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
492#else
493 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
494#endif
495 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
496
497 if (actual_pixel_hash == expected_pixel_hash_) {
498 SkBitmap empty_image;
499 Send(new ShellViewHostMsg_ImageDump(
500 routing_id(), actual_pixel_hash, empty_image));
501 } else {
502 Send(new ShellViewHostMsg_ImageDump(
503 routing_id(), actual_pixel_hash, snapshot));
504 }
[email protected]0ac1a032013-02-12 12:53:37505 }
[email protected]5f20df362013-02-15 19:48:19506
[email protected]0ac1a032013-02-12 12:53:37507 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
508}
509
[email protected]0ac1a032013-02-12 12:53:37510void WebKitTestRunner::OnSetTestConfiguration(
[email protected]26a51632013-02-18 09:48:56511 const ShellViewMsg_SetTestConfiguration_Params& params) {
512 current_working_directory_ = params.current_working_directory;
[email protected]0c5f2222013-02-20 11:26:19513 temp_path_ = params.temp_path;
[email protected]26a51632013-02-18 09:48:56514 enable_pixel_dumping_ = params.enable_pixel_dumping;
515 layout_test_timeout_ = params.layout_test_timeout;
516 allow_external_pages_ = params.allow_external_pages;
517 expected_pixel_hash_ = params.expected_pixel_hash;
[email protected]5f20df362013-02-15 19:48:19518 is_main_window_ = true;
519
520 WebTestInterfaces* interfaces =
521 ShellRenderProcessObserver::GetInstance()->test_interfaces();
522 interfaces->setTestIsRunning(true);
[email protected]26a51632013-02-18 09:48:56523 interfaces->configureForTestWithURL(params.test_url, enable_pixel_dumping_);
[email protected]0d2dfb92d2012-11-05 10:26:45524}
525
[email protected]8f722692013-02-23 14:14:06526void WebKitTestRunner::OnSessionHistory(
527 const std::vector<int>& routing_ids,
528 const std::vector<std::vector<std::string> >& session_histories,
529 const std::vector<unsigned>& current_entry_indexes) {
530 routing_ids_ = routing_ids;
531 session_histories_ = session_histories;
532 current_entry_indexes_ = current_entry_indexes;
533 CaptureDump();
534}
535
[email protected]31d71b02012-01-26 03:42:31536} // namespace content