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