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/child/blink_platform_impl.h"
15 #include "content/public/common/content_switches.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 "third_party/WebKit/public/platform/WebData.h"
24 #include "third_party/WebKit/public/platform/WebFileSystem.h"
25 #include "third_party/WebKit/public/platform/WebStorageArea.h"
26 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
27 #include "third_party/WebKit/public/platform/WebString.h"
28 #include "third_party/WebKit/public/platform/WebURL.h"
29 #include "third_party/WebKit/public/web/WebDatabase.h"
30 #include "third_party/WebKit/public/web/WebKit.h"
31 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
32 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
33 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
34 #include "v8/include/v8.h"
35 #include "webkit/browser/database/vfs_backend.h"
36 #include "webkit/renderer/compositor_bindings/web_compositor_support_impl.h"
38 #if defined(OS_MACOSX)
39 #include "base/mac/mac_util.h"
44 TestWebKitPlatformSupport::TestWebKitPlatformSupport() {
45 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
46 mock_clipboard_
.reset(new MockWebClipboardImpl());
47 v8::V8::SetCounterFunction(base::StatsTable::FindLocation
);
49 blink::initialize(this);
50 blink::setLayoutTestMode(true);
51 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
52 blink::WebString::fromUTF8("test-shell-resource"));
53 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
54 blink::WebString::fromUTF8("test-shell-resource"));
55 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
56 blink::WebString::fromUTF8("test-shell-resource"));
57 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
58 blink::WebString::fromUTF8("test-shell-resource"));
59 blink::WebRuntimeFeatures::enableApplicationCache(true);
60 blink::WebRuntimeFeatures::enableDatabase(true);
61 blink::WebRuntimeFeatures::enableNotifications(true);
62 blink::WebRuntimeFeatures::enableTouch(true);
64 // Load libraries for media and enable the media player.
65 bool enable_media
= false;
66 base::FilePath module_path
;
67 if (PathService::Get(base::DIR_MODULE
, &module_path
)) {
68 #if defined(OS_MACOSX)
69 if (base::mac::AmIBundled())
70 module_path
= module_path
.DirName().DirName().DirName();
72 if (media::InitializeMediaLibrary(module_path
))
75 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media
);
76 LOG_IF(WARNING
, !enable_media
) << "Failed to initialize the media library.\n";
78 file_utilities_
.set_sandbox_enabled(false);
80 if (!file_system_root_
.CreateUniqueTempDir()) {
81 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
82 "FileSystem feature will be disabled.";
83 DCHECK(file_system_root_
.path().empty());
87 // Ensure we pick up the default theme engine.
91 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableFileCookies
);
93 // Test shell always exposes the GC.
94 std::string
flags("--expose-gc");
95 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
98 TestWebKitPlatformSupport::~TestWebKitPlatformSupport() {
99 url_loader_factory_
.reset();
100 mock_clipboard_
.reset();
104 blink::WebMimeRegistry
* TestWebKitPlatformSupport::mimeRegistry() {
105 return &mime_registry_
;
108 blink::WebClipboard
* TestWebKitPlatformSupport::clipboard() {
109 // Mock out clipboard calls so that tests don't mess
110 // with each other's copies/pastes when running in parallel.
111 return mock_clipboard_
.get();
114 blink::WebFileUtilities
* TestWebKitPlatformSupport::fileUtilities() {
115 return &file_utilities_
;
118 blink::WebIDBFactory
* TestWebKitPlatformSupport::idbFactory() {
120 "IndexedDB cannot be tested with in-process harnesses.";
124 blink::WebURLLoader
* TestWebKitPlatformSupport::createURLLoader() {
125 return url_loader_factory_
->CreateURLLoader(
126 BlinkPlatformImpl::createURLLoader());
129 blink::WebData
TestWebKitPlatformSupport::loadResource(const char* name
) {
130 if (!strcmp(name
, "deleteButton")) {
131 // Create a red 30x30 square.
132 const char red_square
[] =
133 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
134 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
135 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
136 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
137 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
138 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
139 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
140 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
141 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
142 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
144 return blink::WebData(red_square
, arraysize(red_square
));
146 return BlinkPlatformImpl::loadResource(name
);
149 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
150 blink::WebLocalizedString::Name name
) {
151 // Returns placeholder strings to check if they are correctly localized.
153 case blink::WebLocalizedString::OtherDateLabel
:
154 return base::ASCIIToUTF16("<<OtherDateLabel>>");
155 case blink::WebLocalizedString::OtherMonthLabel
:
156 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
157 case blink::WebLocalizedString::OtherTimeLabel
:
158 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
159 case blink::WebLocalizedString::OtherWeekLabel
:
160 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
161 case blink::WebLocalizedString::CalendarClear
:
162 return base::ASCIIToUTF16("<<CalendarClear>>");
163 case blink::WebLocalizedString::CalendarToday
:
164 return base::ASCIIToUTF16("<<CalendarToday>>");
165 case blink::WebLocalizedString::ThisMonthButtonLabel
:
166 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
167 case blink::WebLocalizedString::ThisWeekButtonLabel
:
168 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
169 case blink::WebLocalizedString::WeekFormatTemplate
:
170 return base::ASCIIToUTF16("Week $2, $1");
172 return BlinkPlatformImpl::queryLocalizedString(name
);
176 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
177 blink::WebLocalizedString::Name name
,
178 const blink::WebString
& value
) {
179 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
180 return base::ASCIIToUTF16("range underflow");
181 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
182 return base::ASCIIToUTF16("range overflow");
183 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
186 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
187 blink::WebLocalizedString::Name name
,
188 const blink::WebString
& value1
,
189 const blink::WebString
& value2
) {
190 if (name
== blink::WebLocalizedString::ValidationTooLong
)
191 return base::ASCIIToUTF16("too long");
192 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
193 return base::ASCIIToUTF16("step mismatch");
194 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
197 blink::WebString
TestWebKitPlatformSupport::defaultLocale() {
198 return base::ASCIIToUTF16("en-US");
201 #if defined(OS_WIN) || defined(OS_MACOSX)
202 void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine
* engine
) {
203 active_theme_engine_
= engine
?
204 engine
: WebKitPlatformSupportChildImpl::themeEngine();
207 blink::WebThemeEngine
* TestWebKitPlatformSupport::themeEngine() {
208 return active_theme_engine_
;
212 blink::WebCompositorSupport
* TestWebKitPlatformSupport::compositorSupport() {
213 return &compositor_support_
;
216 base::string16
TestWebKitPlatformSupport::GetLocalizedString(int message_id
) {
217 return base::string16();
220 base::StringPiece
TestWebKitPlatformSupport::GetDataResource(
222 ui::ScaleFactor scale_factor
) {
223 return base::StringPiece();
226 webkit_glue::ResourceLoaderBridge
*
227 TestWebKitPlatformSupport::CreateResourceLoader(
228 const webkit_glue::ResourceLoaderBridge::RequestInfo
& request_info
) {
233 WebSocketStreamHandleBridge
*
234 TestWebKitPlatformSupport::CreateWebSocketStreamBridge(
235 blink::WebSocketStreamHandle
* handle
,
236 WebSocketStreamHandleDelegate
* delegate
) {
241 blink::WebGestureCurve
* TestWebKitPlatformSupport::createFlingAnimationCurve(
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
* TestWebKitPlatformSupport::unitTestSupport() {
253 void TestWebKitPlatformSupport::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 TestWebKitPlatformSupport::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 TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL
& url
) {
268 url_loader_factory_
->UnregisterURL(url
);
271 void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
272 url_loader_factory_
->UnregisterAllURLs();
275 void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
276 url_loader_factory_
->ServeAsynchronousRequests();
279 blink::WebString
TestWebKitPlatformSupport::webKitRootDir() {
281 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
282 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
283 path
= base::MakeAbsoluteFilePath(path
);
284 CHECK(!path
.empty());
285 std::string path_ascii
= path
.MaybeAsASCII();
286 CHECK(!path_ascii
.empty());
287 return blink::WebString::fromUTF8(path_ascii
.c_str());
290 blink::WebLayerTreeView
*
291 TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
292 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
293 new WebLayerTreeViewImplForTesting());
296 return view
.release();
299 blink::WebLayerTreeView
*
300 TestWebKitPlatformSupport::createLayerTreeViewForTesting(TestViewType type
) {
301 DCHECK_EQ(TestViewTypeUnitTest
, type
);
302 return createLayerTreeViewForTesting();
305 blink::WebData
TestWebKitPlatformSupport::readFromFile(
306 const blink::WebString
& path
) {
307 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
310 base::ReadFileToString(file_path
, &buffer
);
312 return blink::WebData(buffer
.data(), buffer
.size());
315 } // namespace content