blob: fc662bb5f3e093180b3dea5be608e2f26c3cefc4 [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]d679c862013-03-07 10:31:4184int 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]d679c862013-03-07 10:31:41165 enable_pixel_dumping_(true),
166 layout_test_timeout_(kDefaultLayoutTestTimeoutMs),
167 allow_external_pages_(false),
168 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]8be1c582013-03-06 00:55:03317 ForceResizeRenderView(render_view(), WebSize(rect.width, rect.height));
[email protected]29e82702013-02-14 12:56:40318}
319
320void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06321 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40322}
323
324void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06325 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40326}
327
328void WebKitTestRunner::evaluateInWebInspector(long call_id,
329 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06330 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
331 if (agent)
332 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40333}
334
335void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00336 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40337}
338
339void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00340 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40341}
342
343void WebKitTestRunner::setDeviceScaleFactor(float factor) {
[email protected]db6769c2013-02-24 14:59:14344 render_view()->GetWebView()->setDeviceScaleFactor(factor);
[email protected]29e82702013-02-14 12:56:40345}
346
[email protected]83654ec2013-03-04 15:58:53347void WebKitTestRunner::setFocus(WebTestProxyBase* proxy, bool focus) {
[email protected]95026a02013-03-05 08:34:30348 ProxyToRenderViewVisitor visitor(proxy);
349 RenderView::ForEach(&visitor);
350 if (!visitor.render_view()) {
351 NOTREACHED();
352 return;
353 }
354
355 // Check whether the focused view was closed meanwhile.
356 if (!WebKitTestRunner::Get(focused_view_))
357 focused_view_ = NULL;
358
359 if (focus) {
360 if (focused_view_ != visitor.render_view()) {
361 if (focused_view_)
362 SetFocusAndActivate(focused_view_, false);
363 SetFocusAndActivate(visitor.render_view(), true);
364 focused_view_ = visitor.render_view();
365 }
366 } else {
367 if (focused_view_ == visitor.render_view()) {
368 SetFocusAndActivate(visitor.render_view(), false);
369 focused_view_ = NULL;
370 }
371 }
[email protected]83654ec2013-03-04 15:58:53372}
373
[email protected]29e82702013-02-14 12:56:40374void WebKitTestRunner::setFocus(bool focus) {
[email protected]83654ec2013-03-04 15:58:53375 // TODO(jochen): Remove once the new WebKit API is rolled.
[email protected]29e82702013-02-14 12:56:40376}
377
378void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00379 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40380}
381
382std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19383#if defined(OS_WIN)
384 if (resource.find("/tmp/") == 0) {
385 // We want a temp file.
386 GURL base_url = net::FilePathToFileURL(temp_path_);
387 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
388 }
389#endif
390
391 // Some layout tests use file://// which we resolve as a UNC path. Normalize
392 // them to just file:///.
393 std::string result = resource;
394 while (StringToLowerASCII(result).find("file:////") == 0) {
395 result = result.substr(0, strlen("file:///")) +
396 result.substr(strlen("file:////"));
397 }
398 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40399}
400
401void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00402 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40403}
404
[email protected]29e82702013-02-14 12:56:40405void WebKitTestRunner::testFinished() {
[email protected]d679c862013-03-07 10:31:41406 if (!is_main_window_)
[email protected]5f20df362013-02-15 19:48:19407 return;
408 WebTestInterfaces* interfaces =
409 ShellRenderProcessObserver::GetInstance()->test_interfaces();
410 interfaces->setTestIsRunning(false);
[email protected]8f722692013-02-23 14:14:06411 if (interfaces->testRunner()->shouldDumpBackForwardList()) {
[email protected]f322356c2013-03-01 07:54:02412 SyncNavigationStateVisitor visitor;
413 RenderView::ForEach(&visitor);
[email protected]8f722692013-02-23 14:14:06414 Send(new ShellViewHostMsg_CaptureSessionHistory(routing_id()));
415 } else {
416 CaptureDump();
417 }
[email protected]29e82702013-02-14 12:56:40418}
419
420void WebKitTestRunner::testTimedOut() {
[email protected]d679c862013-03-07 10:31:41421 if (!is_main_window_)
[email protected]5f20df362013-02-15 19:48:19422 return;
423 WebTestInterfaces* interfaces =
424 ShellRenderProcessObserver::GetInstance()->test_interfaces();
425 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40426 Send(new ShellViewHostMsg_TestFinished(routing_id(), true));
427}
428
429bool WebKitTestRunner::isBeingDebugged() {
430 return base::debug::BeingDebugged();
431}
432
433int WebKitTestRunner::layoutTestTimeout() {
[email protected]d679c862013-03-07 10:31:41434 return layout_test_timeout_;
[email protected]29e82702013-02-14 12:56:40435}
436
437void WebKitTestRunner::closeRemainingWindows() {
[email protected]b778e182013-03-04 13:32:16438 Send(new ShellViewHostMsg_CloseRemainingWindows(routing_id()));
[email protected]29e82702013-02-14 12:56:40439}
440
441int WebKitTestRunner::navigationEntryCount() {
[email protected]b283d292013-02-21 08:40:34442 return GetLocalSessionHistoryLength(render_view());
[email protected]29e82702013-02-14 12:56:40443}
444
[email protected]29e82702013-02-14 12:56:40445void WebKitTestRunner::goToOffset(int offset) {
[email protected]d2494ff2013-02-20 08:22:37446 Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset));
[email protected]29e82702013-02-14 12:56:40447}
448
449void WebKitTestRunner::reload() {
[email protected]d2494ff2013-02-20 08:22:37450 Send(new ShellViewHostMsg_Reload(routing_id()));
[email protected]29e82702013-02-14 12:56:40451}
452
453void WebKitTestRunner::loadURLForFrame(const WebURL& url,
454 const std::string& frame_name) {
[email protected]d2494ff2013-02-20 08:22:37455 Send(new ShellViewHostMsg_LoadURLForFrame(
456 routing_id(), url, frame_name));
[email protected]29e82702013-02-14 12:56:40457}
458
459bool WebKitTestRunner::allowExternalPages() {
[email protected]d679c862013-03-07 10:31:41460 return allow_external_pages_;
[email protected]29e82702013-02-14 12:56:40461}
462
463void WebKitTestRunner::captureHistoryForWindow(
[email protected]9063bf62013-02-20 19:42:01464 WebTestProxyBase* proxy,
[email protected]29e82702013-02-14 12:56:40465 WebVector<WebKit::WebHistoryItem>* history,
466 size_t* currentEntryIndex) {
[email protected]8f722692013-02-23 14:14:06467 size_t pos = 0;
468 std::vector<int>::iterator id;
469 for (id = routing_ids_.begin(); id != routing_ids_.end(); ++id, ++pos) {
470 RenderView* render_view = RenderView::FromRoutingID(*id);
471 if (!render_view) {
472 NOTREACHED();
473 continue;
474 }
475 if (WebKitTestRunner::Get(render_view)->proxy() == proxy)
476 break;
477 }
478
479 if (id == routing_ids_.end()) {
480 NOTREACHED();
481 return;
482 }
483 size_t num_entries = session_histories_[pos].size();
484 *currentEntryIndex = current_entry_indexes_[pos];
485 WebVector<WebHistoryItem> result(num_entries);
486 for (size_t entry = 0; entry < num_entries; ++entry) {
487 result[entry] =
488 webkit_glue::HistoryItemFromString(session_histories_[pos][entry]);
489 }
490 history->swap(result);
[email protected]29e82702013-02-14 12:56:40491}
492
[email protected]b2324b092012-11-01 10:34:11493// RenderViewObserver --------------------------------------------------------
494
[email protected]9b4c6cd2012-08-20 10:47:45495void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]d679c862013-03-07 10:31:41496 ShellRenderProcessObserver::GetInstance()->BindTestRunnersToWindow(frame);
[email protected]96b80b472012-07-03 19:41:56497}
498
[email protected]9b4c6cd2012-08-20 10:47:45499bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33500 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45501 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37502 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
503 OnSetTestConfiguration)
[email protected]8f722692013-02-23 14:14:06504 IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory)
[email protected]efb5f572012-01-29 10:57:33505 IPC_MESSAGE_UNHANDLED(handled = false)
506 IPC_END_MESSAGE_MAP()
507
508 return handled;
509}
510
[email protected]b9fad24c2012-11-15 08:22:10511// Public methods - -----------------------------------------------------------
512
[email protected]324825d2012-11-30 12:37:15513void WebKitTestRunner::Reset() {
[email protected]ab683b22013-03-04 09:10:31514 // The proxy_ is always non-NULL, it is set right after construction.
515 proxy_->reset();
[email protected]b3dbcb5e2012-12-17 00:16:37516 prefs_.reset();
[email protected]d679c862013-03-07 10:31:41517 enable_pixel_dumping_ = true;
518 layout_test_timeout_ = kDefaultLayoutTestTimeoutMs;
519 allow_external_pages_ = false;
520 expected_pixel_hash_ = std::string();
[email protected]8f722692013-02-23 14:14:06521 routing_ids_.clear();
522 session_histories_.clear();
523 current_entry_indexes_.clear();
[email protected]324825d2012-11-30 12:37:15524}
525
[email protected]b2324b092012-11-01 10:34:11526// Private methods -----------------------------------------------------------
527
[email protected]0ac1a032013-02-12 12:53:37528void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19529 WebTestInterfaces* interfaces =
530 ShellRenderProcessObserver::GetInstance()->test_interfaces();
531
[email protected]893fc6362013-02-18 13:24:05532 if (interfaces->testRunner()->shouldDumpAsAudio()) {
533 const WebArrayBufferView* audio_data =
534 interfaces->testRunner()->audioData();
535 std::vector<unsigned char> vector_data(
536 static_cast<const unsigned char*>(audio_data->baseAddress()),
537 static_cast<const unsigned char*>(audio_data->baseAddress()) +
538 audio_data->byteLength());
539 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
540 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
541 return;
542 }
543
[email protected]5f20df362013-02-15 19:48:19544 Send(
545 new ShellViewHostMsg_TextDump(routing_id(), proxy()->captureTree(false)));
546
[email protected]d679c862013-03-07 10:31:41547 if (enable_pixel_dumping_ &&
[email protected]16000762013-03-04 12:43:38548 interfaces->testRunner()->shouldGeneratePixelResults()) {
[email protected]5f20df362013-02-15 19:48:19549 SkBitmap snapshot;
550 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
551
552 SkAutoLockPixels snapshot_lock(snapshot);
553 base::MD5Digest digest;
554#if defined(OS_ANDROID)
555 // On Android, pixel layout is RGBA, however, other Chrome platforms use
556 // BGRA.
557 const uint8_t* raw_pixels =
558 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
559 size_t snapshot_size = snapshot.getSize();
560 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
561 for (size_t i = 0; i < snapshot_size; i += 4) {
562 reordered_pixels[i] = raw_pixels[i + 2];
563 reordered_pixels[i + 1] = raw_pixels[i + 1];
564 reordered_pixels[i + 2] = raw_pixels[i];
565 reordered_pixels[i + 3] = raw_pixels[i + 3];
566 }
567 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
568#else
569 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
570#endif
571 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
572
[email protected]d679c862013-03-07 10:31:41573 if (actual_pixel_hash == expected_pixel_hash_) {
[email protected]5f20df362013-02-15 19:48:19574 SkBitmap empty_image;
575 Send(new ShellViewHostMsg_ImageDump(
576 routing_id(), actual_pixel_hash, empty_image));
577 } else {
578 Send(new ShellViewHostMsg_ImageDump(
579 routing_id(), actual_pixel_hash, snapshot));
580 }
[email protected]0ac1a032013-02-12 12:53:37581 }
[email protected]5f20df362013-02-15 19:48:19582
[email protected]0ac1a032013-02-12 12:53:37583 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
584}
585
[email protected]0ac1a032013-02-12 12:53:37586void WebKitTestRunner::OnSetTestConfiguration(
[email protected]d679c862013-03-07 10:31:41587 const ShellViewMsg_SetTestConfiguration_Params& params) {
588 current_working_directory_ = params.current_working_directory;
589 temp_path_ = params.temp_path;
590 enable_pixel_dumping_ = params.enable_pixel_dumping;
591 layout_test_timeout_ = params.layout_test_timeout;
592 allow_external_pages_ = params.allow_external_pages;
593 expected_pixel_hash_ = params.expected_pixel_hash;
[email protected]5f20df362013-02-15 19:48:19594 is_main_window_ = true;
[email protected]d679c862013-03-07 10:31:41595
596 setFocus(proxy_, true);
597
598 WebTestInterfaces* interfaces =
599 ShellRenderProcessObserver::GetInstance()->test_interfaces();
600 interfaces->setTestIsRunning(true);
601 interfaces->configureForTestWithURL(params.test_url, enable_pixel_dumping_);
[email protected]0d2dfb92d2012-11-05 10:26:45602}
603
[email protected]8f722692013-02-23 14:14:06604void WebKitTestRunner::OnSessionHistory(
605 const std::vector<int>& routing_ids,
606 const std::vector<std::vector<std::string> >& session_histories,
607 const std::vector<unsigned>& current_entry_indexes) {
608 routing_ids_ = routing_ids;
609 session_histories_ = session_histories;
610 current_entry_indexes_ = current_entry_indexes;
611 CaptureDump();
612}
613
[email protected]31d71b02012-01-26 03:42:31614} // namespace content