blob: 3a5404e2a3a1c6d6402787ba257a19ec248a0ffc [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]0c5f2222013-02-20 11:26:197#include <algorithm>
[email protected]833b700d2013-02-18 19:43:008#include <clocale>
[email protected]b9fad24c2012-11-15 08:22:109#include <cmath>
10
[email protected]09ec9f92012-12-17 09:46:3611#include "base/base64.h"
[email protected]29e82702013-02-14 12:56:4012#include "base/debug/debugger.h"
[email protected]0799da02012-06-27 10:58:5113#include "base/md5.h"
[email protected]e225b922012-08-18 01:43:0414#include "base/memory/scoped_ptr.h"
[email protected]b2324b092012-11-01 10:34:1115#include "base/message_loop.h"
[email protected]0c5f2222013-02-20 11:26:1916#include "base/string_util.h"
[email protected]c272c5b2012-06-06 09:01:0617#include "base/stringprintf.h"
[email protected]0d2dfb92d2012-11-05 10:26:4518#include "base/sys_string_conversions.h"
[email protected]b2324b092012-11-01 10:34:1119#include "base/time.h"
[email protected]0d2dfb92d2012-11-05 10:26:4520#include "base/utf_string_conversions.h"
[email protected]efb5f572012-01-29 10:57:3321#include "content/public/renderer/render_view.h"
[email protected]f322356c2013-03-01 07:54:0222#include "content/public/renderer/render_view_visitor.h"
[email protected]e5196922012-12-11 22:18:2523#include "content/public/test/layouttest_support.h"
[email protected]efb5f572012-01-29 10:57:3324#include "content/shell/shell_messages.h"
[email protected]b2324b092012-11-01 10:34:1125#include "content/shell/shell_render_process_observer.h"
[email protected]55915a72012-12-18 11:55:2526#include "content/shell/webkit_test_helpers.h"
[email protected]9d9ec982013-01-10 18:53:3927#include "net/base/net_errors.h"
[email protected]0d2dfb92d2012-11-05 10:26:4528#include "net/base/net_util.h"
[email protected]0799da02012-06-27 10:58:5129#include "skia/ext/platform_canvas.h"
[email protected]2a58eb12012-12-18 07:56:4030#include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
[email protected]87884c42013-01-10 01:56:2731#include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h"
32#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
33#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
34#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
35#include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
[email protected]9d9ec982013-01-10 18:53:3936#include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h"
[email protected]0ac1a032013-02-12 12:53:3737#include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
[email protected]893fc6362013-02-18 13:24:0538#include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBufferView.h"
[email protected]b2324b092012-11-01 10:34:1139#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
[email protected]0ac1a032013-02-12 12:53:3740#include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
[email protected]001841c92012-12-11 17:00:1341#include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h"
[email protected]29e82702013-02-14 12:56:4042#include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientation.h"
[email protected]efb5f572012-01-29 10:57:3343#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
44#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
45#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
[email protected]8f722692013-02-23 14:14:0646#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h"
[email protected]b2324b092012-11-01 10:34:1147#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]efb5f572012-01-29 10:57:3348#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b2324b092012-11-01 10:34:1149#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]5f20df362013-02-15 19:48:1950#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestInterfaces.h"
[email protected]b9fad24c2012-11-15 08:22:1051#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]5f20df362013-02-15 19:48:1952#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestRunner.h"
[email protected]c24b40222013-02-20 11:45:4253#include "ui/gfx/rect.h"
[email protected]984a5f72012-12-13 21:41:5054#include "webkit/base/file_path_string_conversions.h"
[email protected]8f722692013-02-23 14:14:0655#include "webkit/glue/glue_serialize.h"
[email protected]0799da02012-06-27 10:58:5156#include "webkit/glue/webkit_glue.h"
[email protected]324825d2012-11-30 12:37:1557#include "webkit/glue/webpreferences.h"
[email protected]efb5f572012-01-29 10:57:3358
[email protected]2a58eb12012-12-18 07:56:4059using WebKit::Platform;
[email protected]893fc6362013-02-18 13:24:0560using WebKit::WebArrayBufferView;
[email protected]b2324b092012-11-01 10:34:1161using WebKit::WebContextMenuData;
[email protected]001841c92012-12-11 17:00:1362using WebKit::WebDevToolsAgent;
[email protected]29e82702013-02-14 12:56:4063using WebKit::WebDeviceOrientation;
[email protected]efb5f572012-01-29 10:57:3364using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1165using WebKit::WebFrame;
66using WebKit::WebGamepads;
[email protected]8f722692013-02-23 14:14:0667using WebKit::WebHistoryItem;
[email protected]0799da02012-06-27 10:58:5168using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0669using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1170using WebKit::WebString;
[email protected]09ec9f92012-12-17 09:46:3671using WebKit::WebURL;
[email protected]9d9ec982013-01-10 18:53:3972using WebKit::WebURLError;
[email protected]b2324b092012-11-01 10:34:1173using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5174using WebKit::WebView;
[email protected]b3dbcb5e2012-12-17 00:16:3775using WebTestRunner::WebPreferences;
[email protected]b2324b092012-11-01 10:34:1176using WebTestRunner::WebTask;
[email protected]5f20df362013-02-15 19:48:1977using WebTestRunner::WebTestInterfaces;
[email protected]9063bf62013-02-20 19:42:0178using WebTestRunner::WebTestProxyBase;
[email protected]efb5f572012-01-29 10:57:3379
[email protected]31d71b02012-01-26 03:42:3180namespace content {
81
[email protected]efb5f572012-01-29 10:57:3382namespace {
83
[email protected]ab683b22013-03-04 09:10:3184int kDefaultLayoutTestTimeoutMs = 30 * 1000;
85
[email protected]b2324b092012-11-01 10:34:1186void InvokeTaskHelper(void* context) {
87 WebTask* task = reinterpret_cast<WebTask*>(context);
88 task->run();
89 delete task;
90}
91
[email protected]0799da02012-06-27 10:58:5192#if !defined(OS_MACOSX)
93void MakeBitmapOpaque(SkBitmap* bitmap) {
94 SkAutoLockPixels lock(*bitmap);
95 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
96 for (int y = 0; y < bitmap->height(); ++y) {
97 uint32_t* row = bitmap->getAddr32(0, y);
98 for (int x = 0; x < bitmap->width(); ++x)
99 row[x] |= 0xFF000000; // Set alpha bits to 1.
100 }
101}
102#endif
103
[email protected]b9fad24c2012-11-15 08:22:10104void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56105 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51106 const SkBitmap& bitmap = device->accessBitmap(false);
107 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
108
109#if !defined(OS_MACOSX)
110 // Only the expected PNGs for Mac have a valid alpha channel.
111 MakeBitmapOpaque(snapshot);
112#endif
113
114}
115
[email protected]f322356c2013-03-01 07:54:02116class SyncNavigationStateVisitor : public RenderViewVisitor {
117 public:
118 SyncNavigationStateVisitor() {}
119 virtual ~SyncNavigationStateVisitor() {}
120
121 virtual bool Visit(RenderView* render_view) OVERRIDE {
122 SyncNavigationState(render_view);
123 return true;
124 }
125 private:
126 DISALLOW_COPY_AND_ASSIGN(SyncNavigationStateVisitor);
127};
128
[email protected]95026a02013-03-05 08:34:30129class ProxyToRenderViewVisitor : public RenderViewVisitor {
130 public:
131 explicit ProxyToRenderViewVisitor(WebTestProxyBase* proxy)
132 : proxy_(proxy),
133 render_view_(NULL) {
134 }
135 virtual ~ProxyToRenderViewVisitor() {}
136
137 RenderView* render_view() const { return render_view_; }
138
139 virtual bool Visit(RenderView* render_view) OVERRIDE {
140 WebKitTestRunner* test_runner = WebKitTestRunner::Get(render_view);
141 if (!test_runner) {
142 NOTREACHED();
143 return true;
144 }
145 if (test_runner->proxy() == proxy_) {
146 render_view_ = render_view;
147 return false;
148 }
149 return true;
150 }
151 private:
152 WebTestProxyBase* proxy_;
153 RenderView* render_view_;
154
155 DISALLOW_COPY_AND_ASSIGN(ProxyToRenderViewVisitor);
156};
157
[email protected]efb5f572012-01-29 10:57:33158} // namespace
[email protected]cccb5cf2012-06-06 22:20:04159
[email protected]9b4c6cd2012-08-20 10:47:45160WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]5f20df362013-02-15 19:48:19161 : RenderViewObserver(render_view),
[email protected]2025a2f42013-02-18 07:25:41162 RenderViewObserverTracker<WebKitTestRunner>(render_view),
[email protected]ab683b22013-03-04 09:10:31163 proxy_(NULL),
[email protected]95026a02013-03-05 08:34:30164 focused_view_(NULL),
[email protected]ab683b22013-03-04 09:10:31165 enable_pixel_dumping_(true),
166 layout_test_timeout_(kDefaultLayoutTestTimeoutMs),
167 allow_external_pages_(false),
[email protected]5f20df362013-02-15 19:48:19168 is_main_window_(false) {
[email protected]31d71b02012-01-26 03:42:31169}
170
[email protected]9b4c6cd2012-08-20 10:47:45171WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31172}
173
[email protected]b2324b092012-11-01 10:34:11174// WebTestDelegate -----------------------------------------------------------
175
[email protected]b2324b092012-11-01 10:34:11176void WebKitTestRunner::clearEditCommand() {
177 render_view()->ClearEditCommands();
178}
179
180void WebKitTestRunner::setEditCommand(const std::string& name,
181 const std::string& value) {
182 render_view()->SetEditCommandForNextKeyEvent(name, value);
183}
184
[email protected]b2324b092012-11-01 10:34:11185void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25186 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11187}
188
189void WebKitTestRunner::printMessage(const std::string& message) {
190 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
191}
192
193void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40194 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11195}
196
197void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
198 MessageLoop::current()->PostDelayedTask(
199 FROM_HERE,
200 base::Bind(&WebTask::run, base::Owned(task)),
201 base::TimeDelta::FromMilliseconds(ms));
202}
203
204WebString WebKitTestRunner::registerIsolatedFileSystem(
205 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]d30a36f2013-02-07 04:16:26206 std::vector<base::FilePath> files;
[email protected]984a5f72012-12-13 21:41:50207 for (size_t i = 0; i < absolute_filenames.size(); ++i)
208 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
209 std::string filesystem_id;
210 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
211 routing_id(), files, &filesystem_id));
212 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11213}
214
215long long WebKitTestRunner::getCurrentTimeInMillisecond() {
216 return base::TimeTicks::Now().ToInternalValue() /
217 base::Time::kMicrosecondsPerMillisecond;
218}
219
220WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
221 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45222#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26223 base::FilePath path(UTF8ToWide(utf8_path));
[email protected]0d2dfb92d2012-11-05 10:26:45224#else
[email protected]d30a36f2013-02-07 04:16:26225 base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
[email protected]0d2dfb92d2012-11-05 10:26:45226#endif
227 if (!path.IsAbsolute()) {
228 GURL base_url =
229 net::FilePathToFileURL(current_working_directory_.Append(
230 FILE_PATH_LITERAL("foo")));
231 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
232 }
[email protected]984a5f72012-12-13 21:41:50233 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11234}
235
[email protected]09ec9f92012-12-17 09:46:36236WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
[email protected]d30a36f2013-02-07 04:16:26237 base::FilePath local_path;
[email protected]09ec9f92012-12-17 09:46:36238 if (!net::FileURLToFilePath(file_url, &local_path))
239 return WebURL();
240
241 std::string contents;
242 Send(new ShellViewHostMsg_ReadFileToString(
243 routing_id(), local_path, &contents));
244
245 std::string contents_base64;
246 if (!base::Base64Encode(contents, &contents_base64))
247 return WebURL();
248
249 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
250 return WebURL(GURL(data_url_prefix + contents_base64));
251}
252
253WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
254 const char kPrefix[] = "file:///tmp/LayoutTests/";
255 const int kPrefixLen = arraysize(kPrefix) - 1;
256
257 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
258 return WebURL(GURL(utf8_url));
259
[email protected]d30a36f2013-02-07 04:16:26260 base::FilePath replace_path =
[email protected]09ec9f92012-12-17 09:46:36261 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
262 FILE_PATH_LITERAL("LayoutTests/"));
263#if defined(OS_WIN)
264 std::string utf8_path = WideToUTF8(replace_path.value());
265#else
266 std::string utf8_path =
267 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
268#endif
269 std::string new_url =
270 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
271 return WebURL(GURL(new_url));
272}
273
[email protected]b3dbcb5e2012-12-17 00:16:37274WebPreferences* WebKitTestRunner::preferences() {
275 return &prefs_;
276}
277
278void WebKitTestRunner::applyPreferences() {
279 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47280 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37281 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25282 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37283}
284
[email protected]9d9ec982013-01-10 18:53:39285std::string WebKitTestRunner::makeURLErrorDescription(
286 const WebURLError& error) {
287 std::string domain = error.domain.utf8();
288 int code = error.reason;
289
290 if (domain == net::kErrorDomain) {
291 domain = "NSURLErrorDomain";
292 switch (error.reason) {
293 case net::ERR_ABORTED:
294 code = -999; // NSURLErrorCancelled
295 break;
296 case net::ERR_UNSAFE_PORT:
297 // Our unsafe port checking happens at the network stack level, but we
298 // make this translation here to match the behavior of stock WebKit.
299 domain = "WebKitErrorDomain";
300 code = 103;
301 break;
302 case net::ERR_ADDRESS_INVALID:
303 case net::ERR_ADDRESS_UNREACHABLE:
304 case net::ERR_NETWORK_ACCESS_DENIED:
305 code = -1004; // NSURLErrorCannotConnectToHost
306 break;
307 }
308 } else {
309 DLOG(WARNING) << "Unknown error domain";
310 }
311
312 return base::StringPrintf("<NSError domain %s, code %d, failing URL \"%s\">",
313 domain.c_str(), code, error.unreachableURL.spec().data());
314}
315
[email protected]29e82702013-02-14 12:56:40316void WebKitTestRunner::setClientWindowRect(const WebRect& rect) {
[email protected]c24b40222013-02-20 11:45:42317 Send(new ShellViewHostMsg_SetClientWindowRect(
318 routing_id(), gfx::Rect(rect)));
[email protected]29e82702013-02-14 12:56:40319}
320
321void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06322 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40323}
324
325void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06326 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40327}
328
329void WebKitTestRunner::evaluateInWebInspector(long call_id,
330 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06331 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
332 if (agent)
333 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40334}
335
336void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00337 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40338}
339
340void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00341 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40342}
343
344void WebKitTestRunner::setDeviceScaleFactor(float factor) {
[email protected]db6769c2013-02-24 14:59:14345 render_view()->GetWebView()->setDeviceScaleFactor(factor);
[email protected]29e82702013-02-14 12:56:40346}
347
[email protected]83654ec2013-03-04 15:58:53348void WebKitTestRunner::setFocus(WebTestProxyBase* proxy, bool focus) {
[email protected]95026a02013-03-05 08:34:30349 ProxyToRenderViewVisitor visitor(proxy);
350 RenderView::ForEach(&visitor);
351 if (!visitor.render_view()) {
352 NOTREACHED();
353 return;
354 }
355
356 // Check whether the focused view was closed meanwhile.
357 if (!WebKitTestRunner::Get(focused_view_))
358 focused_view_ = NULL;
359
360 if (focus) {
361 if (focused_view_ != visitor.render_view()) {
362 if (focused_view_)
363 SetFocusAndActivate(focused_view_, false);
364 SetFocusAndActivate(visitor.render_view(), true);
365 focused_view_ = visitor.render_view();
366 }
367 } else {
368 if (focused_view_ == visitor.render_view()) {
369 SetFocusAndActivate(visitor.render_view(), false);
370 focused_view_ = NULL;
371 }
372 }
[email protected]83654ec2013-03-04 15:58:53373}
374
[email protected]29e82702013-02-14 12:56:40375void WebKitTestRunner::setFocus(bool focus) {
[email protected]83654ec2013-03-04 15:58:53376 // TODO(jochen): Remove once the new WebKit API is rolled.
[email protected]29e82702013-02-14 12:56:40377}
378
379void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00380 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40381}
382
383std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19384#if defined(OS_WIN)
385 if (resource.find("/tmp/") == 0) {
386 // We want a temp file.
387 GURL base_url = net::FilePathToFileURL(temp_path_);
388 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
389 }
390#endif
391
392 // Some layout tests use file://// which we resolve as a UNC path. Normalize
393 // them to just file:///.
394 std::string result = resource;
395 while (StringToLowerASCII(result).find("file:////") == 0) {
396 result = result.substr(0, strlen("file:///")) +
397 result.substr(strlen("file:////"));
398 }
399 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40400}
401
402void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00403 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40404}
405
[email protected]29e82702013-02-14 12:56:40406void WebKitTestRunner::testFinished() {
[email protected]5f20df362013-02-15 19:48:19407 if (!is_main_window_)
408 return;
409 WebTestInterfaces* interfaces =
410 ShellRenderProcessObserver::GetInstance()->test_interfaces();
411 interfaces->setTestIsRunning(false);
[email protected]8f722692013-02-23 14:14:06412 if (interfaces->testRunner()->shouldDumpBackForwardList()) {
[email protected]f322356c2013-03-01 07:54:02413 SyncNavigationStateVisitor visitor;
414 RenderView::ForEach(&visitor);
[email protected]8f722692013-02-23 14:14:06415 Send(new ShellViewHostMsg_CaptureSessionHistory(routing_id()));
416 } else {
417 CaptureDump();
418 }
[email protected]29e82702013-02-14 12:56:40419}
420
421void WebKitTestRunner::testTimedOut() {
[email protected]5f20df362013-02-15 19:48:19422 if (!is_main_window_)
423 return;
424 WebTestInterfaces* interfaces =
425 ShellRenderProcessObserver::GetInstance()->test_interfaces();
426 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40427 Send(new ShellViewHostMsg_TestFinished(routing_id(), true));
428}
429
430bool WebKitTestRunner::isBeingDebugged() {
431 return base::debug::BeingDebugged();
432}
433
434int WebKitTestRunner::layoutTestTimeout() {
435 return layout_test_timeout_;
436}
437
438void WebKitTestRunner::closeRemainingWindows() {
[email protected]b778e182013-03-04 13:32:16439 Send(new ShellViewHostMsg_CloseRemainingWindows(routing_id()));
[email protected]29e82702013-02-14 12:56:40440}
441
442int WebKitTestRunner::navigationEntryCount() {
[email protected]b283d292013-02-21 08:40:34443 return GetLocalSessionHistoryLength(render_view());
[email protected]29e82702013-02-14 12:56:40444}
445
[email protected]29e82702013-02-14 12:56:40446void WebKitTestRunner::goToOffset(int offset) {
[email protected]d2494ff2013-02-20 08:22:37447 Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset));
[email protected]29e82702013-02-14 12:56:40448}
449
450void WebKitTestRunner::reload() {
[email protected]d2494ff2013-02-20 08:22:37451 Send(new ShellViewHostMsg_Reload(routing_id()));
[email protected]29e82702013-02-14 12:56:40452}
453
454void WebKitTestRunner::loadURLForFrame(const WebURL& url,
455 const std::string& frame_name) {
[email protected]d2494ff2013-02-20 08:22:37456 Send(new ShellViewHostMsg_LoadURLForFrame(
457 routing_id(), url, frame_name));
[email protected]29e82702013-02-14 12:56:40458}
459
460bool WebKitTestRunner::allowExternalPages() {
461 return allow_external_pages_;
462}
463
464void WebKitTestRunner::captureHistoryForWindow(
[email protected]9063bf62013-02-20 19:42:01465 WebTestProxyBase* proxy,
[email protected]29e82702013-02-14 12:56:40466 WebVector<WebKit::WebHistoryItem>* history,
467 size_t* currentEntryIndex) {
[email protected]8f722692013-02-23 14:14:06468 size_t pos = 0;
469 std::vector<int>::iterator id;
470 for (id = routing_ids_.begin(); id != routing_ids_.end(); ++id, ++pos) {
471 RenderView* render_view = RenderView::FromRoutingID(*id);
472 if (!render_view) {
473 NOTREACHED();
474 continue;
475 }
476 if (WebKitTestRunner::Get(render_view)->proxy() == proxy)
477 break;
478 }
479
480 if (id == routing_ids_.end()) {
481 NOTREACHED();
482 return;
483 }
484 size_t num_entries = session_histories_[pos].size();
485 *currentEntryIndex = current_entry_indexes_[pos];
486 WebVector<WebHistoryItem> result(num_entries);
487 for (size_t entry = 0; entry < num_entries; ++entry) {
488 result[entry] =
489 webkit_glue::HistoryItemFromString(session_histories_[pos][entry]);
490 }
491 history->swap(result);
[email protected]29e82702013-02-14 12:56:40492}
493
[email protected]b2324b092012-11-01 10:34:11494// RenderViewObserver --------------------------------------------------------
495
[email protected]9b4c6cd2012-08-20 10:47:45496void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]b2324b092012-11-01 10:34:11497 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56498}
499
[email protected]9b4c6cd2012-08-20 10:47:45500bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33501 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45502 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37503 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
504 OnSetTestConfiguration)
[email protected]8f722692013-02-23 14:14:06505 IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory)
[email protected]efb5f572012-01-29 10:57:33506 IPC_MESSAGE_UNHANDLED(handled = false)
507 IPC_END_MESSAGE_MAP()
508
509 return handled;
510}
511
[email protected]b9fad24c2012-11-15 08:22:10512// Public methods - -----------------------------------------------------------
513
[email protected]324825d2012-11-30 12:37:15514void WebKitTestRunner::Reset() {
[email protected]ab683b22013-03-04 09:10:31515 // The proxy_ is always non-NULL, it is set right after construction.
516 proxy_->reset();
[email protected]b3dbcb5e2012-12-17 00:16:37517 prefs_.reset();
[email protected]0ac1a032013-02-12 12:53:37518 enable_pixel_dumping_ = true;
[email protected]ab683b22013-03-04 09:10:31519 layout_test_timeout_ = kDefaultLayoutTestTimeoutMs;
[email protected]0ac1a032013-02-12 12:53:37520 allow_external_pages_ = false;
521 expected_pixel_hash_ = std::string();
[email protected]8f722692013-02-23 14:14:06522 routing_ids_.clear();
523 session_histories_.clear();
524 current_entry_indexes_.clear();
[email protected]324825d2012-11-30 12:37:15525}
526
[email protected]b2324b092012-11-01 10:34:11527// Private methods -----------------------------------------------------------
528
[email protected]0ac1a032013-02-12 12:53:37529void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19530 WebTestInterfaces* interfaces =
531 ShellRenderProcessObserver::GetInstance()->test_interfaces();
532
[email protected]893fc6362013-02-18 13:24:05533 if (interfaces->testRunner()->shouldDumpAsAudio()) {
534 const WebArrayBufferView* audio_data =
535 interfaces->testRunner()->audioData();
536 std::vector<unsigned char> vector_data(
537 static_cast<const unsigned char*>(audio_data->baseAddress()),
538 static_cast<const unsigned char*>(audio_data->baseAddress()) +
539 audio_data->byteLength());
540 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
541 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
542 return;
543 }
544
[email protected]5f20df362013-02-15 19:48:19545 Send(
546 new ShellViewHostMsg_TextDump(routing_id(), proxy()->captureTree(false)));
547
[email protected]16000762013-03-04 12:43:38548 if (enable_pixel_dumping_ &&
549 interfaces->testRunner()->shouldGeneratePixelResults()) {
[email protected]5f20df362013-02-15 19:48:19550 SkBitmap snapshot;
551 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
552
553 SkAutoLockPixels snapshot_lock(snapshot);
554 base::MD5Digest digest;
555#if defined(OS_ANDROID)
556 // On Android, pixel layout is RGBA, however, other Chrome platforms use
557 // BGRA.
558 const uint8_t* raw_pixels =
559 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
560 size_t snapshot_size = snapshot.getSize();
561 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
562 for (size_t i = 0; i < snapshot_size; i += 4) {
563 reordered_pixels[i] = raw_pixels[i + 2];
564 reordered_pixels[i + 1] = raw_pixels[i + 1];
565 reordered_pixels[i + 2] = raw_pixels[i];
566 reordered_pixels[i + 3] = raw_pixels[i + 3];
567 }
568 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
569#else
570 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
571#endif
572 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
573
574 if (actual_pixel_hash == expected_pixel_hash_) {
575 SkBitmap empty_image;
576 Send(new ShellViewHostMsg_ImageDump(
577 routing_id(), actual_pixel_hash, empty_image));
578 } else {
579 Send(new ShellViewHostMsg_ImageDump(
580 routing_id(), actual_pixel_hash, snapshot));
581 }
[email protected]0ac1a032013-02-12 12:53:37582 }
[email protected]5f20df362013-02-15 19:48:19583
[email protected]0ac1a032013-02-12 12:53:37584 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
585}
586
[email protected]0ac1a032013-02-12 12:53:37587void WebKitTestRunner::OnSetTestConfiguration(
[email protected]26a51632013-02-18 09:48:56588 const ShellViewMsg_SetTestConfiguration_Params& params) {
589 current_working_directory_ = params.current_working_directory;
[email protected]0c5f2222013-02-20 11:26:19590 temp_path_ = params.temp_path;
[email protected]26a51632013-02-18 09:48:56591 enable_pixel_dumping_ = params.enable_pixel_dumping;
592 layout_test_timeout_ = params.layout_test_timeout;
593 allow_external_pages_ = params.allow_external_pages;
594 expected_pixel_hash_ = params.expected_pixel_hash;
[email protected]5f20df362013-02-15 19:48:19595 is_main_window_ = true;
596
[email protected]95026a02013-03-05 08:34:30597 setFocus(proxy_, true);
598
[email protected]5f20df362013-02-15 19:48:19599 WebTestInterfaces* interfaces =
600 ShellRenderProcessObserver::GetInstance()->test_interfaces();
601 interfaces->setTestIsRunning(true);
[email protected]26a51632013-02-18 09:48:56602 interfaces->configureForTestWithURL(params.test_url, enable_pixel_dumping_);
[email protected]0d2dfb92d2012-11-05 10:26:45603}
604
[email protected]8f722692013-02-23 14:14:06605void WebKitTestRunner::OnSessionHistory(
606 const std::vector<int>& routing_ids,
607 const std::vector<std::vector<std::string> >& session_histories,
608 const std::vector<unsigned>& current_entry_indexes) {
609 routing_ids_ = routing_ids;
610 session_histories_ = session_histories;
611 current_entry_indexes_ = current_entry_indexes;
612 CaptureDump();
613}
614
[email protected]31d71b02012-01-26 03:42:31615} // namespace content