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/strings/utf_string_conversions.h"
12 #include "components/scheduler/renderer/renderer_scheduler.h"
13 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
14 #include "content/test/mock_webclipboard_impl.h"
15 #include "content/test/web_gesture_curve_mock.h"
16 #include "content/test/web_layer_tree_view_impl_for_testing.h"
17 #include "content/test/weburl_loader_mock_factory.h"
18 #include "media/base/media.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/test/spawned_test_server/spawned_test_server.h"
21 #include "storage/browser/database/vfs_backend.h"
22 #include "third_party/WebKit/public/platform/WebData.h"
23 #include "third_party/WebKit/public/platform/WebFileSystem.h"
24 #include "third_party/WebKit/public/platform/WebStorageArea.h"
25 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
26 #include "third_party/WebKit/public/platform/WebString.h"
27 #include "third_party/WebKit/public/platform/WebURL.h"
28 #include "third_party/WebKit/public/web/WebDatabase.h"
29 #include "third_party/WebKit/public/web/WebKit.h"
30 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
31 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
32 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
33 #include "v8/include/v8.h"
35 #if defined(OS_MACOSX)
36 #include "base/mac/foundation_util.h"
37 #include "base/mac/scoped_nsautorelease_pool.h"
40 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
41 #include "gin/v8_initializer.h"
46 TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
47 #if defined(OS_MACOSX)
48 base::mac::ScopedNSAutoreleasePool autorelease_pool
;
51 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
52 mock_clipboard_
.reset(new MockWebClipboardImpl());
54 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
55 gin::V8Initializer::LoadV8Snapshot();
58 if (base::MessageLoopProxy::current()) {
59 renderer_scheduler_
= scheduler::RendererScheduler::Create();
60 web_thread_
.reset(new scheduler::WebThreadImplForRendererScheduler(
61 renderer_scheduler_
.get()));
64 blink::initialize(this);
65 blink::setLayoutTestMode(true);
66 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
67 blink::WebString::fromUTF8("test-shell-resource"));
68 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
69 blink::WebString::fromUTF8("test-shell-resource"));
70 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
71 blink::WebString::fromUTF8("test-shell-resource"));
72 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
73 blink::WebString::fromUTF8("test-shell-resource"));
74 blink::WebRuntimeFeatures::enableApplicationCache(true);
75 blink::WebRuntimeFeatures::enableDatabase(true);
76 blink::WebRuntimeFeatures::enableNotifications(true);
77 blink::WebRuntimeFeatures::enableTouch(true);
79 // Load libraries for media and enable the media player.
80 bool enable_media
= false;
81 base::FilePath module_path
;
82 if (PathService::Get(base::DIR_MODULE
, &module_path
)) {
83 #if defined(OS_MACOSX)
84 if (base::mac::AmIBundled())
85 module_path
= module_path
.DirName().DirName().DirName();
87 if (media::InitializeMediaLibrary(module_path
))
90 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media
);
91 LOG_IF(WARNING
, !enable_media
) << "Failed to initialize the media library.\n";
93 file_utilities_
.set_sandbox_enabled(false);
95 if (!file_system_root_
.CreateUniqueTempDir()) {
96 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
97 "FileSystem feature will be disabled.";
98 DCHECK(file_system_root_
.path().empty());
102 // Ensure we pick up the default theme engine.
103 SetThemeEngine(NULL
);
106 // Test shell always exposes the GC.
107 std::string
flags("--expose-gc");
108 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
111 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
112 url_loader_factory_
.reset();
113 mock_clipboard_
.reset();
114 if (renderer_scheduler_
)
115 renderer_scheduler_
->Shutdown();
119 blink::WebBlobRegistry
* TestBlinkWebUnitTestSupport::blobRegistry() {
120 return &blob_registry_
;
123 blink::WebClipboard
* TestBlinkWebUnitTestSupport::clipboard() {
124 // Mock out clipboard calls so that tests don't mess
125 // with each other's copies/pastes when running in parallel.
126 return mock_clipboard_
.get();
129 blink::WebFileUtilities
* TestBlinkWebUnitTestSupport::fileUtilities() {
130 return &file_utilities_
;
133 blink::WebIDBFactory
* TestBlinkWebUnitTestSupport::idbFactory() {
135 "IndexedDB cannot be tested with in-process harnesses.";
139 blink::WebMimeRegistry
* TestBlinkWebUnitTestSupport::mimeRegistry() {
140 return &mime_registry_
;
143 blink::WebURLLoader
* TestBlinkWebUnitTestSupport::createURLLoader() {
144 return url_loader_factory_
->CreateURLLoader(
145 BlinkPlatformImpl::createURLLoader());
148 blink::WebString
TestBlinkWebUnitTestSupport::userAgent() {
149 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
152 blink::WebData
TestBlinkWebUnitTestSupport::loadResource(const char* name
) {
153 if (!strcmp(name
, "deleteButton")) {
154 // Create a red 30x30 square.
155 const char red_square
[] =
156 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
157 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
158 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
159 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
160 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
161 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
162 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
163 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
164 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
165 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
167 return blink::WebData(red_square
, arraysize(red_square
));
169 return BlinkPlatformImpl::loadResource(name
);
172 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
173 blink::WebLocalizedString::Name name
) {
174 // Returns placeholder strings to check if they are correctly localized.
176 case blink::WebLocalizedString::OtherDateLabel
:
177 return base::ASCIIToUTF16("<<OtherDateLabel>>");
178 case blink::WebLocalizedString::OtherMonthLabel
:
179 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
180 case blink::WebLocalizedString::OtherTimeLabel
:
181 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
182 case blink::WebLocalizedString::OtherWeekLabel
:
183 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
184 case blink::WebLocalizedString::CalendarClear
:
185 return base::ASCIIToUTF16("<<CalendarClear>>");
186 case blink::WebLocalizedString::CalendarToday
:
187 return base::ASCIIToUTF16("<<CalendarToday>>");
188 case blink::WebLocalizedString::ThisMonthButtonLabel
:
189 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
190 case blink::WebLocalizedString::ThisWeekButtonLabel
:
191 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
192 case blink::WebLocalizedString::WeekFormatTemplate
:
193 return base::ASCIIToUTF16("Week $2, $1");
195 return blink::WebString();
199 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
200 blink::WebLocalizedString::Name name
,
201 const blink::WebString
& value
) {
202 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
203 return base::ASCIIToUTF16("range underflow");
204 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
205 return base::ASCIIToUTF16("range overflow");
206 if (name
== blink::WebLocalizedString::SelectMenuListText
)
207 return base::ASCIIToUTF16("$1 selected");
208 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
211 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
212 blink::WebLocalizedString::Name name
,
213 const blink::WebString
& value1
,
214 const blink::WebString
& value2
) {
215 if (name
== blink::WebLocalizedString::ValidationTooLong
)
216 return base::ASCIIToUTF16("too long");
217 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
218 return base::ASCIIToUTF16("step mismatch");
219 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
222 blink::WebString
TestBlinkWebUnitTestSupport::defaultLocale() {
223 return base::ASCIIToUTF16("en-US");
226 #if defined(OS_WIN) || defined(OS_MACOSX)
227 void TestBlinkWebUnitTestSupport::SetThemeEngine(
228 blink::WebThemeEngine
* engine
) {
229 active_theme_engine_
= engine
? engine
: BlinkPlatformImpl::themeEngine();
232 blink::WebThemeEngine
* TestBlinkWebUnitTestSupport::themeEngine() {
233 return active_theme_engine_
;
237 blink::WebCompositorSupport
* TestBlinkWebUnitTestSupport::compositorSupport() {
238 return &compositor_support_
;
241 blink::WebGestureCurve
* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
242 blink::WebGestureDevice device_source
,
243 const blink::WebFloatPoint
& velocity
,
244 const blink::WebSize
& cumulative_scroll
) {
245 // Caller will retain and release.
246 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
249 blink::WebUnitTestSupport
* TestBlinkWebUnitTestSupport::unitTestSupport() {
253 void TestBlinkWebUnitTestSupport::registerMockedURL(
254 const blink::WebURL
& url
,
255 const blink::WebURLResponse
& response
,
256 const blink::WebString
& file_path
) {
257 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
260 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
261 const blink::WebURL
& url
,
262 const blink::WebURLResponse
& response
,
263 const blink::WebURLError
& error
) {
264 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
267 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
268 const blink::WebURL
& url
) {
269 url_loader_factory_
->UnregisterURL(url
);
272 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
273 url_loader_factory_
->UnregisterAllURLs();
276 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
277 url_loader_factory_
->ServeAsynchronousRequests();
280 blink::WebString
TestBlinkWebUnitTestSupport::webKitRootDir() {
282 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
283 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
284 path
= base::MakeAbsoluteFilePath(path
);
285 CHECK(!path
.empty());
286 std::string path_ascii
= path
.MaybeAsASCII();
287 CHECK(!path_ascii
.empty());
288 return blink::WebString::fromUTF8(path_ascii
.c_str());
291 blink::WebLayerTreeView
*
292 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
293 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
294 new WebLayerTreeViewImplForTesting());
297 return view
.release();
300 blink::WebData
TestBlinkWebUnitTestSupport::readFromFile(
301 const blink::WebString
& path
) {
302 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
305 base::ReadFileToString(file_path
, &buffer
);
307 return blink::WebData(buffer
.data(), buffer
.size());
310 bool TestBlinkWebUnitTestSupport::getBlobItems(
311 const blink::WebString
& uuid
,
312 blink::WebVector
<blink::WebBlobData::Item
*>* items
) {
313 return blob_registry_
.GetBlobItems(uuid
, items
);
316 blink::WebThread
* TestBlinkWebUnitTestSupport::currentThread() {
317 if (web_thread_
&& web_thread_
->isCurrentThread())
318 return web_thread_
.get();
319 return BlinkPlatformImpl::currentThread();
322 } // namespace content