| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/shell/layout_test_controller.h" | 5 #include "content/shell/layout_test_controller.h" |
| 6 | 6 |
| 7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
| 8 #include "content/public/renderer/render_view.h" | 8 #include "content/public/renderer/render_view.h" |
| 9 #include "content/shell/shell_messages.h" | 9 #include "content/shell/shell_messages.h" |
| 10 #include "skia/ext/platform_canvas.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 19 #include "webkit/glue/webkit_glue.h" |
| 16 | 20 |
| 17 using WebKit::WebFrame; | 21 using WebKit::WebFrame; |
| 18 using WebKit::WebElement; | 22 using WebKit::WebElement; |
| 23 using WebKit::WebRect; |
| 19 using WebKit::WebSize; | 24 using WebKit::WebSize; |
| 25 using WebKit::WebView; |
| 20 | 26 |
| 21 namespace content { | 27 namespace content { |
| 22 | 28 |
| 23 namespace { | 29 namespace { |
| 24 | 30 |
| 25 std::string DumpDocumentText(WebFrame* frame) { | 31 std::string DumpDocumentText(WebFrame* frame) { |
| 26 // We use the document element's text instead of the body text here because | 32 // We use the document element's text instead of the body text here because |
| 27 // not all documents have a body, such as XML documents. | 33 // not all documents have a body, such as XML documents. |
| 28 WebElement documentElement = frame->document().documentElement(); | 34 WebElement documentElement = frame->document().documentElement(); |
| 29 if (documentElement.isNull()) | 35 if (documentElement.isNull()) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 83 |
| 78 if (recursive) { | 84 if (recursive) { |
| 79 for (WebFrame* child = frame->firstChild(); child; | 85 for (WebFrame* child = frame->firstChild(); child; |
| 80 child = child->nextSibling()) { | 86 child = child->nextSibling()) { |
| 81 result.append(DumpFrameScrollPosition(child, recursive)); | 87 result.append(DumpFrameScrollPosition(child, recursive)); |
| 82 } | 88 } |
| 83 } | 89 } |
| 84 return result; | 90 return result; |
| 85 } | 91 } |
| 86 | 92 |
| 93 bool PaintViewIntoCanvas(WebView* view, skia::PlatformCanvas& canvas) { |
| 94 view->layout(); |
| 95 const WebSize& size = view->size(); |
| 96 |
| 97 if (!canvas.initialize(size.width, size.height, true)) |
| 98 return false; |
| 99 |
| 100 view->paint(webkit_glue::ToWebCanvas(&canvas), |
| 101 WebRect(0, 0, size.width, size.height)); |
| 102 return true; |
| 103 } |
| 104 |
| 105 #if !defined(OS_MACOSX) |
| 106 void MakeBitmapOpaque(SkBitmap* bitmap) { |
| 107 SkAutoLockPixels lock(*bitmap); |
| 108 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config); |
| 109 for (int y = 0; y < bitmap->height(); ++y) { |
| 110 uint32_t* row = bitmap->getAddr32(0, y); |
| 111 for (int x = 0; x < bitmap->width(); ++x) |
| 112 row[x] |= 0xFF000000; // Set alpha bits to 1. |
| 113 } |
| 114 } |
| 115 #endif |
| 116 |
| 117 void CaptureSnapshot(WebView* view, SkBitmap* snapshot) { |
| 118 skia::PlatformCanvas canvas; |
| 119 if (!PaintViewIntoCanvas(view, canvas)) |
| 120 return; |
| 121 |
| 122 SkDevice* device = skia::GetTopDevice(canvas); |
| 123 |
| 124 const SkBitmap& bitmap = device->accessBitmap(false); |
| 125 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config); |
| 126 |
| 127 #if !defined(OS_MACOSX) |
| 128 // Only the expected PNGs for Mac have a valid alpha channel. |
| 129 MakeBitmapOpaque(snapshot); |
| 130 #endif |
| 131 |
| 132 } |
| 133 |
| 87 } // namespace | 134 } // namespace |
| 88 | 135 |
| 89 LayoutTestController::LayoutTestController(RenderView* render_view) | 136 LayoutTestController::LayoutTestController(RenderView* render_view) |
| 90 : RenderViewObserver(render_view) { | 137 : RenderViewObserver(render_view) { |
| 91 } | 138 } |
| 92 | 139 |
| 93 LayoutTestController::~LayoutTestController() { | 140 LayoutTestController::~LayoutTestController() { |
| 94 } | 141 } |
| 95 | 142 |
| 96 void LayoutTestController::DidFinishLoad(WebFrame* frame) { | 143 void LayoutTestController::DidFinishLoad(WebFrame* frame) { |
| 97 if (!frame->parent()) | 144 if (!frame->parent()) |
| 98 Send(new ShellViewHostMsg_DidFinishLoad(routing_id())); | 145 Send(new ShellViewHostMsg_DidFinishLoad(routing_id())); |
| 99 } | 146 } |
| 100 | 147 |
| 101 bool LayoutTestController::OnMessageReceived(const IPC::Message& message) { | 148 bool LayoutTestController::OnMessageReceived(const IPC::Message& message) { |
| 102 bool handled = true; | 149 bool handled = true; |
| 103 IPC_BEGIN_MESSAGE_MAP(LayoutTestController, message) | 150 IPC_BEGIN_MESSAGE_MAP(LayoutTestController, message) |
| 104 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) | 151 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) |
| 152 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump) |
| 105 IPC_MESSAGE_UNHANDLED(handled = false) | 153 IPC_MESSAGE_UNHANDLED(handled = false) |
| 106 IPC_END_MESSAGE_MAP() | 154 IPC_END_MESSAGE_MAP() |
| 107 | 155 |
| 108 return handled; | 156 return handled; |
| 109 } | 157 } |
| 110 | 158 |
| 111 void LayoutTestController::OnCaptureTextDump(bool as_text, | 159 void LayoutTestController::OnCaptureTextDump(bool as_text, |
| 112 bool printing, | 160 bool printing, |
| 113 bool recursive) { | 161 bool recursive) { |
| 114 WebFrame* frame = render_view()->GetWebView()->mainFrame(); | 162 WebFrame* frame = render_view()->GetWebView()->mainFrame(); |
| 115 std::string dump; | 163 std::string dump; |
| 116 if (as_text) { | 164 if (as_text) { |
| 117 dump = DumpFramesAsText(frame, printing, recursive); | 165 dump = DumpFramesAsText(frame, printing, recursive); |
| 118 } else { | 166 } else { |
| 119 WebFrame::RenderAsTextControls render_text_behavior = | 167 WebFrame::RenderAsTextControls render_text_behavior = |
| 120 WebFrame::RenderAsTextNormal; | 168 WebFrame::RenderAsTextNormal; |
| 121 if (printing) | 169 if (printing) |
| 122 render_text_behavior |= WebFrame::RenderAsTextPrinting; | 170 render_text_behavior |= WebFrame::RenderAsTextPrinting; |
| 123 dump = frame->renderTreeAsText(render_text_behavior).utf8(); | 171 dump = frame->renderTreeAsText(render_text_behavior).utf8(); |
| 124 dump.append(DumpFrameScrollPosition(frame, recursive)); | 172 dump.append(DumpFrameScrollPosition(frame, recursive)); |
| 125 } | 173 } |
| 126 Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); | 174 Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); |
| 127 } | 175 } |
| 128 | 176 |
| 177 void LayoutTestController::OnCaptureImageDump() { |
| 178 SkBitmap snapshot; |
| 179 CaptureSnapshot(render_view()->GetWebView(), &snapshot); |
| 180 Send(new ShellViewHostMsg_ImageDump(routing_id(), snapshot)); |
| 181 } |
| 182 |
| 129 } // namespace content | 183 } // namespace content |
| OLD | NEW |