blob: 27ccf0e5e4ca72e0cc86ae2208110f2ee8972053 [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]f322356c2013-03-01 07:54:0222#include "content/public/renderer/render_view_visitor.h"
[email protected]e5196922012-12-11 22:18:2523#include "content/public/test/layouttest_support.h"
[email protected]efb5f572012-01-29 10:57:3324#include "content/shell/shell_messages.h"
[email protected]b2324b092012-11-01 10:34:1125#include "content/shell/shell_render_process_observer.h"
[email protected]55915a72012-12-18 11:55:2526#include "content/shell/webkit_test_helpers.h"
[email protected]9d9ec982013-01-10 18:53:3927#include "net/base/net_errors.h"
[email protected]0d2dfb92d2012-11-05 10:26:4528#include "net/base/net_util.h"
[email protected]0799da02012-06-27 10:58:5129#include "skia/ext/platform_canvas.h"
[email protected]2a58eb12012-12-18 07:56:4030#include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
[email protected]87884c42013-01-10 01:56:2731#include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h"
32#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
33#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
34#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
35#include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
[email protected]9d9ec982013-01-10 18:53:3936#include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h"
[email protected]0ac1a032013-02-12 12:53:3737#include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
[email protected]893fc6362013-02-18 13:24:0538#include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBufferView.h"
[email protected]b2324b092012-11-01 10:34:1139#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
[email protected]0ac1a032013-02-12 12:53:3740#include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
[email protected]001841c92012-12-11 17:00:1341#include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h"
[email protected]29e82702013-02-14 12:56:4042#include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientation.h"
[email protected]efb5f572012-01-29 10:57:3343#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
44#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
45#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
[email protected]8f722692013-02-23 14:14:0646#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h"
[email protected]b2324b092012-11-01 10:34:1147#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]efb5f572012-01-29 10:57:3348#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b2324b092012-11-01 10:34:1149#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]5f20df362013-02-15 19:48:1950#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestInterfaces.h"
[email protected]b9fad24c2012-11-15 08:22:1051#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]5f20df362013-02-15 19:48:1952#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestRunner.h"
[email protected]c24b40222013-02-20 11:45:4253#include "ui/gfx/rect.h"
[email protected]984a5f72012-12-13 21:41:5054#include "webkit/base/file_path_string_conversions.h"
[email protected]8f722692013-02-23 14:14:0655#include "webkit/glue/glue_serialize.h"
[email protected]0799da02012-06-27 10:58:5156#include "webkit/glue/webkit_glue.h"
[email protected]324825d2012-11-30 12:37:1557#include "webkit/glue/webpreferences.h"
[email protected]efb5f572012-01-29 10:57:3358
[email protected]2a58eb12012-12-18 07:56:4059using WebKit::Platform;
[email protected]893fc6362013-02-18 13:24:0560using WebKit::WebArrayBufferView;
[email protected]b2324b092012-11-01 10:34:1161using WebKit::WebContextMenuData;
[email protected]001841c92012-12-11 17:00:1362using WebKit::WebDevToolsAgent;
[email protected]29e82702013-02-14 12:56:4063using WebKit::WebDeviceOrientation;
[email protected]efb5f572012-01-29 10:57:3364using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1165using WebKit::WebFrame;
66using WebKit::WebGamepads;
[email protected]8f722692013-02-23 14:14:0667using WebKit::WebHistoryItem;
[email protected]0799da02012-06-27 10:58:5168using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0669using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1170using WebKit::WebString;
[email protected]09ec9f92012-12-17 09:46:3671using WebKit::WebURL;
[email protected]9d9ec982013-01-10 18:53:3972using WebKit::WebURLError;
[email protected]b2324b092012-11-01 10:34:1173using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5174using WebKit::WebView;
[email protected]b3dbcb5e2012-12-17 00:16:3775using WebTestRunner::WebPreferences;
[email protected]b2324b092012-11-01 10:34:1176using WebTestRunner::WebTask;
[email protected]5f20df362013-02-15 19:48:1977using WebTestRunner::WebTestInterfaces;
[email protected]9063bf62013-02-20 19:42:0178using WebTestRunner::WebTestProxyBase;
[email protected]efb5f572012-01-29 10:57:3379
[email protected]31d71b02012-01-26 03:42:3180namespace content {
81
[email protected]efb5f572012-01-29 10:57:3382namespace {
83
[email protected]ab683b22013-03-04 09:10:3184int kDefaultLayoutTestTimeoutMs = 30 * 1000;
85
[email protected]b2324b092012-11-01 10:34:1186void InvokeTaskHelper(void* context) {
87 WebTask* task = reinterpret_cast<WebTask*>(context);
88 task->run();
89 delete task;
90}
91
[email protected]0799da02012-06-27 10:58:5192#if !defined(OS_MACOSX)
93void MakeBitmapOpaque(SkBitmap* bitmap) {
94 SkAutoLockPixels lock(*bitmap);
95 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
96 for (int y = 0; y < bitmap->height(); ++y) {
97 uint32_t* row = bitmap->getAddr32(0, y);
98 for (int x = 0; x < bitmap->width(); ++x)
99 row[x] |= 0xFF000000; // Set alpha bits to 1.
100 }
101}
102#endif
103
[email protected]b9fad24c2012-11-15 08:22:10104void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56105 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51106 const SkBitmap& bitmap = device->accessBitmap(false);
107 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
108
109#if !defined(OS_MACOSX)
110 // Only the expected PNGs for Mac have a valid alpha channel.
111 MakeBitmapOpaque(snapshot);
112#endif
113
114}
115
[email protected]f322356c2013-03-01 07:54:02116class SyncNavigationStateVisitor : public RenderViewVisitor {
117 public:
118 SyncNavigationStateVisitor() {}
119 virtual ~SyncNavigationStateVisitor() {}
120
121 virtual bool Visit(RenderView* render_view) OVERRIDE {
122 SyncNavigationState(render_view);
123 return true;
124 }
125 private:
126 DISALLOW_COPY_AND_ASSIGN(SyncNavigationStateVisitor);
127};
128
[email protected]efb5f572012-01-29 10:57:33129} // namespace
[email protected]cccb5cf2012-06-06 22:20:04130
[email protected]9b4c6cd2012-08-20 10:47:45131WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]5f20df362013-02-15 19:48:19132 : RenderViewObserver(render_view),
[email protected]2025a2f42013-02-18 07:25:41133 RenderViewObserverTracker<WebKitTestRunner>(render_view),
[email protected]ab683b22013-03-04 09:10:31134 proxy_(NULL),
135 enable_pixel_dumping_(true),
136 layout_test_timeout_(kDefaultLayoutTestTimeoutMs),
137 allow_external_pages_(false),
[email protected]5f20df362013-02-15 19:48:19138 is_main_window_(false) {
[email protected]31d71b02012-01-26 03:42:31139}
140
[email protected]9b4c6cd2012-08-20 10:47:45141WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31142}
143
[email protected]b2324b092012-11-01 10:34:11144// WebTestDelegate -----------------------------------------------------------
145
[email protected]b2324b092012-11-01 10:34:11146void WebKitTestRunner::clearEditCommand() {
147 render_view()->ClearEditCommands();
148}
149
150void WebKitTestRunner::setEditCommand(const std::string& name,
151 const std::string& value) {
152 render_view()->SetEditCommandForNextKeyEvent(name, value);
153}
154
[email protected]b2324b092012-11-01 10:34:11155void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25156 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11157}
158
159void WebKitTestRunner::printMessage(const std::string& message) {
160 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
161}
162
163void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40164 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11165}
166
167void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
168 MessageLoop::current()->PostDelayedTask(
169 FROM_HERE,
170 base::Bind(&WebTask::run, base::Owned(task)),
171 base::TimeDelta::FromMilliseconds(ms));
172}
173
174WebString WebKitTestRunner::registerIsolatedFileSystem(
175 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]d30a36f2013-02-07 04:16:26176 std::vector<base::FilePath> files;
[email protected]984a5f72012-12-13 21:41:50177 for (size_t i = 0; i < absolute_filenames.size(); ++i)
178 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
179 std::string filesystem_id;
180 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
181 routing_id(), files, &filesystem_id));
182 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11183}
184
185long long WebKitTestRunner::getCurrentTimeInMillisecond() {
186 return base::TimeTicks::Now().ToInternalValue() /
187 base::Time::kMicrosecondsPerMillisecond;
188}
189
190WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
191 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45192#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26193 base::FilePath path(UTF8ToWide(utf8_path));
[email protected]0d2dfb92d2012-11-05 10:26:45194#else
[email protected]d30a36f2013-02-07 04:16:26195 base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
[email protected]0d2dfb92d2012-11-05 10:26:45196#endif
197 if (!path.IsAbsolute()) {
198 GURL base_url =
199 net::FilePathToFileURL(current_working_directory_.Append(
200 FILE_PATH_LITERAL("foo")));
201 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
202 }
[email protected]984a5f72012-12-13 21:41:50203 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11204}
205
[email protected]09ec9f92012-12-17 09:46:36206WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
[email protected]d30a36f2013-02-07 04:16:26207 base::FilePath local_path;
[email protected]09ec9f92012-12-17 09:46:36208 if (!net::FileURLToFilePath(file_url, &local_path))
209 return WebURL();
210
211 std::string contents;
212 Send(new ShellViewHostMsg_ReadFileToString(
213 routing_id(), local_path, &contents));
214
215 std::string contents_base64;
216 if (!base::Base64Encode(contents, &contents_base64))
217 return WebURL();
218
219 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
220 return WebURL(GURL(data_url_prefix + contents_base64));
221}
222
223WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
224 const char kPrefix[] = "file:///tmp/LayoutTests/";
225 const int kPrefixLen = arraysize(kPrefix) - 1;
226
227 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
228 return WebURL(GURL(utf8_url));
229
[email protected]d30a36f2013-02-07 04:16:26230 base::FilePath replace_path =
[email protected]09ec9f92012-12-17 09:46:36231 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
232 FILE_PATH_LITERAL("LayoutTests/"));
233#if defined(OS_WIN)
234 std::string utf8_path = WideToUTF8(replace_path.value());
235#else
236 std::string utf8_path =
237 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
238#endif
239 std::string new_url =
240 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
241 return WebURL(GURL(new_url));
242}
243
[email protected]b3dbcb5e2012-12-17 00:16:37244WebPreferences* WebKitTestRunner::preferences() {
245 return &prefs_;
246}
247
248void WebKitTestRunner::applyPreferences() {
249 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47250 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37251 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25252 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37253}
254
[email protected]9d9ec982013-01-10 18:53:39255std::string WebKitTestRunner::makeURLErrorDescription(
256 const WebURLError& error) {
257 std::string domain = error.domain.utf8();
258 int code = error.reason;
259
260 if (domain == net::kErrorDomain) {
261 domain = "NSURLErrorDomain";
262 switch (error.reason) {
263 case net::ERR_ABORTED:
264 code = -999; // NSURLErrorCancelled
265 break;
266 case net::ERR_UNSAFE_PORT:
267 // Our unsafe port checking happens at the network stack level, but we
268 // make this translation here to match the behavior of stock WebKit.
269 domain = "WebKitErrorDomain";
270 code = 103;
271 break;
272 case net::ERR_ADDRESS_INVALID:
273 case net::ERR_ADDRESS_UNREACHABLE:
274 case net::ERR_NETWORK_ACCESS_DENIED:
275 code = -1004; // NSURLErrorCannotConnectToHost
276 break;
277 }
278 } else {
279 DLOG(WARNING) << "Unknown error domain";
280 }
281
282 return base::StringPrintf("<NSError domain %s, code %d, failing URL \"%s\">",
283 domain.c_str(), code, error.unreachableURL.spec().data());
284}
285
[email protected]29e82702013-02-14 12:56:40286void WebKitTestRunner::setClientWindowRect(const WebRect& rect) {
[email protected]c24b40222013-02-20 11:45:42287 Send(new ShellViewHostMsg_SetClientWindowRect(
288 routing_id(), gfx::Rect(rect)));
[email protected]29e82702013-02-14 12:56:40289}
290
291void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06292 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40293}
294
295void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06296 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40297}
298
299void WebKitTestRunner::evaluateInWebInspector(long call_id,
300 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06301 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
302 if (agent)
303 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40304}
305
306void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00307 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40308}
309
310void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00311 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40312}
313
314void WebKitTestRunner::setDeviceScaleFactor(float factor) {
[email protected]db6769c2013-02-24 14:59:14315 render_view()->GetWebView()->setDeviceScaleFactor(factor);
[email protected]29e82702013-02-14 12:56:40316}
317
318void WebKitTestRunner::setFocus(bool focus) {
[email protected]c24b40222013-02-20 11:45:42319 Send(new ShellViewHostMsg_SetFocus(routing_id(), focus));
[email protected]29e82702013-02-14 12:56:40320}
321
322void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00323 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40324}
325
326std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19327#if defined(OS_WIN)
328 if (resource.find("/tmp/") == 0) {
329 // We want a temp file.
330 GURL base_url = net::FilePathToFileURL(temp_path_);
331 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
332 }
333#endif
334
335 // Some layout tests use file://// which we resolve as a UNC path. Normalize
336 // them to just file:///.
337 std::string result = resource;
338 while (StringToLowerASCII(result).find("file:////") == 0) {
339 result = result.substr(0, strlen("file:///")) +
340 result.substr(strlen("file:////"));
341 }
342 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40343}
344
345void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00346 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40347}
348
[email protected]29e82702013-02-14 12:56:40349void WebKitTestRunner::testFinished() {
[email protected]5f20df362013-02-15 19:48:19350 if (!is_main_window_)
351 return;
352 WebTestInterfaces* interfaces =
353 ShellRenderProcessObserver::GetInstance()->test_interfaces();
354 interfaces->setTestIsRunning(false);
[email protected]8f722692013-02-23 14:14:06355 if (interfaces->testRunner()->shouldDumpBackForwardList()) {
[email protected]f322356c2013-03-01 07:54:02356 SyncNavigationStateVisitor visitor;
357 RenderView::ForEach(&visitor);
[email protected]8f722692013-02-23 14:14:06358 Send(new ShellViewHostMsg_CaptureSessionHistory(routing_id()));
359 } else {
360 CaptureDump();
361 }
[email protected]29e82702013-02-14 12:56:40362}
363
364void WebKitTestRunner::testTimedOut() {
[email protected]5f20df362013-02-15 19:48:19365 if (!is_main_window_)
366 return;
367 WebTestInterfaces* interfaces =
368 ShellRenderProcessObserver::GetInstance()->test_interfaces();
369 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40370 Send(new ShellViewHostMsg_TestFinished(routing_id(), true));
371}
372
373bool WebKitTestRunner::isBeingDebugged() {
374 return base::debug::BeingDebugged();
375}
376
377int WebKitTestRunner::layoutTestTimeout() {
378 return layout_test_timeout_;
379}
380
381void WebKitTestRunner::closeRemainingWindows() {
[email protected]b17e3f92013-02-20 14:05:35382 // We currently always close all remaining windows at the end of each test.
383 // TODO(jochen): Reuse the renderer across tests instead of closing all
384 // windows. http://crbug.com/171128
[email protected]29e82702013-02-14 12:56:40385}
386
387int WebKitTestRunner::navigationEntryCount() {
[email protected]b283d292013-02-21 08:40:34388 return GetLocalSessionHistoryLength(render_view());
[email protected]29e82702013-02-14 12:56:40389}
390
[email protected]29e82702013-02-14 12:56:40391void WebKitTestRunner::goToOffset(int offset) {
[email protected]d2494ff2013-02-20 08:22:37392 Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset));
[email protected]29e82702013-02-14 12:56:40393}
394
395void WebKitTestRunner::reload() {
[email protected]d2494ff2013-02-20 08:22:37396 Send(new ShellViewHostMsg_Reload(routing_id()));
[email protected]29e82702013-02-14 12:56:40397}
398
399void WebKitTestRunner::loadURLForFrame(const WebURL& url,
400 const std::string& frame_name) {
[email protected]d2494ff2013-02-20 08:22:37401 Send(new ShellViewHostMsg_LoadURLForFrame(
402 routing_id(), url, frame_name));
[email protected]29e82702013-02-14 12:56:40403}
404
405bool WebKitTestRunner::allowExternalPages() {
406 return allow_external_pages_;
407}
408
409void WebKitTestRunner::captureHistoryForWindow(
[email protected]9063bf62013-02-20 19:42:01410 WebTestProxyBase* proxy,
[email protected]29e82702013-02-14 12:56:40411 WebVector<WebKit::WebHistoryItem>* history,
412 size_t* currentEntryIndex) {
[email protected]8f722692013-02-23 14:14:06413 size_t pos = 0;
414 std::vector<int>::iterator id;
415 for (id = routing_ids_.begin(); id != routing_ids_.end(); ++id, ++pos) {
416 RenderView* render_view = RenderView::FromRoutingID(*id);
417 if (!render_view) {
418 NOTREACHED();
419 continue;
420 }
421 if (WebKitTestRunner::Get(render_view)->proxy() == proxy)
422 break;
423 }
424
425 if (id == routing_ids_.end()) {
426 NOTREACHED();
427 return;
428 }
429 size_t num_entries = session_histories_[pos].size();
430 *currentEntryIndex = current_entry_indexes_[pos];
431 WebVector<WebHistoryItem> result(num_entries);
432 for (size_t entry = 0; entry < num_entries; ++entry) {
433 result[entry] =
434 webkit_glue::HistoryItemFromString(session_histories_[pos][entry]);
435 }
436 history->swap(result);
[email protected]29e82702013-02-14 12:56:40437}
438
[email protected]b2324b092012-11-01 10:34:11439// RenderViewObserver --------------------------------------------------------
440
[email protected]9b4c6cd2012-08-20 10:47:45441void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11442 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56443}
444
[email protected]9b4c6cd2012-08-20 10:47:45445bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33446 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45447 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37448 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
449 OnSetTestConfiguration)
[email protected]8f722692013-02-23 14:14:06450 IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory)
[email protected]efb5f572012-01-29 10:57:33451 IPC_MESSAGE_UNHANDLED(handled = false)
452 IPC_END_MESSAGE_MAP()
453
454 return handled;
455}
456
[email protected]b9fad24c2012-11-15 08:22:10457// Public methods - -----------------------------------------------------------
458
[email protected]324825d2012-11-30 12:37:15459void WebKitTestRunner::Reset() {
[email protected]ab683b22013-03-04 09:10:31460 // The proxy_ is always non-NULL, it is set right after construction.
461 proxy_->reset();
[email protected]b3dbcb5e2012-12-17 00:16:37462 prefs_.reset();
[email protected]0ac1a032013-02-12 12:53:37463 enable_pixel_dumping_ = true;
[email protected]ab683b22013-03-04 09:10:31464 layout_test_timeout_ = kDefaultLayoutTestTimeoutMs;
[email protected]0ac1a032013-02-12 12:53:37465 allow_external_pages_ = false;
466 expected_pixel_hash_ = std::string();
[email protected]8f722692013-02-23 14:14:06467 routing_ids_.clear();
468 session_histories_.clear();
469 current_entry_indexes_.clear();
[email protected]324825d2012-11-30 12:37:15470}
471
[email protected]b2324b092012-11-01 10:34:11472// Private methods -----------------------------------------------------------
473
[email protected]0ac1a032013-02-12 12:53:37474void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19475 WebTestInterfaces* interfaces =
476 ShellRenderProcessObserver::GetInstance()->test_interfaces();
477
[email protected]893fc6362013-02-18 13:24:05478 if (interfaces->testRunner()->shouldDumpAsAudio()) {
479 const WebArrayBufferView* audio_data =
480 interfaces->testRunner()->audioData();
481 std::vector<unsigned char> vector_data(
482 static_cast<const unsigned char*>(audio_data->baseAddress()),
483 static_cast<const unsigned char*>(audio_data->baseAddress()) +
484 audio_data->byteLength());
485 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
486 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
487 return;
488 }
489
[email protected]5f20df362013-02-15 19:48:19490 Send(
491 new ShellViewHostMsg_TextDump(routing_id(), proxy()->captureTree(false)));
492
[email protected]16000762013-03-04 12:43:38493 if (enable_pixel_dumping_ &&
494 interfaces->testRunner()->shouldGeneratePixelResults()) {
[email protected]5f20df362013-02-15 19:48:19495 SkBitmap snapshot;
496 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
497
498 SkAutoLockPixels snapshot_lock(snapshot);
499 base::MD5Digest digest;
500#if defined(OS_ANDROID)
501 // On Android, pixel layout is RGBA, however, other Chrome platforms use
502 // BGRA.
503 const uint8_t* raw_pixels =
504 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
505 size_t snapshot_size = snapshot.getSize();
506 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
507 for (size_t i = 0; i < snapshot_size; i += 4) {
508 reordered_pixels[i] = raw_pixels[i + 2];
509 reordered_pixels[i + 1] = raw_pixels[i + 1];
510 reordered_pixels[i + 2] = raw_pixels[i];
511 reordered_pixels[i + 3] = raw_pixels[i + 3];
512 }
513 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
514#else
515 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
516#endif
517 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
518
519 if (actual_pixel_hash == expected_pixel_hash_) {
520 SkBitmap empty_image;
521 Send(new ShellViewHostMsg_ImageDump(
522 routing_id(), actual_pixel_hash, empty_image));
523 } else {
524 Send(new ShellViewHostMsg_ImageDump(
525 routing_id(), actual_pixel_hash, snapshot));
526 }
[email protected]0ac1a032013-02-12 12:53:37527 }
[email protected]5f20df362013-02-15 19:48:19528
[email protected]0ac1a032013-02-12 12:53:37529 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
530}
531
[email protected]0ac1a032013-02-12 12:53:37532void WebKitTestRunner::OnSetTestConfiguration(
[email protected]26a51632013-02-18 09:48:56533 const ShellViewMsg_SetTestConfiguration_Params& params) {
534 current_working_directory_ = params.current_working_directory;
[email protected]0c5f2222013-02-20 11:26:19535 temp_path_ = params.temp_path;
[email protected]26a51632013-02-18 09:48:56536 enable_pixel_dumping_ = params.enable_pixel_dumping;
537 layout_test_timeout_ = params.layout_test_timeout;
538 allow_external_pages_ = params.allow_external_pages;
539 expected_pixel_hash_ = params.expected_pixel_hash;
[email protected]5f20df362013-02-15 19:48:19540 is_main_window_ = true;
541
542 WebTestInterfaces* interfaces =
543 ShellRenderProcessObserver::GetInstance()->test_interfaces();
544 interfaces->setTestIsRunning(true);
[email protected]26a51632013-02-18 09:48:56545 interfaces->configureForTestWithURL(params.test_url, enable_pixel_dumping_);
[email protected]0d2dfb92d2012-11-05 10:26:45546}
547
[email protected]8f722692013-02-23 14:14:06548void WebKitTestRunner::OnSessionHistory(
549 const std::vector<int>& routing_ids,
550 const std::vector<std::vector<std::string> >& session_histories,
551 const std::vector<unsigned>& current_entry_indexes) {
552 routing_ids_ = routing_ids;
553 session_histories_ = session_histories;
554 current_entry_indexes_ = current_entry_indexes;
555 CaptureDump();
556}
557
[email protected]31d71b02012-01-26 03:42:31558} // namespace content