blob: d21bdab0aeb59216336b8dc93cc3011ad1e701e9 [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]b9fad24c2012-11-15 08:22:107#include <cmath>
8
[email protected]0799da02012-06-27 10:58:519#include "base/md5.h"
[email protected]e225b922012-08-18 01:43:0410#include "base/memory/scoped_ptr.h"
[email protected]b2324b092012-11-01 10:34:1111#include "base/message_loop.h"
[email protected]c272c5b2012-06-06 09:01:0612#include "base/stringprintf.h"
[email protected]0d2dfb92d2012-11-05 10:26:4513#include "base/sys_string_conversions.h"
[email protected]b2324b092012-11-01 10:34:1114#include "base/time.h"
[email protected]0d2dfb92d2012-11-05 10:26:4515#include "base/utf_string_conversions.h"
[email protected]efb5f572012-01-29 10:57:3316#include "content/public/renderer/render_view.h"
17#include "content/shell/shell_messages.h"
[email protected]b2324b092012-11-01 10:34:1118#include "content/shell/shell_render_process_observer.h"
[email protected]0d2dfb92d2012-11-05 10:26:4519#include "net/base/net_util.h"
[email protected]0799da02012-06-27 10:58:5120#include "skia/ext/platform_canvas.h"
[email protected]efb5f572012-01-29 10:57:3321#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h"
[email protected]b2324b092012-11-01 10:34:1122#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatformSupport.h"
[email protected]f89ce5a2012-10-19 01:07:0223#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]0799da02012-06-27 10:58:5124#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
[email protected]efb5f572012-01-29 10:57:3325#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]b2324b092012-11-01 10:34:1126#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
[email protected]efb5f572012-01-29 10:57:3327#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
28#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
29#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
[email protected]b2324b092012-11-01 10:34:1130#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]efb5f572012-01-29 10:57:3331#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b2324b092012-11-01 10:34:1132#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]b9fad24c2012-11-15 08:22:1033#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]0799da02012-06-27 10:58:5134#include "webkit/glue/webkit_glue.h"
[email protected]efb5f572012-01-29 10:57:3335
[email protected]b2324b092012-11-01 10:34:1136using WebKit::WebContextMenuData;
[email protected]efb5f572012-01-29 10:57:3337using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1138using WebKit::WebFrame;
39using WebKit::WebGamepads;
[email protected]0799da02012-06-27 10:58:5140using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0641using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1142using WebKit::WebString;
43using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5144using WebKit::WebView;
[email protected]b2324b092012-11-01 10:34:1145using WebTestRunner::WebTask;
[email protected]efb5f572012-01-29 10:57:3346
[email protected]31d71b02012-01-26 03:42:3147namespace content {
48
[email protected]efb5f572012-01-29 10:57:3349namespace {
50
[email protected]b2324b092012-11-01 10:34:1151void InvokeTaskHelper(void* context) {
52 WebTask* task = reinterpret_cast<WebTask*>(context);
53 task->run();
54 delete task;
55}
56
[email protected]efb5f572012-01-29 10:57:3357std::string DumpDocumentText(WebFrame* frame) {
58 // We use the document element's text instead of the body text here because
59 // not all documents have a body, such as XML documents.
60 WebElement documentElement = frame->document().documentElement();
61 if (documentElement.isNull())
62 return std::string();
63 return documentElement.innerText().utf8();
64}
65
[email protected]c272c5b2012-06-06 09:01:0666std::string DumpDocumentPrintedText(WebFrame* frame) {
67 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
68}
69
70std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
[email protected]efb5f572012-01-29 10:57:3371 std::string result;
72
[email protected]c272c5b2012-06-06 09:01:0673 // Cannot do printed format for anything other than HTML.
74 if (printing && !frame->document().isHTMLDocument())
75 return std::string();
76
[email protected]efb5f572012-01-29 10:57:3377 // Add header for all but the main frame. Skip emtpy frames.
78 if (frame->parent() && !frame->document().documentElement().isNull()) {
79 result.append("\n--------\nFrame: '");
[email protected]6d454d92012-09-13 17:06:2980 result.append(frame->uniqueName().utf8().data());
[email protected]efb5f572012-01-29 10:57:3381 result.append("'\n--------\n");
82 }
83
[email protected]c272c5b2012-06-06 09:01:0684 result.append(
85 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
[email protected]efb5f572012-01-29 10:57:3386 result.append("\n");
87
88 if (recursive) {
89 for (WebFrame* child = frame->firstChild(); child;
90 child = child->nextSibling()) {
[email protected]c272c5b2012-06-06 09:01:0691 result.append(DumpFramesAsText(child, printing, recursive));
92 }
93 }
94 return result;
95}
96
97std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) {
98 std::string result;
99
100 WebSize offset = frame->scrollOffset();
101 if (offset.width > 0 || offset.height > 0) {
102 if (frame->parent()) {
103 result.append(
[email protected]6d454d92012-09-13 17:06:29104 base::StringPrintf("frame '%s' ", frame->uniqueName().utf8().data()));
[email protected]c272c5b2012-06-06 09:01:06105 }
106 result.append(
107 base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height));
108 }
109
110 if (recursive) {
111 for (WebFrame* child = frame->firstChild(); child;
112 child = child->nextSibling()) {
113 result.append(DumpFrameScrollPosition(child, recursive));
[email protected]efb5f572012-01-29 10:57:33114 }
115 }
116 return result;
117}
118
[email protected]0799da02012-06-27 10:58:51119#if !defined(OS_MACOSX)
120void MakeBitmapOpaque(SkBitmap* bitmap) {
121 SkAutoLockPixels lock(*bitmap);
122 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
123 for (int y = 0; y < bitmap->height(); ++y) {
124 uint32_t* row = bitmap->getAddr32(0, y);
125 for (int x = 0; x < bitmap->width(); ++x)
126 row[x] |= 0xFF000000; // Set alpha bits to 1.
127 }
128}
129#endif
130
[email protected]b9fad24c2012-11-15 08:22:10131void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56132 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51133 const SkBitmap& bitmap = device->accessBitmap(false);
134 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
135
136#if !defined(OS_MACOSX)
137 // Only the expected PNGs for Mac have a valid alpha channel.
138 MakeBitmapOpaque(snapshot);
139#endif
140
141}
142
[email protected]efb5f572012-01-29 10:57:33143} // namespace
[email protected]cccb5cf2012-06-06 22:20:04144
[email protected]9b4c6cd2012-08-20 10:47:45145WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]b2324b092012-11-01 10:34:11146 : RenderViewObserver(render_view),
[email protected]b9fad24c2012-11-15 08:22:10147 RenderViewObserverTracker<WebKitTestRunner>(render_view),
[email protected]b2324b092012-11-01 10:34:11148 is_main_window_(false) {
[email protected]31d71b02012-01-26 03:42:31149}
150
[email protected]9b4c6cd2012-08-20 10:47:45151WebKitTestRunner::~WebKitTestRunner() {
[email protected]b2324b092012-11-01 10:34:11152 if (is_main_window_)
153 ShellRenderProcessObserver::GetInstance()->SetMainWindow(NULL, this);
[email protected]31d71b02012-01-26 03:42:31154}
155
[email protected]b2324b092012-11-01 10:34:11156// WebTestDelegate -----------------------------------------------------------
157
158void WebKitTestRunner::clearContextMenuData() {
159 last_context_menu_data_.reset();
160}
161
162WebContextMenuData* WebKitTestRunner::lastContextMenuData() const {
163 return last_context_menu_data_.get();
164}
165
166void WebKitTestRunner::clearEditCommand() {
167 render_view()->ClearEditCommands();
168}
169
170void WebKitTestRunner::setEditCommand(const std::string& name,
171 const std::string& value) {
172 render_view()->SetEditCommandForNextKeyEvent(name, value);
173}
174
175void WebKitTestRunner::fillSpellingSuggestionList(
176 const WebString& word, WebVector<WebString>* suggestions) {
177 if (word == WebString::fromUTF8("wellcome")) {
178 WebVector<WebString> result(suggestions->size() + 1);
179 for (size_t i = 0; i < suggestions->size(); ++i)
180 result[i] = (*suggestions)[i];
181 result[suggestions->size()] = WebString::fromUTF8("welcome");
182 suggestions->swap(result);
183 }
184}
185
186void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
187 Send(new ShellViewHostMsg_NotImplemented(
188 routing_id(), "WebTestDelegate", "setGamepadData"));
189}
190
191void WebKitTestRunner::printMessage(const std::string& message) {
192 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
193}
194
195void WebKitTestRunner::postTask(WebTask* task) {
196 WebKit::webKitPlatformSupport()->callOnMainThread(InvokeTaskHelper, task);
197}
198
199void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
200 MessageLoop::current()->PostDelayedTask(
201 FROM_HERE,
202 base::Bind(&WebTask::run, base::Owned(task)),
203 base::TimeDelta::FromMilliseconds(ms));
204}
205
206WebString WebKitTestRunner::registerIsolatedFileSystem(
207 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
208 Send(new ShellViewHostMsg_NotImplemented(
209 routing_id(), "WebTestDelegate", "registerIsolatedFileSystem"));
210 return WebString();
211}
212
213long long WebKitTestRunner::getCurrentTimeInMillisecond() {
214 return base::TimeTicks::Now().ToInternalValue() /
215 base::Time::kMicrosecondsPerMillisecond;
216}
217
218WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
219 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45220#if defined(OS_WIN)
221 FilePath path(UTF8ToWide(utf8_path));
222#else
223 FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
224#endif
225 if (!path.IsAbsolute()) {
226 GURL base_url =
227 net::FilePathToFileURL(current_working_directory_.Append(
228 FILE_PATH_LITERAL("foo")));
229 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
230 }
231#if defined(OS_WIN)
232 return WebString(path.value());
233#else
234 return WideToUTF16(base::SysNativeMBToWide(path.value()));
235#endif
[email protected]b2324b092012-11-01 10:34:11236}
237
238// RenderViewObserver --------------------------------------------------------
239
[email protected]9b4c6cd2012-08-20 10:47:45240void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11241 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56242}
243
[email protected]9b4c6cd2012-08-20 10:47:45244void WebKitTestRunner::DidFinishLoad(WebFrame* frame) {
[email protected]c272c5b2012-06-06 09:01:06245 if (!frame->parent())
246 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
247}
248
[email protected]b2324b092012-11-01 10:34:11249void WebKitTestRunner::DidRequestShowContextMenu(
250 WebFrame* frame,
251 const WebContextMenuData& data) {
252 last_context_menu_data_.reset(new WebContextMenuData(data));
253}
254
[email protected]9b4c6cd2012-08-20 10:47:45255bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33256 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45257 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]efb5f572012-01-29 10:57:33258 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
[email protected]0799da02012-06-27 10:58:51259 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
[email protected]0d2dfb92d2012-11-05 10:26:45260 IPC_MESSAGE_HANDLER(ShellViewMsg_SetCurrentWorkingDirectory,
261 OnSetCurrentWorkingDirectory)
[email protected]b2324b092012-11-01 10:34:11262 IPC_MESSAGE_HANDLER(ShellViewMsg_SetIsMainWindow, OnSetIsMainWindow)
[email protected]efb5f572012-01-29 10:57:33263 IPC_MESSAGE_UNHANDLED(handled = false)
264 IPC_END_MESSAGE_MAP()
265
266 return handled;
267}
268
[email protected]b9fad24c2012-11-15 08:22:10269// Public methods - -----------------------------------------------------------
270
271void WebKitTestRunner::Display() {
272 const WebSize& size = render_view()->GetWebView()->size();
273 WebRect rect(0, 0, size.width, size.height);
274 proxy_->setPaintRect(rect);
275 PaintInvalidatedRegion();
276 DisplayRepaintMask();
277}
278
[email protected]b2324b092012-11-01 10:34:11279// Private methods -----------------------------------------------------------
280
[email protected]9b4c6cd2012-08-20 10:47:45281void WebKitTestRunner::OnCaptureTextDump(bool as_text,
[email protected]891f08f2012-08-28 22:00:44282 bool printing,
283 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06284 WebFrame* frame = render_view()->GetWebView()->mainFrame();
285 std::string dump;
286 if (as_text) {
287 dump = DumpFramesAsText(frame, printing, recursive);
288 } else {
289 WebFrame::RenderAsTextControls render_text_behavior =
290 WebFrame::RenderAsTextNormal;
291 if (printing)
292 render_text_behavior |= WebFrame::RenderAsTextPrinting;
293 dump = frame->renderTreeAsText(render_text_behavior).utf8();
294 dump.append(DumpFrameScrollPosition(frame, recursive));
295 }
[email protected]efb5f572012-01-29 10:57:33296 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31297}
298
[email protected]9b4c6cd2012-08-20 10:47:45299void WebKitTestRunner::OnCaptureImageDump(
[email protected]0799da02012-06-27 10:58:51300 const std::string& expected_pixel_hash) {
301 SkBitmap snapshot;
[email protected]b9fad24c2012-11-15 08:22:10302 PaintInvalidatedRegion();
303 CopyCanvasToBitmap(GetCanvas(), &snapshot);
[email protected]0799da02012-06-27 10:58:51304
305 SkAutoLockPixels snapshot_lock(snapshot);
306 base::MD5Digest digest;
[email protected]e225b922012-08-18 01:43:04307#if defined(OS_ANDROID)
308 // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA.
309 const uint8_t* raw_pixels =
310 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
311 size_t snapshot_size = snapshot.getSize();
312 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
313 for (size_t i = 0; i < snapshot_size; i += 4) {
314 reordered_pixels[i] = raw_pixels[i + 2];
315 reordered_pixels[i + 1] = raw_pixels[i + 1];
316 reordered_pixels[i + 2] = raw_pixels[i];
317 reordered_pixels[i + 3] = raw_pixels[i + 3];
318 }
319 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
320#else
[email protected]0799da02012-06-27 10:58:51321 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
[email protected]e225b922012-08-18 01:43:04322#endif
[email protected]0799da02012-06-27 10:58:51323 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
324
325 if (actual_pixel_hash == expected_pixel_hash) {
326 SkBitmap empty_image;
327 Send(new ShellViewHostMsg_ImageDump(
328 routing_id(), actual_pixel_hash, empty_image));
[email protected]e225b922012-08-18 01:43:04329 return;
[email protected]0799da02012-06-27 10:58:51330 }
331 Send(new ShellViewHostMsg_ImageDump(
332 routing_id(), actual_pixel_hash, snapshot));
333}
334
[email protected]0d2dfb92d2012-11-05 10:26:45335void WebKitTestRunner::OnSetCurrentWorkingDirectory(
336 const FilePath& current_working_directory) {
337 current_working_directory_ = current_working_directory;
338}
339
[email protected]b2324b092012-11-01 10:34:11340void WebKitTestRunner::OnSetIsMainWindow() {
341 is_main_window_ = true;
342 ShellRenderProcessObserver::GetInstance()->SetMainWindow(render_view(), this);
343}
344
[email protected]b9fad24c2012-11-15 08:22:10345SkCanvas* WebKitTestRunner::GetCanvas() {
346 WebView* view = render_view()->GetWebView();
347 const WebSize& size = view->size();
348 float device_scale_factor = view->deviceScaleFactor();
349 int width = std::ceil(device_scale_factor * size.width);
350 int height = std::ceil(device_scale_factor * size.height);
351
352 if (canvas_ &&
353 canvas_->getDeviceSize().width() == width &&
354 canvas_->getDeviceSize().height() == height) {
355 return canvas_.get();
356 }
357 canvas_.reset(skia::CreatePlatformCanvas(
358 size.width, size.height, true, 0, skia::RETURN_NULL_ON_FAILURE));
359 return canvas_.get();
360}
361
362void WebKitTestRunner::PaintRect(const WebRect& rect) {
363 WebView* view = render_view()->GetWebView();
364 float device_scale_factor = view->deviceScaleFactor();
365 int scaled_x = device_scale_factor * rect.x;
366 int scaled_y = device_scale_factor * rect.y;
367 int scaled_width = std::ceil(device_scale_factor * rect.width);
368 int scaled_height = std::ceil(device_scale_factor * rect.height);
369 // TODO(jochen): Verify that the scaling is correct once the HiDPI tests
370 // actually work.
371 WebRect device_rect(scaled_x, scaled_y, scaled_width, scaled_height);
372 view->paint(webkit_glue::ToWebCanvas(GetCanvas()), device_rect);
373}
374
375void WebKitTestRunner::PaintInvalidatedRegion() {
376 WebView* view = render_view()->GetWebView();
377 view->animate(0.0);
378 view->layout();
379 const WebSize& widget_size = view->size();
380 WebRect client_rect(0, 0, widget_size.width, widget_size.height);
381
382 // Paint the canvas if necessary. Allow painting to generate extra rects
383 // for the first two calls. This is necessary because some WebCore rendering
384 // objects update their layout only when painted.
385 for (int i = 0; i < 3; ++i) {
386 // Make sure that paint_rect is always inside the RenderView's visible
387 // area.
388 WebRect paint_rect = proxy_->paintRect();
389 int left = std::max(paint_rect.x, client_rect.x);
390 int top = std::max(paint_rect.y, client_rect.y);
391 int right = std::min(paint_rect.x + paint_rect.width,
392 client_rect.x + client_rect.width);
393 int bottom = std::min(paint_rect.y + paint_rect.height,
394 client_rect.y + client_rect.height);
395 WebRect rect;
396 if (left < right && top < bottom)
397 rect = WebRect(left, top, right - left, bottom - top);
398 proxy_->setPaintRect(WebRect());
399 if (rect.isEmpty())
400 continue;
401 PaintRect(rect);
402 }
403 CHECK(proxy_->paintRect().isEmpty());
404}
405
406void WebKitTestRunner::DisplayRepaintMask() {
407 GetCanvas()->drawARGB(167, 0, 0, 0);
408}
409
[email protected]31d71b02012-01-26 03:42:31410} // namespace content