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::WebMimeRegistry
* TestWebKitPlatformSupport::mimeRegistry() {
117 return &mime_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::WebURLLoader
* TestWebKitPlatformSupport::createURLLoader() {
137 return url_loader_factory_
->CreateURLLoader(
138 BlinkPlatformImpl::createURLLoader());
141 blink::WebString
TestWebKitPlatformSupport::userAgent() {
142 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
145 blink::WebData
TestWebKitPlatformSupport::loadResource(const char* name
) {
146 if (!strcmp(name
, "deleteButton")) {
147 // Create a red 30x30 square.
148 const char red_square
[] =
149 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
150 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
151 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
152 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
153 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
154 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
155 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
156 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
157 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
158 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
160 return blink::WebData(red_square
, arraysize(red_square
));
162 return blink::WebData();
165 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
166 blink::WebLocalizedString::Name name
) {
167 // Returns placeholder strings to check if they are correctly localized.
169 case blink::WebLocalizedString::OtherDateLabel
:
170 return base::ASCIIToUTF16("<<OtherDateLabel>>");
171 case blink::WebLocalizedString::OtherMonthLabel
:
172 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
173 case blink::WebLocalizedString::OtherTimeLabel
:
174 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
175 case blink::WebLocalizedString::OtherWeekLabel
:
176 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
177 case blink::WebLocalizedString::CalendarClear
:
178 return base::ASCIIToUTF16("<<CalendarClear>>");
179 case blink::WebLocalizedString::CalendarToday
:
180 return base::ASCIIToUTF16("<<CalendarToday>>");
181 case blink::WebLocalizedString::ThisMonthButtonLabel
:
182 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
183 case blink::WebLocalizedString::ThisWeekButtonLabel
:
184 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
185 case blink::WebLocalizedString::WeekFormatTemplate
:
186 return base::ASCIIToUTF16("Week $2, $1");
188 return blink::WebString();
192 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
193 blink::WebLocalizedString::Name name
,
194 const blink::WebString
& value
) {
195 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
196 return base::ASCIIToUTF16("range underflow");
197 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
198 return base::ASCIIToUTF16("range overflow");
199 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
202 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
203 blink::WebLocalizedString::Name name
,
204 const blink::WebString
& value1
,
205 const blink::WebString
& value2
) {
206 if (name
== blink::WebLocalizedString::ValidationTooLong
)
207 return base::ASCIIToUTF16("too long");
208 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
209 return base::ASCIIToUTF16("step mismatch");
210 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
213 blink::WebString
TestWebKitPlatformSupport::defaultLocale() {
214 return base::ASCIIToUTF16("en-US");
217 #if defined(OS_WIN) || defined(OS_MACOSX)
218 void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine
* engine
) {
219 active_theme_engine_
= engine
? engine
: BlinkPlatformImpl::themeEngine();
222 blink::WebThemeEngine
* TestWebKitPlatformSupport::themeEngine() {
223 return active_theme_engine_
;
227 blink::WebCompositorSupport
* TestWebKitPlatformSupport::compositorSupport() {
228 return &compositor_support_
;
231 blink::WebGestureCurve
* TestWebKitPlatformSupport::createFlingAnimationCurve(
232 blink::WebGestureDevice device_source
,
233 const blink::WebFloatPoint
& velocity
,
234 const blink::WebSize
& cumulative_scroll
) {
235 // Caller will retain and release.
236 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
239 blink::WebUnitTestSupport
* TestWebKitPlatformSupport::unitTestSupport() {
243 void TestWebKitPlatformSupport::registerMockedURL(
244 const blink::WebURL
& url
,
245 const blink::WebURLResponse
& response
,
246 const blink::WebString
& file_path
) {
247 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
250 void TestWebKitPlatformSupport::registerMockedErrorURL(
251 const blink::WebURL
& url
,
252 const blink::WebURLResponse
& response
,
253 const blink::WebURLError
& error
) {
254 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
257 void TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL
& url
) {
258 url_loader_factory_
->UnregisterURL(url
);
261 void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
262 url_loader_factory_
->UnregisterAllURLs();
265 void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
266 url_loader_factory_
->ServeAsynchronousRequests();
269 blink::WebString
TestWebKitPlatformSupport::webKitRootDir() {
271 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
272 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
273 path
= base::MakeAbsoluteFilePath(path
);
274 CHECK(!path
.empty());
275 std::string path_ascii
= path
.MaybeAsASCII();
276 CHECK(!path_ascii
.empty());
277 return blink::WebString::fromUTF8(path_ascii
.c_str());
280 blink::WebLayerTreeView
*
281 TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
282 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
283 new WebLayerTreeViewImplForTesting());
286 return view
.release();
289 blink::WebData
TestWebKitPlatformSupport::readFromFile(
290 const blink::WebString
& path
) {
291 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
294 base::ReadFileToString(file_path
, &buffer
);
296 return blink::WebData(buffer
.data(), buffer
.size());
299 } // namespace content