blob: f759baa9d9cf9150aa64f9c1e9ca3a8d0592adf7 [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]b2324b092012-11-01 10:34:1145#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]efb5f572012-01-29 10:57:3346#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b2324b092012-11-01 10:34:1147#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]5f20df362013-02-15 19:48:1948#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestInterfaces.h"
[email protected]b9fad24c2012-11-15 08:22:1049#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]5f20df362013-02-15 19:48:1950#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestRunner.h"
[email protected]c24b40222013-02-20 11:45:4251#include "ui/gfx/rect.h"
[email protected]984a5f72012-12-13 21:41:5052#include "webkit/base/file_path_string_conversions.h"
[email protected]0799da02012-06-27 10:58:5153#include "webkit/glue/webkit_glue.h"
[email protected]324825d2012-11-30 12:37:1554#include "webkit/glue/webpreferences.h"
[email protected]efb5f572012-01-29 10:57:3355
[email protected]2a58eb12012-12-18 07:56:4056using WebKit::Platform;
[email protected]893fc6362013-02-18 13:24:0557using WebKit::WebArrayBufferView;
[email protected]b2324b092012-11-01 10:34:1158using WebKit::WebContextMenuData;
[email protected]001841c92012-12-11 17:00:1359using WebKit::WebDevToolsAgent;
[email protected]29e82702013-02-14 12:56:4060using WebKit::WebDeviceOrientation;
[email protected]efb5f572012-01-29 10:57:3361using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1162using WebKit::WebFrame;
63using WebKit::WebGamepads;
[email protected]0799da02012-06-27 10:58:5164using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0665using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1166using WebKit::WebString;
[email protected]09ec9f92012-12-17 09:46:3667using WebKit::WebURL;
[email protected]9d9ec982013-01-10 18:53:3968using WebKit::WebURLError;
[email protected]b2324b092012-11-01 10:34:1169using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5170using WebKit::WebView;
[email protected]b3dbcb5e2012-12-17 00:16:3771using WebTestRunner::WebPreferences;
[email protected]b2324b092012-11-01 10:34:1172using WebTestRunner::WebTask;
[email protected]5f20df362013-02-15 19:48:1973using WebTestRunner::WebTestInterfaces;
[email protected]efb5f572012-01-29 10:57:3374
[email protected]31d71b02012-01-26 03:42:3175namespace content {
76
[email protected]efb5f572012-01-29 10:57:3377namespace {
78
[email protected]b2324b092012-11-01 10:34:1179void InvokeTaskHelper(void* context) {
80 WebTask* task = reinterpret_cast<WebTask*>(context);
81 task->run();
82 delete task;
83}
84
[email protected]0799da02012-06-27 10:58:5185#if !defined(OS_MACOSX)
86void MakeBitmapOpaque(SkBitmap* bitmap) {
87 SkAutoLockPixels lock(*bitmap);
88 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
89 for (int y = 0; y < bitmap->height(); ++y) {
90 uint32_t* row = bitmap->getAddr32(0, y);
91 for (int x = 0; x < bitmap->width(); ++x)
92 row[x] |= 0xFF000000; // Set alpha bits to 1.
93 }
94}
95#endif
96
[email protected]b9fad24c2012-11-15 08:22:1097void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:5698 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:5199 const SkBitmap& bitmap = device->accessBitmap(false);
100 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
101
102#if !defined(OS_MACOSX)
103 // Only the expected PNGs for Mac have a valid alpha channel.
104 MakeBitmapOpaque(snapshot);
105#endif
106
107}
108
[email protected]efb5f572012-01-29 10:57:33109} // namespace
[email protected]cccb5cf2012-06-06 22:20:04110
[email protected]29e82702013-02-14 12:56:40111// static
112int WebKitTestRunner::window_count_ = 0;
113
[email protected]9b4c6cd2012-08-20 10:47:45114WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]5f20df362013-02-15 19:48:19115 : RenderViewObserver(render_view),
[email protected]2025a2f42013-02-18 07:25:41116 RenderViewObserverTracker<WebKitTestRunner>(render_view),
[email protected]5f20df362013-02-15 19:48:19117 is_main_window_(false) {
[email protected]29e82702013-02-14 12:56:40118 ++window_count_;
[email protected]31d71b02012-01-26 03:42:31119}
120
[email protected]9b4c6cd2012-08-20 10:47:45121WebKitTestRunner::~WebKitTestRunner() {
[email protected]29e82702013-02-14 12:56:40122 --window_count_;
[email protected]31d71b02012-01-26 03:42:31123}
124
[email protected]b2324b092012-11-01 10:34:11125// WebTestDelegate -----------------------------------------------------------
126
[email protected]b2324b092012-11-01 10:34:11127void WebKitTestRunner::clearEditCommand() {
128 render_view()->ClearEditCommands();
129}
130
131void WebKitTestRunner::setEditCommand(const std::string& name,
132 const std::string& value) {
133 render_view()->SetEditCommandForNextKeyEvent(name, value);
134}
135
[email protected]b2324b092012-11-01 10:34:11136void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25137 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11138}
139
140void WebKitTestRunner::printMessage(const std::string& message) {
141 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
142}
143
144void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40145 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11146}
147
148void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
149 MessageLoop::current()->PostDelayedTask(
150 FROM_HERE,
151 base::Bind(&WebTask::run, base::Owned(task)),
152 base::TimeDelta::FromMilliseconds(ms));
153}
154
155WebString WebKitTestRunner::registerIsolatedFileSystem(
156 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]d30a36f2013-02-07 04:16:26157 std::vector<base::FilePath> files;
[email protected]984a5f72012-12-13 21:41:50158 for (size_t i = 0; i < absolute_filenames.size(); ++i)
159 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
160 std::string filesystem_id;
161 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
162 routing_id(), files, &filesystem_id));
163 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11164}
165
166long long WebKitTestRunner::getCurrentTimeInMillisecond() {
167 return base::TimeTicks::Now().ToInternalValue() /
168 base::Time::kMicrosecondsPerMillisecond;
169}
170
171WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
172 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45173#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26174 base::FilePath path(UTF8ToWide(utf8_path));
[email protected]0d2dfb92d2012-11-05 10:26:45175#else
[email protected]d30a36f2013-02-07 04:16:26176 base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
[email protected]0d2dfb92d2012-11-05 10:26:45177#endif
178 if (!path.IsAbsolute()) {
179 GURL base_url =
180 net::FilePathToFileURL(current_working_directory_.Append(
181 FILE_PATH_LITERAL("foo")));
182 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
183 }
[email protected]984a5f72012-12-13 21:41:50184 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11185}
186
[email protected]09ec9f92012-12-17 09:46:36187WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
[email protected]d30a36f2013-02-07 04:16:26188 base::FilePath local_path;
[email protected]09ec9f92012-12-17 09:46:36189 if (!net::FileURLToFilePath(file_url, &local_path))
190 return WebURL();
191
192 std::string contents;
193 Send(new ShellViewHostMsg_ReadFileToString(
194 routing_id(), local_path, &contents));
195
196 std::string contents_base64;
197 if (!base::Base64Encode(contents, &contents_base64))
198 return WebURL();
199
200 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
201 return WebURL(GURL(data_url_prefix + contents_base64));
202}
203
204WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
205 const char kPrefix[] = "file:///tmp/LayoutTests/";
206 const int kPrefixLen = arraysize(kPrefix) - 1;
207
208 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
209 return WebURL(GURL(utf8_url));
210
[email protected]d30a36f2013-02-07 04:16:26211 base::FilePath replace_path =
[email protected]09ec9f92012-12-17 09:46:36212 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
213 FILE_PATH_LITERAL("LayoutTests/"));
214#if defined(OS_WIN)
215 std::string utf8_path = WideToUTF8(replace_path.value());
216#else
217 std::string utf8_path =
218 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
219#endif
220 std::string new_url =
221 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
222 return WebURL(GURL(new_url));
223}
224
[email protected]b3dbcb5e2012-12-17 00:16:37225WebPreferences* WebKitTestRunner::preferences() {
226 return &prefs_;
227}
228
229void WebKitTestRunner::applyPreferences() {
230 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47231 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37232 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25233 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37234}
235
[email protected]9d9ec982013-01-10 18:53:39236std::string WebKitTestRunner::makeURLErrorDescription(
237 const WebURLError& error) {
238 std::string domain = error.domain.utf8();
239 int code = error.reason;
240
241 if (domain == net::kErrorDomain) {
242 domain = "NSURLErrorDomain";
243 switch (error.reason) {
244 case net::ERR_ABORTED:
245 code = -999; // NSURLErrorCancelled
246 break;
247 case net::ERR_UNSAFE_PORT:
248 // Our unsafe port checking happens at the network stack level, but we
249 // make this translation here to match the behavior of stock WebKit.
250 domain = "WebKitErrorDomain";
251 code = 103;
252 break;
253 case net::ERR_ADDRESS_INVALID:
254 case net::ERR_ADDRESS_UNREACHABLE:
255 case net::ERR_NETWORK_ACCESS_DENIED:
256 code = -1004; // NSURLErrorCannotConnectToHost
257 break;
258 }
259 } else {
260 DLOG(WARNING) << "Unknown error domain";
261 }
262
263 return base::StringPrintf("<NSError domain %s, code %d, failing URL \"%s\">",
264 domain.c_str(), code, error.unreachableURL.spec().data());
265}
266
[email protected]29e82702013-02-14 12:56:40267void WebKitTestRunner::setClientWindowRect(const WebRect& rect) {
[email protected]c24b40222013-02-20 11:45:42268 Send(new ShellViewHostMsg_SetClientWindowRect(
269 routing_id(), gfx::Rect(rect)));
[email protected]29e82702013-02-14 12:56:40270}
271
272void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06273 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40274}
275
276void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06277 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40278}
279
280void WebKitTestRunner::evaluateInWebInspector(long call_id,
281 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06282 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
283 if (agent)
284 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40285}
286
287void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00288 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40289}
290
291void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00292 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40293}
294
295void WebKitTestRunner::setDeviceScaleFactor(float factor) {
296 Send(new ShellViewHostMsg_NotImplemented(
297 routing_id(), "WebKitTestRunner", "setDeviceScaleFactor"));
298}
299
300void WebKitTestRunner::setFocus(bool focus) {
[email protected]c24b40222013-02-20 11:45:42301 Send(new ShellViewHostMsg_SetFocus(routing_id(), focus));
[email protected]29e82702013-02-14 12:56:40302}
303
304void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00305 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40306}
307
308std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19309#if defined(OS_WIN)
310 if (resource.find("/tmp/") == 0) {
311 // We want a temp file.
312 GURL base_url = net::FilePathToFileURL(temp_path_);
313 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
314 }
315#endif
316
317 // Some layout tests use file://// which we resolve as a UNC path. Normalize
318 // them to just file:///.
319 std::string result = resource;
320 while (StringToLowerASCII(result).find("file:////") == 0) {
321 result = result.substr(0, strlen("file:///")) +
322 result.substr(strlen("file:////"));
323 }
324 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40325}
326
327void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00328 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40329}
330
331void WebKitTestRunner::setDeviceOrientation(WebDeviceOrientation& orientation) {
332 Send(new ShellViewHostMsg_NotImplemented(
333 routing_id(), "WebKitTestRunner", "setDeviceOrientation"));
334}
335
336void WebKitTestRunner::didAcquirePointerLock() {
337 Send(new ShellViewHostMsg_NotImplemented(
338 routing_id(), "WebKitTestRunner", "didAcquirePointerLock"));
339}
340
341void WebKitTestRunner::didNotAcquirePointerLock() {
342 Send(new ShellViewHostMsg_NotImplemented(
343 routing_id(), "WebKitTestRunner", "didNotAcquirePointerLock"));
344}
345
346void WebKitTestRunner::didLosePointerLock() {
347 Send(new ShellViewHostMsg_NotImplemented(
348 routing_id(), "WebKitTestRunner", "didLosePointerLock"));
349}
350
351void WebKitTestRunner::setPointerLockWillRespondAsynchronously() {
352 Send(new ShellViewHostMsg_NotImplemented(
353 routing_id(),
354 "WebKitTestRunner",
355 "setPointerLockWillRespondAsynchronously"));
356}
357
358void WebKitTestRunner::setPointerLockWillFailSynchronously() {
359 Send(new ShellViewHostMsg_NotImplemented(
360 routing_id(), "WebKitTestRunner", "setPointerLockWillFailSynchronously"));
361}
362
363int WebKitTestRunner::numberOfPendingGeolocationPermissionRequests() {
364 Send(new ShellViewHostMsg_NotImplemented(
365 routing_id(),
366 "WebKitTestRunner",
367 "numberOfPendingGeolocationPermissionRequests"));
368 return 0;
369}
370
371void WebKitTestRunner::setGeolocationPermission(bool allowed) {
372 Send(new ShellViewHostMsg_NotImplemented(
373 routing_id(), "WebKitTestRunner", "setGeolocationPermission"));
374}
375
376void WebKitTestRunner::setMockGeolocationPosition(double latitude,
377 double longitude,
378 double precision) {
379 Send(new ShellViewHostMsg_NotImplemented(
380 routing_id(), "WebKitTestRunner", "setMockGeolocationPosition"));
381}
382
383void WebKitTestRunner::setMockGeolocationPositionUnavailableError(
384 const std::string& message) {
385 Send(new ShellViewHostMsg_NotImplemented(
386 routing_id(),
387 "WebKitTestRunner",
388 "setMockGeolocationPositionUnavailableError"));
389}
390
391void WebKitTestRunner::addMockSpeechInputResult(const std::string& result,
392 double confidence,
393 const std::string& language) {
394 Send(new ShellViewHostMsg_NotImplemented(
395 routing_id(), "WebKitTestRunner", "addMockSpeechInputResult"));
396}
397
398void WebKitTestRunner::setMockSpeechInputDumpRect(bool dump_rect) {
399 Send(new ShellViewHostMsg_NotImplemented(
400 routing_id(), "WebKitTestRunner", "setMockSpeechInputDumpRect"));
401}
402
403void WebKitTestRunner::addMockSpeechRecognitionResult(
404 const std::string& transcript,
405 double confidence) {
406 Send(new ShellViewHostMsg_NotImplemented(
407 routing_id(), "WebKitTestRunner", "addMockSpeechRecognitionResult"));
408}
409
410void WebKitTestRunner::setMockSpeechRecognitionError(
411 const std::string& error,
412 const std::string& message) {
413 Send(new ShellViewHostMsg_NotImplemented(
414 routing_id(), "WebKitTestRunner", "setMockSpeechRecognitionError"));
415}
416
417bool WebKitTestRunner::wasMockSpeechRecognitionAborted() {
418 Send(new ShellViewHostMsg_NotImplemented(
419 routing_id(), "WebKitTestRunner", "wasMockSpeechRecognitionAborted"));
420 return false;
421}
422
423void WebKitTestRunner::testFinished() {
[email protected]5f20df362013-02-15 19:48:19424 if (!is_main_window_)
425 return;
426 WebTestInterfaces* interfaces =
427 ShellRenderProcessObserver::GetInstance()->test_interfaces();
428 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40429 CaptureDump();
430}
431
432void WebKitTestRunner::testTimedOut() {
[email protected]5f20df362013-02-15 19:48:19433 if (!is_main_window_)
434 return;
435 WebTestInterfaces* interfaces =
436 ShellRenderProcessObserver::GetInstance()->test_interfaces();
437 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40438 Send(new ShellViewHostMsg_TestFinished(routing_id(), true));
439}
440
441bool WebKitTestRunner::isBeingDebugged() {
442 return base::debug::BeingDebugged();
443}
444
445int WebKitTestRunner::layoutTestTimeout() {
446 return layout_test_timeout_;
447}
448
449void WebKitTestRunner::closeRemainingWindows() {
[email protected]b17e3f92013-02-20 14:05:35450 // We currently always close all remaining windows at the end of each test.
451 // TODO(jochen): Reuse the renderer across tests instead of closing all
452 // windows. http://crbug.com/171128
[email protected]29e82702013-02-14 12:56:40453}
454
455int WebKitTestRunner::navigationEntryCount() {
[email protected]b17e3f92013-02-20 14:05:35456 // TODO(jochen): implement this.
[email protected]29e82702013-02-14 12:56:40457 return 0;
458}
459
460int WebKitTestRunner::windowCount() {
461 return window_count_;
462}
463
464void WebKitTestRunner::goToOffset(int offset) {
[email protected]d2494ff2013-02-20 08:22:37465 Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset));
[email protected]29e82702013-02-14 12:56:40466}
467
468void WebKitTestRunner::reload() {
[email protected]d2494ff2013-02-20 08:22:37469 Send(new ShellViewHostMsg_Reload(routing_id()));
[email protected]29e82702013-02-14 12:56:40470}
471
472void WebKitTestRunner::loadURLForFrame(const WebURL& url,
473 const std::string& frame_name) {
[email protected]d2494ff2013-02-20 08:22:37474 Send(new ShellViewHostMsg_LoadURLForFrame(
475 routing_id(), url, frame_name));
[email protected]29e82702013-02-14 12:56:40476}
477
478bool WebKitTestRunner::allowExternalPages() {
479 return allow_external_pages_;
480}
481
482void WebKitTestRunner::captureHistoryForWindow(
483 size_t windowIndex,
484 WebVector<WebKit::WebHistoryItem>* history,
485 size_t* currentEntryIndex) {
486 Send(new ShellViewHostMsg_NotImplemented(
487 routing_id(), "WebKitTestRunner", "captureHistoryForWindow"));
488}
489
[email protected]b2324b092012-11-01 10:34:11490// RenderViewObserver --------------------------------------------------------
491
[email protected]9b4c6cd2012-08-20 10:47:45492void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11493 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56494}
495
[email protected]9b4c6cd2012-08-20 10:47:45496bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33497 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45498 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37499 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
500 OnSetTestConfiguration)
[email protected]efb5f572012-01-29 10:57:33501 IPC_MESSAGE_UNHANDLED(handled = false)
502 IPC_END_MESSAGE_MAP()
503
504 return handled;
505}
506
[email protected]b9fad24c2012-11-15 08:22:10507// Public methods - -----------------------------------------------------------
508
[email protected]324825d2012-11-30 12:37:15509void WebKitTestRunner::Reset() {
[email protected]b3dbcb5e2012-12-17 00:16:37510 prefs_.reset();
[email protected]324825d2012-11-30 12:37:15511 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47512 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]324825d2012-11-30 12:37:15513 render_view()->SetWebkitPreferences(prefs);
[email protected]0ac1a032013-02-12 12:53:37514 enable_pixel_dumping_ = true;
515 layout_test_timeout_ = 30 * 1000;
516 allow_external_pages_ = false;
517 expected_pixel_hash_ = std::string();
[email protected]324825d2012-11-30 12:37:15518}
519
[email protected]b2324b092012-11-01 10:34:11520// Private methods -----------------------------------------------------------
521
[email protected]0ac1a032013-02-12 12:53:37522void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19523 WebTestInterfaces* interfaces =
524 ShellRenderProcessObserver::GetInstance()->test_interfaces();
525
[email protected]893fc6362013-02-18 13:24:05526 if (interfaces->testRunner()->shouldDumpAsAudio()) {
527 const WebArrayBufferView* audio_data =
528 interfaces->testRunner()->audioData();
529 std::vector<unsigned char> vector_data(
530 static_cast<const unsigned char*>(audio_data->baseAddress()),
531 static_cast<const unsigned char*>(audio_data->baseAddress()) +
532 audio_data->byteLength());
533 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
534 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
535 return;
536 }
537
[email protected]5f20df362013-02-15 19:48:19538 Send(
539 new ShellViewHostMsg_TextDump(routing_id(), proxy()->captureTree(false)));
540
541 if (interfaces->testRunner()->shouldGeneratePixelResults()) {
542 SkBitmap snapshot;
543 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
544
545 SkAutoLockPixels snapshot_lock(snapshot);
546 base::MD5Digest digest;
547#if defined(OS_ANDROID)
548 // On Android, pixel layout is RGBA, however, other Chrome platforms use
549 // BGRA.
550 const uint8_t* raw_pixels =
551 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
552 size_t snapshot_size = snapshot.getSize();
553 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
554 for (size_t i = 0; i < snapshot_size; i += 4) {
555 reordered_pixels[i] = raw_pixels[i + 2];
556 reordered_pixels[i + 1] = raw_pixels[i + 1];
557 reordered_pixels[i + 2] = raw_pixels[i];
558 reordered_pixels[i + 3] = raw_pixels[i + 3];
559 }
560 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
561#else
562 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
563#endif
564 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
565
566 if (actual_pixel_hash == expected_pixel_hash_) {
567 SkBitmap empty_image;
568 Send(new ShellViewHostMsg_ImageDump(
569 routing_id(), actual_pixel_hash, empty_image));
570 } else {
571 Send(new ShellViewHostMsg_ImageDump(
572 routing_id(), actual_pixel_hash, snapshot));
573 }
[email protected]0ac1a032013-02-12 12:53:37574 }
[email protected]5f20df362013-02-15 19:48:19575
[email protected]0ac1a032013-02-12 12:53:37576 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
577}
578
[email protected]0ac1a032013-02-12 12:53:37579void WebKitTestRunner::OnSetTestConfiguration(
[email protected]26a51632013-02-18 09:48:56580 const ShellViewMsg_SetTestConfiguration_Params& params) {
581 current_working_directory_ = params.current_working_directory;
[email protected]0c5f2222013-02-20 11:26:19582 temp_path_ = params.temp_path;
[email protected]26a51632013-02-18 09:48:56583 enable_pixel_dumping_ = params.enable_pixel_dumping;
584 layout_test_timeout_ = params.layout_test_timeout;
585 allow_external_pages_ = params.allow_external_pages;
586 expected_pixel_hash_ = params.expected_pixel_hash;
[email protected]5f20df362013-02-15 19:48:19587 is_main_window_ = true;
588
589 WebTestInterfaces* interfaces =
590 ShellRenderProcessObserver::GetInstance()->test_interfaces();
591 interfaces->setTestIsRunning(true);
[email protected]26a51632013-02-18 09:48:56592 interfaces->configureForTestWithURL(params.test_url, enable_pixel_dumping_);
[email protected]0d2dfb92d2012-11-05 10:26:45593}
594
[email protected]31d71b02012-01-26 03:42:31595} // namespace content