[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] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 9 | #include "base/md5.h" |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 11 | #include "base/message_loop.h" |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 12 | #include "base/stringprintf.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 13 | #include "base/sys_string_conversions.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 14 | #include "base/time.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 16 | #include "content/public/renderer/render_view.h" |
| 17 | #include "content/shell/shell_messages.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 18 | #include "content/shell/shell_render_process_observer.h" |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 19 | #include "net/base/net_util.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 20 | #include "skia/ext/platform_canvas.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 21 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 22 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatformSupport.h" |
[email protected] | f89ce5a | 2012-10-19 01:07:02 | [diff] [blame] | 23 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 24 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 25 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 26 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.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] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 32 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h" |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 33 | #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 34 | #include "webkit/glue/webkit_glue.h" |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 35 | #include "webkit/glue/webpreferences.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 36 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 37 | using WebKit::WebContextMenuData; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 38 | using WebKit::WebElement; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 39 | using WebKit::WebFrame; |
| 40 | using WebKit::WebGamepads; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 41 | using WebKit::WebRect; |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 42 | using WebKit::WebSize; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 43 | using WebKit::WebString; |
| 44 | using WebKit::WebVector; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 45 | using WebKit::WebView; |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 46 | using WebTestRunner::WebTask; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 47 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 48 | namespace content { |
| 49 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 50 | namespace { |
| 51 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 52 | void InvokeTaskHelper(void* context) { |
| 53 | WebTask* task = reinterpret_cast<WebTask*>(context); |
| 54 | task->run(); |
| 55 | delete task; |
| 56 | } |
| 57 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 58 | std::string DumpDocumentText(WebFrame* frame) { |
| 59 | // We use the document element's text instead of the body text here because |
| 60 | // not all documents have a body, such as XML documents. |
| 61 | WebElement documentElement = frame->document().documentElement(); |
| 62 | if (documentElement.isNull()) |
| 63 | return std::string(); |
| 64 | return documentElement.innerText().utf8(); |
| 65 | } |
| 66 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 67 | std::string DumpDocumentPrintedText(WebFrame* frame) { |
| 68 | return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8(); |
| 69 | } |
| 70 | |
| 71 | std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 72 | std::string result; |
| 73 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 74 | // Cannot do printed format for anything other than HTML. |
| 75 | if (printing && !frame->document().isHTMLDocument()) |
| 76 | return std::string(); |
| 77 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 78 | // Add header for all but the main frame. Skip emtpy frames. |
| 79 | if (frame->parent() && !frame->document().documentElement().isNull()) { |
| 80 | result.append("\n--------\nFrame: '"); |
[email protected] | 6d454d9 | 2012-09-13 17:06:29 | [diff] [blame] | 81 | result.append(frame->uniqueName().utf8().data()); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 82 | result.append("'\n--------\n"); |
| 83 | } |
| 84 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 85 | result.append( |
| 86 | printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 87 | result.append("\n"); |
| 88 | |
| 89 | if (recursive) { |
| 90 | for (WebFrame* child = frame->firstChild(); child; |
| 91 | child = child->nextSibling()) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 92 | result.append(DumpFramesAsText(child, printing, recursive)); |
| 93 | } |
| 94 | } |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) { |
| 99 | std::string result; |
| 100 | |
| 101 | WebSize offset = frame->scrollOffset(); |
| 102 | if (offset.width > 0 || offset.height > 0) { |
| 103 | if (frame->parent()) { |
| 104 | result.append( |
[email protected] | 6d454d9 | 2012-09-13 17:06:29 | [diff] [blame] | 105 | base::StringPrintf("frame '%s' ", frame->uniqueName().utf8().data())); |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 106 | } |
| 107 | result.append( |
| 108 | base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height)); |
| 109 | } |
| 110 | |
| 111 | if (recursive) { |
| 112 | for (WebFrame* child = frame->firstChild(); child; |
| 113 | child = child->nextSibling()) { |
| 114 | result.append(DumpFrameScrollPosition(child, recursive)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | return result; |
| 118 | } |
| 119 | |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 120 | #if !defined(OS_MACOSX) |
| 121 | void MakeBitmapOpaque(SkBitmap* bitmap) { |
| 122 | SkAutoLockPixels lock(*bitmap); |
| 123 | DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config); |
| 124 | for (int y = 0; y < bitmap->height(); ++y) { |
| 125 | uint32_t* row = bitmap->getAddr32(0, y); |
| 126 | for (int x = 0; x < bitmap->width(); ++x) |
| 127 | row[x] |= 0xFF000000; // Set alpha bits to 1. |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 132 | void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) { |
[email protected] | 9d611ca | 2012-11-14 13:46:56 | [diff] [blame] | 133 | SkDevice* device = skia::GetTopDevice(*canvas); |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 134 | const SkBitmap& bitmap = device->accessBitmap(false); |
| 135 | bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config); |
| 136 | |
| 137 | #if !defined(OS_MACOSX) |
| 138 | // Only the expected PNGs for Mac have a valid alpha channel. |
| 139 | MakeBitmapOpaque(snapshot); |
| 140 | #endif |
| 141 | |
| 142 | } |
| 143 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 144 | } // namespace |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 145 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 146 | WebKitTestRunner::WebKitTestRunner(RenderView* render_view) |
[email protected] | 2dd3a27a | 2012-12-10 11:24:16 | [diff] [blame] | 147 | : RenderViewObserver(render_view) { |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 150 | WebKitTestRunner::~WebKitTestRunner() { |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 153 | // WebTestDelegate ----------------------------------------------------------- |
| 154 | |
| 155 | void WebKitTestRunner::clearContextMenuData() { |
| 156 | last_context_menu_data_.reset(); |
| 157 | } |
| 158 | |
| 159 | WebContextMenuData* WebKitTestRunner::lastContextMenuData() const { |
| 160 | return last_context_menu_data_.get(); |
| 161 | } |
| 162 | |
| 163 | void WebKitTestRunner::clearEditCommand() { |
| 164 | render_view()->ClearEditCommands(); |
| 165 | } |
| 166 | |
| 167 | void WebKitTestRunner::setEditCommand(const std::string& name, |
| 168 | const std::string& value) { |
| 169 | render_view()->SetEditCommandForNextKeyEvent(name, value); |
| 170 | } |
| 171 | |
| 172 | void WebKitTestRunner::fillSpellingSuggestionList( |
| 173 | const WebString& word, WebVector<WebString>* suggestions) { |
| 174 | if (word == WebString::fromUTF8("wellcome")) { |
| 175 | WebVector<WebString> result(suggestions->size() + 1); |
| 176 | for (size_t i = 0; i < suggestions->size(); ++i) |
| 177 | result[i] = (*suggestions)[i]; |
| 178 | result[suggestions->size()] = WebString::fromUTF8("welcome"); |
| 179 | suggestions->swap(result); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) { |
| 184 | Send(new ShellViewHostMsg_NotImplemented( |
| 185 | routing_id(), "WebTestDelegate", "setGamepadData")); |
| 186 | } |
| 187 | |
| 188 | void WebKitTestRunner::printMessage(const std::string& message) { |
| 189 | Send(new ShellViewHostMsg_PrintMessage(routing_id(), message)); |
| 190 | } |
| 191 | |
| 192 | void WebKitTestRunner::postTask(WebTask* task) { |
| 193 | WebKit::webKitPlatformSupport()->callOnMainThread(InvokeTaskHelper, task); |
| 194 | } |
| 195 | |
| 196 | void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) { |
| 197 | MessageLoop::current()->PostDelayedTask( |
| 198 | FROM_HERE, |
| 199 | base::Bind(&WebTask::run, base::Owned(task)), |
| 200 | base::TimeDelta::FromMilliseconds(ms)); |
| 201 | } |
| 202 | |
| 203 | WebString WebKitTestRunner::registerIsolatedFileSystem( |
| 204 | const WebKit::WebVector<WebKit::WebString>& absolute_filenames) { |
| 205 | Send(new ShellViewHostMsg_NotImplemented( |
| 206 | routing_id(), "WebTestDelegate", "registerIsolatedFileSystem")); |
| 207 | return WebString(); |
| 208 | } |
| 209 | |
| 210 | long long WebKitTestRunner::getCurrentTimeInMillisecond() { |
| 211 | return base::TimeTicks::Now().ToInternalValue() / |
| 212 | base::Time::kMicrosecondsPerMillisecond; |
| 213 | } |
| 214 | |
| 215 | WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path( |
| 216 | const std::string& utf8_path) { |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 217 | #if defined(OS_WIN) |
| 218 | FilePath path(UTF8ToWide(utf8_path)); |
| 219 | #else |
| 220 | FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path))); |
| 221 | #endif |
| 222 | if (!path.IsAbsolute()) { |
| 223 | GURL base_url = |
| 224 | net::FilePathToFileURL(current_working_directory_.Append( |
| 225 | FILE_PATH_LITERAL("foo"))); |
| 226 | net::FileURLToFilePath(base_url.Resolve(utf8_path), &path); |
| 227 | } |
| 228 | #if defined(OS_WIN) |
| 229 | return WebString(path.value()); |
| 230 | #else |
| 231 | return WideToUTF16(base::SysNativeMBToWide(path.value())); |
| 232 | #endif |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // RenderViewObserver -------------------------------------------------------- |
| 236 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 237 | void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) { |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 238 | ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame); |
[email protected] | 96b80b47 | 2012-07-03 19:41:56 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 241 | void WebKitTestRunner::DidFinishLoad(WebFrame* frame) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 242 | if (!frame->parent()) |
| 243 | Send(new ShellViewHostMsg_DidFinishLoad(routing_id())); |
| 244 | } |
| 245 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 246 | void WebKitTestRunner::DidRequestShowContextMenu( |
| 247 | WebFrame* frame, |
| 248 | const WebContextMenuData& data) { |
| 249 | last_context_menu_data_.reset(new WebContextMenuData(data)); |
| 250 | } |
| 251 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 252 | bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 253 | bool handled = true; |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 254 | IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 255 | IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 256 | IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump) |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 257 | IPC_MESSAGE_HANDLER(ShellViewMsg_SetCurrentWorkingDirectory, |
| 258 | OnSetCurrentWorkingDirectory) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 259 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 260 | IPC_END_MESSAGE_MAP() |
| 261 | |
| 262 | return handled; |
| 263 | } |
| 264 | |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 265 | // Public methods - ----------------------------------------------------------- |
| 266 | |
| 267 | void WebKitTestRunner::Display() { |
| 268 | const WebSize& size = render_view()->GetWebView()->size(); |
| 269 | WebRect rect(0, 0, size.width, size.height); |
| 270 | proxy_->setPaintRect(rect); |
| 271 | PaintInvalidatedRegion(); |
| 272 | DisplayRepaintMask(); |
| 273 | } |
| 274 | |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 275 | void WebKitTestRunner::SetXSSAuditorEnabled(bool enabled) { |
| 276 | prefs_.xss_auditor_enabled = enabled; |
| 277 | webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences(); |
| 278 | prefs_.Apply(&prefs); |
| 279 | render_view()->SetWebkitPreferences(prefs); |
| 280 | Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs_)); |
| 281 | } |
| 282 | |
[email protected] | 2dd3a27a | 2012-12-10 11:24:16 | [diff] [blame] | 283 | void WebKitTestRunner::NotifyDone() { |
| 284 | Send(new ShellViewHostMsg_NotifyDone(routing_id())); |
| 285 | } |
| 286 | |
| 287 | void WebKitTestRunner::DumpAsText() { |
| 288 | Send(new ShellViewHostMsg_DumpAsText(routing_id())); |
| 289 | } |
| 290 | |
| 291 | void WebKitTestRunner::DumpChildFramesAsText() { |
| 292 | Send(new ShellViewHostMsg_DumpChildFramesAsText(routing_id())); |
| 293 | } |
| 294 | |
| 295 | void WebKitTestRunner::SetPrinting() { |
| 296 | Send(new ShellViewHostMsg_SetPrinting(routing_id())); |
| 297 | } |
| 298 | |
| 299 | void WebKitTestRunner::SetShouldStayOnPageAfterHandlingBeforeUnload( |
| 300 | bool should_stay_on_page) { |
| 301 | Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload( |
| 302 | routing_id(), should_stay_on_page)); |
| 303 | } |
| 304 | |
| 305 | void WebKitTestRunner::WaitUntilDone() { |
| 306 | Send(new ShellViewHostMsg_WaitUntilDone(routing_id())); |
| 307 | } |
| 308 | |
[email protected] | d99d65a | 2012-12-10 17:40:27 | [diff] [blame^] | 309 | void WebKitTestRunner::CanOpenWindows() { |
| 310 | Send(new ShellViewHostMsg_CanOpenWindows(routing_id())); |
| 311 | } |
| 312 | |
[email protected] | 2dd3a27a | 2012-12-10 11:24:16 | [diff] [blame] | 313 | void WebKitTestRunner::NotImplemented(const char* object, const char* method) { |
| 314 | Send(new ShellViewHostMsg_NotImplemented(routing_id(), object, method)); |
| 315 | } |
| 316 | |
[email protected] | 324825d | 2012-11-30 12:37:15 | [diff] [blame] | 317 | void WebKitTestRunner::Reset() { |
| 318 | prefs_ = ShellWebPreferences(); |
| 319 | webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences(); |
| 320 | prefs_.Apply(&prefs); |
| 321 | render_view()->SetWebkitPreferences(prefs); |
| 322 | } |
| 323 | |
[email protected] | b2324b09 | 2012-11-01 10:34:11 | [diff] [blame] | 324 | // Private methods ----------------------------------------------------------- |
| 325 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 326 | void WebKitTestRunner::OnCaptureTextDump(bool as_text, |
[email protected] | 891f08f | 2012-08-28 22:00:44 | [diff] [blame] | 327 | bool printing, |
| 328 | bool recursive) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 329 | WebFrame* frame = render_view()->GetWebView()->mainFrame(); |
| 330 | std::string dump; |
| 331 | if (as_text) { |
| 332 | dump = DumpFramesAsText(frame, printing, recursive); |
| 333 | } else { |
| 334 | WebFrame::RenderAsTextControls render_text_behavior = |
| 335 | WebFrame::RenderAsTextNormal; |
| 336 | if (printing) |
| 337 | render_text_behavior |= WebFrame::RenderAsTextPrinting; |
| 338 | dump = frame->renderTreeAsText(render_text_behavior).utf8(); |
| 339 | dump.append(DumpFrameScrollPosition(frame, recursive)); |
| 340 | } |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 341 | Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame] | 344 | void WebKitTestRunner::OnCaptureImageDump( |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 345 | const std::string& expected_pixel_hash) { |
| 346 | SkBitmap snapshot; |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 347 | PaintInvalidatedRegion(); |
| 348 | CopyCanvasToBitmap(GetCanvas(), &snapshot); |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 349 | |
| 350 | SkAutoLockPixels snapshot_lock(snapshot); |
| 351 | base::MD5Digest digest; |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 352 | #if defined(OS_ANDROID) |
| 353 | // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA. |
| 354 | const uint8_t* raw_pixels = |
| 355 | reinterpret_cast<const uint8_t*>(snapshot.getPixels()); |
| 356 | size_t snapshot_size = snapshot.getSize(); |
| 357 | scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]); |
| 358 | for (size_t i = 0; i < snapshot_size; i += 4) { |
| 359 | reordered_pixels[i] = raw_pixels[i + 2]; |
| 360 | reordered_pixels[i + 1] = raw_pixels[i + 1]; |
| 361 | reordered_pixels[i + 2] = raw_pixels[i]; |
| 362 | reordered_pixels[i + 3] = raw_pixels[i + 3]; |
| 363 | } |
| 364 | base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest); |
| 365 | #else |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 366 | base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest); |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 367 | #endif |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 368 | std::string actual_pixel_hash = base::MD5DigestToBase16(digest); |
| 369 | |
| 370 | if (actual_pixel_hash == expected_pixel_hash) { |
| 371 | SkBitmap empty_image; |
| 372 | Send(new ShellViewHostMsg_ImageDump( |
| 373 | routing_id(), actual_pixel_hash, empty_image)); |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 374 | return; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 375 | } |
| 376 | Send(new ShellViewHostMsg_ImageDump( |
| 377 | routing_id(), actual_pixel_hash, snapshot)); |
| 378 | } |
| 379 | |
[email protected] | 0d2dfb92d | 2012-11-05 10:26:45 | [diff] [blame] | 380 | void WebKitTestRunner::OnSetCurrentWorkingDirectory( |
| 381 | const FilePath& current_working_directory) { |
| 382 | current_working_directory_ = current_working_directory; |
| 383 | } |
| 384 | |
[email protected] | b9fad24c | 2012-11-15 08:22:10 | [diff] [blame] | 385 | SkCanvas* WebKitTestRunner::GetCanvas() { |
| 386 | WebView* view = render_view()->GetWebView(); |
| 387 | const WebSize& size = view->size(); |
| 388 | float device_scale_factor = view->deviceScaleFactor(); |
| 389 | int width = std::ceil(device_scale_factor * size.width); |
| 390 | int height = std::ceil(device_scale_factor * size.height); |
| 391 | |
| 392 | if (canvas_ && |
| 393 | canvas_->getDeviceSize().width() == width && |
| 394 | canvas_->getDeviceSize().height() == height) { |
| 395 | return canvas_.get(); |
| 396 | } |
| 397 | canvas_.reset(skia::CreatePlatformCanvas( |
| 398 | size.width, size.height, true, 0, skia::RETURN_NULL_ON_FAILURE)); |
| 399 | return canvas_.get(); |
| 400 | } |
| 401 | |
| 402 | void WebKitTestRunner::PaintRect(const WebRect& rect) { |
| 403 | WebView* view = render_view()->GetWebView(); |
| 404 | float device_scale_factor = view->deviceScaleFactor(); |
| 405 | int scaled_x = device_scale_factor * rect.x; |
| 406 | int scaled_y = device_scale_factor * rect.y; |
| 407 | int scaled_width = std::ceil(device_scale_factor * rect.width); |
| 408 | int scaled_height = std::ceil(device_scale_factor * rect.height); |
| 409 | // TODO(jochen): Verify that the scaling is correct once the HiDPI tests |
| 410 | // actually work. |
| 411 | WebRect device_rect(scaled_x, scaled_y, scaled_width, scaled_height); |
| 412 | view->paint(webkit_glue::ToWebCanvas(GetCanvas()), device_rect); |
| 413 | } |
| 414 | |
| 415 | void WebKitTestRunner::PaintInvalidatedRegion() { |
| 416 | WebView* view = render_view()->GetWebView(); |
| 417 | view->animate(0.0); |
| 418 | view->layout(); |
| 419 | const WebSize& widget_size = view->size(); |
| 420 | WebRect client_rect(0, 0, widget_size.width, widget_size.height); |
| 421 | |
| 422 | // Paint the canvas if necessary. Allow painting to generate extra rects |
| 423 | // for the first two calls. This is necessary because some WebCore rendering |
| 424 | // objects update their layout only when painted. |
| 425 | for (int i = 0; i < 3; ++i) { |
| 426 | // Make sure that paint_rect is always inside the RenderView's visible |
| 427 | // area. |
| 428 | WebRect paint_rect = proxy_->paintRect(); |
| 429 | int left = std::max(paint_rect.x, client_rect.x); |
| 430 | int top = std::max(paint_rect.y, client_rect.y); |
| 431 | int right = std::min(paint_rect.x + paint_rect.width, |
| 432 | client_rect.x + client_rect.width); |
| 433 | int bottom = std::min(paint_rect.y + paint_rect.height, |
| 434 | client_rect.y + client_rect.height); |
| 435 | WebRect rect; |
| 436 | if (left < right && top < bottom) |
| 437 | rect = WebRect(left, top, right - left, bottom - top); |
| 438 | proxy_->setPaintRect(WebRect()); |
| 439 | if (rect.isEmpty()) |
| 440 | continue; |
| 441 | PaintRect(rect); |
| 442 | } |
| 443 | CHECK(proxy_->paintRect().isEmpty()); |
| 444 | } |
| 445 | |
| 446 | void WebKitTestRunner::DisplayRepaintMask() { |
| 447 | GetCanvas()->drawARGB(167, 0, 0, 0); |
| 448 | } |
| 449 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 450 | } // namespace content |