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/test/mock_webclipboard_impl.h"
17 #include "content/test/web_gesture_curve_mock.h"
18 #include "content/test/web_layer_tree_view_impl_for_testing.h"
19 #include "content/test/weburl_loader_mock_factory.h"
20 #include "media/base/media.h"
21 #include "net/cookies/cookie_monster.h"
22 #include "net/test/spawned_test_server/spawned_test_server.h"
23 #include "storage/browser/database/vfs_backend.h"
24 #include "third_party/WebKit/public/platform/WebData.h"
25 #include "third_party/WebKit/public/platform/WebFileSystem.h"
26 #include "third_party/WebKit/public/platform/WebStorageArea.h"
27 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
28 #include "third_party/WebKit/public/platform/WebString.h"
29 #include "third_party/WebKit/public/platform/WebURL.h"
30 #include "third_party/WebKit/public/web/WebDatabase.h"
31 #include "third_party/WebKit/public/web/WebKit.h"
32 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
33 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
34 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
35 #include "v8/include/v8.h"
37 #if defined(OS_MACOSX)
38 #include "base/mac/foundation_util.h"
39 #include "base/mac/scoped_nsautorelease_pool.h"
42 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
43 #include "gin/public/isolate_holder.h"
48 TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
49 #if defined(OS_MACOSX)
50 base::mac::ScopedNSAutoreleasePool autorelease_pool
;
53 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
54 mock_clipboard_
.reset(new MockWebClipboardImpl());
56 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
57 gin::IsolateHolder::LoadV8Snapshot();
60 if (base::MessageLoopProxy::current()) {
61 renderer_scheduler_
= RendererScheduler::Create();
62 web_scheduler_
.reset(new WebSchedulerImpl(renderer_scheduler_
.get()));
65 blink::initialize(this);
66 blink::setLayoutTestMode(true);
67 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
68 blink::WebString::fromUTF8("test-shell-resource"));
69 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
70 blink::WebString::fromUTF8("test-shell-resource"));
71 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
72 blink::WebString::fromUTF8("test-shell-resource"));
73 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
74 blink::WebString::fromUTF8("test-shell-resource"));
75 blink::WebRuntimeFeatures::enableApplicationCache(true);
76 blink::WebRuntimeFeatures::enableDatabase(true);
77 blink::WebRuntimeFeatures::enableNotifications(true);
78 blink::WebRuntimeFeatures::enableTouch(true);
80 // Load libraries for media and enable the media player.
81 bool enable_media
= false;
82 base::FilePath module_path
;
83 if (PathService::Get(base::DIR_MODULE
, &module_path
)) {
84 #if defined(OS_MACOSX)
85 if (base::mac::AmIBundled())
86 module_path
= module_path
.DirName().DirName().DirName();
88 if (media::InitializeMediaLibrary(module_path
))
91 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media
);
92 LOG_IF(WARNING
, !enable_media
) << "Failed to initialize the media library.\n";
94 file_utilities_
.set_sandbox_enabled(false);
96 if (!file_system_root_
.CreateUniqueTempDir()) {
97 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
98 "FileSystem feature will be disabled.";
99 DCHECK(file_system_root_
.path().empty());
103 // Ensure we pick up the default theme engine.
104 SetThemeEngine(NULL
);
107 base::CommandLine::ForCurrentProcess()->AppendSwitch(
108 switches::kEnableFileCookies
);
110 // Test shell always exposes the GC.
111 std::string
flags("--expose-gc");
112 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
115 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
116 url_loader_factory_
.reset();
117 mock_clipboard_
.reset();
121 blink::WebBlobRegistry
* TestBlinkWebUnitTestSupport::blobRegistry() {
122 return &blob_registry_
;
125 blink::WebClipboard
* TestBlinkWebUnitTestSupport::clipboard() {
126 // Mock out clipboard calls so that tests don't mess
127 // with each other's copies/pastes when running in parallel.
128 return mock_clipboard_
.get();
131 blink::WebFileUtilities
* TestBlinkWebUnitTestSupport::fileUtilities() {
132 return &file_utilities_
;
135 blink::WebIDBFactory
* TestBlinkWebUnitTestSupport::idbFactory() {
137 "IndexedDB cannot be tested with in-process harnesses.";
141 blink::WebMimeRegistry
* TestBlinkWebUnitTestSupport::mimeRegistry() {
142 return &mime_registry_
;
145 blink::WebURLLoader
* TestBlinkWebUnitTestSupport::createURLLoader() {
146 return url_loader_factory_
->CreateURLLoader(
147 BlinkPlatformImpl::createURLLoader());
150 blink::WebString
TestBlinkWebUnitTestSupport::userAgent() {
151 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
154 blink::WebData
TestBlinkWebUnitTestSupport::loadResource(const char* name
) {
155 if (!strcmp(name
, "deleteButton")) {
156 // Create a red 30x30 square.
157 const char red_square
[] =
158 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
159 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
160 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
161 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
162 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
163 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
164 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
165 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
166 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
167 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
169 return blink::WebData(red_square
, arraysize(red_square
));
171 return BlinkPlatformImpl::loadResource(name
);
174 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
175 blink::WebLocalizedString::Name name
) {
176 // Returns placeholder strings to check if they are correctly localized.
178 case blink::WebLocalizedString::OtherDateLabel
:
179 return base::ASCIIToUTF16("<<OtherDateLabel>>");
180 case blink::WebLocalizedString::OtherMonthLabel
:
181 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
182 case blink::WebLocalizedString::OtherTimeLabel
:
183 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
184 case blink::WebLocalizedString::OtherWeekLabel
:
185 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
186 case blink::WebLocalizedString::CalendarClear
:
187 return base::ASCIIToUTF16("<<CalendarClear>>");
188 case blink::WebLocalizedString::CalendarToday
:
189 return base::ASCIIToUTF16("<<CalendarToday>>");
190 case blink::WebLocalizedString::ThisMonthButtonLabel
:
191 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
192 case blink::WebLocalizedString::ThisWeekButtonLabel
:
193 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
194 case blink::WebLocalizedString::WeekFormatTemplate
:
195 return base::ASCIIToUTF16("Week $2, $1");
197 return blink::WebString();
201 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
202 blink::WebLocalizedString::Name name
,
203 const blink::WebString
& value
) {
204 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
205 return base::ASCIIToUTF16("range underflow");
206 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
207 return base::ASCIIToUTF16("range overflow");
208 if (name
== blink::WebLocalizedString::SelectMenuListText
)
209 return base::ASCIIToUTF16("$1 selected");
210 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
213 blink::WebString
TestBlinkWebUnitTestSupport::queryLocalizedString(
214 blink::WebLocalizedString::Name name
,
215 const blink::WebString
& value1
,
216 const blink::WebString
& value2
) {
217 if (name
== blink::WebLocalizedString::ValidationTooLong
)
218 return base::ASCIIToUTF16("too long");
219 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
220 return base::ASCIIToUTF16("step mismatch");
221 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
224 blink::WebString
TestBlinkWebUnitTestSupport::defaultLocale() {
225 return base::ASCIIToUTF16("en-US");
228 #if defined(OS_WIN) || defined(OS_MACOSX)
229 void TestBlinkWebUnitTestSupport::SetThemeEngine(
230 blink::WebThemeEngine
* engine
) {
231 active_theme_engine_
= engine
? engine
: BlinkPlatformImpl::themeEngine();
234 blink::WebThemeEngine
* TestBlinkWebUnitTestSupport::themeEngine() {
235 return active_theme_engine_
;
239 blink::WebCompositorSupport
* TestBlinkWebUnitTestSupport::compositorSupport() {
240 return &compositor_support_
;
243 blink::WebGestureCurve
* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
244 blink::WebGestureDevice device_source
,
245 const blink::WebFloatPoint
& velocity
,
246 const blink::WebSize
& cumulative_scroll
) {
247 // Caller will retain and release.
248 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
251 blink::WebUnitTestSupport
* TestBlinkWebUnitTestSupport::unitTestSupport() {
255 void TestBlinkWebUnitTestSupport::registerMockedURL(
256 const blink::WebURL
& url
,
257 const blink::WebURLResponse
& response
,
258 const blink::WebString
& file_path
) {
259 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
262 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
263 const blink::WebURL
& url
,
264 const blink::WebURLResponse
& response
,
265 const blink::WebURLError
& error
) {
266 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
269 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
270 const blink::WebURL
& url
) {
271 url_loader_factory_
->UnregisterURL(url
);
274 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
275 url_loader_factory_
->UnregisterAllURLs();
278 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
279 url_loader_factory_
->ServeAsynchronousRequests();
282 blink::WebString
TestBlinkWebUnitTestSupport::webKitRootDir() {
284 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
285 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
286 path
= base::MakeAbsoluteFilePath(path
);
287 CHECK(!path
.empty());
288 std::string path_ascii
= path
.MaybeAsASCII();
289 CHECK(!path_ascii
.empty());
290 return blink::WebString::fromUTF8(path_ascii
.c_str());
293 blink::WebLayerTreeView
*
294 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
295 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
296 new WebLayerTreeViewImplForTesting());
299 return view
.release();
302 blink::WebData
TestBlinkWebUnitTestSupport::readFromFile(
303 const blink::WebString
& path
) {
304 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
307 base::ReadFileToString(file_path
, &buffer
);
309 return blink::WebData(buffer
.data(), buffer
.size());
312 bool TestBlinkWebUnitTestSupport::getBlobItems(
313 const blink::WebString
& uuid
,
314 blink::WebVector
<blink::WebBlobData::Item
*>* items
) {
315 return blob_registry_
.GetBlobItems(uuid
, items
);
318 blink::WebScheduler
* TestBlinkWebUnitTestSupport::scheduler() {
319 return web_scheduler_
.get();
322 } // namespace content