blob: cfd9ea14f0974d0935fdc7f618140004d13c01ca [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]0b0e31462012-12-18 21:57:18157 : RenderViewObserver(render_view),
[email protected]09c48ef52013-01-09 12:25:07158 dump_editing_callbacks_(false),
159 dump_frame_load_callbacks_(false),
160 dump_user_gesture_in_frame_load_callbacks_(false),
161 stop_provisional_frame_loads_(false),
162 dump_title_changes_(false),
163 test_is_running_(true),
164 wait_until_done_(false) {
[email protected]31d71b02012-01-26 03:42:31165}
166
[email protected]9b4c6cd2012-08-20 10:47:45167WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31168}
169
[email protected]b2324b092012-11-01 10:34:11170// WebTestDelegate -----------------------------------------------------------
171
172void WebKitTestRunner::clearContextMenuData() {
173 last_context_menu_data_.reset();
174}
175
176WebContextMenuData* WebKitTestRunner::lastContextMenuData() const {
177 return last_context_menu_data_.get();
178}
179
180void WebKitTestRunner::clearEditCommand() {
181 render_view()->ClearEditCommands();
182}
183
184void WebKitTestRunner::setEditCommand(const std::string& name,
185 const std::string& value) {
186 render_view()->SetEditCommandForNextKeyEvent(name, value);
187}
188
189void WebKitTestRunner::fillSpellingSuggestionList(
190 const WebString& word, WebVector<WebString>* suggestions) {
191 if (word == WebString::fromUTF8("wellcome")) {
192 WebVector<WebString> result(suggestions->size() + 1);
193 for (size_t i = 0; i < suggestions->size(); ++i)
194 result[i] = (*suggestions)[i];
195 result[suggestions->size()] = WebString::fromUTF8("welcome");
196 suggestions->swap(result);
197 }
198}
199
200void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25201 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11202}
203
204void WebKitTestRunner::printMessage(const std::string& message) {
205 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
206}
207
208void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40209 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11210}
211
212void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
213 MessageLoop::current()->PostDelayedTask(
214 FROM_HERE,
215 base::Bind(&WebTask::run, base::Owned(task)),
216 base::TimeDelta::FromMilliseconds(ms));
217}
218
219WebString WebKitTestRunner::registerIsolatedFileSystem(
220 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]984a5f72012-12-13 21:41:50221 std::vector<FilePath> files;
222 for (size_t i = 0; i < absolute_filenames.size(); ++i)
223 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
224 std::string filesystem_id;
225 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
226 routing_id(), files, &filesystem_id));
227 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11228}
229
230long long WebKitTestRunner::getCurrentTimeInMillisecond() {
231 return base::TimeTicks::Now().ToInternalValue() /
232 base::Time::kMicrosecondsPerMillisecond;
233}
234
235WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
236 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45237#if defined(OS_WIN)
238 FilePath path(UTF8ToWide(utf8_path));
239#else
240 FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
241#endif
242 if (!path.IsAbsolute()) {
243 GURL base_url =
244 net::FilePathToFileURL(current_working_directory_.Append(
245 FILE_PATH_LITERAL("foo")));
246 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
247 }
[email protected]984a5f72012-12-13 21:41:50248 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11249}
250
[email protected]09ec9f92012-12-17 09:46:36251WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
252 FilePath local_path;
253 if (!net::FileURLToFilePath(file_url, &local_path))
254 return WebURL();
255
256 std::string contents;
257 Send(new ShellViewHostMsg_ReadFileToString(
258 routing_id(), local_path, &contents));
259
260 std::string contents_base64;
261 if (!base::Base64Encode(contents, &contents_base64))
262 return WebURL();
263
264 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
265 return WebURL(GURL(data_url_prefix + contents_base64));
266}
267
268WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
269 const char kPrefix[] = "file:///tmp/LayoutTests/";
270 const int kPrefixLen = arraysize(kPrefix) - 1;
271
272 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
273 return WebURL(GURL(utf8_url));
274
275 FilePath replace_path =
276 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
277 FILE_PATH_LITERAL("LayoutTests/"));
278#if defined(OS_WIN)
279 std::string utf8_path = WideToUTF8(replace_path.value());
280#else
281 std::string utf8_path =
282 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
283#endif
284 std::string new_url =
285 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
286 return WebURL(GURL(new_url));
287}
288
[email protected]b3dbcb5e2012-12-17 00:16:37289WebPreferences* WebKitTestRunner::preferences() {
290 return &prefs_;
291}
292
293void WebKitTestRunner::applyPreferences() {
294 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47295 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37296 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25297 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37298}
299
[email protected]0b0e31462012-12-18 21:57:18300// WebTestRunner -------------------------------------------------------------
301
302bool WebKitTestRunner::shouldDumpEditingCallbacks() const {
303 return dump_editing_callbacks_;
304}
305
[email protected]09c48ef52013-01-09 12:25:07306bool WebKitTestRunner::shouldDumpFrameLoadCallbacks() const {
307 return test_is_running_ && dump_frame_load_callbacks_;
308}
309
310bool WebKitTestRunner::shouldDumpUserGestureInFrameLoadCallbacks() const {
311 return test_is_running_ && dump_user_gesture_in_frame_load_callbacks_;
312}
313
314bool WebKitTestRunner::stopProvisionalFrameLoads() const {
315 return stop_provisional_frame_loads_;
316}
317
318bool WebKitTestRunner::shouldDumpTitleChanges() const {
319 return dump_title_changes_;
320}
321
[email protected]b2324b092012-11-01 10:34:11322// RenderViewObserver --------------------------------------------------------
323
[email protected]9b4c6cd2012-08-20 10:47:45324void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11325 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56326}
327
[email protected]9b4c6cd2012-08-20 10:47:45328void WebKitTestRunner::DidFinishLoad(WebFrame* frame) {
[email protected]09c48ef52013-01-09 12:25:07329 if (!frame->parent()) {
330 if (!wait_until_done_)
331 test_is_running_ = false;
[email protected]c272c5b2012-06-06 09:01:06332 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
[email protected]09c48ef52013-01-09 12:25:07333 }
[email protected]c272c5b2012-06-06 09:01:06334}
335
[email protected]b2324b092012-11-01 10:34:11336void WebKitTestRunner::DidRequestShowContextMenu(
337 WebFrame* frame,
338 const WebContextMenuData& data) {
339 last_context_menu_data_.reset(new WebContextMenuData(data));
340}
341
[email protected]9b4c6cd2012-08-20 10:47:45342bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33343 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45344 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]efb5f572012-01-29 10:57:33345 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
[email protected]0799da02012-06-27 10:58:51346 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump)
[email protected]0d2dfb92d2012-11-05 10:26:45347 IPC_MESSAGE_HANDLER(ShellViewMsg_SetCurrentWorkingDirectory,
348 OnSetCurrentWorkingDirectory)
[email protected]efb5f572012-01-29 10:57:33349 IPC_MESSAGE_UNHANDLED(handled = false)
350 IPC_END_MESSAGE_MAP()
351
352 return handled;
353}
354
[email protected]b9fad24c2012-11-15 08:22:10355// Public methods - -----------------------------------------------------------
356
357void WebKitTestRunner::Display() {
358 const WebSize& size = render_view()->GetWebView()->size();
359 WebRect rect(0, 0, size.width, size.height);
360 proxy_->setPaintRect(rect);
361 PaintInvalidatedRegion();
362 DisplayRepaintMask();
363}
364
[email protected]324825d2012-11-30 12:37:15365void WebKitTestRunner::SetXSSAuditorEnabled(bool enabled) {
[email protected]b3dbcb5e2012-12-17 00:16:37366 prefs_.XSSAuditorEnabled = enabled;
[email protected]55915a72012-12-18 11:55:25367 applyPreferences();
[email protected]324825d2012-11-30 12:37:15368}
369
[email protected]2dd3a27a2012-12-10 11:24:16370void WebKitTestRunner::NotifyDone() {
[email protected]09c48ef52013-01-09 12:25:07371 test_is_running_ = false;
[email protected]2dd3a27a2012-12-10 11:24:16372 Send(new ShellViewHostMsg_NotifyDone(routing_id()));
373}
374
375void WebKitTestRunner::DumpAsText() {
376 Send(new ShellViewHostMsg_DumpAsText(routing_id()));
377}
378
379void WebKitTestRunner::DumpChildFramesAsText() {
380 Send(new ShellViewHostMsg_DumpChildFramesAsText(routing_id()));
381}
382
383void WebKitTestRunner::SetPrinting() {
384 Send(new ShellViewHostMsg_SetPrinting(routing_id()));
385}
386
387void WebKitTestRunner::SetShouldStayOnPageAfterHandlingBeforeUnload(
388 bool should_stay_on_page) {
389 Send(new ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload(
390 routing_id(), should_stay_on_page));
391}
392
393void WebKitTestRunner::WaitUntilDone() {
[email protected]09c48ef52013-01-09 12:25:07394 wait_until_done_ = true;
[email protected]2dd3a27a2012-12-10 11:24:16395 Send(new ShellViewHostMsg_WaitUntilDone(routing_id()));
396}
397
[email protected]d99d65a2012-12-10 17:40:27398void WebKitTestRunner::CanOpenWindows() {
399 Send(new ShellViewHostMsg_CanOpenWindows(routing_id()));
400}
401
[email protected]001841c92012-12-11 17:00:13402void WebKitTestRunner::ShowWebInspector() {
403 Send(new ShellViewHostMsg_ShowWebInspector(routing_id()));
404}
405
406void WebKitTestRunner::CloseWebInspector() {
407 Send(new ShellViewHostMsg_CloseWebInspector(routing_id()));
408}
409
410void WebKitTestRunner::EvaluateInWebInspector(int32_t call_id,
411 const std::string& script) {
412 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
413 if (agent)
414 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
415}
416
[email protected]ab678e32012-12-14 21:17:08417void WebKitTestRunner::ExecCommand(const std::string& command,
418 const std::string& value) {
419 render_view()->GetWebView()->focusedFrame()->executeCommand(
420 WebString::fromUTF8(command), WebString::fromUTF8(value));
421}
422
[email protected]375db13a2012-12-18 21:42:58423void WebKitTestRunner::OverridePreference(const std::string& key,
424 v8::Local<v8::Value> value) {
425 if (key == "WebKitDefaultFontSize") {
426 prefs_.defaultFontSize = value->Int32Value();
427 } else if (key == "WebKitMinimumFontSize") {
428 prefs_.minimumFontSize = value->Int32Value();
429 } else if (key == "WebKitDefaultTextEncodingName") {
430 prefs_.defaultTextEncodingName =
431 WebString::fromUTF8(std::string(*v8::String::AsciiValue(value)));
432 } else if (key == "WebKitJavaScriptEnabled") {
433 prefs_.javaScriptEnabled = value->BooleanValue();
434 } else if (key == "WebKitSupportsMultipleWindows") {
435 prefs_.supportsMultipleWindows = value->BooleanValue();
436 } else if (key == "WebKitDisplayImagesKey") {
437 prefs_.loadsImagesAutomatically = value->BooleanValue();
438 } else if (key == "WebKitPluginsEnabled") {
439 prefs_.pluginsEnabled = value->BooleanValue();
440 } else if (key == "WebKitJavaEnabled") {
441 prefs_.javaEnabled = value->BooleanValue();
442 } else if (key == "WebKitUsesPageCachePreferenceKey") {
443 prefs_.usesPageCache = value->BooleanValue();
444 } else if (key == "WebKitPageCacheSupportsPluginsPreferenceKey") {
445 prefs_.pageCacheSupportsPlugins = value->BooleanValue();
446 } else if (key == "WebKitOfflineWebApplicationCacheEnabled") {
447 prefs_.offlineWebApplicationCacheEnabled = value->BooleanValue();
448 } else if (key == "WebKitTabToLinksPreferenceKey") {
449 prefs_.tabsToLinks = value->BooleanValue();
450 } else if (key == "WebKitWebGLEnabled") {
451 prefs_.experimentalWebGLEnabled = value->BooleanValue();
452 } else if (key == "WebKitCSSRegionsEnabled") {
453 prefs_.experimentalCSSRegionsEnabled = value->BooleanValue();
454 } else if (key == "WebKitCSSGridLayoutEnabled") {
455 prefs_.experimentalCSSGridLayoutEnabled = value->BooleanValue();
456 } else if (key == "WebKitHyperlinkAuditingEnabled") {
457 prefs_.hyperlinkAuditingEnabled = value->BooleanValue();
458 } else if (key == "WebKitEnableCaretBrowsing") {
459 prefs_.caretBrowsingEnabled = value->BooleanValue();
460 } else if (key == "WebKitAllowDisplayingInsecureContent") {
461 prefs_.allowDisplayOfInsecureContent = value->BooleanValue();
462 } else if (key == "WebKitAllowRunningInsecureContent") {
463 prefs_.allowRunningOfInsecureContent = value->BooleanValue();
464 } else if (key == "WebKitCSSCustomFilterEnabled") {
465 prefs_.cssCustomFilterEnabled = value->BooleanValue();
466 } else if (key == "WebKitShouldRespectImageOrientation") {
467 prefs_.shouldRespectImageOrientation = value->BooleanValue();
468 } else if (key == "WebKitWebAudioEnabled") {
469 DCHECK(value->BooleanValue());
470 } else {
471 std::string message("CONSOLE MESSAGE: Invalid name for preference: ");
472 printMessage(message + key + "\n");
473 }
474 applyPreferences();
475}
476
[email protected]0b0e31462012-12-18 21:57:18477void WebKitTestRunner::DumpEditingCallbacks() {
478 dump_editing_callbacks_ = true;
479}
480
[email protected]09c48ef52013-01-09 12:25:07481void WebKitTestRunner::DumpFrameLoadCallbacks() {
482 dump_frame_load_callbacks_ = true;
483}
484
485void WebKitTestRunner::DumpUserGestureInFrameLoadCallbacks() {
486 dump_user_gesture_in_frame_load_callbacks_ = true;
487}
488
489void WebKitTestRunner::StopProvisionalFrameLoads() {
490 stop_provisional_frame_loads_ = true;
491}
492
493void WebKitTestRunner::DumpTitleChanges() {
494 dump_title_changes_ = true;
495}
496
[email protected]43c7e6952012-12-11 13:50:51497void WebKitTestRunner::NotImplemented(const std::string& object,
498 const std::string& method) {
[email protected]2dd3a27a2012-12-10 11:24:16499 Send(new ShellViewHostMsg_NotImplemented(routing_id(), object, method));
500}
501
[email protected]324825d2012-11-30 12:37:15502void WebKitTestRunner::Reset() {
[email protected]b3dbcb5e2012-12-17 00:16:37503 prefs_.reset();
[email protected]324825d2012-11-30 12:37:15504 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47505 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]324825d2012-11-30 12:37:15506 render_view()->SetWebkitPreferences(prefs);
[email protected]0b0e31462012-12-18 21:57:18507 dump_editing_callbacks_ = false;
[email protected]09c48ef52013-01-09 12:25:07508 dump_frame_load_callbacks_ = false;
509 dump_user_gesture_in_frame_load_callbacks_ = false;
510 stop_provisional_frame_loads_ = false;
511 dump_title_changes_ = false;
512 test_is_running_ = true;
513 wait_until_done_ = false;
[email protected]324825d2012-11-30 12:37:15514}
515
[email protected]b2324b092012-11-01 10:34:11516// Private methods -----------------------------------------------------------
517
[email protected]9b4c6cd2012-08-20 10:47:45518void WebKitTestRunner::OnCaptureTextDump(bool as_text,
[email protected]891f08f2012-08-28 22:00:44519 bool printing,
520 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06521 WebFrame* frame = render_view()->GetWebView()->mainFrame();
522 std::string dump;
523 if (as_text) {
524 dump = DumpFramesAsText(frame, printing, recursive);
525 } else {
526 WebFrame::RenderAsTextControls render_text_behavior =
527 WebFrame::RenderAsTextNormal;
528 if (printing)
529 render_text_behavior |= WebFrame::RenderAsTextPrinting;
530 dump = frame->renderTreeAsText(render_text_behavior).utf8();
531 dump.append(DumpFrameScrollPosition(frame, recursive));
532 }
[email protected]efb5f572012-01-29 10:57:33533 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31534}
535
[email protected]9b4c6cd2012-08-20 10:47:45536void WebKitTestRunner::OnCaptureImageDump(
[email protected]0799da02012-06-27 10:58:51537 const std::string& expected_pixel_hash) {
538 SkBitmap snapshot;
[email protected]b9fad24c2012-11-15 08:22:10539 PaintInvalidatedRegion();
540 CopyCanvasToBitmap(GetCanvas(), &snapshot);
[email protected]0799da02012-06-27 10:58:51541
542 SkAutoLockPixels snapshot_lock(snapshot);
543 base::MD5Digest digest;
[email protected]e225b922012-08-18 01:43:04544#if defined(OS_ANDROID)
545 // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA.
546 const uint8_t* raw_pixels =
547 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
548 size_t snapshot_size = snapshot.getSize();
549 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
550 for (size_t i = 0; i < snapshot_size; i += 4) {
551 reordered_pixels[i] = raw_pixels[i + 2];
552 reordered_pixels[i + 1] = raw_pixels[i + 1];
553 reordered_pixels[i + 2] = raw_pixels[i];
554 reordered_pixels[i + 3] = raw_pixels[i + 3];
555 }
556 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
557#else
[email protected]0799da02012-06-27 10:58:51558 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
[email protected]e225b922012-08-18 01:43:04559#endif
[email protected]0799da02012-06-27 10:58:51560 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
561
562 if (actual_pixel_hash == expected_pixel_hash) {
563 SkBitmap empty_image;
564 Send(new ShellViewHostMsg_ImageDump(
565 routing_id(), actual_pixel_hash, empty_image));
[email protected]e225b922012-08-18 01:43:04566 return;
[email protected]0799da02012-06-27 10:58:51567 }
568 Send(new ShellViewHostMsg_ImageDump(
569 routing_id(), actual_pixel_hash, snapshot));
570}
571
[email protected]0d2dfb92d2012-11-05 10:26:45572void WebKitTestRunner::OnSetCurrentWorkingDirectory(
573 const FilePath& current_working_directory) {
574 current_working_directory_ = current_working_directory;
575}
576
[email protected]b9fad24c2012-11-15 08:22:10577SkCanvas* WebKitTestRunner::GetCanvas() {
578 WebView* view = render_view()->GetWebView();
579 const WebSize& size = view->size();
580 float device_scale_factor = view->deviceScaleFactor();
581 int width = std::ceil(device_scale_factor * size.width);
582 int height = std::ceil(device_scale_factor * size.height);
583
584 if (canvas_ &&
585 canvas_->getDeviceSize().width() == width &&
586 canvas_->getDeviceSize().height() == height) {
587 return canvas_.get();
588 }
589 canvas_.reset(skia::CreatePlatformCanvas(
590 size.width, size.height, true, 0, skia::RETURN_NULL_ON_FAILURE));
591 return canvas_.get();
592}
593
594void WebKitTestRunner::PaintRect(const WebRect& rect) {
595 WebView* view = render_view()->GetWebView();
596 float device_scale_factor = view->deviceScaleFactor();
597 int scaled_x = device_scale_factor * rect.x;
598 int scaled_y = device_scale_factor * rect.y;
599 int scaled_width = std::ceil(device_scale_factor * rect.width);
600 int scaled_height = std::ceil(device_scale_factor * rect.height);
601 // TODO(jochen): Verify that the scaling is correct once the HiDPI tests
602 // actually work.
603 WebRect device_rect(scaled_x, scaled_y, scaled_width, scaled_height);
604 view->paint(webkit_glue::ToWebCanvas(GetCanvas()), device_rect);
605}
606
607void WebKitTestRunner::PaintInvalidatedRegion() {
608 WebView* view = render_view()->GetWebView();
609 view->animate(0.0);
610 view->layout();
611 const WebSize& widget_size = view->size();
612 WebRect client_rect(0, 0, widget_size.width, widget_size.height);
613
614 // Paint the canvas if necessary. Allow painting to generate extra rects
615 // for the first two calls. This is necessary because some WebCore rendering
616 // objects update their layout only when painted.
617 for (int i = 0; i < 3; ++i) {
618 // Make sure that paint_rect is always inside the RenderView's visible
619 // area.
620 WebRect paint_rect = proxy_->paintRect();
621 int left = std::max(paint_rect.x, client_rect.x);
622 int top = std::max(paint_rect.y, client_rect.y);
623 int right = std::min(paint_rect.x + paint_rect.width,
624 client_rect.x + client_rect.width);
625 int bottom = std::min(paint_rect.y + paint_rect.height,
626 client_rect.y + client_rect.height);
627 WebRect rect;
628 if (left < right && top < bottom)
629 rect = WebRect(left, top, right - left, bottom - top);
630 proxy_->setPaintRect(WebRect());
631 if (rect.isEmpty())
632 continue;
633 PaintRect(rect);
634 }
635 CHECK(proxy_->paintRect().isEmpty());
636}
637
638void WebKitTestRunner::DisplayRepaintMask() {
639 GetCanvas()->drawARGB(167, 0, 0, 0);
640}
641
[email protected]31d71b02012-01-26 03:42:31642} // namespace content