blob: 2ec0d3a09645514d2725baa55bc73c19aa641c47 [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"
[email protected]96b80b472012-07-03 19:41:5619#include "third_party/WebKit/Source/WebKit/chromium/public/WebTestingSupport.h"
[email protected]efb5f572012-01-29 10:57:3320#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]0799da02012-06-27 10:58:5121#include "webkit/glue/webkit_glue.h"
[email protected]efb5f572012-01-29 10:57:3322
23using WebKit::WebFrame;
24using WebKit::WebElement;
[email protected]0799da02012-06-27 10:58:5125using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0626using WebKit::WebSize;
[email protected]96b80b472012-07-03 19:41:5627using WebKit::WebTestingSupport;
[email protected]0799da02012-06-27 10:58:5128using WebKit::WebView;
[email protected]efb5f572012-01-29 10:57:3329
[email protected]31d71b02012-01-26 03:42:3130namespace content {
31
[email protected]efb5f572012-01-29 10:57:3332namespace {
33
34std::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]c272c5b2012-06-06 09:01:0643std::string DumpDocumentPrintedText(WebFrame* frame) {
44 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
45}
46
47std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
[email protected]efb5f572012-01-29 10:57:3348 std::string result;
49
[email protected]c272c5b2012-06-06 09:01:0650 // Cannot do printed format for anything other than HTML.
51 if (printing && !frame->document().isHTMLDocument())
52 return std::string();
53
[email protected]efb5f572012-01-29 10:57:3354 // 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]c272c5b2012-06-06 09:01:0661 result.append(
62 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
[email protected]efb5f572012-01-29 10:57:3363 result.append("\n");
64
65 if (recursive) {
66 for (WebFrame* child = frame->firstChild(); child;
67 child = child->nextSibling()) {
[email protected]c272c5b2012-06-06 09:01:0668 result.append(DumpFramesAsText(child, printing, recursive));
69 }
70 }
71 return result;
72}
73
74std::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]efb5f572012-01-29 10:57:3391 }
92 }
93 return result;
94}
95
[email protected]0799da02012-06-27 10:58:5196bool 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)
109void 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
120void 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]efb5f572012-01-29 10:57:33137} // namespace
[email protected]cccb5cf2012-06-06 22:20:04138
139LayoutTestController::LayoutTestController(RenderView* render_view)
[email protected]31d71b02012-01-26 03:42:31140 : RenderViewObserver(render_view) {
141}
142
[email protected]cccb5cf2012-06-06 22:20:04143LayoutTestController::~LayoutTestController() {
[email protected]31d71b02012-01-26 03:42:31144}
145
[email protected]96b80b472012-07-03 19:41:56146void LayoutTestController::DidClearWindowObject(WebFrame* frame) {
147 WebTestingSupport::injectInternalsObject(frame);
148}
149
[email protected]cccb5cf2012-06-06 22:20:04150void LayoutTestController::DidFinishLoad(WebFrame* frame) {
[email protected]c272c5b2012-06-06 09:01:06151 if (!frame->parent())
152 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
153}
154
[email protected]cccb5cf2012-06-06 22:20:04155bool LayoutTestController::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33156 bool handled = true;
[email protected]cccb5cf2012-06-06 22:20:04157 IPC_BEGIN_MESSAGE_MAP(LayoutTestController, message)
[email protected]efb5f572012-01-29 10:57:33158 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
[email protected]0799da02012-06-27 10:58:51159 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
[email protected]efb5f572012-01-29 10:57:33160 IPC_MESSAGE_UNHANDLED(handled = false)
161 IPC_END_MESSAGE_MAP()
162
163 return handled;
164}
165
[email protected]cccb5cf2012-06-06 22:20:04166void LayoutTestController::OnCaptureTextDump(bool as_text,
167 bool printing,
168 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06169 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]efb5f572012-01-29 10:57:33181 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31182}
183
[email protected]0799da02012-06-27 10:58:51184void 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]31d71b02012-01-26 03:42:31203} // namespace content