blob: 3c670ab141f41e992c7568d0fbf2c1e80843c171 [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]b2324b092012-11-01 10:34:1184void InvokeTaskHelper(void* context) {
85 WebTask* task = reinterpret_cast<WebTask*>(context);
86 task->run();
87 delete task;
88}
89
[email protected]0799da02012-06-27 10:58:5190#if !defined(OS_MACOSX)
91void MakeBitmapOpaque(SkBitmap* bitmap) {
92 SkAutoLockPixels lock(*bitmap);
93 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
94 for (int y = 0; y < bitmap->height(); ++y) {
95 uint32_t* row = bitmap->getAddr32(0, y);
96 for (int x = 0; x < bitmap->width(); ++x)
97 row[x] |= 0xFF000000; // Set alpha bits to 1.
98 }
99}
100#endif
101
[email protected]b9fad24c2012-11-15 08:22:10102void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56103 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51104 const SkBitmap& bitmap = device->accessBitmap(false);
105 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
106
107#if !defined(OS_MACOSX)
108 // Only the expected PNGs for Mac have a valid alpha channel.
109 MakeBitmapOpaque(snapshot);
110#endif
111
112}
113
[email protected]f322356c2013-03-01 07:54:02114class SyncNavigationStateVisitor : public RenderViewVisitor {
115 public:
116 SyncNavigationStateVisitor() {}
117 virtual ~SyncNavigationStateVisitor() {}
118
119 virtual bool Visit(RenderView* render_view) OVERRIDE {
120 SyncNavigationState(render_view);
121 return true;
122 }
123 private:
124 DISALLOW_COPY_AND_ASSIGN(SyncNavigationStateVisitor);
125};
126
[email protected]95026a02013-03-05 08:34:30127class ProxyToRenderViewVisitor : public RenderViewVisitor {
128 public:
129 explicit ProxyToRenderViewVisitor(WebTestProxyBase* proxy)
130 : proxy_(proxy),
131 render_view_(NULL) {
132 }
133 virtual ~ProxyToRenderViewVisitor() {}
134
135 RenderView* render_view() const { return render_view_; }
136
137 virtual bool Visit(RenderView* render_view) OVERRIDE {
138 WebKitTestRunner* test_runner = WebKitTestRunner::Get(render_view);
139 if (!test_runner) {
140 NOTREACHED();
141 return true;
142 }
143 if (test_runner->proxy() == proxy_) {
144 render_view_ = render_view;
145 return false;
146 }
147 return true;
148 }
149 private:
150 WebTestProxyBase* proxy_;
151 RenderView* render_view_;
152
153 DISALLOW_COPY_AND_ASSIGN(ProxyToRenderViewVisitor);
154};
155
[email protected]efb5f572012-01-29 10:57:33156} // namespace
[email protected]cccb5cf2012-06-06 22:20:04157
[email protected]9b4c6cd2012-08-20 10:47:45158WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]5f20df362013-02-15 19:48:19159 : RenderViewObserver(render_view),
[email protected]2025a2f42013-02-18 07:25:41160 RenderViewObserverTracker<WebKitTestRunner>(render_view),
[email protected]ab683b22013-03-04 09:10:31161 proxy_(NULL),
[email protected]95026a02013-03-05 08:34:30162 focused_view_(NULL),
[email protected]d679c862013-03-07 10:31:41163 is_main_window_(false) {
[email protected]31d71b02012-01-26 03:42:31164}
165
[email protected]9b4c6cd2012-08-20 10:47:45166WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31167}
168
[email protected]b2324b092012-11-01 10:34:11169// WebTestDelegate -----------------------------------------------------------
170
[email protected]b2324b092012-11-01 10:34:11171void WebKitTestRunner::clearEditCommand() {
172 render_view()->ClearEditCommands();
173}
174
175void WebKitTestRunner::setEditCommand(const std::string& name,
176 const std::string& value) {
177 render_view()->SetEditCommandForNextKeyEvent(name, value);
178}
179
[email protected]b2324b092012-11-01 10:34:11180void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25181 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11182}
183
184void WebKitTestRunner::printMessage(const std::string& message) {
185 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
186}
187
188void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40189 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11190}
191
192void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
193 MessageLoop::current()->PostDelayedTask(
194 FROM_HERE,
195 base::Bind(&WebTask::run, base::Owned(task)),
196 base::TimeDelta::FromMilliseconds(ms));
197}
198
199WebString WebKitTestRunner::registerIsolatedFileSystem(
200 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]d30a36f2013-02-07 04:16:26201 std::vector<base::FilePath> files;
[email protected]984a5f72012-12-13 21:41:50202 for (size_t i = 0; i < absolute_filenames.size(); ++i)
203 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
204 std::string filesystem_id;
205 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
206 routing_id(), files, &filesystem_id));
207 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11208}
209
210long long WebKitTestRunner::getCurrentTimeInMillisecond() {
211 return base::TimeTicks::Now().ToInternalValue() /
212 base::Time::kMicrosecondsPerMillisecond;
213}
214
215WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
216 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45217#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26218 base::FilePath path(UTF8ToWide(utf8_path));
[email protected]0d2dfb92d2012-11-05 10:26:45219#else
[email protected]d30a36f2013-02-07 04:16:26220 base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
[email protected]0d2dfb92d2012-11-05 10:26:45221#endif
222 if (!path.IsAbsolute()) {
223 GURL base_url =
224 net::FilePathToFileURL(current_working_directory_.Append(
225 FILE_PATH_LITERAL("foo")));
226 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
227 }
[email protected]984a5f72012-12-13 21:41:50228 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11229}
230
[email protected]09ec9f92012-12-17 09:46:36231WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
[email protected]d30a36f2013-02-07 04:16:26232 base::FilePath local_path;
[email protected]09ec9f92012-12-17 09:46:36233 if (!net::FileURLToFilePath(file_url, &local_path))
234 return WebURL();
235
236 std::string contents;
237 Send(new ShellViewHostMsg_ReadFileToString(
238 routing_id(), local_path, &contents));
239
240 std::string contents_base64;
241 if (!base::Base64Encode(contents, &contents_base64))
242 return WebURL();
243
244 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
245 return WebURL(GURL(data_url_prefix + contents_base64));
246}
247
248WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
249 const char kPrefix[] = "file:///tmp/LayoutTests/";
250 const int kPrefixLen = arraysize(kPrefix) - 1;
251
252 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
253 return WebURL(GURL(utf8_url));
254
[email protected]d30a36f2013-02-07 04:16:26255 base::FilePath replace_path =
[email protected]09ec9f92012-12-17 09:46:36256 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
257 FILE_PATH_LITERAL("LayoutTests/"));
258#if defined(OS_WIN)
259 std::string utf8_path = WideToUTF8(replace_path.value());
260#else
261 std::string utf8_path =
262 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
263#endif
264 std::string new_url =
265 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
266 return WebURL(GURL(new_url));
267}
268
[email protected]b3dbcb5e2012-12-17 00:16:37269WebPreferences* WebKitTestRunner::preferences() {
270 return &prefs_;
271}
272
273void WebKitTestRunner::applyPreferences() {
274 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47275 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37276 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25277 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37278}
279
[email protected]9d9ec982013-01-10 18:53:39280std::string WebKitTestRunner::makeURLErrorDescription(
281 const WebURLError& error) {
282 std::string domain = error.domain.utf8();
283 int code = error.reason;
284
285 if (domain == net::kErrorDomain) {
286 domain = "NSURLErrorDomain";
287 switch (error.reason) {
288 case net::ERR_ABORTED:
289 code = -999; // NSURLErrorCancelled
290 break;
291 case net::ERR_UNSAFE_PORT:
292 // Our unsafe port checking happens at the network stack level, but we
293 // make this translation here to match the behavior of stock WebKit.
294 domain = "WebKitErrorDomain";
295 code = 103;
296 break;
297 case net::ERR_ADDRESS_INVALID:
298 case net::ERR_ADDRESS_UNREACHABLE:
299 case net::ERR_NETWORK_ACCESS_DENIED:
300 code = -1004; // NSURLErrorCannotConnectToHost
301 break;
302 }
303 } else {
304 DLOG(WARNING) << "Unknown error domain";
305 }
306
307 return base::StringPrintf("<NSError domain %s, code %d, failing URL \"%s\">",
308 domain.c_str(), code, error.unreachableURL.spec().data());
309}
310
[email protected]29e82702013-02-14 12:56:40311void WebKitTestRunner::setClientWindowRect(const WebRect& rect) {
[email protected]8be1c582013-03-06 00:55:03312 ForceResizeRenderView(render_view(), WebSize(rect.width, rect.height));
[email protected]29e82702013-02-14 12:56:40313}
314
315void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06316 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40317}
318
319void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06320 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40321}
322
323void WebKitTestRunner::evaluateInWebInspector(long call_id,
324 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06325 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
326 if (agent)
327 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40328}
329
330void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00331 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40332}
333
334void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00335 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40336}
337
338void WebKitTestRunner::setDeviceScaleFactor(float factor) {
[email protected]db6769c2013-02-24 14:59:14339 render_view()->GetWebView()->setDeviceScaleFactor(factor);
[email protected]29e82702013-02-14 12:56:40340}
341
[email protected]83654ec2013-03-04 15:58:53342void WebKitTestRunner::setFocus(WebTestProxyBase* proxy, bool focus) {
[email protected]95026a02013-03-05 08:34:30343 ProxyToRenderViewVisitor visitor(proxy);
344 RenderView::ForEach(&visitor);
345 if (!visitor.render_view()) {
346 NOTREACHED();
347 return;
348 }
349
350 // Check whether the focused view was closed meanwhile.
351 if (!WebKitTestRunner::Get(focused_view_))
352 focused_view_ = NULL;
353
354 if (focus) {
355 if (focused_view_ != visitor.render_view()) {
356 if (focused_view_)
357 SetFocusAndActivate(focused_view_, false);
358 SetFocusAndActivate(visitor.render_view(), true);
359 focused_view_ = visitor.render_view();
360 }
361 } else {
362 if (focused_view_ == visitor.render_view()) {
363 SetFocusAndActivate(visitor.render_view(), false);
364 focused_view_ = NULL;
365 }
366 }
[email protected]83654ec2013-03-04 15:58:53367}
368
[email protected]29e82702013-02-14 12:56:40369void WebKitTestRunner::setFocus(bool focus) {
[email protected]83654ec2013-03-04 15:58:53370 // TODO(jochen): Remove once the new WebKit API is rolled.
[email protected]29e82702013-02-14 12:56:40371}
372
373void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00374 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40375}
376
377std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19378#if defined(OS_WIN)
379 if (resource.find("/tmp/") == 0) {
380 // We want a temp file.
381 GURL base_url = net::FilePathToFileURL(temp_path_);
382 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
383 }
384#endif
385
386 // Some layout tests use file://// which we resolve as a UNC path. Normalize
387 // them to just file:///.
388 std::string result = resource;
389 while (StringToLowerASCII(result).find("file:////") == 0) {
390 result = result.substr(0, strlen("file:///")) +
391 result.substr(strlen("file:////"));
392 }
393 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40394}
395
396void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00397 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40398}
399
[email protected]29e82702013-02-14 12:56:40400void WebKitTestRunner::testFinished() {
[email protected]d679c862013-03-07 10:31:41401 if (!is_main_window_)
[email protected]5f20df362013-02-15 19:48:19402 return;
403 WebTestInterfaces* interfaces =
404 ShellRenderProcessObserver::GetInstance()->test_interfaces();
405 interfaces->setTestIsRunning(false);
[email protected]8f722692013-02-23 14:14:06406 if (interfaces->testRunner()->shouldDumpBackForwardList()) {
[email protected]f322356c2013-03-01 07:54:02407 SyncNavigationStateVisitor visitor;
408 RenderView::ForEach(&visitor);
[email protected]8f722692013-02-23 14:14:06409 Send(new ShellViewHostMsg_CaptureSessionHistory(routing_id()));
410 } else {
411 CaptureDump();
412 }
[email protected]29e82702013-02-14 12:56:40413}
414
415void WebKitTestRunner::testTimedOut() {
[email protected]d679c862013-03-07 10:31:41416 if (!is_main_window_)
[email protected]5f20df362013-02-15 19:48:19417 return;
418 WebTestInterfaces* interfaces =
419 ShellRenderProcessObserver::GetInstance()->test_interfaces();
420 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40421 Send(new ShellViewHostMsg_TestFinished(routing_id(), true));
422}
423
424bool WebKitTestRunner::isBeingDebugged() {
425 return base::debug::BeingDebugged();
426}
427
428int WebKitTestRunner::layoutTestTimeout() {
[email protected]4f92df82013-03-07 13:21:13429 return test_config_.layout_test_timeout;
[email protected]29e82702013-02-14 12:56:40430}
431
432void WebKitTestRunner::closeRemainingWindows() {
[email protected]b778e182013-03-04 13:32:16433 Send(new ShellViewHostMsg_CloseRemainingWindows(routing_id()));
[email protected]29e82702013-02-14 12:56:40434}
435
436int WebKitTestRunner::navigationEntryCount() {
[email protected]b283d292013-02-21 08:40:34437 return GetLocalSessionHistoryLength(render_view());
[email protected]29e82702013-02-14 12:56:40438}
439
[email protected]29e82702013-02-14 12:56:40440void WebKitTestRunner::goToOffset(int offset) {
[email protected]d2494ff2013-02-20 08:22:37441 Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset));
[email protected]29e82702013-02-14 12:56:40442}
443
444void WebKitTestRunner::reload() {
[email protected]d2494ff2013-02-20 08:22:37445 Send(new ShellViewHostMsg_Reload(routing_id()));
[email protected]29e82702013-02-14 12:56:40446}
447
448void WebKitTestRunner::loadURLForFrame(const WebURL& url,
449 const std::string& frame_name) {
[email protected]d2494ff2013-02-20 08:22:37450 Send(new ShellViewHostMsg_LoadURLForFrame(
451 routing_id(), url, frame_name));
[email protected]29e82702013-02-14 12:56:40452}
453
454bool WebKitTestRunner::allowExternalPages() {
[email protected]4f92df82013-03-07 13:21:13455 return test_config_.allow_external_pages;
[email protected]29e82702013-02-14 12:56:40456}
457
458void WebKitTestRunner::captureHistoryForWindow(
[email protected]9063bf62013-02-20 19:42:01459 WebTestProxyBase* proxy,
[email protected]29e82702013-02-14 12:56:40460 WebVector<WebKit::WebHistoryItem>* history,
461 size_t* currentEntryIndex) {
[email protected]8f722692013-02-23 14:14:06462 size_t pos = 0;
463 std::vector<int>::iterator id;
464 for (id = routing_ids_.begin(); id != routing_ids_.end(); ++id, ++pos) {
465 RenderView* render_view = RenderView::FromRoutingID(*id);
466 if (!render_view) {
467 NOTREACHED();
468 continue;
469 }
470 if (WebKitTestRunner::Get(render_view)->proxy() == proxy)
471 break;
472 }
473
474 if (id == routing_ids_.end()) {
475 NOTREACHED();
476 return;
477 }
478 size_t num_entries = session_histories_[pos].size();
479 *currentEntryIndex = current_entry_indexes_[pos];
480 WebVector<WebHistoryItem> result(num_entries);
481 for (size_t entry = 0; entry < num_entries; ++entry) {
482 result[entry] =
483 webkit_glue::HistoryItemFromString(session_histories_[pos][entry]);
484 }
485 history->swap(result);
[email protected]29e82702013-02-14 12:56:40486}
487
[email protected]b2324b092012-11-01 10:34:11488// RenderViewObserver --------------------------------------------------------
489
[email protected]9b4c6cd2012-08-20 10:47:45490void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]d679c862013-03-07 10:31:41491 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56492}
493
[email protected]9b4c6cd2012-08-20 10:47:45494bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33495 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45496 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37497 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
498 OnSetTestConfiguration)
[email protected]8f722692013-02-23 14:14:06499 IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory)
[email protected]efb5f572012-01-29 10:57:33500 IPC_MESSAGE_UNHANDLED(handled = false)
501 IPC_END_MESSAGE_MAP()
502
503 return handled;
504}
505
[email protected]b9fad24c2012-11-15 08:22:10506// Public methods - -----------------------------------------------------------
507
[email protected]324825d2012-11-30 12:37:15508void WebKitTestRunner::Reset() {
[email protected]ab683b22013-03-04 09:10:31509 // The proxy_ is always non-NULL, it is set right after construction.
510 proxy_->reset();
[email protected]b3dbcb5e2012-12-17 00:16:37511 prefs_.reset();
[email protected]8f722692013-02-23 14:14:06512 routing_ids_.clear();
513 session_histories_.clear();
514 current_entry_indexes_.clear();
[email protected]324825d2012-11-30 12:37:15515}
516
[email protected]b2324b092012-11-01 10:34:11517// Private methods -----------------------------------------------------------
518
[email protected]0ac1a032013-02-12 12:53:37519void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19520 WebTestInterfaces* interfaces =
521 ShellRenderProcessObserver::GetInstance()->test_interfaces();
522
[email protected]893fc6362013-02-18 13:24:05523 if (interfaces->testRunner()->shouldDumpAsAudio()) {
524 const WebArrayBufferView* audio_data =
525 interfaces->testRunner()->audioData();
526 std::vector<unsigned char> vector_data(
527 static_cast<const unsigned char*>(audio_data->baseAddress()),
528 static_cast<const unsigned char*>(audio_data->baseAddress()) +
529 audio_data->byteLength());
530 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
531 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
532 return;
533 }
534
[email protected]5f20df362013-02-15 19:48:19535 Send(
536 new ShellViewHostMsg_TextDump(routing_id(), proxy()->captureTree(false)));
537
[email protected]4f92df82013-03-07 13:21:13538 if (test_config_.enable_pixel_dumping &&
[email protected]16000762013-03-04 12:43:38539 interfaces->testRunner()->shouldGeneratePixelResults()) {
[email protected]5f20df362013-02-15 19:48:19540 SkBitmap snapshot;
541 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
542
543 SkAutoLockPixels snapshot_lock(snapshot);
544 base::MD5Digest digest;
545#if defined(OS_ANDROID)
546 // On Android, pixel layout is RGBA, however, other Chrome platforms use
547 // BGRA.
548 const uint8_t* raw_pixels =
549 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
550 size_t snapshot_size = snapshot.getSize();
551 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
552 for (size_t i = 0; i < snapshot_size; i += 4) {
553 reordered_pixels[i] = raw_pixels[i + 2];
554 reordered_pixels[i + 1] = raw_pixels[i + 1];
555 reordered_pixels[i + 2] = raw_pixels[i];
556 reordered_pixels[i + 3] = raw_pixels[i + 3];
557 }
558 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
559#else
560 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
561#endif
562 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
563
[email protected]4f92df82013-03-07 13:21:13564 if (actual_pixel_hash == test_config_.expected_pixel_hash) {
[email protected]5f20df362013-02-15 19:48:19565 SkBitmap empty_image;
566 Send(new ShellViewHostMsg_ImageDump(
567 routing_id(), actual_pixel_hash, empty_image));
568 } else {
569 Send(new ShellViewHostMsg_ImageDump(
570 routing_id(), actual_pixel_hash, snapshot));
571 }
[email protected]0ac1a032013-02-12 12:53:37572 }
[email protected]5f20df362013-02-15 19:48:19573
[email protected]0ac1a032013-02-12 12:53:37574 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
575}
576
[email protected]0ac1a032013-02-12 12:53:37577void WebKitTestRunner::OnSetTestConfiguration(
[email protected]4f92df82013-03-07 13:21:13578 const ShellTestConfiguration& params) {
579 test_config_ = params;
[email protected]5f20df362013-02-15 19:48:19580 is_main_window_ = true;
[email protected]d679c862013-03-07 10:31:41581
582 setFocus(proxy_, true);
583
584 WebTestInterfaces* interfaces =
585 ShellRenderProcessObserver::GetInstance()->test_interfaces();
586 interfaces->setTestIsRunning(true);
[email protected]4f92df82013-03-07 13:21:13587 interfaces->configureForTestWithURL(params.test_url,
588 params.enable_pixel_dumping);
[email protected]0d2dfb92d2012-11-05 10:26:45589}
590
[email protected]8f722692013-02-23 14:14:06591void WebKitTestRunner::OnSessionHistory(
592 const std::vector<int>& routing_ids,
593 const std::vector<std::vector<std::string> >& session_histories,
594 const std::vector<unsigned>& current_entry_indexes) {
595 routing_ids_ = routing_ids;
596 session_histories_ = session_histories;
597 current_entry_indexes_ = current_entry_indexes;
598 CaptureDump();
599}
600
[email protected]31d71b02012-01-26 03:42:31601} // namespace content