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_webkit_platform_support.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/metrics/stats_counters.h"
12 #include "base/path_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/test/mock_webclipboard_impl.h"
16 #include "content/test/web_gesture_curve_mock.h"
17 #include "content/test/web_layer_tree_view_impl_for_testing.h"
18 #include "content/test/weburl_loader_mock_factory.h"
19 #include "media/base/media.h"
20 #include "net/cookies/cookie_monster.h"
21 #include "net/test/spawned_test_server/spawned_test_server.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"
34 #include "webkit/browser/database/vfs_backend.h"
36 #if defined(OS_MACOSX)
37 #include "base/mac/mac_util.h"
38 #include "base/mac/scoped_nsautorelease_pool.h"
43 TestWebKitPlatformSupport::TestWebKitPlatformSupport() {
44 #if defined(OS_MACOSX)
45 base::mac::ScopedNSAutoreleasePool autorelease_pool
;
48 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
49 mock_clipboard_
.reset(new MockWebClipboardImpl());
51 // Create an anonymous stats table since we don't need to share between
54 new base::StatsTable(base::StatsTable::TableIdentifier(), 20, 200));
55 base::StatsTable::set_current(stats_table_
.get());
57 blink::initialize(this);
58 blink::mainThreadIsolate()->SetCounterFunction(
59 base::StatsTable::FindLocation
);
60 blink::setLayoutTestMode(true);
61 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
62 blink::WebString::fromUTF8("test-shell-resource"));
63 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
64 blink::WebString::fromUTF8("test-shell-resource"));
65 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
66 blink::WebString::fromUTF8("test-shell-resource"));
67 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
68 blink::WebString::fromUTF8("test-shell-resource"));
69 blink::WebRuntimeFeatures::enableApplicationCache(true);
70 blink::WebRuntimeFeatures::enableDatabase(true);
71 blink::WebRuntimeFeatures::enableNotifications(true);
72 blink::WebRuntimeFeatures::enableTouch(true);
74 // Load libraries for media and enable the media player.
75 bool enable_media
= false;
76 base::FilePath module_path
;
77 if (PathService::Get(base::DIR_MODULE
, &module_path
)) {
78 #if defined(OS_MACOSX)
79 if (base::mac::AmIBundled())
80 module_path
= module_path
.DirName().DirName().DirName();
82 if (media::InitializeMediaLibrary(module_path
))
85 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media
);
86 LOG_IF(WARNING
, !enable_media
) << "Failed to initialize the media library.\n";
88 file_utilities_
.set_sandbox_enabled(false);
90 if (!file_system_root_
.CreateUniqueTempDir()) {
91 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
92 "FileSystem feature will be disabled.";
93 DCHECK(file_system_root_
.path().empty());
97 // Ensure we pick up the default theme engine.
101 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableFileCookies
);
103 // Test shell always exposes the GC.
104 std::string
flags("--expose-gc");
105 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
108 TestWebKitPlatformSupport::~TestWebKitPlatformSupport() {
109 url_loader_factory_
.reset();
110 mock_clipboard_
.reset();
112 base::StatsTable::set_current(NULL
);
113 stats_table_
.reset();
116 blink::WebBlobRegistry
* TestWebKitPlatformSupport::blobRegistry() {
117 return &blob_registry_
;
120 blink::WebClipboard
* TestWebKitPlatformSupport::clipboard() {
121 // Mock out clipboard calls so that tests don't mess
122 // with each other's copies/pastes when running in parallel.
123 return mock_clipboard_
.get();
126 blink::WebFileUtilities
* TestWebKitPlatformSupport::fileUtilities() {
127 return &file_utilities_
;
130 blink::WebIDBFactory
* TestWebKitPlatformSupport::idbFactory() {
132 "IndexedDB cannot be tested with in-process harnesses.";
136 blink::WebMimeRegistry
* TestWebKitPlatformSupport::mimeRegistry() {
137 return &mime_registry_
;
140 blink::WebURLLoader
* TestWebKitPlatformSupport::createURLLoader() {
141 return url_loader_factory_
->CreateURLLoader(
142 BlinkPlatformImpl::createURLLoader());
145 blink::WebString
TestWebKitPlatformSupport::userAgent() {
146 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
149 blink::WebData
TestWebKitPlatformSupport::loadResource(const char* name
) {
150 if (!strcmp(name
, "deleteButton")) {
151 // Create a red 30x30 square.
152 const char red_square
[] =
153 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
154 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
155 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
156 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
157 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
158 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
159 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
160 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
161 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
162 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
164 return blink::WebData(red_square
, arraysize(red_square
));
166 return blink::WebData();
169 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
170 blink::WebLocalizedString::Name name
) {
171 // Returns placeholder strings to check if they are correctly localized.
173 case blink::WebLocalizedString::OtherDateLabel
:
174 return base::ASCIIToUTF16("<<OtherDateLabel>>");
175 case blink::WebLocalizedString::OtherMonthLabel
:
176 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
177 case blink::WebLocalizedString::OtherTimeLabel
:
178 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
179 case blink::WebLocalizedString::OtherWeekLabel
:
180 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
181 case blink::WebLocalizedString::CalendarClear
:
182 return base::ASCIIToUTF16("<<CalendarClear>>");
183 case blink::WebLocalizedString::CalendarToday
:
184 return base::ASCIIToUTF16("<<CalendarToday>>");
185 case blink::WebLocalizedString::ThisMonthButtonLabel
:
186 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
187 case blink::WebLocalizedString::ThisWeekButtonLabel
:
188 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
189 case blink::WebLocalizedString::WeekFormatTemplate
:
190 return base::ASCIIToUTF16("Week $2, $1");
192 return blink::WebString();
196 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
197 blink::WebLocalizedString::Name name
,
198 const blink::WebString
& value
) {
199 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
200 return base::ASCIIToUTF16("range underflow");
201 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
202 return base::ASCIIToUTF16("range overflow");
203 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
206 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
207 blink::WebLocalizedString::Name name
,
208 const blink::WebString
& value1
,
209 const blink::WebString
& value2
) {
210 if (name
== blink::WebLocalizedString::ValidationTooLong
)
211 return base::ASCIIToUTF16("too long");
212 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
213 return base::ASCIIToUTF16("step mismatch");
214 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
217 blink::WebString
TestWebKitPlatformSupport::defaultLocale() {
218 return base::ASCIIToUTF16("en-US");
221 #if defined(OS_WIN) || defined(OS_MACOSX)
222 void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine
* engine
) {
223 active_theme_engine_
= engine
? engine
: BlinkPlatformImpl::themeEngine();
226 blink::WebThemeEngine
* TestWebKitPlatformSupport::themeEngine() {
227 return active_theme_engine_
;
231 blink::WebCompositorSupport
* TestWebKitPlatformSupport::compositorSupport() {
232 return &compositor_support_
;
235 blink::WebGestureCurve
* TestWebKitPlatformSupport::createFlingAnimationCurve(
236 blink::WebGestureDevice device_source
,
237 const blink::WebFloatPoint
& velocity
,
238 const blink::WebSize
& cumulative_scroll
) {
239 // Caller will retain and release.
240 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
243 blink::WebUnitTestSupport
* TestWebKitPlatformSupport::unitTestSupport() {
247 void TestWebKitPlatformSupport::registerMockedURL(
248 const blink::WebURL
& url
,
249 const blink::WebURLResponse
& response
,
250 const blink::WebString
& file_path
) {
251 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
254 void TestWebKitPlatformSupport::registerMockedErrorURL(
255 const blink::WebURL
& url
,
256 const blink::WebURLResponse
& response
,
257 const blink::WebURLError
& error
) {
258 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
261 void TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL
& url
) {
262 url_loader_factory_
->UnregisterURL(url
);
265 void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
266 url_loader_factory_
->UnregisterAllURLs();
269 void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
270 url_loader_factory_
->ServeAsynchronousRequests();
273 blink::WebString
TestWebKitPlatformSupport::webKitRootDir() {
275 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
276 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
277 path
= base::MakeAbsoluteFilePath(path
);
278 CHECK(!path
.empty());
279 std::string path_ascii
= path
.MaybeAsASCII();
280 CHECK(!path_ascii
.empty());
281 return blink::WebString::fromUTF8(path_ascii
.c_str());
284 blink::WebLayerTreeView
*
285 TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
286 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
287 new WebLayerTreeViewImplForTesting());
290 return view
.release();
293 blink::WebData
TestWebKitPlatformSupport::readFromFile(
294 const blink::WebString
& path
) {
295 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
298 base::ReadFileToString(file_path
, &buffer
);
300 return blink::WebData(buffer
.data(), buffer
.size());
303 } // namespace content