blob: 3a7439f57e2cabbfe9ab0f031c2fee6737399b77 [file] [log] [blame]
[email protected]31d71b02012-01-26 03:42:311// 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]cccb5cf2012-06-06 22:20:045#include "content/shell/layout_test_controller.h"
[email protected]31d71b02012-01-26 03:42:316
[email protected]0799da02012-06-27 10:58:517#include "base/md5.h"
[email protected]c272c5b2012-06-06 09:01:068#include "base/stringprintf.h"
[email protected]efb5f572012-01-29 10:57:339#include "content/public/renderer/render_view.h"
10#include "content/shell/shell_messages.h"
[email protected]0799da02012-06-27 10:58:5111#include "skia/ext/platform_canvas.h"
[email protected]efb5f572012-01-29 10:57:3312#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h"
[email protected]0799da02012-06-27 10:58:5113#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]efb5f572012-01-29 10:57:3315#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"
19#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]0799da02012-06-27 10:58:5120#include "webkit/glue/webkit_glue.h"
[email protected]efb5f572012-01-29 10:57:3321
22using WebKit::WebFrame;
23using WebKit::WebElement;
[email protected]0799da02012-06-27 10:58:5124using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0625using WebKit::WebSize;
[email protected]0799da02012-06-27 10:58:5126using WebKit::WebView;
[email protected]efb5f572012-01-29 10:57:3327
[email protected]31d71b02012-01-26 03:42:3128namespace content {
29
[email protected]efb5f572012-01-29 10:57:3330namespace {
31
32std::string DumpDocumentText(WebFrame* frame) {
33 // We use the document element's text instead of the body text here because
34 // not all documents have a body, such as XML documents.
35 WebElement documentElement = frame->document().documentElement();
36 if (documentElement.isNull())
37 return std::string();
38 return documentElement.innerText().utf8();
39}
40
[email protected]c272c5b2012-06-06 09:01:0641std::string DumpDocumentPrintedText(WebFrame* frame) {
42 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
43}
44
45std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
[email protected]efb5f572012-01-29 10:57:3346 std::string result;
47
[email protected]c272c5b2012-06-06 09:01:0648 // Cannot do printed format for anything other than HTML.
49 if (printing && !frame->document().isHTMLDocument())
50 return std::string();
51
[email protected]efb5f572012-01-29 10:57:3352 // Add header for all but the main frame. Skip emtpy frames.
53 if (frame->parent() && !frame->document().documentElement().isNull()) {
54 result.append("\n--------\nFrame: '");
55 result.append(frame->name().utf8().data());
56 result.append("'\n--------\n");
57 }
58
[email protected]c272c5b2012-06-06 09:01:0659 result.append(
60 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
[email protected]efb5f572012-01-29 10:57:3361 result.append("\n");
62
63 if (recursive) {
64 for (WebFrame* child = frame->firstChild(); child;
65 child = child->nextSibling()) {
[email protected]c272c5b2012-06-06 09:01:0666 result.append(DumpFramesAsText(child, printing, recursive));
67 }
68 }
69 return result;
70}
71
72std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) {
73 std::string result;
74
75 WebSize offset = frame->scrollOffset();
76 if (offset.width > 0 || offset.height > 0) {
77 if (frame->parent()) {
78 result.append(
79 base::StringPrintf("frame '%s' ", frame->name().utf8().data()));
80 }
81 result.append(
82 base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height));
83 }
84
85 if (recursive) {
86 for (WebFrame* child = frame->firstChild(); child;
87 child = child->nextSibling()) {
88 result.append(DumpFrameScrollPosition(child, recursive));
[email protected]efb5f572012-01-29 10:57:3389 }
90 }
91 return result;
92}
93
[email protected]0799da02012-06-27 10:58:5194bool PaintViewIntoCanvas(WebView* view, skia::PlatformCanvas& canvas) {
95 view->layout();
96 const WebSize& size = view->size();
97
98 if (!canvas.initialize(size.width, size.height, true))
99 return false;
100
101 view->paint(webkit_glue::ToWebCanvas(&canvas),
102 WebRect(0, 0, size.width, size.height));
103 return true;
104}
105
106#if !defined(OS_MACOSX)
107void MakeBitmapOpaque(SkBitmap* bitmap) {
108 SkAutoLockPixels lock(*bitmap);
109 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
110 for (int y = 0; y < bitmap->height(); ++y) {
111 uint32_t* row = bitmap->getAddr32(0, y);
112 for (int x = 0; x < bitmap->width(); ++x)
113 row[x] |= 0xFF000000; // Set alpha bits to 1.
114 }
115}
116#endif
117
118void CaptureSnapshot(WebView* view, SkBitmap* snapshot) {
119 skia::PlatformCanvas canvas;
120 if (!PaintViewIntoCanvas(view, canvas))
121 return;
122
123 SkDevice* device = skia::GetTopDevice(canvas);
124
125 const SkBitmap& bitmap = device->accessBitmap(false);
126 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
127
128#if !defined(OS_MACOSX)
129 // Only the expected PNGs for Mac have a valid alpha channel.
130 MakeBitmapOpaque(snapshot);
131#endif
132
133}
134
[email protected]efb5f572012-01-29 10:57:33135} // namespace
[email protected]cccb5cf2012-06-06 22:20:04136
137LayoutTestController::LayoutTestController(RenderView* render_view)
[email protected]31d71b02012-01-26 03:42:31138 : RenderViewObserver(render_view) {
139}
140
[email protected]cccb5cf2012-06-06 22:20:04141LayoutTestController::~LayoutTestController() {
[email protected]31d71b02012-01-26 03:42:31142}
143
[email protected]cccb5cf2012-06-06 22:20:04144void LayoutTestController::DidFinishLoad(WebFrame* frame) {
[email protected]c272c5b2012-06-06 09:01:06145 if (!frame->parent())
146 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
147}
148
[email protected]cccb5cf2012-06-06 22:20:04149bool LayoutTestController::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33150 bool handled = true;
[email protected]cccb5cf2012-06-06 22:20:04151 IPC_BEGIN_MESSAGE_MAP(LayoutTestController, message)
[email protected]efb5f572012-01-29 10:57:33152 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
[email protected]0799da02012-06-27 10:58:51153 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
[email protected]efb5f572012-01-29 10:57:33154 IPC_MESSAGE_UNHANDLED(handled = false)
155 IPC_END_MESSAGE_MAP()
156
157 return handled;
158}
159
[email protected]cccb5cf2012-06-06 22:20:04160void LayoutTestController::OnCaptureTextDump(bool as_text,
161 bool printing,
162 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06163 WebFrame* frame = render_view()->GetWebView()->mainFrame();
164 std::string dump;
165 if (as_text) {
166 dump = DumpFramesAsText(frame, printing, recursive);
167 } else {
168 WebFrame::RenderAsTextControls render_text_behavior =
169 WebFrame::RenderAsTextNormal;
170 if (printing)
171 render_text_behavior |= WebFrame::RenderAsTextPrinting;
172 dump = frame->renderTreeAsText(render_text_behavior).utf8();
173 dump.append(DumpFrameScrollPosition(frame, recursive));
174 }
[email protected]efb5f572012-01-29 10:57:33175 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31176}
177
[email protected]0799da02012-06-27 10:58:51178void LayoutTestController::OnCaptureImageDump(
179 const std::string& expected_pixel_hash) {
180 SkBitmap snapshot;
181 CaptureSnapshot(render_view()->GetWebView(), &snapshot);
182
183 SkAutoLockPixels snapshot_lock(snapshot);
184 base::MD5Digest digest;
185 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
186 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
187
188 if (actual_pixel_hash == expected_pixel_hash) {
189 SkBitmap empty_image;
190 Send(new ShellViewHostMsg_ImageDump(
191 routing_id(), actual_pixel_hash, empty_image));
192 }
193 Send(new ShellViewHostMsg_ImageDump(
194 routing_id(), actual_pixel_hash, snapshot));
195}
196
[email protected]31d71b02012-01-26 03:42:31197} // namespace content