blob: d229b245d6a27953bc7bd06c2ce8624bb89bd270 [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]40d11e02013-03-28 17:43:1418#include "base/strings/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"
[email protected]613ba94c2013-03-08 14:14:3132#include "third_party/WebKit/Source/Platform/chromium/public/WebPoint.h"
[email protected]87884c42013-01-10 01:56:2733#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
34#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
35#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
36#include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
[email protected]9d9ec982013-01-10 18:53:3937#include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h"
[email protected]1afb9af2013-03-18 15:30:3238#include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
[email protected]0ac1a032013-02-12 12:53:3739#include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
[email protected]893fc6362013-02-18 13:24:0540#include "third_party/WebKit/Source/WebKit/chromium/public/WebArrayBufferView.h"
[email protected]b2324b092012-11-01 10:34:1141#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
[email protected]0ac1a032013-02-12 12:53:3742#include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
[email protected]001841c92012-12-11 17:00:1343#include "third_party/WebKit/Source/WebKit/chromium/public/WebDevToolsAgent.h"
[email protected]29e82702013-02-14 12:56:4044#include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceOrientation.h"
[email protected]efb5f572012-01-29 10:57:3345#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
46#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
47#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
[email protected]8f722692013-02-23 14:14:0648#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h"
[email protected]b2324b092012-11-01 10:34:1149#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]1ca4fac2013-04-08 14:15:1150#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h"
[email protected]435b763d2013-03-07 13:27:5951#include "third_party/WebKit/Source/WebKit/chromium/public/WebTestingSupport.h"
[email protected]efb5f572012-01-29 10:57:3352#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b2324b092012-11-01 10:34:1153#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTask.h"
[email protected]5f20df362013-02-15 19:48:1954#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestInterfaces.h"
[email protected]b9fad24c2012-11-15 08:22:1055#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
[email protected]5f20df362013-02-15 19:48:1956#include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestRunner.h"
[email protected]c24b40222013-02-20 11:45:4257#include "ui/gfx/rect.h"
[email protected]984a5f72012-12-13 21:41:5058#include "webkit/base/file_path_string_conversions.h"
[email protected]8f722692013-02-23 14:14:0659#include "webkit/glue/glue_serialize.h"
[email protected]0799da02012-06-27 10:58:5160#include "webkit/glue/webkit_glue.h"
[email protected]324825d2012-11-30 12:37:1561#include "webkit/glue/webpreferences.h"
[email protected]321d9eb2013-04-04 18:25:2662#include "webkit/mocks/test_media_stream_client.h"
[email protected]efb5f572012-01-29 10:57:3363
[email protected]2a58eb12012-12-18 07:56:4064using WebKit::Platform;
[email protected]893fc6362013-02-18 13:24:0565using WebKit::WebArrayBufferView;
[email protected]b2324b092012-11-01 10:34:1166using WebKit::WebContextMenuData;
[email protected]001841c92012-12-11 17:00:1367using WebKit::WebDevToolsAgent;
[email protected]29e82702013-02-14 12:56:4068using WebKit::WebDeviceOrientation;
[email protected]efb5f572012-01-29 10:57:3369using WebKit::WebElement;
[email protected]b2324b092012-11-01 10:34:1170using WebKit::WebFrame;
71using WebKit::WebGamepads;
[email protected]8f722692013-02-23 14:14:0672using WebKit::WebHistoryItem;
[email protected]321d9eb2013-04-04 18:25:2673using WebKit::WebMediaPlayer;
74using WebKit::WebMediaPlayerClient;
[email protected]613ba94c2013-03-08 14:14:3175using WebKit::WebPoint;
[email protected]0799da02012-06-27 10:58:5176using WebKit::WebRect;
[email protected]1ca4fac2013-04-08 14:15:1177using WebKit::WebScriptSource;
[email protected]c272c5b2012-06-06 09:01:0678using WebKit::WebSize;
[email protected]b2324b092012-11-01 10:34:1179using WebKit::WebString;
[email protected]09ec9f92012-12-17 09:46:3680using WebKit::WebURL;
[email protected]9d9ec982013-01-10 18:53:3981using WebKit::WebURLError;
[email protected]1afb9af2013-03-18 15:30:3282using WebKit::WebURLRequest;
[email protected]435b763d2013-03-07 13:27:5983using WebKit::WebTestingSupport;
[email protected]b2324b092012-11-01 10:34:1184using WebKit::WebVector;
[email protected]0799da02012-06-27 10:58:5185using WebKit::WebView;
[email protected]b3dbcb5e2012-12-17 00:16:3786using WebTestRunner::WebPreferences;
[email protected]b2324b092012-11-01 10:34:1187using WebTestRunner::WebTask;
[email protected]5f20df362013-02-15 19:48:1988using WebTestRunner::WebTestInterfaces;
[email protected]9063bf62013-02-20 19:42:0189using WebTestRunner::WebTestProxyBase;
[email protected]efb5f572012-01-29 10:57:3390
[email protected]31d71b02012-01-26 03:42:3191namespace content {
92
[email protected]efb5f572012-01-29 10:57:3393namespace {
94
[email protected]b2324b092012-11-01 10:34:1195void InvokeTaskHelper(void* context) {
96 WebTask* task = reinterpret_cast<WebTask*>(context);
97 task->run();
98 delete task;
99}
100
[email protected]0799da02012-06-27 10:58:51101#if !defined(OS_MACOSX)
102void MakeBitmapOpaque(SkBitmap* bitmap) {
103 SkAutoLockPixels lock(*bitmap);
104 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
105 for (int y = 0; y < bitmap->height(); ++y) {
106 uint32_t* row = bitmap->getAddr32(0, y);
107 for (int x = 0; x < bitmap->width(); ++x)
108 row[x] |= 0xFF000000; // Set alpha bits to 1.
109 }
110}
111#endif
112
[email protected]b9fad24c2012-11-15 08:22:10113void CopyCanvasToBitmap(SkCanvas* canvas, SkBitmap* snapshot) {
[email protected]9d611ca2012-11-14 13:46:56114 SkDevice* device = skia::GetTopDevice(*canvas);
[email protected]0799da02012-06-27 10:58:51115 const SkBitmap& bitmap = device->accessBitmap(false);
116 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config);
117
118#if !defined(OS_MACOSX)
119 // Only the expected PNGs for Mac have a valid alpha channel.
120 MakeBitmapOpaque(snapshot);
121#endif
122
123}
124
[email protected]f322356c2013-03-01 07:54:02125class SyncNavigationStateVisitor : public RenderViewVisitor {
126 public:
127 SyncNavigationStateVisitor() {}
128 virtual ~SyncNavigationStateVisitor() {}
129
130 virtual bool Visit(RenderView* render_view) OVERRIDE {
131 SyncNavigationState(render_view);
132 return true;
133 }
134 private:
135 DISALLOW_COPY_AND_ASSIGN(SyncNavigationStateVisitor);
136};
137
[email protected]95026a02013-03-05 08:34:30138class ProxyToRenderViewVisitor : public RenderViewVisitor {
139 public:
140 explicit ProxyToRenderViewVisitor(WebTestProxyBase* proxy)
141 : proxy_(proxy),
142 render_view_(NULL) {
143 }
144 virtual ~ProxyToRenderViewVisitor() {}
145
146 RenderView* render_view() const { return render_view_; }
147
148 virtual bool Visit(RenderView* render_view) OVERRIDE {
149 WebKitTestRunner* test_runner = WebKitTestRunner::Get(render_view);
150 if (!test_runner) {
151 NOTREACHED();
152 return true;
153 }
154 if (test_runner->proxy() == proxy_) {
155 render_view_ = render_view;
156 return false;
157 }
158 return true;
159 }
160 private:
161 WebTestProxyBase* proxy_;
162 RenderView* render_view_;
163
164 DISALLOW_COPY_AND_ASSIGN(ProxyToRenderViewVisitor);
165};
166
[email protected]2aa9b7a2013-04-15 11:07:47167class NavigateAwayVisitor : public RenderViewVisitor {
168 public:
169 NavigateAwayVisitor(RenderView* main_render_view)
170 : main_render_view_(main_render_view) {}
171 virtual ~NavigateAwayVisitor() {}
172
173 virtual bool Visit(RenderView* render_view) OVERRIDE {
174 if (render_view == main_render_view_)
175 return true;
176 render_view->GetWebView()->mainFrame()
177 ->loadRequest(WebURLRequest(GURL("about:blank")));
178 return true;
179 }
180
181 private:
182 RenderView* main_render_view_;
183
184 DISALLOW_COPY_AND_ASSIGN(NavigateAwayVisitor);
185};
186
[email protected]efb5f572012-01-29 10:57:33187} // namespace
[email protected]cccb5cf2012-06-06 22:20:04188
[email protected]9b4c6cd2012-08-20 10:47:45189WebKitTestRunner::WebKitTestRunner(RenderView* render_view)
[email protected]5f20df362013-02-15 19:48:19190 : RenderViewObserver(render_view),
[email protected]2025a2f42013-02-18 07:25:41191 RenderViewObserverTracker<WebKitTestRunner>(render_view),
[email protected]ab683b22013-03-04 09:10:31192 proxy_(NULL),
[email protected]95026a02013-03-05 08:34:30193 focused_view_(NULL),
[email protected]37d9dcce2013-03-22 22:27:45194 is_main_window_(false),
195 focus_on_next_commit_(false) {
[email protected]31d71b02012-01-26 03:42:31196}
197
[email protected]9b4c6cd2012-08-20 10:47:45198WebKitTestRunner::~WebKitTestRunner() {
[email protected]31d71b02012-01-26 03:42:31199}
200
[email protected]b2324b092012-11-01 10:34:11201// WebTestDelegate -----------------------------------------------------------
202
[email protected]b2324b092012-11-01 10:34:11203void WebKitTestRunner::clearEditCommand() {
204 render_view()->ClearEditCommands();
205}
206
207void WebKitTestRunner::setEditCommand(const std::string& name,
208 const std::string& value) {
209 render_view()->SetEditCommandForNextKeyEvent(name, value);
210}
211
[email protected]b2324b092012-11-01 10:34:11212void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
[email protected]e5196922012-12-11 22:18:25213 SetMockGamepads(gamepads);
[email protected]b2324b092012-11-01 10:34:11214}
215
216void WebKitTestRunner::printMessage(const std::string& message) {
217 Send(new ShellViewHostMsg_PrintMessage(routing_id(), message));
218}
219
220void WebKitTestRunner::postTask(WebTask* task) {
[email protected]2a58eb12012-12-18 07:56:40221 Platform::current()->callOnMainThread(InvokeTaskHelper, task);
[email protected]b2324b092012-11-01 10:34:11222}
223
224void WebKitTestRunner::postDelayedTask(WebTask* task, long long ms) {
225 MessageLoop::current()->PostDelayedTask(
226 FROM_HERE,
227 base::Bind(&WebTask::run, base::Owned(task)),
228 base::TimeDelta::FromMilliseconds(ms));
229}
230
231WebString WebKitTestRunner::registerIsolatedFileSystem(
232 const WebKit::WebVector<WebKit::WebString>& absolute_filenames) {
[email protected]d30a36f2013-02-07 04:16:26233 std::vector<base::FilePath> files;
[email protected]984a5f72012-12-13 21:41:50234 for (size_t i = 0; i < absolute_filenames.size(); ++i)
235 files.push_back(webkit_base::WebStringToFilePath(absolute_filenames[i]));
236 std::string filesystem_id;
237 Send(new ShellViewHostMsg_RegisterIsolatedFileSystem(
238 routing_id(), files, &filesystem_id));
239 return WebString::fromUTF8(filesystem_id);
[email protected]b2324b092012-11-01 10:34:11240}
241
242long long WebKitTestRunner::getCurrentTimeInMillisecond() {
[email protected]2b035f32013-03-15 20:47:53243 return base::TimeDelta(base::Time::Now() -
244 base::Time::UnixEpoch()).ToInternalValue() /
245 base::Time::kMicrosecondsPerMillisecond;
[email protected]b2324b092012-11-01 10:34:11246}
247
248WebString WebKitTestRunner::getAbsoluteWebStringFromUTF8Path(
249 const std::string& utf8_path) {
[email protected]0d2dfb92d2012-11-05 10:26:45250#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26251 base::FilePath path(UTF8ToWide(utf8_path));
[email protected]0d2dfb92d2012-11-05 10:26:45252#else
[email protected]d30a36f2013-02-07 04:16:26253 base::FilePath path(base::SysWideToNativeMB(base::SysUTF8ToWide(utf8_path)));
[email protected]0d2dfb92d2012-11-05 10:26:45254#endif
255 if (!path.IsAbsolute()) {
256 GURL base_url =
[email protected]38c1555c62013-03-16 19:20:55257 net::FilePathToFileURL(test_config_.current_working_directory.Append(
[email protected]0d2dfb92d2012-11-05 10:26:45258 FILE_PATH_LITERAL("foo")));
259 net::FileURLToFilePath(base_url.Resolve(utf8_path), &path);
260 }
[email protected]984a5f72012-12-13 21:41:50261 return webkit_base::FilePathToWebString(path);
[email protected]b2324b092012-11-01 10:34:11262}
263
[email protected]09ec9f92012-12-17 09:46:36264WebURL WebKitTestRunner::localFileToDataURL(const WebURL& file_url) {
[email protected]d30a36f2013-02-07 04:16:26265 base::FilePath local_path;
[email protected]09ec9f92012-12-17 09:46:36266 if (!net::FileURLToFilePath(file_url, &local_path))
267 return WebURL();
268
269 std::string contents;
270 Send(new ShellViewHostMsg_ReadFileToString(
271 routing_id(), local_path, &contents));
272
273 std::string contents_base64;
274 if (!base::Base64Encode(contents, &contents_base64))
275 return WebURL();
276
277 const char data_url_prefix[] = "data:text/css:charset=utf-8;base64,";
278 return WebURL(GURL(data_url_prefix + contents_base64));
279}
280
281WebURL WebKitTestRunner::rewriteLayoutTestsURL(const std::string& utf8_url) {
282 const char kPrefix[] = "file:///tmp/LayoutTests/";
283 const int kPrefixLen = arraysize(kPrefix) - 1;
284
285 if (utf8_url.compare(0, kPrefixLen, kPrefix, kPrefixLen))
286 return WebURL(GURL(utf8_url));
287
[email protected]d30a36f2013-02-07 04:16:26288 base::FilePath replace_path =
[email protected]09ec9f92012-12-17 09:46:36289 ShellRenderProcessObserver::GetInstance()->webkit_source_dir().Append(
290 FILE_PATH_LITERAL("LayoutTests/"));
291#if defined(OS_WIN)
292 std::string utf8_path = WideToUTF8(replace_path.value());
293#else
294 std::string utf8_path =
295 WideToUTF8(base::SysNativeMBToWide(replace_path.value()));
296#endif
297 std::string new_url =
298 std::string("file://") + utf8_path + utf8_url.substr(kPrefixLen);
299 return WebURL(GURL(new_url));
300}
301
[email protected]b3dbcb5e2012-12-17 00:16:37302WebPreferences* WebKitTestRunner::preferences() {
303 return &prefs_;
304}
305
306void WebKitTestRunner::applyPreferences() {
307 webkit_glue::WebPreferences prefs = render_view()->GetWebkitPreferences();
[email protected]d48eab082012-12-19 09:25:47308 ExportLayoutTestSpecificPreferences(prefs_, &prefs);
[email protected]b3dbcb5e2012-12-17 00:16:37309 render_view()->SetWebkitPreferences(prefs);
[email protected]55915a72012-12-18 11:55:25310 Send(new ShellViewHostMsg_OverridePreferences(routing_id(), prefs));
[email protected]b3dbcb5e2012-12-17 00:16:37311}
312
[email protected]9d9ec982013-01-10 18:53:39313std::string WebKitTestRunner::makeURLErrorDescription(
314 const WebURLError& error) {
315 std::string domain = error.domain.utf8();
316 int code = error.reason;
317
318 if (domain == net::kErrorDomain) {
319 domain = "NSURLErrorDomain";
320 switch (error.reason) {
321 case net::ERR_ABORTED:
322 code = -999; // NSURLErrorCancelled
323 break;
324 case net::ERR_UNSAFE_PORT:
325 // Our unsafe port checking happens at the network stack level, but we
326 // make this translation here to match the behavior of stock WebKit.
327 domain = "WebKitErrorDomain";
328 code = 103;
329 break;
330 case net::ERR_ADDRESS_INVALID:
331 case net::ERR_ADDRESS_UNREACHABLE:
332 case net::ERR_NETWORK_ACCESS_DENIED:
333 code = -1004; // NSURLErrorCannotConnectToHost
334 break;
335 }
336 } else {
337 DLOG(WARNING) << "Unknown error domain";
338 }
339
340 return base::StringPrintf("<NSError domain %s, code %d, failing URL \"%s\">",
341 domain.c_str(), code, error.unreachableURL.spec().data());
342}
343
[email protected]29e82702013-02-14 12:56:40344void WebKitTestRunner::setClientWindowRect(const WebRect& rect) {
[email protected]8be1c582013-03-06 00:55:03345 ForceResizeRenderView(render_view(), WebSize(rect.width, rect.height));
[email protected]29e82702013-02-14 12:56:40346}
347
348void WebKitTestRunner::showDevTools() {
[email protected]f85c5842013-02-15 14:15:06349 Send(new ShellViewHostMsg_ShowDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40350}
351
352void WebKitTestRunner::closeDevTools() {
[email protected]f85c5842013-02-15 14:15:06353 Send(new ShellViewHostMsg_CloseDevTools(routing_id()));
[email protected]29e82702013-02-14 12:56:40354}
355
356void WebKitTestRunner::evaluateInWebInspector(long call_id,
357 const std::string& script) {
[email protected]f85c5842013-02-15 14:15:06358 WebDevToolsAgent* agent = render_view()->GetWebView()->devToolsAgent();
359 if (agent)
360 agent->evaluateInWebInspector(call_id, WebString::fromUTF8(script));
[email protected]29e82702013-02-14 12:56:40361}
362
363void WebKitTestRunner::clearAllDatabases() {
[email protected]833b700d2013-02-18 19:43:00364 Send(new ShellViewHostMsg_ClearAllDatabases(routing_id()));
[email protected]29e82702013-02-14 12:56:40365}
366
367void WebKitTestRunner::setDatabaseQuota(int quota) {
[email protected]833b700d2013-02-18 19:43:00368 Send(new ShellViewHostMsg_SetDatabaseQuota(routing_id(), quota));
[email protected]29e82702013-02-14 12:56:40369}
370
371void WebKitTestRunner::setDeviceScaleFactor(float factor) {
[email protected]3a1c8a8032013-03-18 22:35:32372 SetDeviceScaleFactor(render_view(), factor);
[email protected]29e82702013-02-14 12:56:40373}
374
[email protected]83654ec2013-03-04 15:58:53375void WebKitTestRunner::setFocus(WebTestProxyBase* proxy, bool focus) {
[email protected]95026a02013-03-05 08:34:30376 ProxyToRenderViewVisitor visitor(proxy);
377 RenderView::ForEach(&visitor);
378 if (!visitor.render_view()) {
379 NOTREACHED();
380 return;
381 }
382
383 // Check whether the focused view was closed meanwhile.
384 if (!WebKitTestRunner::Get(focused_view_))
385 focused_view_ = NULL;
386
387 if (focus) {
388 if (focused_view_ != visitor.render_view()) {
389 if (focused_view_)
390 SetFocusAndActivate(focused_view_, false);
391 SetFocusAndActivate(visitor.render_view(), true);
392 focused_view_ = visitor.render_view();
393 }
394 } else {
395 if (focused_view_ == visitor.render_view()) {
396 SetFocusAndActivate(visitor.render_view(), false);
397 focused_view_ = NULL;
398 }
399 }
[email protected]83654ec2013-03-04 15:58:53400}
401
[email protected]29e82702013-02-14 12:56:40402void WebKitTestRunner::setAcceptAllCookies(bool accept) {
[email protected]833b700d2013-02-18 19:43:00403 Send(new ShellViewHostMsg_AcceptAllCookies(routing_id(), accept));
[email protected]29e82702013-02-14 12:56:40404}
405
406std::string WebKitTestRunner::pathToLocalResource(const std::string& resource) {
[email protected]0c5f2222013-02-20 11:26:19407#if defined(OS_WIN)
408 if (resource.find("/tmp/") == 0) {
409 // We want a temp file.
[email protected]38c1555c62013-03-16 19:20:55410 GURL base_url = net::FilePathToFileURL(test_config_.temp_path);
[email protected]0c5f2222013-02-20 11:26:19411 return base_url.Resolve(resource.substr(strlen("/tmp/"))).spec();
412 }
413#endif
414
415 // Some layout tests use file://// which we resolve as a UNC path. Normalize
416 // them to just file:///.
417 std::string result = resource;
418 while (StringToLowerASCII(result).find("file:////") == 0) {
419 result = result.substr(0, strlen("file:///")) +
420 result.substr(strlen("file:////"));
421 }
422 return rewriteLayoutTestsURL(result).spec();
[email protected]29e82702013-02-14 12:56:40423}
424
425void WebKitTestRunner::setLocale(const std::string& locale) {
[email protected]833b700d2013-02-18 19:43:00426 setlocale(LC_ALL, locale.c_str());
[email protected]29e82702013-02-14 12:56:40427}
428
[email protected]29e82702013-02-14 12:56:40429void WebKitTestRunner::testFinished() {
[email protected]1ca4fac2013-04-08 14:15:11430 if (!is_main_window_) {
431 Send(new ShellViewHostMsg_TestFinishedInSecondaryWindow(routing_id()));
[email protected]5f20df362013-02-15 19:48:19432 return;
[email protected]1ca4fac2013-04-08 14:15:11433 }
[email protected]5f20df362013-02-15 19:48:19434 WebTestInterfaces* interfaces =
435 ShellRenderProcessObserver::GetInstance()->test_interfaces();
436 interfaces->setTestIsRunning(false);
[email protected]8f722692013-02-23 14:14:06437 if (interfaces->testRunner()->shouldDumpBackForwardList()) {
[email protected]f322356c2013-03-01 07:54:02438 SyncNavigationStateVisitor visitor;
439 RenderView::ForEach(&visitor);
[email protected]8f722692013-02-23 14:14:06440 Send(new ShellViewHostMsg_CaptureSessionHistory(routing_id()));
441 } else {
442 CaptureDump();
443 }
[email protected]29e82702013-02-14 12:56:40444}
445
446void WebKitTestRunner::testTimedOut() {
[email protected]d679c862013-03-07 10:31:41447 if (!is_main_window_)
[email protected]5f20df362013-02-15 19:48:19448 return;
449 WebTestInterfaces* interfaces =
450 ShellRenderProcessObserver::GetInstance()->test_interfaces();
451 interfaces->setTestIsRunning(false);
[email protected]29e82702013-02-14 12:56:40452 Send(new ShellViewHostMsg_TestFinished(routing_id(), true));
453}
454
455bool WebKitTestRunner::isBeingDebugged() {
456 return base::debug::BeingDebugged();
457}
458
459int WebKitTestRunner::layoutTestTimeout() {
[email protected]4f92df82013-03-07 13:21:13460 return test_config_.layout_test_timeout;
[email protected]29e82702013-02-14 12:56:40461}
462
463void WebKitTestRunner::closeRemainingWindows() {
[email protected]2aa9b7a2013-04-15 11:07:47464 NavigateAwayVisitor visitor(render_view());
465 RenderView::ForEach(&visitor);
[email protected]b778e182013-03-04 13:32:16466 Send(new ShellViewHostMsg_CloseRemainingWindows(routing_id()));
[email protected]29e82702013-02-14 12:56:40467}
468
469int WebKitTestRunner::navigationEntryCount() {
[email protected]b283d292013-02-21 08:40:34470 return GetLocalSessionHistoryLength(render_view());
[email protected]29e82702013-02-14 12:56:40471}
472
[email protected]29e82702013-02-14 12:56:40473void WebKitTestRunner::goToOffset(int offset) {
[email protected]d2494ff2013-02-20 08:22:37474 Send(new ShellViewHostMsg_GoToOffset(routing_id(), offset));
[email protected]29e82702013-02-14 12:56:40475}
476
477void WebKitTestRunner::reload() {
[email protected]d2494ff2013-02-20 08:22:37478 Send(new ShellViewHostMsg_Reload(routing_id()));
[email protected]29e82702013-02-14 12:56:40479}
480
481void WebKitTestRunner::loadURLForFrame(const WebURL& url,
482 const std::string& frame_name) {
[email protected]d2494ff2013-02-20 08:22:37483 Send(new ShellViewHostMsg_LoadURLForFrame(
484 routing_id(), url, frame_name));
[email protected]29e82702013-02-14 12:56:40485}
486
487bool WebKitTestRunner::allowExternalPages() {
[email protected]4f92df82013-03-07 13:21:13488 return test_config_.allow_external_pages;
[email protected]29e82702013-02-14 12:56:40489}
490
491void WebKitTestRunner::captureHistoryForWindow(
[email protected]9063bf62013-02-20 19:42:01492 WebTestProxyBase* proxy,
[email protected]29e82702013-02-14 12:56:40493 WebVector<WebKit::WebHistoryItem>* history,
494 size_t* currentEntryIndex) {
[email protected]8f722692013-02-23 14:14:06495 size_t pos = 0;
496 std::vector<int>::iterator id;
497 for (id = routing_ids_.begin(); id != routing_ids_.end(); ++id, ++pos) {
498 RenderView* render_view = RenderView::FromRoutingID(*id);
499 if (!render_view) {
500 NOTREACHED();
501 continue;
502 }
503 if (WebKitTestRunner::Get(render_view)->proxy() == proxy)
504 break;
505 }
506
507 if (id == routing_ids_.end()) {
508 NOTREACHED();
509 return;
510 }
511 size_t num_entries = session_histories_[pos].size();
512 *currentEntryIndex = current_entry_indexes_[pos];
513 WebVector<WebHistoryItem> result(num_entries);
514 for (size_t entry = 0; entry < num_entries; ++entry) {
515 result[entry] =
516 webkit_glue::HistoryItemFromString(session_histories_[pos][entry]);
517 }
518 history->swap(result);
[email protected]29e82702013-02-14 12:56:40519}
520
[email protected]321d9eb2013-04-04 18:25:26521WebMediaPlayer* WebKitTestRunner::createWebMediaPlayer(
522 WebFrame* frame, const WebURL& url, WebMediaPlayerClient* client)
523{
524 if (!test_media_stream_client_.get()) {
525 test_media_stream_client_.reset(
526 new webkit_glue::TestMediaStreamClient());
527 }
528 return webkit_glue::CreateMediaPlayer(
529 frame, url, client, test_media_stream_client_.get());
530}
531
[email protected]b2324b092012-11-01 10:34:11532// RenderViewObserver --------------------------------------------------------
533
[email protected]9b4c6cd2012-08-20 10:47:45534void WebKitTestRunner::DidClearWindowObject(WebFrame* frame) {
[email protected]435b763d2013-03-07 13:27:59535 WebTestingSupport::injectInternalsObject(frame);
536 ShellRenderProcessObserver::GetInstance()->test_interfaces()->bindTo(frame);
[email protected]96b80b472012-07-03 19:41:56537}
538
[email protected]9b4c6cd2012-08-20 10:47:45539bool WebKitTestRunner::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33540 bool handled = true;
[email protected]9b4c6cd2012-08-20 10:47:45541 IPC_BEGIN_MESSAGE_MAP(WebKitTestRunner, message)
[email protected]0ac1a032013-02-12 12:53:37542 IPC_MESSAGE_HANDLER(ShellViewMsg_SetTestConfiguration,
543 OnSetTestConfiguration)
[email protected]8f722692013-02-23 14:14:06544 IPC_MESSAGE_HANDLER(ShellViewMsg_SessionHistory, OnSessionHistory)
[email protected]1afb9af2013-03-18 15:30:32545 IPC_MESSAGE_HANDLER(ShellViewMsg_Reset, OnReset)
[email protected]1ca4fac2013-04-08 14:15:11546 IPC_MESSAGE_HANDLER(ShellViewMsg_NotifyDone, OnNotifyDone)
[email protected]efb5f572012-01-29 10:57:33547 IPC_MESSAGE_UNHANDLED(handled = false)
548 IPC_END_MESSAGE_MAP()
549
550 return handled;
551}
552
[email protected]37d9dcce2013-03-22 22:27:45553void WebKitTestRunner::Navigate(const GURL& url) {
554 focus_on_next_commit_ = true;
[email protected]1ca4fac2013-04-08 14:15:11555 if (!is_main_window_ &&
556 ShellRenderProcessObserver::GetInstance()->main_test_runner() == this) {
557 WebTestInterfaces* interfaces =
558 ShellRenderProcessObserver::GetInstance()->test_interfaces();
559 interfaces->setTestIsRunning(true);
560 interfaces->configureForTestWithURL(GURL(), false);
561 }
[email protected]37d9dcce2013-03-22 22:27:45562}
563
564void WebKitTestRunner::DidCommitProvisionalLoad(WebFrame* frame,
565 bool is_new_navigation) {
566 if (!focus_on_next_commit_)
567 return;
568 focus_on_next_commit_ = false;
569 render_view()->GetWebView()->setFocusedFrame(frame);
570}
571
572void WebKitTestRunner::DidFailProvisionalLoad(WebFrame* frame,
573 const WebURLError& error) {
574 focus_on_next_commit_ = false;
575}
576
[email protected]b9fad24c2012-11-15 08:22:10577// Public methods - -----------------------------------------------------------
578
[email protected]324825d2012-11-30 12:37:15579void WebKitTestRunner::Reset() {
[email protected]ab683b22013-03-04 09:10:31580 // The proxy_ is always non-NULL, it is set right after construction.
581 proxy_->reset();
[email protected]b3dbcb5e2012-12-17 00:16:37582 prefs_.reset();
[email protected]8f722692013-02-23 14:14:06583 routing_ids_.clear();
584 session_histories_.clear();
585 current_entry_indexes_.clear();
[email protected]613ba94c2013-03-08 14:14:31586
587 render_view()->ClearEditCommands();
588 render_view()->GetWebView()->mainFrame()->setName(WebString());
589 render_view()->GetWebView()->mainFrame()->clearOpener();
[email protected]e8f35bc2013-03-18 15:22:01590 render_view()->GetWebView()->setPageScaleFactorLimits(-1, -1);
[email protected]613ba94c2013-03-08 14:14:31591 render_view()->GetWebView()->setPageScaleFactor(1, WebPoint(0, 0));
592 render_view()->GetWebView()->enableFixedLayoutMode(false);
593 render_view()->GetWebView()->setFixedLayoutSize(WebSize(0, 0));
594
[email protected]435b763d2013-03-07 13:27:59595 // Resetting the internals object also overrides the WebPreferences, so we
596 // have to sync them to WebKit again.
597 WebTestingSupport::resetInternalsObject(
598 render_view()->GetWebView()->mainFrame());
599 render_view()->SetWebkitPreferences(render_view()->GetWebkitPreferences());
[email protected]324825d2012-11-30 12:37:15600}
601
[email protected]b2324b092012-11-01 10:34:11602// Private methods -----------------------------------------------------------
603
[email protected]0ac1a032013-02-12 12:53:37604void WebKitTestRunner::CaptureDump() {
[email protected]5f20df362013-02-15 19:48:19605 WebTestInterfaces* interfaces =
606 ShellRenderProcessObserver::GetInstance()->test_interfaces();
607
[email protected]893fc6362013-02-18 13:24:05608 if (interfaces->testRunner()->shouldDumpAsAudio()) {
609 const WebArrayBufferView* audio_data =
610 interfaces->testRunner()->audioData();
611 std::vector<unsigned char> vector_data(
612 static_cast<const unsigned char*>(audio_data->baseAddress()),
613 static_cast<const unsigned char*>(audio_data->baseAddress()) +
614 audio_data->byteLength());
615 Send(new ShellViewHostMsg_AudioDump(routing_id(), vector_data));
[email protected]8b68e2bd2013-03-23 02:49:05616 } else {
617 Send(new ShellViewHostMsg_TextDump(routing_id(),
618 proxy()->captureTree(false)));
[email protected]893fc6362013-02-18 13:24:05619
[email protected]8b68e2bd2013-03-23 02:49:05620 if (test_config_.enable_pixel_dumping &&
621 interfaces->testRunner()->shouldGeneratePixelResults()) {
622 SkBitmap snapshot;
623 CopyCanvasToBitmap(proxy()->capturePixels(), &snapshot);
[email protected]5f20df362013-02-15 19:48:19624
[email protected]8b68e2bd2013-03-23 02:49:05625 SkAutoLockPixels snapshot_lock(snapshot);
626 base::MD5Digest digest;
[email protected]5f20df362013-02-15 19:48:19627#if defined(OS_ANDROID)
[email protected]8b68e2bd2013-03-23 02:49:05628 // On Android, pixel layout is RGBA, however, other Chrome platforms use
629 // BGRA.
630 const uint8_t* raw_pixels =
631 reinterpret_cast<const uint8_t*>(snapshot.getPixels());
632 size_t snapshot_size = snapshot.getSize();
[email protected]4ddc0c92013-04-11 02:09:42633 scoped_ptr<uint8_t[]> reordered_pixels(new uint8_t[snapshot_size]);
[email protected]8b68e2bd2013-03-23 02:49:05634 for (size_t i = 0; i < snapshot_size; i += 4) {
635 reordered_pixels[i] = raw_pixels[i + 2];
636 reordered_pixels[i + 1] = raw_pixels[i + 1];
637 reordered_pixels[i + 2] = raw_pixels[i];
638 reordered_pixels[i + 3] = raw_pixels[i + 3];
639 }
640 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest);
[email protected]5f20df362013-02-15 19:48:19641#else
[email protected]8b68e2bd2013-03-23 02:49:05642 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest);
[email protected]5f20df362013-02-15 19:48:19643#endif
[email protected]8b68e2bd2013-03-23 02:49:05644 std::string actual_pixel_hash = base::MD5DigestToBase16(digest);
[email protected]5f20df362013-02-15 19:48:19645
[email protected]8b68e2bd2013-03-23 02:49:05646 if (actual_pixel_hash == test_config_.expected_pixel_hash) {
647 SkBitmap empty_image;
648 Send(new ShellViewHostMsg_ImageDump(
649 routing_id(), actual_pixel_hash, empty_image));
650 } else {
651 Send(new ShellViewHostMsg_ImageDump(
652 routing_id(), actual_pixel_hash, snapshot));
653 }
[email protected]5f20df362013-02-15 19:48:19654 }
[email protected]0ac1a032013-02-12 12:53:37655 }
[email protected]5f20df362013-02-15 19:48:19656
[email protected]6d74fab2013-03-20 15:47:33657 MessageLoop::current()->PostTask(
658 FROM_HERE,
[email protected]8b68e2bd2013-03-23 02:49:05659 base::Bind(base::IgnoreResult(&WebKitTestRunner::Send),
660 base::Unretained(this),
661 new ShellViewHostMsg_TestFinished(routing_id(), false)));
[email protected]0ac1a032013-02-12 12:53:37662}
663
[email protected]0ac1a032013-02-12 12:53:37664void WebKitTestRunner::OnSetTestConfiguration(
[email protected]4f92df82013-03-07 13:21:13665 const ShellTestConfiguration& params) {
666 test_config_ = params;
[email protected]5f20df362013-02-15 19:48:19667 is_main_window_ = true;
[email protected]d679c862013-03-07 10:31:41668
669 setFocus(proxy_, true);
670
671 WebTestInterfaces* interfaces =
672 ShellRenderProcessObserver::GetInstance()->test_interfaces();
673 interfaces->setTestIsRunning(true);
[email protected]4f92df82013-03-07 13:21:13674 interfaces->configureForTestWithURL(params.test_url,
675 params.enable_pixel_dumping);
[email protected]0d2dfb92d2012-11-05 10:26:45676}
677
[email protected]8f722692013-02-23 14:14:06678void WebKitTestRunner::OnSessionHistory(
679 const std::vector<int>& routing_ids,
680 const std::vector<std::vector<std::string> >& session_histories,
681 const std::vector<unsigned>& current_entry_indexes) {
682 routing_ids_ = routing_ids;
683 session_histories_ = session_histories;
684 current_entry_indexes_ = current_entry_indexes;
685 CaptureDump();
686}
687
[email protected]1afb9af2013-03-18 15:30:32688void WebKitTestRunner::OnReset() {
[email protected]7e8c8962013-03-25 14:32:48689 ShellRenderProcessObserver::GetInstance()->test_interfaces()->resetAll();
690 Reset();
[email protected]8b68e2bd2013-03-23 02:49:05691 // Navigating to about:blank will make sure that no new loads are initiated
692 // by the renderer.
693 render_view()->GetWebView()->mainFrame()
694 ->loadRequest(WebURLRequest(GURL("about:blank")));
[email protected]1afb9af2013-03-18 15:30:32695 Send(new ShellViewHostMsg_ResetDone(routing_id()));
696}
697
[email protected]1ca4fac2013-04-08 14:15:11698void WebKitTestRunner::OnNotifyDone() {
699 render_view()->GetWebView()->mainFrame()->executeScript(
700 WebScriptSource(WebString::fromUTF8("testRunner.notifyDone();")));
701}
702
[email protected]31d71b02012-01-26 03:42:31703} // namespace content