blob: e484d8dc1325c806a3e45ee78bccba681047736a [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]324825d2012-11-30 12:37:1535#include "webkit/glue/webpreferences.h"
[email protected]efb5f572012-01-29 10:57:3336
[email protected]b2324b092012-11-01 10:34:1137using WebKit::WebContextMenuData;
[email protected]efb5f572012-01-29 10:57:3338using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1139using WebKit::WebFrame;
40using WebKit::WebGamepads;
[email protected]0799da02012-06-27 10:58:5141using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0642using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1143using WebKit::WebString;
44using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5145using WebKit::WebView;
[email protected]b2324b092012-11-01 10:34:1146using WebTestRunner::WebTask;
[email protected]efb5f572012-01-29 10:57:3347
[email protected]31d71b02012-01-26 03:42:3148namespace content {
49
[email protected]efb5f572012-01-29 10:57:3350namespace {
51
[email protected]b2324b092012-11-01 10:34:1152void InvokeTaskHelper(void* context) {
53 WebTask* task = reinterpret_cast<WebTask*>(context);
54 task->run();
55 delete task;
56}
57
[email protected]efb5f572012-01-29 10:57:3358std::string DumpDocumentText(WebFrame* frame) {
59 // We use the document element's text instead of the body text here because
60 // not all documents have a body, such as XML documents.
61 WebElement documentElement = frame->document().documentElement();
62 if (documentElement.isNull())
63 return std::string();
64 return documentElement.innerText().utf8();
65}
66
[email protected]c272c5b2012-06-06 09:01:0667std::string DumpDocumentPrintedText(WebFrame* frame) {
68 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
69}
70
71std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
[email protected]efb5f572012-01-29 10:57:3372 std::string result;
73
[email protected]c272c5b2012-06-06 09:01:0674 // Cannot do printed format for anything other than HTML.
75 if (printing && !frame->document().isHTMLDocument())
76 return std::string();
77
[email protected]efb5f572012-01-29 10:57:3378 // Add header for all but the main frame. Skip emtpy frames.
79 if (frame->parent() && !frame->document().documentElement().isNull()) {
80 result.append("\n--------\nFrame: '");
[email protected]6d454d92012-09-13 17:06:2981 result.append(frame->uniqueName().utf8().data());
[email protected]efb5f572012-01-29 10:57:3382 result.append("'\n--------\n");
83 }
84
[email protected]c272c5b2012-06-06 09:01:0685 result.append(
86 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
[email protected]efb5f572012-01-29 10:57:3387 result.append("\n");
88
89 if (recursive) {
90 for (WebFrame* child = frame->firstChild(); child;
91 child = child->nextSibling()) {
[email protected]c272c5b2012-06-06 09:01:0692 result.append(DumpFramesAsText(child, printing, recursive));
93 }
94 }
95 return result;
96}
97
98std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) {
99 std::string result;
100
101 WebSize offset = frame->scrollOffset();
102 if (offset.width > 0 || offset.height > 0) {
103 if (frame->parent()) {
104 result.append(
[email protected]6d454d92012-09-13 17:06:29105 base::StringPrintf("frame '%s' ", frame->uniqueName().utf8().data()));
[email protected]c272c5b2012-06-06 09:01:06106 }
107 result.append(
108 base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height));
109 }
110
111 if (recursive) {
112 for (WebFrame* child = frame->firstChild(); child;
113 child = child->nextSibling()) {
114 result.append(DumpFrameScrollPosition(child, recursive));
[email protected]efb5f572012-01-29 10:57:33115 }
116 }
117 return result;
118}
119
[email protected]0799da02012-06-27 10:58:51120#if !defined(OS_MACOSX)
121void MakeBitmapOpaque(SkBitmap* bitmap) {
122 SkAutoLockPixels lock(*bitmap);
123 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
124 for (int y = 0; y < bitmap->height(); ++y) {
125 uint32_t* row = bitmap->getAddr32(0, y);
126 for (int x = 0; x < bitmap->width(); ++x)
127 row[x] |= 0xFF000000; // Set alpha bits to 1.
128 }
129}
130#endif
131
[email protected]b9fad24c2012-11-15 08:22:10132void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56133 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51134 const SkBitmap& bitmap = device->accessBitmap(false);
135 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
136
137#if !defined(OS_MACOSX)
138 // Only the expected PNGs for Mac have a valid alpha channel.
139 MakeBitmapOpaque(snapshot);
140#endif
141
142}
143
[email protected]efb5f572012-01-29 10:57:33144} // namespace
[email protected]cccb5cf2012-06-06 22:20:04145
[email protected]9b4c6cd2012-08-20 10:47:45146WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]2dd3a27a2012-12-10 11:24:16147 : RenderViewObserver(render_view) {
[email protected]31d71b02012-01-26 03:42:31148}
149
[email protected]9b4c6cd2012-08-20 10:47:45150WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31151}
152
[email protected]b2324b092012-11-01 10:34:11153// WebTestDelegate -----------------------------------------------------------
154
155void WebKitTestRunner::clearContextMenuData() {
156 last_context_menu_data_.reset();
157}
158
159WebContextMenuData* WebKitTestRunner::lastContextMenuData() const {
160 return last_context_menu_data_.get();
161}
162
163void WebKitTestRunner::clearEditCommand() {
164 render_view()->ClearEditCommands();
165}
166
167void WebKitTestRunner::setEditCommand(const std::string& name,
168 const std::string& value) {
169 render_view()->SetEditCommandForNextKeyEvent(name, value);
170}
171
172void WebKitTestRunner::fillSpellingSuggestionList(
173 const WebString& word, WebVector<WebString>* suggestions) {
174 if (word == WebString::fromUTF8("wellcome")) {
175 WebVector<WebString> result(suggestions->size() + 1);
176 for (size_t i = 0; i < suggestions->size(); ++i)
177 result[i] = (*suggestions)[i];
178 result[suggestions->size()] = WebString::fromUTF8("welcome");
179 suggestions->swap(result);
180 }
181}
182
183void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
184 Send(new ShellViewHostMsg_NotImplemented(
185 routing_id(), "WebTestDelegate", "setGamepadData"));
186}
187
188void WebKitTestRunner::printMessage(const std::string& message) {
189 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
190}
191
192void WebKitTestRunner::postTask(WebTask* task) {
193 WebKit::webKitPlatformSupport()->callOnMainThread(InvokeTaskHelper, task);
194}
195
196void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
197 MessageLoop::current()->PostDelayedTask(
198 FROM_HERE,
199 base::Bind(&WebTask::run, base::Owned(task)),
200 base::TimeDelta::FromMilliseconds(ms));
201}
202
203WebString WebKitTestRunner::registerIsolatedFileSystem(
204 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
205 Send(new ShellViewHostMsg_NotImplemented(
206 routing_id(), "WebTestDelegate", "registerIsolatedFileSystem"));
207 return WebString();
208}
209
210long long WebKitTestRunner::getCurrentTimeInMillisecond() {
211 return base::TimeTicks::Now().ToInternalValue() /
212 base::Time::kMicrosecondsPerMillisecond;
213}
214
215WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
216 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45217#if defined(OS_WIN)
218 FilePath path(UTF8ToWide(utf8_path));
219#else
220 FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
221#endif
222 if (!path.IsAbsolute()) {
223 GURL base_url =
224 net::FilePathToFileURL(current_working_directory_.Append(
225 FILE_PATH_LITERAL("foo")));
226 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
227 }
228#if defined(OS_WIN)
229 return WebString(path.value());
230#else
231 return WideToUTF16(base::SysNativeMBToWide(path.value()));
232#endif
[email protected]b2324b092012-11-01 10:34:11233}
234
235// RenderViewObserver --------------------------------------------------------
236
[email protected]9b4c6cd2012-08-20 10:47:45237void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11238 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56239}
240
[email protected]9b4c6cd2012-08-20 10:47:45241void WebKitTestRunner::DidFinishLoad(WebFrame* frame) {
[email protected]c272c5b2012-06-06 09:01:06242 if (!frame->parent())
243 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
244}
245
[email protected]b2324b092012-11-01 10:34:11246void WebKitTestRunner::DidRequestShowContextMenu(
247 WebFrame* frame,
248 const WebContextMenuData& data) {
249 last_context_menu_data_.reset(new WebContextMenuData(data));
250}
251
[email protected]9b4c6cd2012-08-20 10:47:45252bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33253 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45254 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]efb5f572012-01-29 10:57:33255 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
[email protected]0799da02012-06-27 10:58:51256 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
[email protected]0d2dfb92d2012-11-05 10:26:45257 IPC_MESSAGE_HANDLER(ShellViewMsg_SetCurrentWorkingDirectory,
258 OnSetCurrentWorkingDirectory)
[email protected]efb5f572012-01-29 10:57:33259 IPC_MESSAGE_UNHANDLED(handled = false)
260 IPC_END_MESSAGE_MAP()
261
262 return handled;
263}
264
[email protected]b9fad24c2012-11-15 08:22:10265// Public methods - -----------------------------------------------------------
266
267void WebKitTestRunner::Display() {
268 const WebSize& size = render_view()->GetWebView()->size();
269 WebRect rect(0, 0, size.width, size.height);
270 proxy_->setPaintRect(rect);
271 PaintInvalidatedRegion();
272 DisplayRepaintMask();
273}
274
[email protected]324825d2012-11-30 12:37:15275void WebKitTestRunner::SetXSSAuditorEnabled(bool enabled) {
276 prefs_.xss_auditor_enabled = enabled;
277 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
278 prefs_.Apply(&prefs);
279 render_view()->SetWebkitPreferences(prefs);
280 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs_));
281}
282
[email protected]2dd3a27a2012-12-10 11:24:16283void WebKitTestRunner::NotifyDone() {
284 Send(new ShellViewHostMsg_NotifyDone(routing_id()));
285}
286
287void WebKitTestRunner::DumpAsText() {
288 Send(new ShellViewHostMsg_DumpAsText(routing_id()));
289}
290
291void WebKitTestRunner::DumpChildFramesAsText() {
292 Send(new ShellViewHostMsg_DumpChildFramesAsText(routing_id()));
293}
294
295void WebKitTestRunner::SetPrinting() {
296 Send(new ShellViewHostMsg_SetPrinting(routing_id()));
297}
298
299void WebKitTestRunner::SetShouldStayOnPageAfterHandlingBeforeUnload(
300 bool should_stay_on_page) {
301 Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload(
302 routing_id(), should_stay_on_page));
303}
304
305void WebKitTestRunner::WaitUntilDone() {
306 Send(new ShellViewHostMsg_WaitUntilDone(routing_id()));
307}
308
[email protected]d99d65a2012-12-10 17:40:27309void WebKitTestRunner::CanOpenWindows() {
310 Send(new ShellViewHostMsg_CanOpenWindows(routing_id()));
311}
312
[email protected]2dd3a27a2012-12-10 11:24:16313void WebKitTestRunner::NotImplemented(const char* object, const char* method) {
314 Send(new ShellViewHostMsg_NotImplemented(routing_id(), object, method));
315}
316
[email protected]324825d2012-11-30 12:37:15317void WebKitTestRunner::Reset() {
318 prefs_ = ShellWebPreferences();
319 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
320 prefs_.Apply(&prefs);
321 render_view()->SetWebkitPreferences(prefs);
322}
323
[email protected]b2324b092012-11-01 10:34:11324// Private methods -----------------------------------------------------------
325
[email protected]9b4c6cd2012-08-20 10:47:45326void WebKitTestRunner::OnCaptureTextDump(bool as_text,
[email protected]891f08f2012-08-28 22:00:44327 bool printing,
328 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06329 WebFrame* frame = render_view()->GetWebView()->mainFrame();
330 std::string dump;
331 if (as_text) {
332 dump = DumpFramesAsText(frame, printing, recursive);
333 } else {
334 WebFrame::RenderAsTextControls render_text_behavior =
335 WebFrame::RenderAsTextNormal;
336 if (printing)
337 render_text_behavior |= WebFrame::RenderAsTextPrinting;
338 dump = frame->renderTreeAsText(render_text_behavior).utf8();
339 dump.append(DumpFrameScrollPosition(frame, recursive));
340 }
[email protected]efb5f572012-01-29 10:57:33341 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31342}
343
[email protected]9b4c6cd2012-08-20 10:47:45344void WebKitTestRunner::OnCaptureImageDump(
[email protected]0799da02012-06-27 10:58:51345 const std::string& expected_pixel_hash) {
346 SkBitmap snapshot;
[email protected]b9fad24c2012-11-15 08:22:10347 PaintInvalidatedRegion();
348 CopyCanvasToBitmap(GetCanvas(), &snapshot);
[email protected]0799da02012-06-27 10:58:51349
350 SkAutoLockPixels snapshot_lock(snapshot);
351 base::MD5Digest digest;
[email protected]e225b922012-08-18 01:43:04352#if defined(OS_ANDROID)
353 // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA.
354 const uint8_t* raw_pixels =
355 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
356 size_t snapshot_size = snapshot.getSize();
357 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
358 for (size_t i = 0; i < snapshot_size; i += 4) {
359 reordered_pixels[i] = raw_pixels[i + 2];
360 reordered_pixels[i + 1] = raw_pixels[i + 1];
361 reordered_pixels[i + 2] = raw_pixels[i];
362 reordered_pixels[i + 3] = raw_pixels[i + 3];
363 }
364 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
365#else
[email protected]0799da02012-06-27 10:58:51366 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
[email protected]e225b922012-08-18 01:43:04367#endif
[email protected]0799da02012-06-27 10:58:51368 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
369
370 if (actual_pixel_hash == expected_pixel_hash) {
371 SkBitmap empty_image;
372 Send(new ShellViewHostMsg_ImageDump(
373 routing_id(), actual_pixel_hash, empty_image));
[email protected]e225b922012-08-18 01:43:04374 return;
[email protected]0799da02012-06-27 10:58:51375 }
376 Send(new ShellViewHostMsg_ImageDump(
377 routing_id(), actual_pixel_hash, snapshot));
378}
379
[email protected]0d2dfb92d2012-11-05 10:26:45380void WebKitTestRunner::OnSetCurrentWorkingDirectory(
381 const FilePath& current_working_directory) {
382 current_working_directory_ = current_working_directory;
383}
384
[email protected]b9fad24c2012-11-15 08:22:10385SkCanvas* WebKitTestRunner::GetCanvas() {
386 WebView* view = render_view()->GetWebView();
387 const WebSize& size = view->size();
388 float device_scale_factor = view->deviceScaleFactor();
389 int width = std::ceil(device_scale_factor * size.width);
390 int height = std::ceil(device_scale_factor * size.height);
391
392 if (canvas_ &&
393 canvas_->getDeviceSize().width() == width &&
394 canvas_->getDeviceSize().height() == height) {
395 return canvas_.get();
396 }
397 canvas_.reset(skia::CreatePlatformCanvas(
398 size.width, size.height, true, 0, skia::RETURN_NULL_ON_FAILURE));
399 return canvas_.get();
400}
401
402void WebKitTestRunner::PaintRect(const WebRect& rect) {
403 WebView* view = render_view()->GetWebView();
404 float device_scale_factor = view->deviceScaleFactor();
405 int scaled_x = device_scale_factor * rect.x;
406 int scaled_y = device_scale_factor * rect.y;
407 int scaled_width = std::ceil(device_scale_factor * rect.width);
408 int scaled_height = std::ceil(device_scale_factor * rect.height);
409 // TODO(jochen): Verify that the scaling is correct once the HiDPI tests
410 // actually work.
411 WebRect device_rect(scaled_x, scaled_y, scaled_width, scaled_height);
412 view->paint(webkit_glue::ToWebCanvas(GetCanvas()), device_rect);
413}
414
415void WebKitTestRunner::PaintInvalidatedRegion() {
416 WebView* view = render_view()->GetWebView();
417 view->animate(0.0);
418 view->layout();
419 const WebSize& widget_size = view->size();
420 WebRect client_rect(0, 0, widget_size.width, widget_size.height);
421
422 // Paint the canvas if necessary. Allow painting to generate extra rects
423 // for the first two calls. This is necessary because some WebCore rendering
424 // objects update their layout only when painted.
425 for (int i = 0; i < 3; ++i) {
426 // Make sure that paint_rect is always inside the RenderView's visible
427 // area.
428 WebRect paint_rect = proxy_->paintRect();
429 int left = std::max(paint_rect.x, client_rect.x);
430 int top = std::max(paint_rect.y, client_rect.y);
431 int right = std::min(paint_rect.x + paint_rect.width,
432 client_rect.x + client_rect.width);
433 int bottom = std::min(paint_rect.y + paint_rect.height,
434 client_rect.y + client_rect.height);
435 WebRect rect;
436 if (left < right && top < bottom)
437 rect = WebRect(left, top, right - left, bottom - top);
438 proxy_->setPaintRect(WebRect());
439 if (rect.isEmpty())
440 continue;
441 PaintRect(rect);
442 }
443 CHECK(proxy_->paintRect().isEmpty());
444}
445
446void WebKitTestRunner::DisplayRepaintMask() {
447 GetCanvas()->drawARGB(167, 0, 0, 0);
448}
449
[email protected]31d71b02012-01-26 03:42:31450} // namespace content