blob: f63b20f6af6a1eb80287aab1d94f80c25c929479 [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]0bfe1752013-03-07 08:44:2648#include "third_party/WebKit/Source/WebKit/chromium/public/WebTestingSupport.h"
[email protected]efb5f572012-01-29 10:57:3349#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b2324b092012-11-01 10:34:1150#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]5f20df362013-02-15 19:48:1951#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestInterfaces.h"
[email protected]b9fad24c2012-11-15 08:22:1052#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]5f20df362013-02-15 19:48:1953#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestRunner.h"
[email protected]c24b40222013-02-20 11:45:4254#include "ui/gfx/rect.h"
[email protected]984a5f72012-12-13 21:41:5055#include "webkit/base/file_path_string_conversions.h"
[email protected]8f722692013-02-23 14:14:0656#include "webkit/glue/glue_serialize.h"
[email protected]0799da02012-06-27 10:58:5157#include "webkit/glue/webkit_glue.h"
[email protected]324825d2012-11-30 12:37:1558#include "webkit/glue/webpreferences.h"
[email protected]efb5f572012-01-29 10:57:3359
[email protected]2a58eb12012-12-18 07:56:4060using WebKit::Platform;
[email protected]893fc6362013-02-18 13:24:0561using WebKit::WebArrayBufferView;
[email protected]b2324b092012-11-01 10:34:1162using WebKit::WebContextMenuData;
[email protected]001841c92012-12-11 17:00:1363using WebKit::WebDevToolsAgent;
[email protected]29e82702013-02-14 12:56:4064using WebKit::WebDeviceOrientation;
[email protected]efb5f572012-01-29 10:57:3365using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1166using WebKit::WebFrame;
67using WebKit::WebGamepads;
[email protected]8f722692013-02-23 14:14:0668using WebKit::WebHistoryItem;
[email protected]0799da02012-06-27 10:58:5169using WebKit::WebRect;
[email protected]c272c5b2012-06-06 09:01:0670using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1171using WebKit::WebString;
[email protected]0bfe1752013-03-07 08:44:2672using WebKit::WebTestingSupport;
[email protected]09ec9f92012-12-17 09:46:3673using WebKit::WebURL;
[email protected]9d9ec982013-01-10 18:53:3974using WebKit::WebURLError;
[email protected]b2324b092012-11-01 10:34:1175using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5176using WebKit::WebView;
[email protected]b3dbcb5e2012-12-17 00:16:3777using WebTestRunner::WebPreferences;
[email protected]b2324b092012-11-01 10:34:1178using WebTestRunner::WebTask;
[email protected]5f20df362013-02-15 19:48:1979using WebTestRunner::WebTestInterfaces;
[email protected]9063bf62013-02-20 19:42:0180using WebTestRunner::WebTestProxyBase;
[email protected]efb5f572012-01-29 10:57:3381
[email protected]31d71b02012-01-26 03:42:3182namespace content {
83
[email protected]efb5f572012-01-29 10:57:3384namespace {
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]0bfe1752013-03-07 08:44:26165 is_main_window_(false),
166 reset_on_next_navigation_(false),
167 test_is_running_(false) {
[email protected]31d71b02012-01-26 03:42:31168}
169
[email protected]9b4c6cd2012-08-20 10:47:45170WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31171}
172
[email protected]b2324b092012-11-01 10:34:11173// WebTestDelegate -----------------------------------------------------------
174
[email protected]b2324b092012-11-01 10:34:11175void WebKitTestRunner::clearEditCommand() {
176 render_view()->ClearEditCommands();
177}
178
179void WebKitTestRunner::setEditCommand(const std::string& name,
180 const std::string& value) {
181 render_view()->SetEditCommandForNextKeyEvent(name, value);
182}
183
[email protected]b2324b092012-11-01 10:34:11184void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25185 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11186}
187
188void WebKitTestRunner::printMessage(const std::string& message) {
189 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
190}
191
192void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40193 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11194}
195
196void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
197 MessageLoop::current()->PostDelayedTask(
198 FROM_HERE,
199 base::Bind(&WebTask::run, base::Owned(task)),
200 base::TimeDelta::FromMilliseconds(ms));
201}
202
203WebString WebKitTestRunner::registerIsolatedFileSystem(
204 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]d30a36f2013-02-07 04:16:26205 std::vector<base::FilePath> files;
[email protected]984a5f72012-12-13 21:41:50206 for (size_t i = 0; i < absolute_filenames.size(); ++i)
207 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
208 std::string filesystem_id;
209 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
210 routing_id(), files, &filesystem_id));
211 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11212}
213
214long long WebKitTestRunner::getCurrentTimeInMillisecond() {
215 return base::TimeTicks::Now().ToInternalValue() /
216 base::Time::kMicrosecondsPerMillisecond;
217}
218
219WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
220 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45221#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26222 base::FilePath path(UTF8ToWide(utf8_path));
[email protected]0d2dfb92d2012-11-05 10:26:45223#else
[email protected]d30a36f2013-02-07 04:16:26224 base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
[email protected]0d2dfb92d2012-11-05 10:26:45225#endif
226 if (!path.IsAbsolute()) {
227 GURL base_url =
228 net::FilePathToFileURL(current_working_directory_.Append(
229 FILE_PATH_LITERAL("foo")));
230 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
231 }
[email protected]984a5f72012-12-13 21:41:50232 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11233}
234
[email protected]09ec9f92012-12-17 09:46:36235WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
[email protected]d30a36f2013-02-07 04:16:26236 base::FilePath local_path;
[email protected]09ec9f92012-12-17 09:46:36237 if (!net::FileURLToFilePath(file_url, &local_path))
238 return WebURL();
239
240 std::string contents;
241 Send(new ShellViewHostMsg_ReadFileToString(
242 routing_id(), local_path, &contents));
243
244 std::string contents_base64;
245 if (!base::Base64Encode(contents, &contents_base64))
246 return WebURL();
247
248 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
249 return WebURL(GURL(data_url_prefix + contents_base64));
250}
251
252WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
253 const char kPrefix[] = "file:///tmp/LayoutTests/";
254 const int kPrefixLen = arraysize(kPrefix) - 1;
255
256 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
257 return WebURL(GURL(utf8_url));
258
[email protected]d30a36f2013-02-07 04:16:26259 base::FilePath replace_path =
[email protected]09ec9f92012-12-17 09:46:36260 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
261 FILE_PATH_LITERAL("LayoutTests/"));
262#if defined(OS_WIN)
263 std::string utf8_path = WideToUTF8(replace_path.value());
264#else
265 std::string utf8_path =
266 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
267#endif
268 std::string new_url =
269 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
270 return WebURL(GURL(new_url));
271}
272
[email protected]b3dbcb5e2012-12-17 00:16:37273WebPreferences* WebKitTestRunner::preferences() {
274 return &prefs_;
275}
276
277void WebKitTestRunner::applyPreferences() {
278 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47279 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37280 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25281 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37282}
283
[email protected]9d9ec982013-01-10 18:53:39284std::string WebKitTestRunner::makeURLErrorDescription(
285 const WebURLError& error) {
286 std::string domain = error.domain.utf8();
287 int code = error.reason;
288
289 if (domain == net::kErrorDomain) {
290 domain = "NSURLErrorDomain";
291 switch (error.reason) {
292 case net::ERR_ABORTED:
293 code = -999; // NSURLErrorCancelled
294 break;
295 case net::ERR_UNSAFE_PORT:
296 // Our unsafe port checking happens at the network stack level, but we
297 // make this translation here to match the behavior of stock WebKit.
298 domain = "WebKitErrorDomain";
299 code = 103;
300 break;
301 case net::ERR_ADDRESS_INVALID:
302 case net::ERR_ADDRESS_UNREACHABLE:
303 case net::ERR_NETWORK_ACCESS_DENIED:
304 code = -1004; // NSURLErrorCannotConnectToHost
305 break;
306 }
307 } else {
308 DLOG(WARNING) << "Unknown error domain";
309 }
310
311 return base::StringPrintf("<NSError domain %s, code %d, failing URL \"%s\">",
312 domain.c_str(), code, error.unreachableURL.spec().data());
313}
314
[email protected]29e82702013-02-14 12:56:40315void WebKitTestRunner::setClientWindowRect(const WebRect& rect) {
[email protected]8be1c582013-03-06 00:55:03316 ForceResizeRenderView(render_view(), WebSize(rect.width, rect.height));
[email protected]29e82702013-02-14 12:56:40317}
318
319void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06320 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40321}
322
323void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06324 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40325}
326
327void WebKitTestRunner::evaluateInWebInspector(long call_id,
328 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06329 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
330 if (agent)
331 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40332}
333
334void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00335 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40336}
337
338void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00339 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40340}
341
342void WebKitTestRunner::setDeviceScaleFactor(float factor) {
[email protected]db6769c2013-02-24 14:59:14343 render_view()->GetWebView()->setDeviceScaleFactor(factor);
[email protected]29e82702013-02-14 12:56:40344}
345
[email protected]83654ec2013-03-04 15:58:53346void WebKitTestRunner::setFocus(WebTestProxyBase* proxy, bool focus) {
[email protected]95026a02013-03-05 08:34:30347 ProxyToRenderViewVisitor visitor(proxy);
348 RenderView::ForEach(&visitor);
349 if (!visitor.render_view()) {
350 NOTREACHED();
351 return;
352 }
353
354 // Check whether the focused view was closed meanwhile.
355 if (!WebKitTestRunner::Get(focused_view_))
356 focused_view_ = NULL;
357
358 if (focus) {
359 if (focused_view_ != visitor.render_view()) {
360 if (focused_view_)
361 SetFocusAndActivate(focused_view_, false);
362 SetFocusAndActivate(visitor.render_view(), true);
363 focused_view_ = visitor.render_view();
364 }
365 } else {
366 if (focused_view_ == visitor.render_view()) {
367 SetFocusAndActivate(visitor.render_view(), false);
368 focused_view_ = NULL;
369 }
370 }
[email protected]83654ec2013-03-04 15:58:53371}
372
[email protected]29e82702013-02-14 12:56:40373void WebKitTestRunner::setFocus(bool focus) {
[email protected]83654ec2013-03-04 15:58:53374 // TODO(jochen): Remove once the new WebKit API is rolled.
[email protected]29e82702013-02-14 12:56:40375}
376
377void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00378 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40379}
380
381std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19382#if defined(OS_WIN)
383 if (resource.find("/tmp/") == 0) {
384 // We want a temp file.
385 GURL base_url = net::FilePathToFileURL(temp_path_);
386 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
387 }
388#endif
389
390 // Some layout tests use file://// which we resolve as a UNC path. Normalize
391 // them to just file:///.
392 std::string result = resource;
393 while (StringToLowerASCII(result).find("file:////") == 0) {
394 result = result.substr(0, strlen("file:///")) +
395 result.substr(strlen("file:////"));
396 }
397 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40398}
399
400void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00401 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40402}
403
[email protected]29e82702013-02-14 12:56:40404void WebKitTestRunner::testFinished() {
[email protected]0bfe1752013-03-07 08:44:26405 if (!is_main_window_ || !test_is_running_)
[email protected]5f20df362013-02-15 19:48:19406 return;
407 WebTestInterfaces* interfaces =
408 ShellRenderProcessObserver::GetInstance()->test_interfaces();
409 interfaces->setTestIsRunning(false);
[email protected]0bfe1752013-03-07 08:44:26410 test_is_running_ = 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]0bfe1752013-03-07 08:44:26421 if (!is_main_window_ || !test_is_running_)
[email protected]5f20df362013-02-15 19:48:19422 return;
423 WebTestInterfaces* interfaces =
424 ShellRenderProcessObserver::GetInstance()->test_interfaces();
425 interfaces->setTestIsRunning(false);
[email protected]0bfe1752013-03-07 08:44:26426 test_is_running_ = 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() {
[email protected]0bfe1752013-03-07 08:44:26435 return test_config_.layout_test_timeout;
[email protected]29e82702013-02-14 12:56:40436}
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() {
[email protected]0bfe1752013-03-07 08:44:26461 return test_config_.allow_external_pages;
[email protected]29e82702013-02-14 12:56:40462}
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]0bfe1752013-03-07 08:44:26497 WebTestingSupport::injectInternalsObject(frame);
498 ShellRenderProcessObserver::GetInstance()->test_interfaces()->bindTo(frame);
[email protected]96b80b472012-07-03 19:41:56499}
500
[email protected]9b4c6cd2012-08-20 10:47:45501bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33502 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45503 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37504 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
505 OnSetTestConfiguration)
[email protected]8f722692013-02-23 14:14:06506 IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory)
[email protected]efb5f572012-01-29 10:57:33507 IPC_MESSAGE_UNHANDLED(handled = false)
508 IPC_END_MESSAGE_MAP()
509
510 return handled;
511}
512
[email protected]0bfe1752013-03-07 08:44:26513void WebKitTestRunner::Navigate(const GURL& url) {
514 if (!reset_on_next_navigation_)
515 return;
516
517 reset_on_next_navigation_ = false;
518 Reset();
519
520 WebTestInterfaces* interfaces =
521 ShellRenderProcessObserver::GetInstance()->test_interfaces();
522 interfaces->configureForTestWithURL(test_config_.test_url,
523 test_config_.enable_pixel_dumping);
524 interfaces->setTestIsRunning(true);
525 test_is_running_ = true;
526}
527
[email protected]b9fad24c2012-11-15 08:22:10528// Public methods - -----------------------------------------------------------
529
[email protected]324825d2012-11-30 12:37:15530void WebKitTestRunner::Reset() {
[email protected]0bfe1752013-03-07 08:44:26531 ShellRenderProcessObserver::GetInstance()->test_interfaces()->resetAll();
[email protected]ab683b22013-03-04 09:10:31532 // The proxy_ is always non-NULL, it is set right after construction.
533 proxy_->reset();
[email protected]b3dbcb5e2012-12-17 00:16:37534 prefs_.reset();
[email protected]8f722692013-02-23 14:14:06535 routing_ids_.clear();
536 session_histories_.clear();
537 current_entry_indexes_.clear();
[email protected]0bfe1752013-03-07 08:44:26538 // This overrides the WebPreferences, so we have to reset them again.
539 WebTestingSupport::resetInternalsObject(
540 render_view()->GetWebView()->mainFrame());
541 render_view()->SetWebkitPreferences(render_view()->GetWebkitPreferences());
542 setFocus(proxy_, true);
[email protected]324825d2012-11-30 12:37:15543}
544
[email protected]b2324b092012-11-01 10:34:11545// Private methods -----------------------------------------------------------
546
[email protected]0ac1a032013-02-12 12:53:37547void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19548 WebTestInterfaces* interfaces =
549 ShellRenderProcessObserver::GetInstance()->test_interfaces();
550
[email protected]893fc6362013-02-18 13:24:05551 if (interfaces->testRunner()->shouldDumpAsAudio()) {
552 const WebArrayBufferView* audio_data =
553 interfaces->testRunner()->audioData();
554 std::vector<unsigned char> vector_data(
555 static_cast<const unsigned char*>(audio_data->baseAddress()),
556 static_cast<const unsigned char*>(audio_data->baseAddress()) +
557 audio_data->byteLength());
558 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
559 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
560 return;
561 }
562
[email protected]5f20df362013-02-15 19:48:19563 Send(
564 new ShellViewHostMsg_TextDump(routing_id(), proxy()->captureTree(false)));
565
[email protected]0bfe1752013-03-07 08:44:26566 if (test_config_.enable_pixel_dumping &&
[email protected]16000762013-03-04 12:43:38567 interfaces->testRunner()->shouldGeneratePixelResults()) {
[email protected]5f20df362013-02-15 19:48:19568 SkBitmap snapshot;
569 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
570
571 SkAutoLockPixels snapshot_lock(snapshot);
572 base::MD5Digest digest;
573#if defined(OS_ANDROID)
574 // On Android, pixel layout is RGBA, however, other Chrome platforms use
575 // BGRA.
576 const uint8_t* raw_pixels =
577 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
578 size_t snapshot_size = snapshot.getSize();
579 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]);
580 for (size_t i = 0; i < snapshot_size; i += 4) {
581 reordered_pixels[i] = raw_pixels[i + 2];
582 reordered_pixels[i + 1] = raw_pixels[i + 1];
583 reordered_pixels[i + 2] = raw_pixels[i];
584 reordered_pixels[i + 3] = raw_pixels[i + 3];
585 }
586 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
587#else
588 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
589#endif
590 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
591
[email protected]0bfe1752013-03-07 08:44:26592 if (actual_pixel_hash == test_config_.expected_pixel_hash) {
[email protected]5f20df362013-02-15 19:48:19593 SkBitmap empty_image;
594 Send(new ShellViewHostMsg_ImageDump(
595 routing_id(), actual_pixel_hash, empty_image));
596 } else {
597 Send(new ShellViewHostMsg_ImageDump(
598 routing_id(), actual_pixel_hash, snapshot));
599 }
[email protected]0ac1a032013-02-12 12:53:37600 }
[email protected]5f20df362013-02-15 19:48:19601
[email protected]0ac1a032013-02-12 12:53:37602 Send(new ShellViewHostMsg_TestFinished(routing_id(), false));
603}
604
[email protected]0ac1a032013-02-12 12:53:37605void WebKitTestRunner::OnSetTestConfiguration(
[email protected]0bfe1752013-03-07 08:44:26606 const ShellTestConfiguration& params) {
607 test_config_ = params;
608 reset_on_next_navigation_ = true;
[email protected]5f20df362013-02-15 19:48:19609 is_main_window_ = true;
[email protected]0d2dfb92d2012-11-05 10:26:45610}
611
[email protected]8f722692013-02-23 14:14:06612void WebKitTestRunner::OnSessionHistory(
613 const std::vector<int>& routing_ids,
614 const std::vector<std::vector<std::string> >& session_histories,
615 const std::vector<unsigned>& current_entry_indexes) {
616 routing_ids_ = routing_ids;
617 session_histories_ = session_histories;
618 current_entry_indexes_ = current_entry_indexes;
619 CaptureDump();
620}
621
[email protected]31d71b02012-01-26 03:42:31622} // namespace content