[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 5 | #include "content/shell/webkit_test_runner.h" |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 6 | |
[email protected] | 0c5f222 | 2013-02-20 11:26:19 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 833b700d | 2013-02-18 19:43:00 | [diff] [blame] | 8 | #include <clocale> |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 9 | #include <cmath> |
| 10 | |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 11 | #include "base/base64.h" |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 12 | #include "base/debug/debugger.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 13 | #include "base/md5.h" |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 15 | #include "base/message_loop.h" |
[email protected] | 0c5f222 | 2013-02-20 11:26:19 | [diff] [blame] | 16 | #include "base/string_util.h" |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 17 | #include "base/stringprintf.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 18 | #include "base/sys_string_conversions.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 19 | #include "base/time.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 20 | #include "base/utf_string_conversions.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 21 | #include "content/public/renderer/render_view.h" |
[email protected] | e519692 | 2012-12-11 22:18:25 | [diff] [blame] | 22 | #include "content/public/test/layouttest_support.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 23 | #include "content/shell/shell_messages.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 24 | #include "content/shell/shell_render_process_observer.h" |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 25 | #include "content/shell/webkit_test_helpers.h" |
[email protected] | 9d9ec98 | 2013-01-10 18:53:39 | [diff] [blame] | 26 | #include "net/base/net_errors.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 27 | #include "net/base/net_util.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 28 | #include "skia/ext/platform_canvas.h" |
[email protected] | 2a58eb1 | 2012-12-18 07:56:40 | [diff] [blame] | 29 | #include "third_party/WebKit/Source/Platform/chromium/public/Platform.h" |
[email protected] | 87884c4 | 2013-01-10 01:56:27 | [diff] [blame] | 30 | #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] | 9d9ec98 | 2013-01-10 18:53:39 | [diff] [blame] | 35 | #include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h" |
[email protected] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 36 | #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h" |
[email protected] | 893fc636 | 2013-02-18 13:24:05 | [diff] [blame] | 37 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBufferView.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 38 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" |
[email protected] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 39 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 40 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h" |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 41 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientation.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 42 | #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] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 45 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 46 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 47 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 48 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h" |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 49 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestInterfaces.h" |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 50 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h" |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 51 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestRunner.h" |
[email protected] | c24b4022 | 2013-02-20 11:45:42 | [diff] [blame] | 52 | #include "ui/gfx/rect.h" |
[email protected] | 984a5f7 | 2012-12-13 21:41:50 | [diff] [blame] | 53 | #include "webkit/base/file_path_string_conversions.h" |
[email protected] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 54 | #include "webkit/glue/glue_serialize.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 55 | #include "webkit/glue/webkit_glue.h" |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 56 | #include "webkit/glue/webpreferences.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 57 | |
[email protected] | 2a58eb1 | 2012-12-18 07:56:40 | [diff] [blame] | 58 | using WebKit::Platform; |
[email protected] | 893fc636 | 2013-02-18 13:24:05 | [diff] [blame] | 59 | using WebKit::WebArrayBufferView; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 60 | using WebKit::WebContextMenuData; |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 61 | using WebKit::WebDevToolsAgent; |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 62 | using WebKit::WebDeviceOrientation; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 63 | using WebKit::WebElement; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 64 | using WebKit::WebFrame; |
| 65 | using WebKit::WebGamepads; |
[email protected] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 66 | using WebKit::WebHistoryItem; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 67 | using WebKit::WebRect; |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 68 | using WebKit::WebSize; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 69 | using WebKit::WebString; |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 70 | using WebKit::WebURL; |
[email protected] | 9d9ec98 | 2013-01-10 18:53:39 | [diff] [blame] | 71 | using WebKit::WebURLError; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 72 | using WebKit::WebVector; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 73 | using WebKit::WebView; |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 74 | using WebTestRunner::WebPreferences; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 75 | using WebTestRunner::WebTask; |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 76 | using WebTestRunner::WebTestInterfaces; |
[email protected] | 9063bf6 | 2013-02-20 19:42:01 | [diff] [blame] | 77 | using WebTestRunner::WebTestProxyBase; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 78 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 79 | namespace content { |
| 80 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 81 | namespace { |
| 82 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 83 | void InvokeTaskHelper(void* context) { |
| 84 | WebTask* task = reinterpret_cast<WebTask*>(context); |
| 85 | task->run(); |
| 86 | delete task; |
| 87 | } |
| 88 | |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 89 | #if !defined(OS_MACOSX) |
| 90 | void 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] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 101 | void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) { |
[email protected] | 9d611ca | 2012-11-14 13:46:56 | [diff] [blame] | 102 | SkDevice* device = skia::GetTopDevice(*canvas); |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 103 | 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] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 113 | } // namespace |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 114 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 115 | WebKitTestRunner::WebKitTestRunner(RenderView* render_view) |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 116 | : RenderViewObserver(render_view), |
[email protected] | 2025a2f4 | 2013-02-18 07:25:41 | [diff] [blame] | 117 | RenderViewObserverTracker<WebKitTestRunner>(render_view), |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 118 | is_main_window_(false) { |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 121 | WebKitTestRunner::~WebKitTestRunner() { |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 124 | // WebTestDelegate ----------------------------------------------------------- |
| 125 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 126 | void WebKitTestRunner::clearEditCommand() { |
| 127 | render_view()->ClearEditCommands(); |
| 128 | } |
| 129 | |
| 130 | void WebKitTestRunner::setEditCommand(const std::string& name, |
| 131 | const std::string& value) { |
| 132 | render_view()->SetEditCommandForNextKeyEvent(name, value); |
| 133 | } |
| 134 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 135 | void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) { |
[email protected] | e519692 | 2012-12-11 22:18:25 | [diff] [blame] | 136 | SetMockGamepads(gamepads); |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void WebKitTestRunner::printMessage(const std::string& message) { |
| 140 | Send(new ShellViewHostMsg_PrintMessage(routing_id(), message)); |
| 141 | } |
| 142 | |
| 143 | void WebKitTestRunner::postTask(WebTask* task) { |
[email protected] | 2a58eb1 | 2012-12-18 07:56:40 | [diff] [blame] | 144 | Platform::current()->callOnMainThread(InvokeTaskHelper, task); |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void 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 | |
| 154 | WebString WebKitTestRunner::registerIsolatedFileSystem( |
| 155 | const WebKit::WebVector<WebKit::WebString>& absolute_filenames) { |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 156 | std::vector<base::FilePath> files; |
[email protected] | 984a5f7 | 2012-12-13 21:41:50 | [diff] [blame] | 157 | 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] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | long long WebKitTestRunner::getCurrentTimeInMillisecond() { |
| 166 | return base::TimeTicks::Now().ToInternalValue() / |
| 167 | base::Time::kMicrosecondsPerMillisecond; |
| 168 | } |
| 169 | |
| 170 | WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path( |
| 171 | const std::string& utf8_path) { |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 172 | #if defined(OS_WIN) |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 173 | base::FilePath path(UTF8ToWide(utf8_path)); |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 174 | #else |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 175 | base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path))); |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 176 | #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] | 984a5f7 | 2012-12-13 21:41:50 | [diff] [blame] | 183 | return webkit_base::FilePathToWebString(path); |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 186 | WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) { |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 187 | base::FilePath local_path; |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 188 | 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 | |
| 203 | WebURL 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] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 210 | base::FilePath replace_path = |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 211 | 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] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 224 | WebPreferences* WebKitTestRunner::preferences() { |
| 225 | return &prefs_; |
| 226 | } |
| 227 | |
| 228 | void WebKitTestRunner::applyPreferences() { |
| 229 | webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences(); |
[email protected] | d48eab08 | 2012-12-19 09:25:47 | [diff] [blame] | 230 | ExportLayoutTestSpecificPreferences(prefs_, &prefs); |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 231 | render_view()->SetWebkitPreferences(prefs); |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 232 | Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs)); |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 9d9ec98 | 2013-01-10 18:53:39 | [diff] [blame] | 235 | std::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] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 266 | void WebKitTestRunner::setClientWindowRect(const WebRect& rect) { |
[email protected] | c24b4022 | 2013-02-20 11:45:42 | [diff] [blame] | 267 | Send(new ShellViewHostMsg_SetClientWindowRect( |
| 268 | routing_id(), gfx::Rect(rect))); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void WebKitTestRunner::showDevTools() { |
[email protected] | f85c584 | 2013-02-15 14:15:06 | [diff] [blame] | 272 | Send(new ShellViewHostMsg_ShowDevTools(routing_id())); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void WebKitTestRunner::closeDevTools() { |
[email protected] | f85c584 | 2013-02-15 14:15:06 | [diff] [blame] | 276 | Send(new ShellViewHostMsg_CloseDevTools(routing_id())); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void WebKitTestRunner::evaluateInWebInspector(long call_id, |
| 280 | const std::string& script) { |
[email protected] | f85c584 | 2013-02-15 14:15:06 | [diff] [blame] | 281 | WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent(); |
| 282 | if (agent) |
| 283 | agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script)); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void WebKitTestRunner::clearAllDatabases() { |
[email protected] | 833b700d | 2013-02-18 19:43:00 | [diff] [blame] | 287 | Send(new ShellViewHostMsg_ClearAllDatabases(routing_id())); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void WebKitTestRunner::setDatabaseQuota(int quota) { |
[email protected] | 833b700d | 2013-02-18 19:43:00 | [diff] [blame] | 291 | Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota)); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void WebKitTestRunner::setDeviceScaleFactor(float factor) { |
[email protected] | db6769c | 2013-02-24 14:59:14 | [diff] [blame^] | 295 | render_view()->GetWebView()->setDeviceScaleFactor(factor); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void WebKitTestRunner::setFocus(bool focus) { |
[email protected] | c24b4022 | 2013-02-20 11:45:42 | [diff] [blame] | 299 | Send(new ShellViewHostMsg_SetFocus(routing_id(), focus)); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void WebKitTestRunner::setAcceptAllCookies(bool accept) { |
[email protected] | 833b700d | 2013-02-18 19:43:00 | [diff] [blame] | 303 | Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept)); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) { |
[email protected] | 0c5f222 | 2013-02-20 11:26:19 | [diff] [blame] | 307 | #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] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void WebKitTestRunner::setLocale(const std::string& locale) { |
[email protected] | 833b700d | 2013-02-18 19:43:00 | [diff] [blame] | 326 | setlocale(LC_ALL, locale.c_str()); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 327 | } |
| 328 | |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 329 | void WebKitTestRunner::testFinished() { |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 330 | if (!is_main_window_) |
| 331 | return; |
| 332 | WebTestInterfaces* interfaces = |
| 333 | ShellRenderProcessObserver::GetInstance()->test_interfaces(); |
| 334 | interfaces->setTestIsRunning(false); |
[email protected] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 335 | if (interfaces->testRunner()->shouldDumpBackForwardList()) { |
| 336 | Send(new ShellViewHostMsg_CaptureSessionHistory(routing_id())); |
| 337 | } else { |
| 338 | CaptureDump(); |
| 339 | } |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void WebKitTestRunner::testTimedOut() { |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 343 | if (!is_main_window_) |
| 344 | return; |
| 345 | WebTestInterfaces* interfaces = |
| 346 | ShellRenderProcessObserver::GetInstance()->test_interfaces(); |
| 347 | interfaces->setTestIsRunning(false); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 348 | Send(new ShellViewHostMsg_TestFinished(routing_id(), true)); |
| 349 | } |
| 350 | |
| 351 | bool WebKitTestRunner::isBeingDebugged() { |
| 352 | return base::debug::BeingDebugged(); |
| 353 | } |
| 354 | |
| 355 | int WebKitTestRunner::layoutTestTimeout() { |
| 356 | return layout_test_timeout_; |
| 357 | } |
| 358 | |
| 359 | void WebKitTestRunner::closeRemainingWindows() { |
[email protected] | b17e3f9 | 2013-02-20 14:05:35 | [diff] [blame] | 360 | // 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] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | int WebKitTestRunner::navigationEntryCount() { |
[email protected] | b283d29 | 2013-02-21 08:40:34 | [diff] [blame] | 366 | return GetLocalSessionHistoryLength(render_view()); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 367 | } |
| 368 | |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 369 | void WebKitTestRunner::goToOffset(int offset) { |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 370 | Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset)); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void WebKitTestRunner::reload() { |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 374 | Send(new ShellViewHostMsg_Reload(routing_id())); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void WebKitTestRunner::loadURLForFrame(const WebURL& url, |
| 378 | const std::string& frame_name) { |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 379 | Send(new ShellViewHostMsg_LoadURLForFrame( |
| 380 | routing_id(), url, frame_name)); |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | bool WebKitTestRunner::allowExternalPages() { |
| 384 | return allow_external_pages_; |
| 385 | } |
| 386 | |
| 387 | void WebKitTestRunner::captureHistoryForWindow( |
[email protected] | 9063bf6 | 2013-02-20 19:42:01 | [diff] [blame] | 388 | WebTestProxyBase* proxy, |
[email protected] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 389 | WebVector<WebKit::WebHistoryItem>* history, |
| 390 | size_t* currentEntryIndex) { |
[email protected] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 391 | 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] | 29e8270 | 2013-02-14 12:56:40 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 417 | // RenderViewObserver -------------------------------------------------------- |
| 418 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 419 | void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) { |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 420 | ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame); |
[email protected] | 96b80b47 | 2012-07-03 19:41:56 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 423 | bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 424 | bool handled = true; |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 425 | IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message) |
[email protected] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 426 | IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration, |
| 427 | OnSetTestConfiguration) |
[email protected] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 428 | IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 429 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 430 | IPC_END_MESSAGE_MAP() |
| 431 | |
| 432 | return handled; |
| 433 | } |
| 434 | |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 435 | // Public methods - ----------------------------------------------------------- |
| 436 | |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 437 | void WebKitTestRunner::Reset() { |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 438 | prefs_.reset(); |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 439 | webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences(); |
[email protected] | d48eab08 | 2012-12-19 09:25:47 | [diff] [blame] | 440 | ExportLayoutTestSpecificPreferences(prefs_, &prefs); |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 441 | render_view()->SetWebkitPreferences(prefs); |
[email protected] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 442 | enable_pixel_dumping_ = true; |
| 443 | layout_test_timeout_ = 30 * 1000; |
| 444 | allow_external_pages_ = false; |
| 445 | expected_pixel_hash_ = std::string(); |
[email protected] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 446 | routing_ids_.clear(); |
| 447 | session_histories_.clear(); |
| 448 | current_entry_indexes_.clear(); |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 449 | } |
| 450 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 451 | // Private methods ----------------------------------------------------------- |
| 452 | |
[email protected] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 453 | void WebKitTestRunner::CaptureDump() { |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 454 | WebTestInterfaces* interfaces = |
| 455 | ShellRenderProcessObserver::GetInstance()->test_interfaces(); |
| 456 | |
[email protected] | 893fc636 | 2013-02-18 13:24:05 | [diff] [blame] | 457 | 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] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 469 | 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] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 505 | } |
[email protected] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 506 | |
[email protected] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 507 | Send(new ShellViewHostMsg_TestFinished(routing_id(), false)); |
| 508 | } |
| 509 | |
[email protected] | 0ac1a03 | 2013-02-12 12:53:37 | [diff] [blame] | 510 | void WebKitTestRunner::OnSetTestConfiguration( |
[email protected] | 26a5163 | 2013-02-18 09:48:56 | [diff] [blame] | 511 | const ShellViewMsg_SetTestConfiguration_Params& params) { |
| 512 | current_working_directory_ = params.current_working_directory; |
[email protected] | 0c5f222 | 2013-02-20 11:26:19 | [diff] [blame] | 513 | temp_path_ = params.temp_path; |
[email protected] | 26a5163 | 2013-02-18 09:48:56 | [diff] [blame] | 514 | 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] | 5f20df36 | 2013-02-15 19:48:19 | [diff] [blame] | 518 | is_main_window_ = true; |
| 519 | |
| 520 | WebTestInterfaces* interfaces = |
| 521 | ShellRenderProcessObserver::GetInstance()->test_interfaces(); |
| 522 | interfaces->setTestIsRunning(true); |
[email protected] | 26a5163 | 2013-02-18 09:48:56 | [diff] [blame] | 523 | interfaces->configureForTestWithURL(params.test_url, enable_pixel_dumping_); |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 524 | } |
| 525 | |
[email protected] | 8f72269 | 2013-02-23 14:14:06 | [diff] [blame] | 526 | void 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] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 536 | } // namespace content |