blob: 63f430765ce853eb07eff95c451eb382f4fba3b7 [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]9b4c6cd2012-08-20 10:47:455#include "content/shell/webkit_test_runner.h"
[email protected]31d71b02012-01-26 03:42:316
[email protected]0799da02012-06-27 10:58:517#include "base/md5.h"
[email protected]e225b922012-08-18 01:43:048#include "base/memory/scoped_ptr.h"
[email protected]c272c5b2012-06-06 09:01:069#include "base/stringprintf.h"
[email protected]efb5f572012-01-29 10:57:3310#include "content/public/renderer/render_view.h"
11#include "content/shell/shell_messages.h"
[email protected]0799da02012-06-27 10:58:5112#include "skia/ext/platform_canvas.h"
[email protected]efb5f572012-01-29 10:57:3313#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h"
[email protected]0799da02012-06-27 10:58:5114#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]efb5f572012-01-29 10:57:3316#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]96b80b472012-07-03 19:41:5620#include "third_party/WebKit/Source/WebKit/chromium/public/WebTestingSupport.h"
[email protected]efb5f572012-01-29 10:57:3321#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]0799da02012-06-27 10:58:5122#include "webkit/glue/webkit_glue.h"
[email protected]efb5f572012-01-29 10:57:3323
24using WebKit::WebFrame;
25using WebKit::WebElement;
[email protected]0799da02012-06-27 10:58:5126using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0627using WebKit::WebSize;
[email protected]96b80b472012-07-03 19:41:5628using WebKit::WebTestingSupport;
[email protected]0799da02012-06-27 10:58:5129using WebKit::WebView;
[email protected]efb5f572012-01-29 10:57:3330
[email protected]31d71b02012-01-26 03:42:3131namespace content {
32
[email protected]efb5f572012-01-29 10:57:3333namespace {
34
35std::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]c272c5b2012-06-06 09:01:0644std::string DumpDocumentPrintedText(WebFrame* frame) {
45 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
46}
47
48std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
[email protected]efb5f572012-01-29 10:57:3349 std::string result;
50
[email protected]c272c5b2012-06-06 09:01:0651 // Cannot do printed format for anything other than HTML.
52 if (printing && !frame->document().isHTMLDocument())
53 return std::string();
54
[email protected]efb5f572012-01-29 10:57:3355 // 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]c272c5b2012-06-06 09:01:0662 result.append(
63 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
[email protected]efb5f572012-01-29 10:57:3364 result.append("\n");
65
66 if (recursive) {
67 for (WebFrame* child = frame->firstChild(); child;
68 child = child->nextSibling()) {
[email protected]c272c5b2012-06-06 09:01:0669 result.append(DumpFramesAsText(child, printing, recursive));
70 }
71 }
72 return result;
73}
74
75std::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]efb5f572012-01-29 10:57:3392 }
93 }
94 return result;
95}
96
[email protected]0799da02012-06-27 10:58:5197bool 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)
110void 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
121void 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]efb5f572012-01-29 10:57:33138} // namespace
[email protected]cccb5cf2012-06-06 22:20:04139
[email protected]9b4c6cd2012-08-20 10:47:45140WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]31d71b02012-01-26 03:42:31141 : RenderViewObserver(render_view) {
142}
143
[email protected]9b4c6cd2012-08-20 10:47:45144WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31145}
146
[email protected]9b4c6cd2012-08-20 10:47:45147void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]96b80b472012-07-03 19:41:56148 WebTestingSupport::injectInternalsObject(frame);
149}
150
[email protected]9b4c6cd2012-08-20 10:47:45151void WebKitTestRunner::DidFinishLoad(WebFrame* frame) {
[email protected]c272c5b2012-06-06 09:01:06152 if (!frame->parent())
153 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
154}
155
[email protected]9b4c6cd2012-08-20 10:47:45156bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33157 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45158 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]efb5f572012-01-29 10:57:33159 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
[email protected]0799da02012-06-27 10:58:51160 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
[email protected]efb5f572012-01-29 10:57:33161 IPC_MESSAGE_UNHANDLED(handled = false)
162 IPC_END_MESSAGE_MAP()
163
164 return handled;
165}
166
[email protected]9b4c6cd2012-08-20 10:47:45167void WebKitTestRunner::OnCaptureTextDump(bool as_text,
[email protected]cccb5cf2012-06-06 22:20:04168 bool printing,
169 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06170 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]efb5f572012-01-29 10:57:33182 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31183}
184
[email protected]9b4c6cd2012-08-20 10:47:45185void WebKitTestRunner::OnCaptureImageDump(
[email protected]0799da02012-06-27 10:58:51186 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]e225b922012-08-18 01:43:04192#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]0799da02012-06-27 10:58:51206 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
[email protected]e225b922012-08-18 01:43:04207#endif
[email protected]0799da02012-06-27 10:58:51208 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]e225b922012-08-18 01:43:04214 return;
[email protected]0799da02012-06-27 10:58:51215 }
216 Send(new ShellViewHostMsg_ImageDump(
217 routing_id(), actual_pixel_hash, snapshot));
218}
219
[email protected]31d71b02012-01-26 03:42:31220} // namespace content