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/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/renderer/scheduler/renderer_scheduler.h"
15 #include "content/renderer/scheduler/web_scheduler_impl.h"
16 #include "content/renderer/scheduler/webthread_impl_for_scheduler.h"
17 #include "content/test/mock_webclipboard_impl.h"
18 #include "content/test/web_gesture_curve_mock.h"
19 #include "content/test/web_layer_tree_view_impl_for_testing.h"
20 #include "content/test/weburl_loader_mock_factory.h"
21 #include "media/base/media.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/test/spawned_test_server/spawned_test_server.h"
24 #include "storage/browser/database/vfs_backend.h"
25 #include "third_party/WebKit/public/platform/WebData.h"
26 #include "third_party/WebKit/public/platform/WebFileSystem.h"
27 #include "third_party/WebKit/public/platform/WebStorageArea.h"
28 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
29 #include "third_party/WebKit/public/platform/WebString.h"
30 #include "third_party/WebKit/public/platform/WebURL.h"
31 #include "third_party/WebKit/public/web/WebDatabase.h"
32 #include "third_party/WebKit/public/web/WebKit.h"
33 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
34 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
35 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
36 #include "v8/include/v8.h"
38 #if defined(OS_MACOSX)
39 #include "base/mac/foundation_util.h"
40 #include "base/mac/scoped_nsautorelease_pool.h"
43 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
44 #include "gin/public/isolate_holder.h"
49 TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
50 #if defined(OS_MACOSX)
51 base::mac::ScopedNSAutoreleasePool autorelease_pool
;
54 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
55 mock_clipboard_
.reset(new MockWebClipboardImpl());
57 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
58 gin::IsolateHolder::LoadV8Snapshot();
61 if (base::MessageLoopProxy::current()) {
62 renderer_scheduler_
= RendererScheduler::Create();
63 web_scheduler_
.reset(new WebSchedulerImpl(renderer_scheduler_
.get()));
64 web_thread_
.reset(new WebThreadImplForScheduler(renderer_scheduler_
.get()));
67 blink::initialize(this);
68 blink::setLayoutTestMode(true);
69 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
70 blink::WebString::fromUTF8("test-shell-resource"));
71 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
72 blink::WebString::fromUTF8("test-shell-resource"));
73 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
74 blink::WebString::fromUTF8("test-shell-resource"));
75 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
76 blink::WebString::fromUTF8("test-shell-resource"));
77 blink::WebRuntimeFeatures::enableApplicationCache(true);
78 blink::WebRuntimeFeatures::enableDatabase(true);
79 blink::WebRuntimeFeatures::enableNotifications(true);
80 blink::WebRuntimeFeatures::enableTouch(true);
82 // Load libraries for media and enable the media player.
83 bool enable_media
= false;
84 base::FilePath module_path
;
85 if (PathService::Get(base::DIR_MODULE
, &module_path
)) {
86 #if defined(OS_MACOSX)
87 if (base::mac::AmIBundled())
88 module_path
= module_path
.DirName().DirName().DirName();
90 if (media::InitializeMediaLibrary(module_path
))
93 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media
);
94 LOG_IF(WARNING
, !enable_media
) << "Failed to initialize the media library.\n";
96 file_utilities_
.set_sandbox_enabled(false);
98 if (!file_system_root_
.CreateUniqueTempDir()) {
99 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
100 "FileSystem feature will be disabled.";
101 DCHECK(file_system_root_
.path().empty());
105 // Ensure we pick up the default theme engine.
106 SetThemeEngine(NULL
);
109 base::CommandLine::ForCurrentProcess()->AppendSwitch(
110 switches::kEnableFileCookies
);
112 // Test shell always exposes the GC.
113 std::string
flags("--expose-gc");
114 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
117 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
118 url_loader_factory_
.reset();
119 mock_clipboard_
.reset();
123 blink::WebBlobRegistry
* TestBlinkWebUnitTestSupport::blobRegistry() {
124 return &blob_registry_
;
127 blink::WebClipboard
* TestBlinkWebUnitTestSupport::clipboard() {
128 // Mock out clipboard calls so that tests don't mess
129 // with each other's copies/pastes when running in parallel.
130 return mock_clipboard_
.get();
133 blink::WebFileUtilities
* TestBlinkWebUnitTestSupport::fileUtilities() {
134 return &file_utilities_
;
137 blink::WebIDBFactory
* TestBlinkWebUnitTestSupport::idbFactory() {
139 "IndexedDB cannot be tested with in-process harnesses.";
143 blink::WebMimeRegistry
* TestBlinkWebUnitTestSupport::mimeRegistry() {
144 return &mime_registry_
;
147 blink::WebURLLoader
* TestBlinkWebUnitTestSupport::createURLLoader() {
148 return url_loader_factory_
->CreateURLLoader(
149 BlinkPlatformImpl::createURLLoader());
152 blink::WebString
TestBlinkWebUnitTestSupport::userAgent() {
153 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
156 blink::WebData
TestBlinkWebUnitTestSupport::loadResource(const char* name
) {
157 if (!strcmp(name
, "deleteButton")) {
158 // Create a red 30x30 square.
159 const char red_square
[] =
160 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
161 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
162 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
163 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
164 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
165 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
166 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
167 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
168 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
169 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
171 return blink::WebData(red_square
, arraysize(red_square
));
173 return BlinkPlatformImpl::loadResource(name
);
176 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
177 blink::WebLocalizedString::Name name
) {
178 // Returns placeholder strings to check if they are correctly localized.
180 case blink::WebLocalizedString::OtherDateLabel
:
181 return base::ASCIIToUTF16("<<OtherDateLabel>>");
182 case blink::WebLocalizedString::OtherMonthLabel
:
183 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
184 case blink::WebLocalizedString::OtherTimeLabel
:
185 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
186 case blink::WebLocalizedString::OtherWeekLabel
:
187 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
188 case blink::WebLocalizedString::CalendarClear
:
189 return base::ASCIIToUTF16("<<CalendarClear>>");
190 case blink::WebLocalizedString::CalendarToday
:
191 return base::ASCIIToUTF16("<<CalendarToday>>");
192 case blink::WebLocalizedString::ThisMonthButtonLabel
:
193 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
194 case blink::WebLocalizedString::ThisWeekButtonLabel
:
195 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
196 case blink::WebLocalizedString::WeekFormatTemplate
:
197 return base::ASCIIToUTF16("Week $2, $1");
199 return blink::WebString();
203 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
204 blink::WebLocalizedString::Name name
,
205 const blink::WebString
& value
) {
206 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
207 return base::ASCIIToUTF16("range underflow");
208 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
209 return base::ASCIIToUTF16("range overflow");
210 if (name
== blink::WebLocalizedString::SelectMenuListText
)
211 return base::ASCIIToUTF16("$1 selected");
212 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
215 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
216 blink::WebLocalizedString::Name name
,
217 const blink::WebString
& value1
,
218 const blink::WebString
& value2
) {
219 if (name
== blink::WebLocalizedString::ValidationTooLong
)
220 return base::ASCIIToUTF16("too long");
221 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
222 return base::ASCIIToUTF16("step mismatch");
223 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
226 blink::WebString
TestBlinkWebUnitTestSupport::defaultLocale() {
227 return base::ASCIIToUTF16("en-US");
230 #if defined(OS_WIN) || defined(OS_MACOSX)
231 void TestBlinkWebUnitTestSupport::SetThemeEngine(
232 blink::WebThemeEngine
* engine
) {
233 active_theme_engine_
= engine
? engine
: BlinkPlatformImpl::themeEngine();
236 blink::WebThemeEngine
* TestBlinkWebUnitTestSupport::themeEngine() {
237 return active_theme_engine_
;
241 blink::WebCompositorSupport
* TestBlinkWebUnitTestSupport::compositorSupport() {
242 return &compositor_support_
;
245 blink::WebGestureCurve
* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
246 blink::WebGestureDevice device_source
,
247 const blink::WebFloatPoint
& velocity
,
248 const blink::WebSize
& cumulative_scroll
) {
249 // Caller will retain and release.
250 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
253 blink::WebUnitTestSupport
* TestBlinkWebUnitTestSupport::unitTestSupport() {
257 void TestBlinkWebUnitTestSupport::registerMockedURL(
258 const blink::WebURL
& url
,
259 const blink::WebURLResponse
& response
,
260 const blink::WebString
& file_path
) {
261 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
264 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
265 const blink::WebURL
& url
,
266 const blink::WebURLResponse
& response
,
267 const blink::WebURLError
& error
) {
268 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
271 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
272 const blink::WebURL
& url
) {
273 url_loader_factory_
->UnregisterURL(url
);
276 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
277 url_loader_factory_
->UnregisterAllURLs();
280 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
281 url_loader_factory_
->ServeAsynchronousRequests();
284 blink::WebString
TestBlinkWebUnitTestSupport::webKitRootDir() {
286 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
287 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
288 path
= base::MakeAbsoluteFilePath(path
);
289 CHECK(!path
.empty());
290 std::string path_ascii
= path
.MaybeAsASCII();
291 CHECK(!path_ascii
.empty());
292 return blink::WebString::fromUTF8(path_ascii
.c_str());
295 blink::WebLayerTreeView
*
296 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
297 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
298 new WebLayerTreeViewImplForTesting());
301 return view
.release();
304 blink::WebData
TestBlinkWebUnitTestSupport::readFromFile(
305 const blink::WebString
& path
) {
306 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
309 base::ReadFileToString(file_path
, &buffer
);
311 return blink::WebData(buffer
.data(), buffer
.size());
314 bool TestBlinkWebUnitTestSupport::getBlobItems(
315 const blink::WebString
& uuid
,
316 blink::WebVector
<blink::WebBlobData::Item
*>* items
) {
317 return blob_registry_
.GetBlobItems(uuid
, items
);
320 blink::WebScheduler
* TestBlinkWebUnitTestSupport::scheduler() {
321 return web_scheduler_
.get();
324 blink::WebThread
* TestBlinkWebUnitTestSupport::currentThread() {
325 if (web_thread_
&& web_thread_
->isCurrentThread())
326 return web_thread_
.get();
327 return BlinkPlatformImpl::currentThread();
330 } // namespace content