blob: f4674e20bbfa98a255896dbbee5ba4abff6b9ead [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]09ec9f92012-12-17 09:46:369#include "base/base64.h"
[email protected]0799da02012-06-27 10:58:5110#include "base/md5.h"
[email protected]e225b922012-08-18 01:43:0411#include "base/memory/scoped_ptr.h"
[email protected]b2324b092012-11-01 10:34:1112#include "base/message_loop.h"
[email protected]c272c5b2012-06-06 09:01:0613#include "base/stringprintf.h"
[email protected]0d2dfb92d2012-11-05 10:26:4514#include "base/sys_string_conversions.h"
[email protected]b2324b092012-11-01 10:34:1115#include "base/time.h"
[email protected]0d2dfb92d2012-11-05 10:26:4516#include "base/utf_string_conversions.h"
[email protected]efb5f572012-01-29 10:57:3317#include "content/public/renderer/render_view.h"
[email protected]e5196922012-12-11 22:18:2518#include "content/public/test/layouttest_support.h"
[email protected]efb5f572012-01-29 10:57:3319#include "content/shell/shell_messages.h"
[email protected]b2324b092012-11-01 10:34:1120#include "content/shell/shell_render_process_observer.h"
[email protected]55915a72012-12-18 11:55:2521#include "content/shell/webkit_test_helpers.h"
[email protected]0d2dfb92d2012-11-05 10:26:4522#include "net/base/net_util.h"
[email protected]0799da02012-06-27 10:58:5123#include "skia/ext/platform_canvas.h"
[email protected]2a58eb12012-12-18 07:56:4024#include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
[email protected]b2324b092012-11-01 10:34:1125#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
[email protected]001841c92012-12-11 17:00:1326#include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.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]09ec9f92012-12-17 09:46:3632#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h"
[email protected]09ec9f92012-12-17 09:46:3633#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"
[email protected]b2324b092012-11-01 10:34:1137#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]b9fad24c2012-11-15 08:22:1038#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]984a5f72012-12-13 21:41:5039#include "webkit/base/file_path_string_conversions.h"
[email protected]0799da02012-06-27 10:58:5140#include "webkit/glue/webkit_glue.h"
[email protected]324825d2012-11-30 12:37:1541#include "webkit/glue/webpreferences.h"
[email protected]efb5f572012-01-29 10:57:3342
[email protected]2a58eb12012-12-18 07:56:4043using WebKit::Platform;
[email protected]b2324b092012-11-01 10:34:1144using WebKit::WebContextMenuData;
[email protected]001841c92012-12-11 17:00:1345using WebKit::WebDevToolsAgent;
[email protected]efb5f572012-01-29 10:57:3346using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1147using WebKit::WebFrame;
48using WebKit::WebGamepads;
[email protected]0799da02012-06-27 10:58:5149using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0650using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1151using WebKit::WebString;
[email protected]09ec9f92012-12-17 09:46:3652using WebKit::WebURL;
[email protected]b2324b092012-11-01 10:34:1153using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5154using WebKit::WebView;
[email protected]b3dbcb5e2012-12-17 00:16:3755using WebTestRunner::WebPreferences;
[email protected]b2324b092012-11-01 10:34:1156using WebTestRunner::WebTask;
[email protected]efb5f572012-01-29 10:57:3357
[email protected]31d71b02012-01-26 03:42:3158namespace content {
59
[email protected]efb5f572012-01-29 10:57:3360namespace {
61
[email protected]b2324b092012-11-01 10:34:1162void InvokeTaskHelper(void* context) {
63 WebTask* task = reinterpret_cast<WebTask*>(context);
64 task->run();
65 delete task;
66}
67
[email protected]efb5f572012-01-29 10:57:3368std::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();
75}
76
[email protected]c272c5b2012-06-06 09:01:0677std::string DumpDocumentPrintedText(WebFrame* frame) {
78 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
79}
80
81std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
[email protected]efb5f572012-01-29 10:57:3382 std::string result;
83
[email protected]c272c5b2012-06-06 09:01:0684 // Cannot do printed format for anything other than HTML.
85 if (printing && !frame->document().isHTMLDocument())
86 return std::string();
87
[email protected]efb5f572012-01-29 10:57:3388 // Add header for all but the main frame. Skip emtpy frames.
89 if (frame->parent() && !frame->document().documentElement().isNull()) {
90 result.append("\n--------\nFrame: '");
[email protected]6d454d92012-09-13 17:06:2991 result.append(frame->uniqueName().utf8().data());
[email protected]efb5f572012-01-29 10:57:3392 result.append("'\n--------\n");
93 }
94
[email protected]c272c5b2012-06-06 09:01:0695 result.append(
96 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
[email protected]efb5f572012-01-29 10:57:3397 result.append("\n");
98
99 if (recursive) {
100 for (WebFrame* child = frame->firstChild(); child;
101 child = child->nextSibling()) {
[email protected]c272c5b2012-06-06 09:01:06102 result.append(DumpFramesAsText(child, printing, recursive));
103 }
104 }
105 return result;
106}
107
108std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) {
109 std::string result;
110
111 WebSize offset = frame->scrollOffset();
112 if (offset.width > 0 || offset.height > 0) {
113 if (frame->parent()) {
114 result.append(
[email protected]6d454d92012-09-13 17:06:29115 base::StringPrintf("frame '%s' ", frame->uniqueName().utf8().data()));
[email protected]c272c5b2012-06-06 09:01:06116 }
117 result.append(
118 base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height));
119 }
120
121 if (recursive) {
122 for (WebFrame* child = frame->firstChild(); child;
123 child = child->nextSibling()) {
124 result.append(DumpFrameScrollPosition(child, recursive));
[email protected]efb5f572012-01-29 10:57:33125 }
126 }
127 return result;
128}
129
[email protected]0799da02012-06-27 10:58:51130#if !defined(OS_MACOSX)
131void 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.
138 }
139}
140#endif
141
[email protected]b9fad24c2012-11-15 08:22:10142void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56143 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51144 const SkBitmap& bitmap = device->accessBitmap(false);
145 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
146
147#if !defined(OS_MACOSX)
148 // Only the expected PNGs for Mac have a valid alpha channel.
149 MakeBitmapOpaque(snapshot);
150#endif
151
152}
153
[email protected]efb5f572012-01-29 10:57:33154} // namespace
[email protected]cccb5cf2012-06-06 22:20:04155
[email protected]9b4c6cd2012-08-20 10:47:45156WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]01f0cad02013-01-09 21:42:50157 : RenderViewObserver(render_view) {
158 Reset();
[email protected]31d71b02012-01-26 03:42:31159}
160
[email protected]9b4c6cd2012-08-20 10:47:45161WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31162}
163
[email protected]b2324b092012-11-01 10:34:11164// WebTestDelegate -----------------------------------------------------------
165
166void WebKitTestRunner::clearContextMenuData() {
167 last_context_menu_data_.reset();
168}
169
170WebContextMenuData* WebKitTestRunner::lastContextMenuData() const {
171 return last_context_menu_data_.get();
172}
173
174void WebKitTestRunner::clearEditCommand() {
175 render_view()->ClearEditCommands();
176}
177
178void WebKitTestRunner::setEditCommand(const std::string& name,
179 const std::string& value) {
180 render_view()->SetEditCommandForNextKeyEvent(name, value);
181}
182
183void WebKitTestRunner::fillSpellingSuggestionList(
184 const WebString& word, WebVector<WebString>* suggestions) {
185 if (word == WebString::fromUTF8("wellcome")) {
186 WebVector<WebString> result(suggestions->size() + 1);
187 for (size_t i = 0; i < suggestions->size(); ++i)
188 result[i] = (*suggestions)[i];
189 result[suggestions->size()] = WebString::fromUTF8("welcome");
190 suggestions->swap(result);
191 }
192}
193
194void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25195 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11196}
197
198void WebKitTestRunner::printMessage(const std::string& message) {
199 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
200}
201
202void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40203 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11204}
205
206void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
207 MessageLoop::current()->PostDelayedTask(
208 FROM_HERE,
209 base::Bind(&WebTask::run, base::Owned(task)),
210 base::TimeDelta::FromMilliseconds(ms));
211}
212
213WebString WebKitTestRunner::registerIsolatedFileSystem(
214 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]984a5f72012-12-13 21:41:50215 std::vector<FilePath> files;
216 for (size_t i = 0; i < absolute_filenames.size(); ++i)
217 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
218 std::string filesystem_id;
219 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
220 routing_id(), files, &filesystem_id));
221 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11222}
223
224long long WebKitTestRunner::getCurrentTimeInMillisecond() {
225 return base::TimeTicks::Now().ToInternalValue() /
226 base::Time::kMicrosecondsPerMillisecond;
227}
228
229WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
230 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45231#if defined(OS_WIN)
232 FilePath path(UTF8ToWide(utf8_path));
233#else
234 FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
235#endif
236 if (!path.IsAbsolute()) {
237 GURL base_url =
238 net::FilePathToFileURL(current_working_directory_.Append(
239 FILE_PATH_LITERAL("foo")));
240 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
241 }
[email protected]984a5f72012-12-13 21:41:50242 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11243}
244
[email protected]09ec9f92012-12-17 09:46:36245WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
246 FilePath local_path;
247 if (!net::FileURLToFilePath(file_url, &local_path))
248 return WebURL();
249
250 std::string contents;
251 Send(new ShellViewHostMsg_ReadFileToString(
252 routing_id(), local_path, &contents));
253
254 std::string contents_base64;
255 if (!base::Base64Encode(contents, &contents_base64))
256 return WebURL();
257
258 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
259 return WebURL(GURL(data_url_prefix + contents_base64));
260}
261
262WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
263 const char kPrefix[] = "file:///tmp/LayoutTests/";
264 const int kPrefixLen = arraysize(kPrefix) - 1;
265
266 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
267 return WebURL(GURL(utf8_url));
268
269 FilePath replace_path =
270 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
271 FILE_PATH_LITERAL("LayoutTests/"));
272#if defined(OS_WIN)
273 std::string utf8_path = WideToUTF8(replace_path.value());
274#else
275 std::string utf8_path =
276 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
277#endif
278 std::string new_url =
279 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
280 return WebURL(GURL(new_url));
281}
282
[email protected]b3dbcb5e2012-12-17 00:16:37283WebPreferences* WebKitTestRunner::preferences() {
284 return &prefs_;
285}
286
287void WebKitTestRunner::applyPreferences() {
288 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47289 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37290 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25291 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37292}
293
[email protected]0b0e31462012-12-18 21:57:18294// WebTestRunner -------------------------------------------------------------
295
296bool WebKitTestRunner::shouldDumpEditingCallbacks() const {
297 return dump_editing_callbacks_;
298}
299
[email protected]09c48ef52013-01-09 12:25:07300bool WebKitTestRunner::shouldDumpFrameLoadCallbacks() const {
301 return test_is_running_ && dump_frame_load_callbacks_;
302}
303
304bool WebKitTestRunner::shouldDumpUserGestureInFrameLoadCallbacks() const {
305 return test_is_running_ && dump_user_gesture_in_frame_load_callbacks_;
306}
307
308bool WebKitTestRunner::stopProvisionalFrameLoads() const {
309 return stop_provisional_frame_loads_;
310}
311
312bool WebKitTestRunner::shouldDumpTitleChanges() const {
313 return dump_title_changes_;
314}
315
[email protected]b2324b092012-11-01 10:34:11316// RenderViewObserver --------------------------------------------------------
317
[email protected]9b4c6cd2012-08-20 10:47:45318void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11319 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56320}
321
[email protected]9b4c6cd2012-08-20 10:47:45322void WebKitTestRunner::DidFinishLoad(WebFrame* frame) {
[email protected]09c48ef52013-01-09 12:25:07323 if (!frame->parent()) {
324 if (!wait_until_done_)
325 test_is_running_ = false;
[email protected]01f0cad02013-01-09 21:42:50326 load_finished_ = true;
[email protected]c272c5b2012-06-06 09:01:06327 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
[email protected]09c48ef52013-01-09 12:25:07328 }
[email protected]c272c5b2012-06-06 09:01:06329}
330
[email protected]b2324b092012-11-01 10:34:11331void WebKitTestRunner::DidRequestShowContextMenu(
332 WebFrame* frame,
333 const WebContextMenuData& data) {
334 last_context_menu_data_.reset(new WebContextMenuData(data));
335}
336
[email protected]9b4c6cd2012-08-20 10:47:45337bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33338 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45339 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]efb5f572012-01-29 10:57:33340 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
[email protected]0799da02012-06-27 10:58:51341 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
[email protected]0d2dfb92d2012-11-05 10:26:45342 IPC_MESSAGE_HANDLER(ShellViewMsg_SetCurrentWorkingDirectory,
343 OnSetCurrentWorkingDirectory)
[email protected]efb5f572012-01-29 10:57:33344 IPC_MESSAGE_UNHANDLED(handled = false)
345 IPC_END_MESSAGE_MAP()
346
347 return handled;
348}
349
[email protected]b9fad24c2012-11-15 08:22:10350// Public methods - -----------------------------------------------------------
351
352void WebKitTestRunner::Display() {
353 const WebSize& size = render_view()->GetWebView()->size();
354 WebRect rect(0, 0, size.width, size.height);
355 proxy_->setPaintRect(rect);
356 PaintInvalidatedRegion();
357 DisplayRepaintMask();
358}
359
[email protected]324825d2012-11-30 12:37:15360void WebKitTestRunner::SetXSSAuditorEnabled(bool enabled) {
[email protected]b3dbcb5e2012-12-17 00:16:37361 prefs_.XSSAuditorEnabled = enabled;
[email protected]55915a72012-12-18 11:55:25362 applyPreferences();
[email protected]324825d2012-11-30 12:37:15363}
364
[email protected]2dd3a27a2012-12-10 11:24:16365void WebKitTestRunner::NotifyDone() {
[email protected]01f0cad02013-01-09 21:42:50366 if (load_finished_)
367 test_is_running_ = false;
368 else
369 wait_until_done_ = false;
[email protected]2dd3a27a2012-12-10 11:24:16370 Send(new ShellViewHostMsg_NotifyDone(routing_id()));
371}
372
373void WebKitTestRunner::DumpAsText() {
374 Send(new ShellViewHostMsg_DumpAsText(routing_id()));
375}
376
377void WebKitTestRunner::DumpChildFramesAsText() {
378 Send(new ShellViewHostMsg_DumpChildFramesAsText(routing_id()));
379}
380
381void WebKitTestRunner::SetPrinting() {
382 Send(new ShellViewHostMsg_SetPrinting(routing_id()));
383}
384
385void WebKitTestRunner::SetShouldStayOnPageAfterHandlingBeforeUnload(
386 bool should_stay_on_page) {
387 Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload(
388 routing_id(), should_stay_on_page));
389}
390
391void WebKitTestRunner::WaitUntilDone() {
[email protected]09c48ef52013-01-09 12:25:07392 wait_until_done_ = true;
[email protected]2dd3a27a2012-12-10 11:24:16393 Send(new ShellViewHostMsg_WaitUntilDone(routing_id()));
394}
395
[email protected]d99d65a2012-12-10 17:40:27396void WebKitTestRunner::CanOpenWindows() {
397 Send(new ShellViewHostMsg_CanOpenWindows(routing_id()));
398}
399
[email protected]001841c92012-12-11 17:00:13400void WebKitTestRunner::ShowWebInspector() {
401 Send(new ShellViewHostMsg_ShowWebInspector(routing_id()));
402}
403
404void WebKitTestRunner::CloseWebInspector() {
405 Send(new ShellViewHostMsg_CloseWebInspector(routing_id()));
406}
407
408void WebKitTestRunner::EvaluateInWebInspector(int32_t call_id,
409 const std::string& script) {
410 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
411 if (agent)
412 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
413}
414
[email protected]ab678e32012-12-14 21:17:08415void WebKitTestRunner::ExecCommand(const std::string& command,
416 const std::string& value) {
417 render_view()->GetWebView()->focusedFrame()->executeCommand(
418 WebString::fromUTF8(command), WebString::fromUTF8(value));
419}
420
[email protected]375db13a2012-12-18 21:42:58421void WebKitTestRunner::OverridePreference(const std::string& key,
422 v8::Local<v8::Value> value) {
423 if (key == "WebKitDefaultFontSize") {
424 prefs_.defaultFontSize = value->Int32Value();
425 } else if (key == "WebKitMinimumFontSize") {
426 prefs_.minimumFontSize = value->Int32Value();
427 } else if (key == "WebKitDefaultTextEncodingName") {
428 prefs_.defaultTextEncodingName =
429 WebString::fromUTF8(std::string(*v8::String::AsciiValue(value)));
430 } else if (key == "WebKitJavaScriptEnabled") {
431 prefs_.javaScriptEnabled = value->BooleanValue();
432 } else if (key == "WebKitSupportsMultipleWindows") {
433 prefs_.supportsMultipleWindows = value->BooleanValue();
434 } else if (key == "WebKitDisplayImagesKey") {
435 prefs_.loadsImagesAutomatically = value->BooleanValue();
436 } else if (key == "WebKitPluginsEnabled") {
437 prefs_.pluginsEnabled = value->BooleanValue();
438 } else if (key == "WebKitJavaEnabled") {
439 prefs_.javaEnabled = value->BooleanValue();
440 } else if (key == "WebKitUsesPageCachePreferenceKey") {
441 prefs_.usesPageCache = value->BooleanValue();
442 } else if (key == "WebKitPageCacheSupportsPluginsPreferenceKey") {
443 prefs_.pageCacheSupportsPlugins = value->BooleanValue();
444 } else if (key == "WebKitOfflineWebApplicationCacheEnabled") {
445 prefs_.offlineWebApplicationCacheEnabled = value->BooleanValue();
446 } else if (key == "WebKitTabToLinksPreferenceKey") {
447 prefs_.tabsToLinks = value->BooleanValue();
448 } else if (key == "WebKitWebGLEnabled") {
449 prefs_.experimentalWebGLEnabled = value->BooleanValue();
450 } else if (key == "WebKitCSSRegionsEnabled") {
451 prefs_.experimentalCSSRegionsEnabled = value->BooleanValue();
452 } else if (key == "WebKitCSSGridLayoutEnabled") {
453 prefs_.experimentalCSSGridLayoutEnabled = value->BooleanValue();
454 } else if (key == "WebKitHyperlinkAuditingEnabled") {
455 prefs_.hyperlinkAuditingEnabled = value->BooleanValue();
456 } else if (key == "WebKitEnableCaretBrowsing") {
457 prefs_.caretBrowsingEnabled = value->BooleanValue();
458 } else if (key == "WebKitAllowDisplayingInsecureContent") {
459 prefs_.allowDisplayOfInsecureContent = value->BooleanValue();
460 } else if (key == "WebKitAllowRunningInsecureContent") {
461 prefs_.allowRunningOfInsecureContent = value->BooleanValue();
462 } else if (key == "WebKitCSSCustomFilterEnabled") {
463 prefs_.cssCustomFilterEnabled = value->BooleanValue();
464 } else if (key == "WebKitShouldRespectImageOrientation") {
465 prefs_.shouldRespectImageOrientation = value->BooleanValue();
466 } else if (key == "WebKitWebAudioEnabled") {
467 DCHECK(value->BooleanValue());
468 } else {
469 std::string message("CONSOLE MESSAGE: Invalid name for preference: ");
470 printMessage(message + key + "\n");
471 }
472 applyPreferences();
473}
474
[email protected]0b0e31462012-12-18 21:57:18475void WebKitTestRunner::DumpEditingCallbacks() {
476 dump_editing_callbacks_ = true;
477}
478
[email protected]09c48ef52013-01-09 12:25:07479void WebKitTestRunner::DumpFrameLoadCallbacks() {
480 dump_frame_load_callbacks_ = true;
481}
482
483void WebKitTestRunner::DumpUserGestureInFrameLoadCallbacks() {
484 dump_user_gesture_in_frame_load_callbacks_ = true;
485}
486
487void WebKitTestRunner::StopProvisionalFrameLoads() {
488 stop_provisional_frame_loads_ = true;
489}
490
491void WebKitTestRunner::DumpTitleChanges() {
492 dump_title_changes_ = true;
493}
494
[email protected]43c7e6952012-12-11 13:50:51495void WebKitTestRunner::NotImplemented(const std::string& object,
496 const std::string& method) {
[email protected]2dd3a27a2012-12-10 11:24:16497 Send(new ShellViewHostMsg_NotImplemented(routing_id(), object, method));
498}
499
[email protected]324825d2012-11-30 12:37:15500void WebKitTestRunner::Reset() {
[email protected]b3dbcb5e2012-12-17 00:16:37501 prefs_.reset();
[email protected]324825d2012-11-30 12:37:15502 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47503 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]324825d2012-11-30 12:37:15504 render_view()->SetWebkitPreferences(prefs);
[email protected]0b0e31462012-12-18 21:57:18505 dump_editing_callbacks_ = false;
[email protected]09c48ef52013-01-09 12:25:07506 dump_frame_load_callbacks_ = false;
507 dump_user_gesture_in_frame_load_callbacks_ = false;
508 stop_provisional_frame_loads_ = false;
509 dump_title_changes_ = false;
510 test_is_running_ = true;
[email protected]01f0cad02013-01-09 21:42:50511 load_finished_ = false;
[email protected]09c48ef52013-01-09 12:25:07512 wait_until_done_ = false;
[email protected]324825d2012-11-30 12:37:15513}
514
[email protected]b2324b092012-11-01 10:34:11515// Private methods -----------------------------------------------------------
516
[email protected]9b4c6cd2012-08-20 10:47:45517void WebKitTestRunner::OnCaptureTextDump(bool as_text,
[email protected]891f08f2012-08-28 22:00:44518 bool printing,
519 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06520 WebFrame* frame = render_view()->GetWebView()->mainFrame();
521 std::string dump;
522 if (as_text) {
523 dump = DumpFramesAsText(frame, printing, recursive);
524 } else {
525 WebFrame::RenderAsTextControls render_text_behavior =
526 WebFrame::RenderAsTextNormal;
527 if (printing)
528 render_text_behavior |= WebFrame::RenderAsTextPrinting;
529 dump = frame->renderTreeAsText(render_text_behavior).utf8();
530 dump.append(DumpFrameScrollPosition(frame, recursive));
531 }
[email protected]efb5f572012-01-29 10:57:33532 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31533}
534
[email protected]9b4c6cd2012-08-20 10:47:45535void WebKitTestRunner::OnCaptureImageDump(
[email protected]0799da02012-06-27 10:58:51536 const std::string& expected_pixel_hash) {
537 SkBitmap snapshot;
[email protected]b9fad24c2012-11-15 08:22:10538 PaintInvalidatedRegion();
539 CopyCanvasToBitmap(GetCanvas(), &snapshot);
[email protected]0799da02012-06-27 10:58:51540
541 SkAutoLockPixels snapshot_lock(snapshot);
542 base::MD5Digest digest;
[email protected]e225b922012-08-18 01:43:04543#if defined(OS_ANDROID)
544 // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA.
545 const uint8_t* raw_pixels =
546 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
547 size_t snapshot_size = snapshot.getSize();
548 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
549 for (size_t i = 0; i < snapshot_size; i += 4) {
550 reordered_pixels[i] = raw_pixels[i + 2];
551 reordered_pixels[i + 1] = raw_pixels[i + 1];
552 reordered_pixels[i + 2] = raw_pixels[i];
553 reordered_pixels[i + 3] = raw_pixels[i + 3];
554 }
555 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
556#else
[email protected]0799da02012-06-27 10:58:51557 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
[email protected]e225b922012-08-18 01:43:04558#endif
[email protected]0799da02012-06-27 10:58:51559 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
560
561 if (actual_pixel_hash == expected_pixel_hash) {
562 SkBitmap empty_image;
563 Send(new ShellViewHostMsg_ImageDump(
564 routing_id(), actual_pixel_hash, empty_image));
[email protected]e225b922012-08-18 01:43:04565 return;
[email protected]0799da02012-06-27 10:58:51566 }
567 Send(new ShellViewHostMsg_ImageDump(
568 routing_id(), actual_pixel_hash, snapshot));
569}
570
[email protected]0d2dfb92d2012-11-05 10:26:45571void WebKitTestRunner::OnSetCurrentWorkingDirectory(
572 const FilePath& current_working_directory) {
573 current_working_directory_ = current_working_directory;
[email protected]01f0cad02013-01-09 21:42:50574 std::vector<FilePath::StringType> components;
575 current_working_directory_.GetComponents(&components);
576 for (unsigned i = 0; i < components.size(); ++i) {
577 if (components[i] == FILE_PATH_LITERAL("loading"))
578 dump_frame_load_callbacks_ = true;
579 }
[email protected]0d2dfb92d2012-11-05 10:26:45580}
581
[email protected]b9fad24c2012-11-15 08:22:10582SkCanvas* WebKitTestRunner::GetCanvas() {
583 WebView* view = render_view()->GetWebView();
584 const WebSize& size = view->size();
585 float device_scale_factor = view->deviceScaleFactor();
586 int width = std::ceil(device_scale_factor * size.width);
587 int height = std::ceil(device_scale_factor * size.height);
588
589 if (canvas_ &&
590 canvas_->getDeviceSize().width() == width &&
591 canvas_->getDeviceSize().height() == height) {
592 return canvas_.get();
593 }
594 canvas_.reset(skia::CreatePlatformCanvas(
595 size.width, size.height, true, 0, skia::RETURN_NULL_ON_FAILURE));
596 return canvas_.get();
597}
598
599void WebKitTestRunner::PaintRect(const WebRect& rect) {
600 WebView* view = render_view()->GetWebView();
601 float device_scale_factor = view->deviceScaleFactor();
602 int scaled_x = device_scale_factor * rect.x;
603 int scaled_y = device_scale_factor * rect.y;
604 int scaled_width = std::ceil(device_scale_factor * rect.width);
605 int scaled_height = std::ceil(device_scale_factor * rect.height);
606 // TODO(jochen): Verify that the scaling is correct once the HiDPI tests
607 // actually work.
608 WebRect device_rect(scaled_x, scaled_y, scaled_width, scaled_height);
609 view->paint(webkit_glue::ToWebCanvas(GetCanvas()), device_rect);
610}
611
612void WebKitTestRunner::PaintInvalidatedRegion() {
613 WebView* view = render_view()->GetWebView();
614 view->animate(0.0);
615 view->layout();
616 const WebSize& widget_size = view->size();
617 WebRect client_rect(0, 0, widget_size.width, widget_size.height);
618
619 // Paint the canvas if necessary. Allow painting to generate extra rects
620 // for the first two calls. This is necessary because some WebCore rendering
621 // objects update their layout only when painted.
622 for (int i = 0; i < 3; ++i) {
623 // Make sure that paint_rect is always inside the RenderView's visible
624 // area.
625 WebRect paint_rect = proxy_->paintRect();
626 int left = std::max(paint_rect.x, client_rect.x);
627 int top = std::max(paint_rect.y, client_rect.y);
628 int right = std::min(paint_rect.x + paint_rect.width,
629 client_rect.x + client_rect.width);
630 int bottom = std::min(paint_rect.y + paint_rect.height,
631 client_rect.y + client_rect.height);
632 WebRect rect;
633 if (left < right && top < bottom)
634 rect = WebRect(left, top, right - left, bottom - top);
635 proxy_->setPaintRect(WebRect());
636 if (rect.isEmpty())
637 continue;
638 PaintRect(rect);
639 }
640 CHECK(proxy_->paintRect().isEmpty());
641}
642
643void WebKitTestRunner::DisplayRepaintMask() {
644 GetCanvas()->drawARGB(167, 0, 0, 0);
645}
646
[email protected]31d71b02012-01-26 03:42:31647} // namespace content