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/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 "third_party/WebKit/public/platform/WebData.h"
22 #include "third_party/WebKit/public/platform/WebFileSystem.h"
23 #include "third_party/WebKit/public/platform/WebStorageArea.h"
24 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
25 #include "third_party/WebKit/public/platform/WebString.h"
26 #include "third_party/WebKit/public/platform/WebURL.h"
27 #include "third_party/WebKit/public/web/WebDatabase.h"
28 #include "third_party/WebKit/public/web/WebKit.h"
29 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
30 #include "third_party/WebKit/public/web/WebScriptController.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"
35 #include "webkit/child/webkitplatformsupport_impl.h"
36 #include "webkit/glue/simple_webmimeregistry_impl.h"
37 #include "webkit/glue/webkit_glue.h"
38 #include "webkit/renderer/compositor_bindings/web_compositor_support_impl.h"
41 #include "third_party/WebKit/public/platform/win/WebThemeEngine.h"
42 #elif defined(OS_MACOSX)
43 #include "base/mac/mac_util.h"
46 using blink::WebScriptController
;
47 using webkit::WebLayerTreeViewImplForTesting
;
51 TestWebKitPlatformSupport::TestWebKitPlatformSupport() {
52 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
53 mock_clipboard_
.reset(new MockWebClipboardImpl());
54 v8::V8::SetCounterFunction(base::StatsTable::FindLocation
);
56 blink::initialize(this);
57 blink::setLayoutTestMode(true);
58 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
59 blink::WebString::fromUTF8("test-shell-resource"));
60 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
61 blink::WebString::fromUTF8("test-shell-resource"));
62 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
63 blink::WebString::fromUTF8("test-shell-resource"));
64 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
65 blink::WebString::fromUTF8("test-shell-resource"));
66 WebScriptController::enableV8SingleThreadMode();
67 blink::WebRuntimeFeatures::enableApplicationCache(true);
68 blink::WebRuntimeFeatures::enableDatabase(true);
69 blink::WebRuntimeFeatures::enableNotifications(true);
70 blink::WebRuntimeFeatures::enableTouch(true);
72 // Load libraries for media and enable the media player.
73 bool enable_media
= false;
74 base::FilePath module_path
;
75 if (PathService::Get(base::DIR_MODULE
, &module_path
)) {
76 #if defined(OS_MACOSX)
77 if (base::mac::AmIBundled())
78 module_path
= module_path
.DirName().DirName().DirName();
80 if (media::InitializeMediaLibrary(module_path
))
83 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media
);
84 LOG_IF(WARNING
, !enable_media
) << "Failed to initialize the media library.\n";
86 // TODO(joth): Make a dummy geolocation service implemenation for
87 // test_shell, and set this to true. http://crbug.com/36451
88 blink::WebRuntimeFeatures::enableGeolocation(false);
90 file_utilities_
.set_sandbox_enabled(false);
92 if (!file_system_root_
.CreateUniqueTempDir()) {
93 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
94 "FileSystem feature will be disabled.";
95 DCHECK(file_system_root_
.path().empty());
99 // Ensure we pick up the default theme engine.
100 SetThemeEngine(NULL
);
103 net::CookieMonster::EnableFileScheme();
105 // Test shell always exposes the GC.
106 webkit_glue::SetJavaScriptFlags(" --expose-gc");
109 TestWebKitPlatformSupport::~TestWebKitPlatformSupport() {
110 url_loader_factory_
.reset();
111 mock_clipboard_
.reset();
115 blink::WebMimeRegistry
* TestWebKitPlatformSupport::mimeRegistry() {
116 return &mime_registry_
;
119 blink::WebClipboard
* TestWebKitPlatformSupport::clipboard() {
120 // Mock out clipboard calls so that tests don't mess
121 // with each other's copies/pastes when running in parallel.
122 return mock_clipboard_
.get();
125 blink::WebFileUtilities
* TestWebKitPlatformSupport::fileUtilities() {
126 return &file_utilities_
;
129 blink::WebIDBFactory
* TestWebKitPlatformSupport::idbFactory() {
131 "IndexedDB cannot be tested with in-process harnesses.";
135 blink::WebURLLoader
* TestWebKitPlatformSupport::createURLLoader() {
136 return url_loader_factory_
->CreateURLLoader(
137 webkit_glue::WebKitPlatformSupportImpl::createURLLoader());
140 blink::WebData
TestWebKitPlatformSupport::loadResource(const char* name
) {
141 if (!strcmp(name
, "deleteButton")) {
142 // Create a red 30x30 square.
143 const char red_square
[] =
144 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
145 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
146 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
147 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
148 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
149 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
150 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
151 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
152 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
153 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
155 return blink::WebData(red_square
, arraysize(red_square
));
157 return webkit_glue::WebKitPlatformSupportImpl::loadResource(name
);
160 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
161 blink::WebLocalizedString::Name name
) {
162 // Returns placeholder strings to check if they are correctly localized.
164 case blink::WebLocalizedString::OtherDateLabel
:
165 return ASCIIToUTF16("<<OtherDateLabel>>");
166 case blink::WebLocalizedString::OtherMonthLabel
:
167 return ASCIIToUTF16("<<OtherMonthLabel>>");
168 case blink::WebLocalizedString::OtherTimeLabel
:
169 return ASCIIToUTF16("<<OtherTimeLabel>>");
170 case blink::WebLocalizedString::OtherWeekLabel
:
171 return ASCIIToUTF16("<<OtherWeekLabel>>");
172 case blink::WebLocalizedString::CalendarClear
:
173 return ASCIIToUTF16("<<CalendarClear>>");
174 case blink::WebLocalizedString::CalendarToday
:
175 return ASCIIToUTF16("<<CalendarToday>>");
176 case blink::WebLocalizedString::ThisMonthButtonLabel
:
177 return ASCIIToUTF16("<<ThisMonthLabel>>");
178 case blink::WebLocalizedString::ThisWeekButtonLabel
:
179 return ASCIIToUTF16("<<ThisWeekLabel>>");
180 case blink::WebLocalizedString::WeekFormatTemplate
:
181 return ASCIIToUTF16("Week $2, $1");
183 return WebKitPlatformSupportImpl::queryLocalizedString(name
);
187 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
188 blink::WebLocalizedString::Name name
,
189 const blink::WebString
& value
) {
190 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
191 return ASCIIToUTF16("range underflow");
192 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
193 return ASCIIToUTF16("range overflow");
194 return WebKitPlatformSupportImpl::queryLocalizedString(name
, value
);
197 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
198 blink::WebLocalizedString::Name name
,
199 const blink::WebString
& value1
,
200 const blink::WebString
& value2
) {
201 if (name
== blink::WebLocalizedString::ValidationTooLong
)
202 return ASCIIToUTF16("too long");
203 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
204 return ASCIIToUTF16("step mismatch");
205 return WebKitPlatformSupportImpl::queryLocalizedString(name
, value1
, value2
);
208 blink::WebString
TestWebKitPlatformSupport::defaultLocale() {
209 return ASCIIToUTF16("en-US");
212 #if defined(OS_WIN) || defined(OS_MACOSX)
213 void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine
* engine
) {
214 active_theme_engine_
= engine
?
215 engine
: WebKitPlatformSupportChildImpl::themeEngine();
218 blink::WebThemeEngine
* TestWebKitPlatformSupport::themeEngine() {
219 return active_theme_engine_
;
223 blink::WebCompositorSupport
* TestWebKitPlatformSupport::compositorSupport() {
224 return &compositor_support_
;
227 base::string16
TestWebKitPlatformSupport::GetLocalizedString(int message_id
) {
228 return base::string16();
231 base::StringPiece
TestWebKitPlatformSupport::GetDataResource(
233 ui::ScaleFactor scale_factor
) {
234 return base::StringPiece();
237 webkit_glue::ResourceLoaderBridge
*
238 TestWebKitPlatformSupport::CreateResourceLoader(
239 const webkit_glue::ResourceLoaderBridge::RequestInfo
& request_info
) {
244 webkit_glue::WebSocketStreamHandleBridge
*
245 TestWebKitPlatformSupport::CreateWebSocketStreamBridge(
246 blink::WebSocketStreamHandle
* handle
,
247 webkit_glue::WebSocketStreamHandleDelegate
* delegate
) {
252 blink::WebGestureCurve
* TestWebKitPlatformSupport::createFlingAnimationCurve(
254 const blink::WebFloatPoint
& velocity
,
255 const blink::WebSize
& cumulative_scroll
) {
256 // Caller will retain and release.
257 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
260 blink::WebUnitTestSupport
* TestWebKitPlatformSupport::unitTestSupport() {
264 void TestWebKitPlatformSupport::registerMockedURL(
265 const blink::WebURL
& url
,
266 const blink::WebURLResponse
& response
,
267 const blink::WebString
& file_path
) {
268 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
271 void TestWebKitPlatformSupport::registerMockedErrorURL(
272 const blink::WebURL
& url
,
273 const blink::WebURLResponse
& response
,
274 const blink::WebURLError
& error
) {
275 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
278 void TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL
& url
) {
279 url_loader_factory_
->UnregisterURL(url
);
282 void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
283 url_loader_factory_
->UnregisterAllURLs();
286 void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
287 url_loader_factory_
->ServeAsynchronousRequests();
290 blink::WebString
TestWebKitPlatformSupport::webKitRootDir() {
292 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
293 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
294 path
= base::MakeAbsoluteFilePath(path
);
295 CHECK(!path
.empty());
296 std::string path_ascii
= path
.MaybeAsASCII();
297 CHECK(!path_ascii
.empty());
298 return blink::WebString::fromUTF8(path_ascii
.c_str());
301 blink::WebLayerTreeView
*
302 TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
303 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
304 new WebLayerTreeViewImplForTesting());
306 if (!view
->Initialize())
308 return view
.release();
311 blink::WebLayerTreeView
*
312 TestWebKitPlatformSupport::createLayerTreeViewForTesting(TestViewType type
) {
313 DCHECK_EQ(TestViewTypeUnitTest
, type
);
314 return createLayerTreeViewForTesting();
317 blink::WebData
TestWebKitPlatformSupport::readFromFile(
318 const blink::WebString
& path
) {
319 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
322 base::ReadFileToString(file_path
, &buffer
);
324 return blink::WebData(buffer
.data(), buffer
.size());
327 } // namespace content