[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / shell / webkit_test_runner.cc
blob52ad1266aa8771431eefdc507eb66687c54fcc51
1 // 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.
5 #include "content/shell/webkit_test_runner.h"
7 #include <cmath>
9 #include "base/base64.h"
10 #include "base/md5.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop.h"
13 #include "base/stringprintf.h"
14 #include "base/sys_string_conversions.h"
15 #include "base/time.h"
16 #include "base/utf_string_conversions.h"
17 #include "content/public/renderer/render_view.h"
18 #include "content/public/test/layouttest_support.h"
19 #include "content/shell/shell_messages.h"
20 #include "content/shell/shell_render_process_observer.h"
21 #include "content/shell/webkit_test_helpers.h"
22 #include "net/base/net_util.h"
23 #include "skia/ext/platform_canvas.h"
24 #include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h"
27 #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"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
34 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
35 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
36 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
37 #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
38 #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
39 #include "webkit/base/file_path_string_conversions.h"
40 #include "webkit/glue/webkit_glue.h"
41 #include "webkit/glue/webpreferences.h"
43 using WebKit::Platform;
44 using WebKit::WebContextMenuData;
45 using WebKit::WebDevToolsAgent;
46 using WebKit::WebElement;
47 using WebKit::WebFrame;
48 using WebKit::WebGamepads;
49 using WebKit::WebRect;
50 using WebKit::WebSize;
51 using WebKit::WebString;
52 using WebKit::WebURL;
53 using WebKit::WebVector;
54 using WebKit::WebView;
55 using WebTestRunner::WebPreferences;
56 using WebTestRunner::WebTask;
58 namespace content {
60 namespace {
62 void InvokeTaskHelper(void* context) {
63 WebTask* task = reinterpret_cast<WebTask*>(context);
64 task->run();
65 delete task;
68 std::string DumpDocumentText(WebFrame* frame) {
69 // We use the document element's text instead of the body text here because
70 // not all documents have a body, such as XML documents.
71 WebElement documentElement = frame->document().documentElement();
72 if (documentElement.isNull())
73 return std::string();
74 return documentElement.innerText().utf8();
77 std::string DumpDocumentPrintedText(WebFrame* frame) {
78 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
81 std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
82 std::string result;
84 // Cannot do printed format for anything other than HTML.
85 if (printing && !frame->document().isHTMLDocument())
86 return std::string();
88 // Add header for all but the main frame. Skip emtpy frames.
89 if (frame->parent() && !frame->document().documentElement().isNull()) {
90 result.append("\n--------\nFrame: '");
91 result.append(frame->uniqueName().utf8().data());
92 result.append("'\n--------\n");
95 result.append(
96 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
97 result.append("\n");
99 if (recursive) {
100 for (WebFrame* child = frame->firstChild(); child;
101 child = child->nextSibling()) {
102 result.append(DumpFramesAsText(child, printing, recursive));
105 return result;
108 std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) {
109 std::string result;
111 WebSize offset = frame->scrollOffset();
112 if (offset.width > 0 || offset.height > 0) {
113 if (frame->parent()) {
114 result.append(
115 base::StringPrintf("frame '%s' ", frame->uniqueName().utf8().data()));
117 result.append(
118 base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height));
121 if (recursive) {
122 for (WebFrame* child = frame->firstChild(); child;
123 child = child->nextSibling()) {
124 result.append(DumpFrameScrollPosition(child, recursive));
127 return result;
130 #if !defined(OS_MACOSX)
131 void MakeBitmapOpaque(SkBitmap* bitmap) {
132 SkAutoLockPixels lock(*bitmap);
133 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
134 for (int y = 0; y < bitmap->height(); ++y) {
135 uint32_t* row = bitmap->getAddr32(0, y);
136 for (int x = 0; x < bitmap->width(); ++x)
137 row[x] |= 0xFF000000; // Set alpha bits to 1.
140 #endif
142 void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
143 SkDevice* device = skia::GetTopDevice(*canvas);
144 const SkBitmap& bitmap = device->accessBitmap(false);
145 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
147 #if !defined(OS_MACOSX)
148 // Only the expected PNGs for Mac have a valid alpha channel.
149 MakeBitmapOpaque(snapshot);
150 #endif
154 } // namespace
156 WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
157 : RenderViewObserver(render_view) {
160 WebKitTestRunner::~WebKitTestRunner() {
163 // WebTestDelegate -----------------------------------------------------------
165 void WebKitTestRunner::clearContextMenuData() {
166 last_context_menu_data_.reset();
169 WebContextMenuData* WebKitTestRunner::lastContextMenuData() const {
170 return last_context_menu_data_.get();
173 void WebKitTestRunner::clearEditCommand() {
174 render_view()->ClearEditCommands();
177 void WebKitTestRunner::setEditCommand(const std::string& name,
178 const std::string& value) {
179 render_view()->SetEditCommandForNextKeyEvent(name, value);
182 void WebKitTestRunner::fillSpellingSuggestionList(
183 const WebString& word, WebVector<WebString>* suggestions) {
184 if (word == WebString::fromUTF8("wellcome")) {
185 WebVector<WebString> result(suggestions->size() + 1);
186 for (size_t i = 0; i < suggestions->size(); ++i)
187 result[i] = (*suggestions)[i];
188 result[suggestions->size()] = WebString::fromUTF8("welcome");
189 suggestions->swap(result);
193 void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
194 SetMockGamepads(gamepads);
197 void WebKitTestRunner::printMessage(const std::string& message) {
198 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
201 void WebKitTestRunner::postTask(WebTask* task) {
202 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
205 void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
206 MessageLoop::current()->PostDelayedTask(
207 FROM_HERE,
208 base::Bind(&WebTask::run, base::Owned(task)),
209 base::TimeDelta::FromMilliseconds(ms));
212 WebString WebKitTestRunner::registerIsolatedFileSystem(
213 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
214 std::vector<FilePath> files;
215 for (size_t i = 0; i < absolute_filenames.size(); ++i)
216 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
217 std::string filesystem_id;
218 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
219 routing_id(), files, &filesystem_id));
220 return WebString::fromUTF8(filesystem_id);
223 long long WebKitTestRunner::getCurrentTimeInMillisecond() {
224 return base::TimeTicks::Now().ToInternalValue() /
225 base::Time::kMicrosecondsPerMillisecond;
228 WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
229 const std::string& utf8_path) {
230 #if defined(OS_WIN)
231 FilePath path(UTF8ToWide(utf8_path));
232 #else
233 FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
234 #endif
235 if (!path.IsAbsolute()) {
236 GURL base_url =
237 net::FilePathToFileURL(current_working_directory_.Append(
238 FILE_PATH_LITERAL("foo")));
239 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
241 return webkit_base::FilePathToWebString(path);
244 WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
245 FilePath local_path;
246 if (!net::FileURLToFilePath(file_url, &local_path))
247 return WebURL();
249 std::string contents;
250 Send(new ShellViewHostMsg_ReadFileToString(
251 routing_id(), local_path, &contents));
253 std::string contents_base64;
254 if (!base::Base64Encode(contents, &contents_base64))
255 return WebURL();
257 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
258 return WebURL(GURL(data_url_prefix + contents_base64));
261 WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
262 const char kPrefix[] = "file:///tmp/LayoutTests/";
263 const int kPrefixLen = arraysize(kPrefix) - 1;
265 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
266 return WebURL(GURL(utf8_url));
268 FilePath replace_path =
269 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
270 FILE_PATH_LITERAL("LayoutTests/"));
271 #if defined(OS_WIN)
272 std::string utf8_path = WideToUTF8(replace_path.value());
273 #else
274 std::string utf8_path =
275 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
276 #endif
277 std::string new_url =
278 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
279 return WebURL(GURL(new_url));
282 WebPreferences* WebKitTestRunner::preferences() {
283 return &prefs_;
286 void WebKitTestRunner::applyPreferences() {
287 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
288 ExportPreferences(prefs_, &prefs);
289 render_view()->SetWebkitPreferences(prefs);
290 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
293 // RenderViewObserver --------------------------------------------------------
295 void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
296 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
299 void WebKitTestRunner::DidFinishLoad(WebFrame* frame) {
300 if (!frame->parent())
301 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
304 void WebKitTestRunner::DidRequestShowContextMenu(
305 WebFrame* frame,
306 const WebContextMenuData& data) {
307 last_context_menu_data_.reset(new WebContextMenuData(data));
310 bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
311 bool handled = true;
312 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
313 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
314 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
315 IPC_MESSAGE_HANDLER(ShellViewMsg_SetCurrentWorkingDirectory,
316 OnSetCurrentWorkingDirectory)
317 IPC_MESSAGE_UNHANDLED(handled = false)
318 IPC_END_MESSAGE_MAP()
320 return handled;
323 // Public methods - -----------------------------------------------------------
325 void WebKitTestRunner::Display() {
326 const WebSize& size = render_view()->GetWebView()->size();
327 WebRect rect(0, 0, size.width, size.height);
328 proxy_->setPaintRect(rect);
329 PaintInvalidatedRegion();
330 DisplayRepaintMask();
333 void WebKitTestRunner::SetXSSAuditorEnabled(bool enabled) {
334 prefs_.XSSAuditorEnabled = enabled;
335 applyPreferences();
338 void WebKitTestRunner::NotifyDone() {
339 Send(new ShellViewHostMsg_NotifyDone(routing_id()));
342 void WebKitTestRunner::DumpAsText() {
343 Send(new ShellViewHostMsg_DumpAsText(routing_id()));
346 void WebKitTestRunner::DumpChildFramesAsText() {
347 Send(new ShellViewHostMsg_DumpChildFramesAsText(routing_id()));
350 void WebKitTestRunner::SetPrinting() {
351 Send(new ShellViewHostMsg_SetPrinting(routing_id()));
354 void WebKitTestRunner::SetShouldStayOnPageAfterHandlingBeforeUnload(
355 bool should_stay_on_page) {
356 Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload(
357 routing_id(), should_stay_on_page));
360 void WebKitTestRunner::WaitUntilDone() {
361 Send(new ShellViewHostMsg_WaitUntilDone(routing_id()));
364 void WebKitTestRunner::CanOpenWindows() {
365 Send(new ShellViewHostMsg_CanOpenWindows(routing_id()));
368 void WebKitTestRunner::ShowWebInspector() {
369 Send(new ShellViewHostMsg_ShowWebInspector(routing_id()));
372 void WebKitTestRunner::CloseWebInspector() {
373 Send(new ShellViewHostMsg_CloseWebInspector(routing_id()));
376 void WebKitTestRunner::EvaluateInWebInspector(int32_t call_id,
377 const std::string& script) {
378 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
379 if (agent)
380 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
383 void WebKitTestRunner::ExecCommand(const std::string& command,
384 const std::string& value) {
385 render_view()->GetWebView()->focusedFrame()->executeCommand(
386 WebString::fromUTF8(command), WebString::fromUTF8(value));
389 void WebKitTestRunner::OverridePreference(const std::string& key,
390 v8::Local<v8::Value> value) {
391 if (key == "WebKitDefaultFontSize") {
392 prefs_.defaultFontSize = value->Int32Value();
393 } else if (key == "WebKitMinimumFontSize") {
394 prefs_.minimumFontSize = value->Int32Value();
395 } else if (key == "WebKitDefaultTextEncodingName") {
396 prefs_.defaultTextEncodingName =
397 WebString::fromUTF8(std::string(*v8::String::AsciiValue(value)));
398 } else if (key == "WebKitJavaScriptEnabled") {
399 prefs_.javaScriptEnabled = value->BooleanValue();
400 } else if (key == "WebKitSupportsMultipleWindows") {
401 prefs_.supportsMultipleWindows = value->BooleanValue();
402 } else if (key == "WebKitDisplayImagesKey") {
403 prefs_.loadsImagesAutomatically = value->BooleanValue();
404 } else if (key == "WebKitPluginsEnabled") {
405 prefs_.pluginsEnabled = value->BooleanValue();
406 } else if (key == "WebKitJavaEnabled") {
407 prefs_.javaEnabled = value->BooleanValue();
408 } else if (key == "WebKitUsesPageCachePreferenceKey") {
409 prefs_.usesPageCache = value->BooleanValue();
410 } else if (key == "WebKitPageCacheSupportsPluginsPreferenceKey") {
411 prefs_.pageCacheSupportsPlugins = value->BooleanValue();
412 } else if (key == "WebKitOfflineWebApplicationCacheEnabled") {
413 prefs_.offlineWebApplicationCacheEnabled = value->BooleanValue();
414 } else if (key == "WebKitTabToLinksPreferenceKey") {
415 prefs_.tabsToLinks = value->BooleanValue();
416 } else if (key == "WebKitWebGLEnabled") {
417 prefs_.experimentalWebGLEnabled = value->BooleanValue();
418 } else if (key == "WebKitCSSRegionsEnabled") {
419 prefs_.experimentalCSSRegionsEnabled = value->BooleanValue();
420 } else if (key == "WebKitCSSGridLayoutEnabled") {
421 prefs_.experimentalCSSGridLayoutEnabled = value->BooleanValue();
422 } else if (key == "WebKitHyperlinkAuditingEnabled") {
423 prefs_.hyperlinkAuditingEnabled = value->BooleanValue();
424 } else if (key == "WebKitEnableCaretBrowsing") {
425 prefs_.caretBrowsingEnabled = value->BooleanValue();
426 } else if (key == "WebKitAllowDisplayingInsecureContent") {
427 prefs_.allowDisplayOfInsecureContent = value->BooleanValue();
428 } else if (key == "WebKitAllowRunningInsecureContent") {
429 prefs_.allowRunningOfInsecureContent = value->BooleanValue();
430 } else if (key == "WebKitCSSCustomFilterEnabled") {
431 prefs_.cssCustomFilterEnabled = value->BooleanValue();
432 } else if (key == "WebKitShouldRespectImageOrientation") {
433 prefs_.shouldRespectImageOrientation = value->BooleanValue();
434 } else if (key == "WebKitWebAudioEnabled") {
435 DCHECK(value->BooleanValue());
436 } else {
437 std::string message("CONSOLE MESSAGE: Invalid name for preference: ");
438 printMessage(message + key + "\n");
440 applyPreferences();
443 void WebKitTestRunner::NotImplemented(const std::string& object,
444 const std::string& method) {
445 Send(new ShellViewHostMsg_NotImplemented(routing_id(), object, method));
448 void WebKitTestRunner::Reset() {
449 prefs_.reset();
450 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
451 ExportPreferences(prefs_, &prefs);
452 render_view()->SetWebkitPreferences(prefs);
455 // Private methods -----------------------------------------------------------
457 void WebKitTestRunner::OnCaptureTextDump(bool as_text,
458 bool printing,
459 bool recursive) {
460 WebFrame* frame = render_view()->GetWebView()->mainFrame();
461 std::string dump;
462 if (as_text) {
463 dump = DumpFramesAsText(frame, printing, recursive);
464 } else {
465 WebFrame::RenderAsTextControls render_text_behavior =
466 WebFrame::RenderAsTextNormal;
467 if (printing)
468 render_text_behavior |= WebFrame::RenderAsTextPrinting;
469 dump = frame->renderTreeAsText(render_text_behavior).utf8();
470 dump.append(DumpFrameScrollPosition(frame, recursive));
472 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
475 void WebKitTestRunner::OnCaptureImageDump(
476 const std::string& expected_pixel_hash) {
477 SkBitmap snapshot;
478 PaintInvalidatedRegion();
479 CopyCanvasToBitmap(GetCanvas(), &snapshot);
481 SkAutoLockPixels snapshot_lock(snapshot);
482 base::MD5Digest digest;
483 #if defined(OS_ANDROID)
484 // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA.
485 const uint8_t* raw_pixels =
486 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
487 size_t snapshot_size = snapshot.getSize();
488 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
489 for (size_t i = 0; i < snapshot_size; i += 4) {
490 reordered_pixels[i] = raw_pixels[i + 2];
491 reordered_pixels[i + 1] = raw_pixels[i + 1];
492 reordered_pixels[i + 2] = raw_pixels[i];
493 reordered_pixels[i + 3] = raw_pixels[i + 3];
495 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
496 #else
497 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
498 #endif
499 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
501 if (actual_pixel_hash == expected_pixel_hash) {
502 SkBitmap empty_image;
503 Send(new ShellViewHostMsg_ImageDump(
504 routing_id(), actual_pixel_hash, empty_image));
505 return;
507 Send(new ShellViewHostMsg_ImageDump(
508 routing_id(), actual_pixel_hash, snapshot));
511 void WebKitTestRunner::OnSetCurrentWorkingDirectory(
512 const FilePath& current_working_directory) {
513 current_working_directory_ = current_working_directory;
516 SkCanvas* WebKitTestRunner::GetCanvas() {
517 WebView* view = render_view()->GetWebView();
518 const WebSize& size = view->size();
519 float device_scale_factor = view->deviceScaleFactor();
520 int width = std::ceil(device_scale_factor * size.width);
521 int height = std::ceil(device_scale_factor * size.height);
523 if (canvas_ &&
524 canvas_->getDeviceSize().width() == width &&
525 canvas_->getDeviceSize().height() == height) {
526 return canvas_.get();
528 canvas_.reset(skia::CreatePlatformCanvas(
529 size.width, size.height, true, 0, skia::RETURN_NULL_ON_FAILURE));
530 return canvas_.get();
533 void WebKitTestRunner::PaintRect(const WebRect& rect) {
534 WebView* view = render_view()->GetWebView();
535 float device_scale_factor = view->deviceScaleFactor();
536 int scaled_x = device_scale_factor * rect.x;
537 int scaled_y = device_scale_factor * rect.y;
538 int scaled_width = std::ceil(device_scale_factor * rect.width);
539 int scaled_height = std::ceil(device_scale_factor * rect.height);
540 // TODO(jochen): Verify that the scaling is correct once the HiDPI tests
541 // actually work.
542 WebRect device_rect(scaled_x, scaled_y, scaled_width, scaled_height);
543 view->paint(webkit_glue::ToWebCanvas(GetCanvas()), device_rect);
546 void WebKitTestRunner::PaintInvalidatedRegion() {
547 WebView* view = render_view()->GetWebView();
548 view->animate(0.0);
549 view->layout();
550 const WebSize& widget_size = view->size();
551 WebRect client_rect(0, 0, widget_size.width, widget_size.height);
553 // Paint the canvas if necessary. Allow painting to generate extra rects
554 // for the first two calls. This is necessary because some WebCore rendering
555 // objects update their layout only when painted.
556 for (int i = 0; i < 3; ++i) {
557 // Make sure that paint_rect is always inside the RenderView's visible
558 // area.
559 WebRect paint_rect = proxy_->paintRect();
560 int left = std::max(paint_rect.x, client_rect.x);
561 int top = std::max(paint_rect.y, client_rect.y);
562 int right = std::min(paint_rect.x + paint_rect.width,
563 client_rect.x + client_rect.width);
564 int bottom = std::min(paint_rect.y + paint_rect.height,
565 client_rect.y + client_rect.height);
566 WebRect rect;
567 if (left < right && top < bottom)
568 rect = WebRect(left, top, right - left, bottom - top);
569 proxy_->setPaintRect(WebRect());
570 if (rect.isEmpty())
571 continue;
572 PaintRect(rect);
574 CHECK(proxy_->paintRect().isEmpty());
577 void WebKitTestRunner::DisplayRepaintMask() {
578 GetCanvas()->drawARGB(167, 0, 0, 0);
581 } // namespace content