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