[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] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 7 | #include <cmath> |
| 8 | |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 9 | #include "base/base64.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 10 | #include "base/md5.h" |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 12 | #include "base/message_loop.h" |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 13 | #include "base/stringprintf.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 14 | #include "base/sys_string_conversions.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 15 | #include "base/time.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 16 | #include "base/utf_string_conversions.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 17 | #include "content/public/renderer/render_view.h" |
[email protected] | e519692 | 2012-12-11 22:18:25 | [diff] [blame] | 18 | #include "content/public/test/layouttest_support.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 19 | #include "content/shell/shell_messages.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 20 | #include "content/shell/shell_render_process_observer.h" |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 21 | #include "content/shell/webkit_test_helpers.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 22 | #include "net/base/net_util.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 23 | #include "skia/ext/platform_canvas.h" |
[email protected] | 2a58eb1 | 2012-12-18 07:56:40 | [diff] [blame] | 24 | #include "third_party/WebKit/Source/Platform/chromium/public/Platform.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 25 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 26 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 27 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 28 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 29 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 30 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 31 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 32 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h" |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 33 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
| 34 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
| 35 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 36 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 37 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h" |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 38 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h" |
[email protected] | 984a5f7 | 2012-12-13 21:41:50 | [diff] [blame] | 39 | #include "webkit/base/file_path_string_conversions.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 40 | #include "webkit/glue/webkit_glue.h" |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 41 | #include "webkit/glue/webpreferences.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 42 | |
[email protected] | 2a58eb1 | 2012-12-18 07:56:40 | [diff] [blame] | 43 | using WebKit::Platform; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 44 | using WebKit::WebContextMenuData; |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 45 | using WebKit::WebDevToolsAgent; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 46 | using WebKit::WebElement; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 47 | using WebKit::WebFrame; |
| 48 | using WebKit::WebGamepads; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 49 | using WebKit::WebRect; |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 50 | using WebKit::WebSize; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 51 | using WebKit::WebString; |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 52 | using WebKit::WebURL; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 53 | using WebKit::WebVector; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 54 | using WebKit::WebView; |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 55 | using WebTestRunner::WebPreferences; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 56 | using WebTestRunner::WebTask; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 57 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 58 | namespace content { |
| 59 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 60 | namespace { |
| 61 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 62 | void InvokeTaskHelper(void* context) { |
| 63 | WebTask* task = reinterpret_cast<WebTask*>(context); |
| 64 | task->run(); |
| 65 | delete task; |
| 66 | } |
| 67 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 68 | std::string DumpDocumentText(WebFrame* frame) { |
| 69 | // We use the document element's text instead of the body text here because |
| 70 | // not all documents have a body, such as XML documents. |
| 71 | WebElement documentElement = frame->document().documentElement(); |
| 72 | if (documentElement.isNull()) |
| 73 | return std::string(); |
| 74 | return documentElement.innerText().utf8(); |
| 75 | } |
| 76 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 77 | std::string DumpDocumentPrintedText(WebFrame* frame) { |
| 78 | return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8(); |
| 79 | } |
| 80 | |
| 81 | std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 82 | std::string result; |
| 83 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 84 | // Cannot do printed format for anything other than HTML. |
| 85 | if (printing && !frame->document().isHTMLDocument()) |
| 86 | return std::string(); |
| 87 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 88 | // Add header for all but the main frame. Skip emtpy frames. |
| 89 | if (frame->parent() && !frame->document().documentElement().isNull()) { |
| 90 | result.append("\n--------\nFrame: '"); |
[email protected] | 6d454d9 | 2012-09-13 17:06:29 | [diff] [blame] | 91 | result.append(frame->uniqueName().utf8().data()); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 92 | result.append("'\n--------\n"); |
| 93 | } |
| 94 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 95 | result.append( |
| 96 | printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 97 | result.append("\n"); |
| 98 | |
| 99 | if (recursive) { |
| 100 | for (WebFrame* child = frame->firstChild(); child; |
| 101 | child = child->nextSibling()) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 102 | result.append(DumpFramesAsText(child, printing, recursive)); |
| 103 | } |
| 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) { |
| 109 | std::string result; |
| 110 | |
| 111 | WebSize offset = frame->scrollOffset(); |
| 112 | if (offset.width > 0 || offset.height > 0) { |
| 113 | if (frame->parent()) { |
| 114 | result.append( |
[email protected] | 6d454d9 | 2012-09-13 17:06:29 | [diff] [blame] | 115 | base::StringPrintf("frame '%s' ", frame->uniqueName().utf8().data())); |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 116 | } |
| 117 | result.append( |
| 118 | base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height)); |
| 119 | } |
| 120 | |
| 121 | if (recursive) { |
| 122 | for (WebFrame* child = frame->firstChild(); child; |
| 123 | child = child->nextSibling()) { |
| 124 | result.append(DumpFrameScrollPosition(child, recursive)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | return result; |
| 128 | } |
| 129 | |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 130 | #if !defined(OS_MACOSX) |
| 131 | void MakeBitmapOpaque(SkBitmap* bitmap) { |
| 132 | SkAutoLockPixels lock(*bitmap); |
| 133 | DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config); |
| 134 | for (int y = 0; y < bitmap->height(); ++y) { |
| 135 | uint32_t* row = bitmap->getAddr32(0, y); |
| 136 | for (int x = 0; x < bitmap->width(); ++x) |
| 137 | row[x] |= 0xFF000000; // Set alpha bits to 1. |
| 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 142 | void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) { |
[email protected] | 9d611ca | 2012-11-14 13:46:56 | [diff] [blame] | 143 | SkDevice* device = skia::GetTopDevice(*canvas); |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 144 | const SkBitmap& bitmap = device->accessBitmap(false); |
| 145 | bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config); |
| 146 | |
| 147 | #if !defined(OS_MACOSX) |
| 148 | // Only the expected PNGs for Mac have a valid alpha channel. |
| 149 | MakeBitmapOpaque(snapshot); |
| 150 | #endif |
| 151 | |
| 152 | } |
| 153 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 154 | } // namespace |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 155 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 156 | WebKitTestRunner::WebKitTestRunner(RenderView* render_view) |
[email protected] | 01f0cad0 | 2013-01-09 21:42:50 | [diff] [blame^] | 157 | : RenderViewObserver(render_view) { |
| 158 | Reset(); |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 161 | WebKitTestRunner::~WebKitTestRunner() { |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 164 | // WebTestDelegate ----------------------------------------------------------- |
| 165 | |
| 166 | void WebKitTestRunner::clearContextMenuData() { |
| 167 | last_context_menu_data_.reset(); |
| 168 | } |
| 169 | |
| 170 | WebContextMenuData* WebKitTestRunner::lastContextMenuData() const { |
| 171 | return last_context_menu_data_.get(); |
| 172 | } |
| 173 | |
| 174 | void WebKitTestRunner::clearEditCommand() { |
| 175 | render_view()->ClearEditCommands(); |
| 176 | } |
| 177 | |
| 178 | void WebKitTestRunner::setEditCommand(const std::string& name, |
| 179 | const std::string& value) { |
| 180 | render_view()->SetEditCommandForNextKeyEvent(name, value); |
| 181 | } |
| 182 | |
| 183 | void WebKitTestRunner::fillSpellingSuggestionList( |
| 184 | const WebString& word, WebVector<WebString>* suggestions) { |
| 185 | if (word == WebString::fromUTF8("wellcome")) { |
| 186 | WebVector<WebString> result(suggestions->size() + 1); |
| 187 | for (size_t i = 0; i < suggestions->size(); ++i) |
| 188 | result[i] = (*suggestions)[i]; |
| 189 | result[suggestions->size()] = WebString::fromUTF8("welcome"); |
| 190 | suggestions->swap(result); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) { |
[email protected] | e519692 | 2012-12-11 22:18:25 | [diff] [blame] | 195 | SetMockGamepads(gamepads); |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void WebKitTestRunner::printMessage(const std::string& message) { |
| 199 | Send(new ShellViewHostMsg_PrintMessage(routing_id(), message)); |
| 200 | } |
| 201 | |
| 202 | void WebKitTestRunner::postTask(WebTask* task) { |
[email protected] | 2a58eb1 | 2012-12-18 07:56:40 | [diff] [blame] | 203 | Platform::current()->callOnMainThread(InvokeTaskHelper, task); |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) { |
| 207 | MessageLoop::current()->PostDelayedTask( |
| 208 | FROM_HERE, |
| 209 | base::Bind(&WebTask::run, base::Owned(task)), |
| 210 | base::TimeDelta::FromMilliseconds(ms)); |
| 211 | } |
| 212 | |
| 213 | WebString WebKitTestRunner::registerIsolatedFileSystem( |
| 214 | const WebKit::WebVector<WebKit::WebString>& absolute_filenames) { |
[email protected] | 984a5f7 | 2012-12-13 21:41:50 | [diff] [blame] | 215 | std::vector<FilePath> files; |
| 216 | for (size_t i = 0; i < absolute_filenames.size(); ++i) |
| 217 | files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i])); |
| 218 | std::string filesystem_id; |
| 219 | Send(new ShellViewHostMsg_RegisterIsolatedFileSystem( |
| 220 | routing_id(), files, &filesystem_id)); |
| 221 | return WebString::fromUTF8(filesystem_id); |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | long long WebKitTestRunner::getCurrentTimeInMillisecond() { |
| 225 | return base::TimeTicks::Now().ToInternalValue() / |
| 226 | base::Time::kMicrosecondsPerMillisecond; |
| 227 | } |
| 228 | |
| 229 | WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path( |
| 230 | const std::string& utf8_path) { |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 231 | #if defined(OS_WIN) |
| 232 | FilePath path(UTF8ToWide(utf8_path)); |
| 233 | #else |
| 234 | FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path))); |
| 235 | #endif |
| 236 | if (!path.IsAbsolute()) { |
| 237 | GURL base_url = |
| 238 | net::FilePathToFileURL(current_working_directory_.Append( |
| 239 | FILE_PATH_LITERAL("foo"))); |
| 240 | net::FileURLToFilePath(base_url.Resolve(utf8_path), &path); |
| 241 | } |
[email protected] | 984a5f7 | 2012-12-13 21:41:50 | [diff] [blame] | 242 | return webkit_base::FilePathToWebString(path); |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 09ec9f9 | 2012-12-17 09:46:36 | [diff] [blame] | 245 | WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) { |
| 246 | FilePath local_path; |
| 247 | if (!net::FileURLToFilePath(file_url, &local_path)) |
| 248 | return WebURL(); |
| 249 | |
| 250 | std::string contents; |
| 251 | Send(new ShellViewHostMsg_ReadFileToString( |
| 252 | routing_id(), local_path, &contents)); |
| 253 | |
| 254 | std::string contents_base64; |
| 255 | if (!base::Base64Encode(contents, &contents_base64)) |
| 256 | return WebURL(); |
| 257 | |
| 258 | const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,"; |
| 259 | return WebURL(GURL(data_url_prefix + contents_base64)); |
| 260 | } |
| 261 | |
| 262 | WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) { |
| 263 | const char kPrefix[] = "file:///tmp/LayoutTests/"; |
| 264 | const int kPrefixLen = arraysize(kPrefix) - 1; |
| 265 | |
| 266 | if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen)) |
| 267 | return WebURL(GURL(utf8_url)); |
| 268 | |
| 269 | FilePath replace_path = |
| 270 | ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append( |
| 271 | FILE_PATH_LITERAL("LayoutTests/")); |
| 272 | #if defined(OS_WIN) |
| 273 | std::string utf8_path = WideToUTF8(replace_path.value()); |
| 274 | #else |
| 275 | std::string utf8_path = |
| 276 | WideToUTF8(base::SysNativeMBToWide(replace_path.value())); |
| 277 | #endif |
| 278 | std::string new_url = |
| 279 | std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen); |
| 280 | return WebURL(GURL(new_url)); |
| 281 | } |
| 282 | |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 283 | WebPreferences* WebKitTestRunner::preferences() { |
| 284 | return &prefs_; |
| 285 | } |
| 286 | |
| 287 | void WebKitTestRunner::applyPreferences() { |
| 288 | webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences(); |
[email protected] | d48eab08 | 2012-12-19 09:25:47 | [diff] [blame] | 289 | ExportLayoutTestSpecificPreferences(prefs_, &prefs); |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 290 | render_view()->SetWebkitPreferences(prefs); |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 291 | Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs)); |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | 0b0e3146 | 2012-12-18 21:57:18 | [diff] [blame] | 294 | // WebTestRunner ------------------------------------------------------------- |
| 295 | |
| 296 | bool WebKitTestRunner::shouldDumpEditingCallbacks() const { |
| 297 | return dump_editing_callbacks_; |
| 298 | } |
| 299 | |
[email protected] | 09c48ef5 | 2013-01-09 12:25:07 | [diff] [blame] | 300 | bool WebKitTestRunner::shouldDumpFrameLoadCallbacks() const { |
| 301 | return test_is_running_ && dump_frame_load_callbacks_; |
| 302 | } |
| 303 | |
| 304 | bool WebKitTestRunner::shouldDumpUserGestureInFrameLoadCallbacks() const { |
| 305 | return test_is_running_ && dump_user_gesture_in_frame_load_callbacks_; |
| 306 | } |
| 307 | |
| 308 | bool WebKitTestRunner::stopProvisionalFrameLoads() const { |
| 309 | return stop_provisional_frame_loads_; |
| 310 | } |
| 311 | |
| 312 | bool WebKitTestRunner::shouldDumpTitleChanges() const { |
| 313 | return dump_title_changes_; |
| 314 | } |
| 315 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 316 | // RenderViewObserver -------------------------------------------------------- |
| 317 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 318 | void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) { |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 319 | ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame); |
[email protected] | 96b80b47 | 2012-07-03 19:41:56 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 322 | void WebKitTestRunner::DidFinishLoad(WebFrame* frame) { |
[email protected] | 09c48ef5 | 2013-01-09 12:25:07 | [diff] [blame] | 323 | if (!frame->parent()) { |
| 324 | if (!wait_until_done_) |
| 325 | test_is_running_ = false; |
[email protected] | 01f0cad0 | 2013-01-09 21:42:50 | [diff] [blame^] | 326 | load_finished_ = true; |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 327 | Send(new ShellViewHostMsg_DidFinishLoad(routing_id())); |
[email protected] | 09c48ef5 | 2013-01-09 12:25:07 | [diff] [blame] | 328 | } |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 331 | void WebKitTestRunner::DidRequestShowContextMenu( |
| 332 | WebFrame* frame, |
| 333 | const WebContextMenuData& data) { |
| 334 | last_context_menu_data_.reset(new WebContextMenuData(data)); |
| 335 | } |
| 336 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 337 | bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 338 | bool handled = true; |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 339 | IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 340 | IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 341 | IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump) |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 342 | IPC_MESSAGE_HANDLER(ShellViewMsg_SetCurrentWorkingDirectory, |
| 343 | OnSetCurrentWorkingDirectory) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 344 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 345 | IPC_END_MESSAGE_MAP() |
| 346 | |
| 347 | return handled; |
| 348 | } |
| 349 | |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 350 | // Public methods - ----------------------------------------------------------- |
| 351 | |
| 352 | void WebKitTestRunner::Display() { |
| 353 | const WebSize& size = render_view()->GetWebView()->size(); |
| 354 | WebRect rect(0, 0, size.width, size.height); |
| 355 | proxy_->setPaintRect(rect); |
| 356 | PaintInvalidatedRegion(); |
| 357 | DisplayRepaintMask(); |
| 358 | } |
| 359 | |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 360 | void WebKitTestRunner::SetXSSAuditorEnabled(bool enabled) { |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 361 | prefs_.XSSAuditorEnabled = enabled; |
[email protected] | 55915a7 | 2012-12-18 11:55:25 | [diff] [blame] | 362 | applyPreferences(); |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | 2dd3a27a | 2012-12-10 11:24:16 | [diff] [blame] | 365 | void WebKitTestRunner::NotifyDone() { |
[email protected] | 01f0cad0 | 2013-01-09 21:42:50 | [diff] [blame^] | 366 | if (load_finished_) |
| 367 | test_is_running_ = false; |
| 368 | else |
| 369 | wait_until_done_ = false; |
[email protected] | 2dd3a27a | 2012-12-10 11:24:16 | [diff] [blame] | 370 | Send(new ShellViewHostMsg_NotifyDone(routing_id())); |
| 371 | } |
| 372 | |
| 373 | void WebKitTestRunner::DumpAsText() { |
| 374 | Send(new ShellViewHostMsg_DumpAsText(routing_id())); |
| 375 | } |
| 376 | |
| 377 | void WebKitTestRunner::DumpChildFramesAsText() { |
| 378 | Send(new ShellViewHostMsg_DumpChildFramesAsText(routing_id())); |
| 379 | } |
| 380 | |
| 381 | void WebKitTestRunner::SetPrinting() { |
| 382 | Send(new ShellViewHostMsg_SetPrinting(routing_id())); |
| 383 | } |
| 384 | |
| 385 | void WebKitTestRunner::SetShouldStayOnPageAfterHandlingBeforeUnload( |
| 386 | bool should_stay_on_page) { |
| 387 | Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload( |
| 388 | routing_id(), should_stay_on_page)); |
| 389 | } |
| 390 | |
| 391 | void WebKitTestRunner::WaitUntilDone() { |
[email protected] | 09c48ef5 | 2013-01-09 12:25:07 | [diff] [blame] | 392 | wait_until_done_ = true; |
[email protected] | 2dd3a27a | 2012-12-10 11:24:16 | [diff] [blame] | 393 | Send(new ShellViewHostMsg_WaitUntilDone(routing_id())); |
| 394 | } |
| 395 | |
[email protected] | d99d65a | 2012-12-10 17:40:27 | [diff] [blame] | 396 | void WebKitTestRunner::CanOpenWindows() { |
| 397 | Send(new ShellViewHostMsg_CanOpenWindows(routing_id())); |
| 398 | } |
| 399 | |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 400 | void WebKitTestRunner::ShowWebInspector() { |
| 401 | Send(new ShellViewHostMsg_ShowWebInspector(routing_id())); |
| 402 | } |
| 403 | |
| 404 | void WebKitTestRunner::CloseWebInspector() { |
| 405 | Send(new ShellViewHostMsg_CloseWebInspector(routing_id())); |
| 406 | } |
| 407 | |
| 408 | void WebKitTestRunner::EvaluateInWebInspector(int32_t call_id, |
| 409 | const std::string& script) { |
| 410 | WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent(); |
| 411 | if (agent) |
| 412 | agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script)); |
| 413 | } |
| 414 | |
[email protected] | ab678e3 | 2012-12-14 21:17:08 | [diff] [blame] | 415 | void WebKitTestRunner::ExecCommand(const std::string& command, |
| 416 | const std::string& value) { |
| 417 | render_view()->GetWebView()->focusedFrame()->executeCommand( |
| 418 | WebString::fromUTF8(command), WebString::fromUTF8(value)); |
| 419 | } |
| 420 | |
[email protected] | 375db13a | 2012-12-18 21:42:58 | [diff] [blame] | 421 | void WebKitTestRunner::OverridePreference(const std::string& key, |
| 422 | v8::Local<v8::Value> value) { |
| 423 | if (key == "WebKitDefaultFontSize") { |
| 424 | prefs_.defaultFontSize = value->Int32Value(); |
| 425 | } else if (key == "WebKitMinimumFontSize") { |
| 426 | prefs_.minimumFontSize = value->Int32Value(); |
| 427 | } else if (key == "WebKitDefaultTextEncodingName") { |
| 428 | prefs_.defaultTextEncodingName = |
| 429 | WebString::fromUTF8(std::string(*v8::String::AsciiValue(value))); |
| 430 | } else if (key == "WebKitJavaScriptEnabled") { |
| 431 | prefs_.javaScriptEnabled = value->BooleanValue(); |
| 432 | } else if (key == "WebKitSupportsMultipleWindows") { |
| 433 | prefs_.supportsMultipleWindows = value->BooleanValue(); |
| 434 | } else if (key == "WebKitDisplayImagesKey") { |
| 435 | prefs_.loadsImagesAutomatically = value->BooleanValue(); |
| 436 | } else if (key == "WebKitPluginsEnabled") { |
| 437 | prefs_.pluginsEnabled = value->BooleanValue(); |
| 438 | } else if (key == "WebKitJavaEnabled") { |
| 439 | prefs_.javaEnabled = value->BooleanValue(); |
| 440 | } else if (key == "WebKitUsesPageCachePreferenceKey") { |
| 441 | prefs_.usesPageCache = value->BooleanValue(); |
| 442 | } else if (key == "WebKitPageCacheSupportsPluginsPreferenceKey") { |
| 443 | prefs_.pageCacheSupportsPlugins = value->BooleanValue(); |
| 444 | } else if (key == "WebKitOfflineWebApplicationCacheEnabled") { |
| 445 | prefs_.offlineWebApplicationCacheEnabled = value->BooleanValue(); |
| 446 | } else if (key == "WebKitTabToLinksPreferenceKey") { |
| 447 | prefs_.tabsToLinks = value->BooleanValue(); |
| 448 | } else if (key == "WebKitWebGLEnabled") { |
| 449 | prefs_.experimentalWebGLEnabled = value->BooleanValue(); |
| 450 | } else if (key == "WebKitCSSRegionsEnabled") { |
| 451 | prefs_.experimentalCSSRegionsEnabled = value->BooleanValue(); |
| 452 | } else if (key == "WebKitCSSGridLayoutEnabled") { |
| 453 | prefs_.experimentalCSSGridLayoutEnabled = value->BooleanValue(); |
| 454 | } else if (key == "WebKitHyperlinkAuditingEnabled") { |
| 455 | prefs_.hyperlinkAuditingEnabled = value->BooleanValue(); |
| 456 | } else if (key == "WebKitEnableCaretBrowsing") { |
| 457 | prefs_.caretBrowsingEnabled = value->BooleanValue(); |
| 458 | } else if (key == "WebKitAllowDisplayingInsecureContent") { |
| 459 | prefs_.allowDisplayOfInsecureContent = value->BooleanValue(); |
| 460 | } else if (key == "WebKitAllowRunningInsecureContent") { |
| 461 | prefs_.allowRunningOfInsecureContent = value->BooleanValue(); |
| 462 | } else if (key == "WebKitCSSCustomFilterEnabled") { |
| 463 | prefs_.cssCustomFilterEnabled = value->BooleanValue(); |
| 464 | } else if (key == "WebKitShouldRespectImageOrientation") { |
| 465 | prefs_.shouldRespectImageOrientation = value->BooleanValue(); |
| 466 | } else if (key == "WebKitWebAudioEnabled") { |
| 467 | DCHECK(value->BooleanValue()); |
| 468 | } else { |
| 469 | std::string message("CONSOLE MESSAGE: Invalid name for preference: "); |
| 470 | printMessage(message + key + "\n"); |
| 471 | } |
| 472 | applyPreferences(); |
| 473 | } |
| 474 | |
[email protected] | 0b0e3146 | 2012-12-18 21:57:18 | [diff] [blame] | 475 | void WebKitTestRunner::DumpEditingCallbacks() { |
| 476 | dump_editing_callbacks_ = true; |
| 477 | } |
| 478 | |
[email protected] | 09c48ef5 | 2013-01-09 12:25:07 | [diff] [blame] | 479 | void WebKitTestRunner::DumpFrameLoadCallbacks() { |
| 480 | dump_frame_load_callbacks_ = true; |
| 481 | } |
| 482 | |
| 483 | void WebKitTestRunner::DumpUserGestureInFrameLoadCallbacks() { |
| 484 | dump_user_gesture_in_frame_load_callbacks_ = true; |
| 485 | } |
| 486 | |
| 487 | void WebKitTestRunner::StopProvisionalFrameLoads() { |
| 488 | stop_provisional_frame_loads_ = true; |
| 489 | } |
| 490 | |
| 491 | void WebKitTestRunner::DumpTitleChanges() { |
| 492 | dump_title_changes_ = true; |
| 493 | } |
| 494 | |
[email protected] | 43c7e695 | 2012-12-11 13:50:51 | [diff] [blame] | 495 | void WebKitTestRunner::NotImplemented(const std::string& object, |
| 496 | const std::string& method) { |
[email protected] | 2dd3a27a | 2012-12-10 11:24:16 | [diff] [blame] | 497 | Send(new ShellViewHostMsg_NotImplemented(routing_id(), object, method)); |
| 498 | } |
| 499 | |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 500 | void WebKitTestRunner::Reset() { |
[email protected] | b3dbcb5e | 2012-12-17 00:16:37 | [diff] [blame] | 501 | prefs_.reset(); |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 502 | webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences(); |
[email protected] | d48eab08 | 2012-12-19 09:25:47 | [diff] [blame] | 503 | ExportLayoutTestSpecificPreferences(prefs_, &prefs); |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 504 | render_view()->SetWebkitPreferences(prefs); |
[email protected] | 0b0e3146 | 2012-12-18 21:57:18 | [diff] [blame] | 505 | dump_editing_callbacks_ = false; |
[email protected] | 09c48ef5 | 2013-01-09 12:25:07 | [diff] [blame] | 506 | dump_frame_load_callbacks_ = false; |
| 507 | dump_user_gesture_in_frame_load_callbacks_ = false; |
| 508 | stop_provisional_frame_loads_ = false; |
| 509 | dump_title_changes_ = false; |
| 510 | test_is_running_ = true; |
[email protected] | 01f0cad0 | 2013-01-09 21:42:50 | [diff] [blame^] | 511 | load_finished_ = false; |
[email protected] | 09c48ef5 | 2013-01-09 12:25:07 | [diff] [blame] | 512 | wait_until_done_ = false; |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 513 | } |
| 514 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 515 | // Private methods ----------------------------------------------------------- |
| 516 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 517 | void WebKitTestRunner::OnCaptureTextDump(bool as_text, |
[email protected] | 891f08f | 2012-08-28 22:00:44 | [diff] [blame] | 518 | bool printing, |
| 519 | bool recursive) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 520 | WebFrame* frame = render_view()->GetWebView()->mainFrame(); |
| 521 | std::string dump; |
| 522 | if (as_text) { |
| 523 | dump = DumpFramesAsText(frame, printing, recursive); |
| 524 | } else { |
| 525 | WebFrame::RenderAsTextControls render_text_behavior = |
| 526 | WebFrame::RenderAsTextNormal; |
| 527 | if (printing) |
| 528 | render_text_behavior |= WebFrame::RenderAsTextPrinting; |
| 529 | dump = frame->renderTreeAsText(render_text_behavior).utf8(); |
| 530 | dump.append(DumpFrameScrollPosition(frame, recursive)); |
| 531 | } |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 532 | Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 533 | } |
| 534 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 535 | void WebKitTestRunner::OnCaptureImageDump( |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 536 | const std::string& expected_pixel_hash) { |
| 537 | SkBitmap snapshot; |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 538 | PaintInvalidatedRegion(); |
| 539 | CopyCanvasToBitmap(GetCanvas(), &snapshot); |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 540 | |
| 541 | SkAutoLockPixels snapshot_lock(snapshot); |
| 542 | base::MD5Digest digest; |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 543 | #if defined(OS_ANDROID) |
| 544 | // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA. |
| 545 | const uint8_t* raw_pixels = |
| 546 | reinterpret_cast<const uint8_t*>(snapshot.getPixels()); |
| 547 | size_t snapshot_size = snapshot.getSize(); |
| 548 | scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]); |
| 549 | for (size_t i = 0; i < snapshot_size; i += 4) { |
| 550 | reordered_pixels[i] = raw_pixels[i + 2]; |
| 551 | reordered_pixels[i + 1] = raw_pixels[i + 1]; |
| 552 | reordered_pixels[i + 2] = raw_pixels[i]; |
| 553 | reordered_pixels[i + 3] = raw_pixels[i + 3]; |
| 554 | } |
| 555 | base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest); |
| 556 | #else |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 557 | base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest); |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 558 | #endif |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 559 | std::string actual_pixel_hash = base::MD5DigestToBase16(digest); |
| 560 | |
| 561 | if (actual_pixel_hash == expected_pixel_hash) { |
| 562 | SkBitmap empty_image; |
| 563 | Send(new ShellViewHostMsg_ImageDump( |
| 564 | routing_id(), actual_pixel_hash, empty_image)); |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 565 | return; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 566 | } |
| 567 | Send(new ShellViewHostMsg_ImageDump( |
| 568 | routing_id(), actual_pixel_hash, snapshot)); |
| 569 | } |
| 570 | |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 571 | void WebKitTestRunner::OnSetCurrentWorkingDirectory( |
| 572 | const FilePath& current_working_directory) { |
| 573 | current_working_directory_ = current_working_directory; |
[email protected] | 01f0cad0 | 2013-01-09 21:42:50 | [diff] [blame^] | 574 | std::vector<FilePath::StringType> components; |
| 575 | current_working_directory_.GetComponents(&components); |
| 576 | for (unsigned i = 0; i < components.size(); ++i) { |
| 577 | if (components[i] == FILE_PATH_LITERAL("loading")) |
| 578 | dump_frame_load_callbacks_ = true; |
| 579 | } |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 580 | } |
| 581 | |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 582 | SkCanvas* WebKitTestRunner::GetCanvas() { |
| 583 | WebView* view = render_view()->GetWebView(); |
| 584 | const WebSize& size = view->size(); |
| 585 | float device_scale_factor = view->deviceScaleFactor(); |
| 586 | int width = std::ceil(device_scale_factor * size.width); |
| 587 | int height = std::ceil(device_scale_factor * size.height); |
| 588 | |
| 589 | if (canvas_ && |
| 590 | canvas_->getDeviceSize().width() == width && |
| 591 | canvas_->getDeviceSize().height() == height) { |
| 592 | return canvas_.get(); |
| 593 | } |
| 594 | canvas_.reset(skia::CreatePlatformCanvas( |
| 595 | size.width, size.height, true, 0, skia::RETURN_NULL_ON_FAILURE)); |
| 596 | return canvas_.get(); |
| 597 | } |
| 598 | |
| 599 | void WebKitTestRunner::PaintRect(const WebRect& rect) { |
| 600 | WebView* view = render_view()->GetWebView(); |
| 601 | float device_scale_factor = view->deviceScaleFactor(); |
| 602 | int scaled_x = device_scale_factor * rect.x; |
| 603 | int scaled_y = device_scale_factor * rect.y; |
| 604 | int scaled_width = std::ceil(device_scale_factor * rect.width); |
| 605 | int scaled_height = std::ceil(device_scale_factor * rect.height); |
| 606 | // TODO(jochen): Verify that the scaling is correct once the HiDPI tests |
| 607 | // actually work. |
| 608 | WebRect device_rect(scaled_x, scaled_y, scaled_width, scaled_height); |
| 609 | view->paint(webkit_glue::ToWebCanvas(GetCanvas()), device_rect); |
| 610 | } |
| 611 | |
| 612 | void WebKitTestRunner::PaintInvalidatedRegion() { |
| 613 | WebView* view = render_view()->GetWebView(); |
| 614 | view->animate(0.0); |
| 615 | view->layout(); |
| 616 | const WebSize& widget_size = view->size(); |
| 617 | WebRect client_rect(0, 0, widget_size.width, widget_size.height); |
| 618 | |
| 619 | // Paint the canvas if necessary. Allow painting to generate extra rects |
| 620 | // for the first two calls. This is necessary because some WebCore rendering |
| 621 | // objects update their layout only when painted. |
| 622 | for (int i = 0; i < 3; ++i) { |
| 623 | // Make sure that paint_rect is always inside the RenderView's visible |
| 624 | // area. |
| 625 | WebRect paint_rect = proxy_->paintRect(); |
| 626 | int left = std::max(paint_rect.x, client_rect.x); |
| 627 | int top = std::max(paint_rect.y, client_rect.y); |
| 628 | int right = std::min(paint_rect.x + paint_rect.width, |
| 629 | client_rect.x + client_rect.width); |
| 630 | int bottom = std::min(paint_rect.y + paint_rect.height, |
| 631 | client_rect.y + client_rect.height); |
| 632 | WebRect rect; |
| 633 | if (left < right && top < bottom) |
| 634 | rect = WebRect(left, top, right - left, bottom - top); |
| 635 | proxy_->setPaintRect(WebRect()); |
| 636 | if (rect.isEmpty()) |
| 637 | continue; |
| 638 | PaintRect(rect); |
| 639 | } |
| 640 | CHECK(proxy_->paintRect().isEmpty()); |
| 641 | } |
| 642 | |
| 643 | void WebKitTestRunner::DisplayRepaintMask() { |
| 644 | GetCanvas()->drawARGB(167, 0, 0, 0); |
| 645 | } |
| 646 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 647 | } // namespace content |