[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] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 7 | #include "base/md5.h" |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 9 | #include "base/stringprintf.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 10 | #include "content/public/renderer/render_view.h" |
| 11 | #include "content/shell/shell_messages.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 12 | #include "skia/ext/platform_canvas.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
| 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 16 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 17 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 18 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 19 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
[email protected] | 96b80b47 | 2012-07-03 19:41:56 | [diff] [blame] | 20 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebTestingSupport.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 21 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 22 | #include "webkit/glue/webkit_glue.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 23 | |
| 24 | using WebKit::WebFrame; |
| 25 | using WebKit::WebElement; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 26 | using WebKit::WebRect; |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 27 | using WebKit::WebSize; |
[email protected] | 96b80b47 | 2012-07-03 19:41:56 | [diff] [blame] | 28 | using WebKit::WebTestingSupport; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 29 | using WebKit::WebView; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 30 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 31 | namespace content { |
| 32 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | std::string DumpDocumentText(WebFrame* frame) { |
| 36 | // We use the document element's text instead of the body text here because |
| 37 | // not all documents have a body, such as XML documents. |
| 38 | WebElement documentElement = frame->document().documentElement(); |
| 39 | if (documentElement.isNull()) |
| 40 | return std::string(); |
| 41 | return documentElement.innerText().utf8(); |
| 42 | } |
| 43 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 44 | std::string DumpDocumentPrintedText(WebFrame* frame) { |
| 45 | return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8(); |
| 46 | } |
| 47 | |
| 48 | std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 49 | std::string result; |
| 50 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 51 | // Cannot do printed format for anything other than HTML. |
| 52 | if (printing && !frame->document().isHTMLDocument()) |
| 53 | return std::string(); |
| 54 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 55 | // Add header for all but the main frame. Skip emtpy frames. |
| 56 | if (frame->parent() && !frame->document().documentElement().isNull()) { |
| 57 | result.append("\n--------\nFrame: '"); |
| 58 | result.append(frame->name().utf8().data()); |
| 59 | result.append("'\n--------\n"); |
| 60 | } |
| 61 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 62 | result.append( |
| 63 | printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 64 | result.append("\n"); |
| 65 | |
| 66 | if (recursive) { |
| 67 | for (WebFrame* child = frame->firstChild(); child; |
| 68 | child = child->nextSibling()) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 69 | result.append(DumpFramesAsText(child, printing, recursive)); |
| 70 | } |
| 71 | } |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) { |
| 76 | std::string result; |
| 77 | |
| 78 | WebSize offset = frame->scrollOffset(); |
| 79 | if (offset.width > 0 || offset.height > 0) { |
| 80 | if (frame->parent()) { |
| 81 | result.append( |
| 82 | base::StringPrintf("frame '%s' ", frame->name().utf8().data())); |
| 83 | } |
| 84 | result.append( |
| 85 | base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height)); |
| 86 | } |
| 87 | |
| 88 | if (recursive) { |
| 89 | for (WebFrame* child = frame->firstChild(); child; |
| 90 | child = child->nextSibling()) { |
| 91 | result.append(DumpFrameScrollPosition(child, recursive)); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 97 | bool PaintViewIntoCanvas(WebView* view, skia::PlatformCanvas& canvas) { |
| 98 | view->layout(); |
| 99 | const WebSize& size = view->size(); |
| 100 | |
| 101 | if (!canvas.initialize(size.width, size.height, true)) |
| 102 | return false; |
| 103 | |
| 104 | view->paint(webkit_glue::ToWebCanvas(&canvas), |
| 105 | WebRect(0, 0, size.width, size.height)); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | #if !defined(OS_MACOSX) |
| 110 | void MakeBitmapOpaque(SkBitmap* bitmap) { |
| 111 | SkAutoLockPixels lock(*bitmap); |
| 112 | DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config); |
| 113 | for (int y = 0; y < bitmap->height(); ++y) { |
| 114 | uint32_t* row = bitmap->getAddr32(0, y); |
| 115 | for (int x = 0; x < bitmap->width(); ++x) |
| 116 | row[x] |= 0xFF000000; // Set alpha bits to 1. |
| 117 | } |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | void CaptureSnapshot(WebView* view, SkBitmap* snapshot) { |
| 122 | skia::PlatformCanvas canvas; |
| 123 | if (!PaintViewIntoCanvas(view, canvas)) |
| 124 | return; |
| 125 | |
| 126 | SkDevice* device = skia::GetTopDevice(canvas); |
| 127 | |
| 128 | const SkBitmap& bitmap = device->accessBitmap(false); |
| 129 | bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config); |
| 130 | |
| 131 | #if !defined(OS_MACOSX) |
| 132 | // Only the expected PNGs for Mac have a valid alpha channel. |
| 133 | MakeBitmapOpaque(snapshot); |
| 134 | #endif |
| 135 | |
| 136 | } |
| 137 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 138 | } // namespace |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 139 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 140 | WebKitTestRunner::WebKitTestRunner(RenderView* render_view) |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 141 | : RenderViewObserver(render_view) { |
| 142 | } |
| 143 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 144 | WebKitTestRunner::~WebKitTestRunner() { |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 147 | void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) { |
[email protected] | 96b80b47 | 2012-07-03 19:41:56 | [diff] [blame] | 148 | WebTestingSupport::injectInternalsObject(frame); |
| 149 | } |
| 150 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 151 | void WebKitTestRunner::DidFinishLoad(WebFrame* frame) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 152 | if (!frame->parent()) |
| 153 | Send(new ShellViewHostMsg_DidFinishLoad(routing_id())); |
| 154 | } |
| 155 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 156 | bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 157 | bool handled = true; |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 158 | IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 159 | IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 160 | IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump) |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 161 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 162 | IPC_END_MESSAGE_MAP() |
| 163 | |
| 164 | return handled; |
| 165 | } |
| 166 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 167 | void WebKitTestRunner::OnCaptureTextDump(bool as_text, |
[email protected] | cccb5cf | 2012-06-06 22:20:04 | [diff] [blame] | 168 | bool printing, |
| 169 | bool recursive) { |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 170 | WebFrame* frame = render_view()->GetWebView()->mainFrame(); |
| 171 | std::string dump; |
| 172 | if (as_text) { |
| 173 | dump = DumpFramesAsText(frame, printing, recursive); |
| 174 | } else { |
| 175 | WebFrame::RenderAsTextControls render_text_behavior = |
| 176 | WebFrame::RenderAsTextNormal; |
| 177 | if (printing) |
| 178 | render_text_behavior |= WebFrame::RenderAsTextPrinting; |
| 179 | dump = frame->renderTreeAsText(render_text_behavior).utf8(); |
| 180 | dump.append(DumpFrameScrollPosition(frame, recursive)); |
| 181 | } |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 182 | Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 9b4c6cd | 2012-08-20 10:47:45 | [diff] [blame^] | 185 | void WebKitTestRunner::OnCaptureImageDump( |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 186 | const std::string& expected_pixel_hash) { |
| 187 | SkBitmap snapshot; |
| 188 | CaptureSnapshot(render_view()->GetWebView(), &snapshot); |
| 189 | |
| 190 | SkAutoLockPixels snapshot_lock(snapshot); |
| 191 | base::MD5Digest digest; |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 192 | #if defined(OS_ANDROID) |
| 193 | // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA. |
| 194 | const uint8_t* raw_pixels = |
| 195 | reinterpret_cast<const uint8_t*>(snapshot.getPixels()); |
| 196 | size_t snapshot_size = snapshot.getSize(); |
| 197 | scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]); |
| 198 | for (size_t i = 0; i < snapshot_size; i += 4) { |
| 199 | reordered_pixels[i] = raw_pixels[i + 2]; |
| 200 | reordered_pixels[i + 1] = raw_pixels[i + 1]; |
| 201 | reordered_pixels[i + 2] = raw_pixels[i]; |
| 202 | reordered_pixels[i + 3] = raw_pixels[i + 3]; |
| 203 | } |
| 204 | base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest); |
| 205 | #else |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 206 | base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest); |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 207 | #endif |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 208 | std::string actual_pixel_hash = base::MD5DigestToBase16(digest); |
| 209 | |
| 210 | if (actual_pixel_hash == expected_pixel_hash) { |
| 211 | SkBitmap empty_image; |
| 212 | Send(new ShellViewHostMsg_ImageDump( |
| 213 | routing_id(), actual_pixel_hash, empty_image)); |
[email protected] | e225b92 | 2012-08-18 01:43:04 | [diff] [blame] | 214 | return; |
[email protected] | 0799da0 | 2012-06-27 10:58:51 | [diff] [blame] | 215 | } |
| 216 | Send(new ShellViewHostMsg_ImageDump( |
| 217 | routing_id(), actual_pixel_hash, snapshot)); |
| 218 | } |
| 219 | |
[email protected] | 31d71b0 | 2012-01-26 03:42:31 | [diff] [blame] | 220 | } // namespace content |