1 // Copyright 2013 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.
5 #include "content/test/test_blink_web_unit_test_support.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/path_service.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/platform_thread.h"
15 #include "components/scheduler/renderer/renderer_scheduler_impl.h"
16 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
17 #include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h"
18 #include "content/test/mock_webclipboard_impl.h"
19 #include "content/test/web_gesture_curve_mock.h"
20 #include "content/test/web_layer_tree_view_impl_for_testing.h"
21 #include "content/test/weburl_loader_mock_factory.h"
22 #include "media/base/media.h"
23 #include "net/cookies/cookie_monster.h"
24 #include "net/test/spawned_test_server/spawned_test_server.h"
25 #include "storage/browser/database/vfs_backend.h"
26 #include "third_party/WebKit/public/platform/WebData.h"
27 #include "third_party/WebKit/public/platform/WebFileSystem.h"
28 #include "third_party/WebKit/public/platform/WebPluginListBuilder.h"
29 #include "third_party/WebKit/public/platform/WebStorageArea.h"
30 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
31 #include "third_party/WebKit/public/platform/WebString.h"
32 #include "third_party/WebKit/public/platform/WebURL.h"
33 #include "third_party/WebKit/public/web/WebDatabase.h"
34 #include "third_party/WebKit/public/web/WebKit.h"
35 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
36 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
37 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
38 #include "v8/include/v8.h"
40 #if defined(OS_MACOSX)
41 #include "base/mac/foundation_util.h"
42 #include "base/mac/scoped_nsautorelease_pool.h"
45 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
46 #include "gin/v8_initializer.h"
51 class DummyTaskRunner
: public base::SingleThreadTaskRunner
{
53 DummyTaskRunner() : thread_id_(base::PlatformThread::CurrentId()) {}
55 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
56 const base::Closure
& task
,
57 base::TimeDelta delay
) override
{
62 bool PostNonNestableDelayedTask(const tracked_objects::Location
& from_here
,
63 const base::Closure
& task
,
64 base::TimeDelta delay
) override
{
69 bool RunsTasksOnCurrentThread() const override
{
70 return thread_id_
== base::PlatformThread::CurrentId();
74 ~DummyTaskRunner() override
{}
76 base::PlatformThreadId thread_id_
;
78 DISALLOW_COPY_AND_ASSIGN(DummyTaskRunner
);
85 TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
86 #if defined(OS_MACOSX)
87 base::mac::ScopedNSAutoreleasePool autorelease_pool
;
90 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
91 mock_clipboard_
.reset(new MockWebClipboardImpl());
93 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
94 gin::V8Initializer::LoadV8Snapshot();
95 gin::V8Initializer::LoadV8Natives();
98 scoped_refptr
<base::SingleThreadTaskRunner
> dummy_task_runner
;
99 scoped_ptr
<base::ThreadTaskRunnerHandle
> dummy_task_runner_handle
;
100 if (!base::ThreadTaskRunnerHandle::IsSet()) {
101 // Dummy task runner is initialized here because the blink::initialize
102 // creates IsolateHolder which needs the current task runner handle. There
103 // should be no task posted to this task runner. The message loop is not
104 // created before this initialization because some tests need specific kinds
105 // of message loops, and their types are not known upfront. Some tests also
106 // create their own thread bundles or message loops, and doing the same in
107 // TestBlinkWebUnitTestSupport would introduce a conflict.
108 dummy_task_runner
= make_scoped_refptr(new DummyTaskRunner());
109 dummy_task_runner_handle
.reset(
110 new base::ThreadTaskRunnerHandle(dummy_task_runner
));
112 renderer_scheduler_
= make_scoped_ptr(new scheduler::RendererSchedulerImpl(
113 scheduler::LazySchedulerMessageLoopDelegateForTests::Create()));
114 web_thread_
.reset(new scheduler::WebThreadImplForRendererScheduler(
115 renderer_scheduler_
.get()));
117 blink::initialize(this);
118 blink::setLayoutTestMode(true);
119 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
120 blink::WebString::fromUTF8("test-shell-resource"));
121 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
122 blink::WebString::fromUTF8("test-shell-resource"));
123 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
124 blink::WebString::fromUTF8("test-shell-resource"));
125 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
126 blink::WebString::fromUTF8("test-shell-resource"));
127 blink::WebRuntimeFeatures::enableApplicationCache(true);
128 blink::WebRuntimeFeatures::enableDatabase(true);
129 blink::WebRuntimeFeatures::enableNotifications(true);
130 blink::WebRuntimeFeatures::enableTouch(true);
132 // Initialize libraries for media and enable the media player.
133 media::InitializeMediaLibrary();
134 blink::WebRuntimeFeatures::enableMediaPlayer(true);
136 file_utilities_
.set_sandbox_enabled(false);
138 if (!file_system_root_
.CreateUniqueTempDir()) {
139 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
140 "FileSystem feature will be disabled.";
141 DCHECK(file_system_root_
.path().empty());
145 // Ensure we pick up the default theme engine.
146 SetThemeEngine(NULL
);
149 // Test shell always exposes the GC.
150 std::string
flags("--expose-gc");
151 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
154 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
155 url_loader_factory_
.reset();
156 mock_clipboard_
.reset();
157 if (renderer_scheduler_
)
158 renderer_scheduler_
->Shutdown();
162 blink::WebBlobRegistry
* TestBlinkWebUnitTestSupport::blobRegistry() {
163 return &blob_registry_
;
166 blink::WebClipboard
* TestBlinkWebUnitTestSupport::clipboard() {
167 // Mock out clipboard calls so that tests don't mess
168 // with each other's copies/pastes when running in parallel.
169 return mock_clipboard_
.get();
172 blink::WebFileUtilities
* TestBlinkWebUnitTestSupport::fileUtilities() {
173 return &file_utilities_
;
176 blink::WebIDBFactory
* TestBlinkWebUnitTestSupport::idbFactory() {
178 "IndexedDB cannot be tested with in-process harnesses.";
182 blink::WebMimeRegistry
* TestBlinkWebUnitTestSupport::mimeRegistry() {
183 return &mime_registry_
;
186 blink::WebURLLoader
* TestBlinkWebUnitTestSupport::createURLLoader() {
187 return url_loader_factory_
->CreateURLLoader(
188 BlinkPlatformImpl::createURLLoader());
191 blink::WebString
TestBlinkWebUnitTestSupport::userAgent() {
192 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
195 blink::WebData
TestBlinkWebUnitTestSupport::loadResource(const char* name
) {
196 if (!strcmp(name
, "deleteButton")) {
197 // Create a red 30x30 square.
198 const char red_square
[] =
199 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
200 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
201 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
202 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
203 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
204 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
205 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
206 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
207 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
208 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
210 return blink::WebData(red_square
, arraysize(red_square
));
212 return BlinkPlatformImpl::loadResource(name
);
215 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
216 blink::WebLocalizedString::Name name
) {
217 // Returns placeholder strings to check if they are correctly localized.
219 case blink::WebLocalizedString::OtherDateLabel
:
220 return base::ASCIIToUTF16("<<OtherDateLabel>>");
221 case blink::WebLocalizedString::OtherMonthLabel
:
222 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
223 case blink::WebLocalizedString::OtherTimeLabel
:
224 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
225 case blink::WebLocalizedString::OtherWeekLabel
:
226 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
227 case blink::WebLocalizedString::CalendarClear
:
228 return base::ASCIIToUTF16("<<CalendarClear>>");
229 case blink::WebLocalizedString::CalendarToday
:
230 return base::ASCIIToUTF16("<<CalendarToday>>");
231 case blink::WebLocalizedString::ThisMonthButtonLabel
:
232 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
233 case blink::WebLocalizedString::ThisWeekButtonLabel
:
234 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
235 case blink::WebLocalizedString::WeekFormatTemplate
:
236 return base::ASCIIToUTF16("Week $2, $1");
238 return blink::WebString();
242 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
243 blink::WebLocalizedString::Name name
,
244 const blink::WebString
& value
) {
245 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
246 return base::ASCIIToUTF16("range underflow");
247 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
248 return base::ASCIIToUTF16("range overflow");
249 if (name
== blink::WebLocalizedString::SelectMenuListText
)
250 return base::ASCIIToUTF16("$1 selected");
251 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
254 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
255 blink::WebLocalizedString::Name name
,
256 const blink::WebString
& value1
,
257 const blink::WebString
& value2
) {
258 if (name
== blink::WebLocalizedString::ValidationTooLong
)
259 return base::ASCIIToUTF16("too long");
260 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
261 return base::ASCIIToUTF16("step mismatch");
262 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
265 blink::WebString
TestBlinkWebUnitTestSupport::defaultLocale() {
266 return base::ASCIIToUTF16("en-US");
269 #if defined(OS_WIN) || defined(OS_MACOSX)
270 void TestBlinkWebUnitTestSupport::SetThemeEngine(
271 blink::WebThemeEngine
* engine
) {
272 active_theme_engine_
= engine
? engine
: BlinkPlatformImpl::themeEngine();
275 blink::WebThemeEngine
* TestBlinkWebUnitTestSupport::themeEngine() {
276 return active_theme_engine_
;
280 blink::WebCompositorSupport
* TestBlinkWebUnitTestSupport::compositorSupport() {
281 return &compositor_support_
;
284 blink::WebGestureCurve
* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
285 blink::WebGestureDevice device_source
,
286 const blink::WebFloatPoint
& velocity
,
287 const blink::WebSize
& cumulative_scroll
) {
288 // Caller will retain and release.
289 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
292 blink::WebUnitTestSupport
* TestBlinkWebUnitTestSupport::unitTestSupport() {
296 void TestBlinkWebUnitTestSupport::registerMockedURL(
297 const blink::WebURL
& url
,
298 const blink::WebURLResponse
& response
,
299 const blink::WebString
& file_path
) {
300 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
303 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
304 const blink::WebURL
& url
,
305 const blink::WebURLResponse
& response
,
306 const blink::WebURLError
& error
) {
307 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
310 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
311 const blink::WebURL
& url
) {
312 url_loader_factory_
->UnregisterURL(url
);
315 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
316 url_loader_factory_
->UnregisterAllURLs();
319 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
320 url_loader_factory_
->ServeAsynchronousRequests();
323 blink::WebString
TestBlinkWebUnitTestSupport::webKitRootDir() {
325 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
326 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
327 path
= base::MakeAbsoluteFilePath(path
);
328 CHECK(!path
.empty());
329 std::string path_ascii
= path
.MaybeAsASCII();
330 CHECK(!path_ascii
.empty());
331 return blink::WebString::fromUTF8(path_ascii
.c_str());
334 blink::WebLayerTreeView
*
335 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
336 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
337 new WebLayerTreeViewImplForTesting());
340 return view
.release();
343 blink::WebData
TestBlinkWebUnitTestSupport::readFromFile(
344 const blink::WebString
& path
) {
345 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
348 base::ReadFileToString(file_path
, &buffer
);
350 return blink::WebData(buffer
.data(), buffer
.size());
353 bool TestBlinkWebUnitTestSupport::getBlobItems(
354 const blink::WebString
& uuid
,
355 blink::WebVector
<blink::WebBlobData::Item
*>* items
) {
356 return blob_registry_
.GetBlobItems(uuid
, items
);
359 blink::WebThread
* TestBlinkWebUnitTestSupport::currentThread() {
360 if (web_thread_
&& web_thread_
->isCurrentThread())
361 return web_thread_
.get();
362 return BlinkPlatformImpl::currentThread();
365 void TestBlinkWebUnitTestSupport::enterRunLoop() {
366 DCHECK(base::MessageLoop::current());
367 DCHECK(!base::MessageLoop::current()->is_running());
368 base::MessageLoop::current()->Run();
371 void TestBlinkWebUnitTestSupport::exitRunLoop() {
372 base::MessageLoop::current()->Quit();
375 void TestBlinkWebUnitTestSupport::getPluginList(
376 bool refresh
, blink::WebPluginListBuilder
* builder
) {
377 builder
->addPlugin("pdf", "pdf", "pdf-files");
378 builder
->addMediaTypeToLastPlugin("application/pdf", "pdf");
381 } // namespace content