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"
35 #include "webkit/renderer/compositor_bindings/web_compositor_support_impl.h"
37 #if defined(OS_MACOSX)
38 #include "base/mac/mac_util.h"
43 TestWebKitPlatformSupport::TestWebKitPlatformSupport() {
44 url_loader_factory_
.reset(new WebURLLoaderMockFactory());
45 mock_clipboard_
.reset(new MockWebClipboardImpl());
46 v8::V8::SetCounterFunction(base::StatsTable::FindLocation
);
48 blink::initialize(this);
49 blink::setLayoutTestMode(true);
50 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
51 blink::WebString::fromUTF8("test-shell-resource"));
52 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
53 blink::WebString::fromUTF8("test-shell-resource"));
54 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
55 blink::WebString::fromUTF8("test-shell-resource"));
56 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
57 blink::WebString::fromUTF8("test-shell-resource"));
58 blink::WebRuntimeFeatures::enableApplicationCache(true);
59 blink::WebRuntimeFeatures::enableDatabase(true);
60 blink::WebRuntimeFeatures::enableNotifications(true);
61 blink::WebRuntimeFeatures::enableTouch(true);
63 // Load libraries for media and enable the media player.
64 bool enable_media
= false;
65 base::FilePath module_path
;
66 if (PathService::Get(base::DIR_MODULE
, &module_path
)) {
67 #if defined(OS_MACOSX)
68 if (base::mac::AmIBundled())
69 module_path
= module_path
.DirName().DirName().DirName();
71 if (media::InitializeMediaLibrary(module_path
))
74 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media
);
75 LOG_IF(WARNING
, !enable_media
) << "Failed to initialize the media library.\n";
77 file_utilities_
.set_sandbox_enabled(false);
79 if (!file_system_root_
.CreateUniqueTempDir()) {
80 LOG(WARNING
) << "Failed to create a temp dir for the filesystem."
81 "FileSystem feature will be disabled.";
82 DCHECK(file_system_root_
.path().empty());
86 // Ensure we pick up the default theme engine.
90 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableFileCookies
);
92 // Test shell always exposes the GC.
93 std::string
flags("--expose-gc");
94 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
97 TestWebKitPlatformSupport::~TestWebKitPlatformSupport() {
98 url_loader_factory_
.reset();
99 mock_clipboard_
.reset();
103 blink::WebMimeRegistry
* TestWebKitPlatformSupport::mimeRegistry() {
104 return &mime_registry_
;
107 blink::WebClipboard
* TestWebKitPlatformSupport::clipboard() {
108 // Mock out clipboard calls so that tests don't mess
109 // with each other's copies/pastes when running in parallel.
110 return mock_clipboard_
.get();
113 blink::WebFileUtilities
* TestWebKitPlatformSupport::fileUtilities() {
114 return &file_utilities_
;
117 blink::WebIDBFactory
* TestWebKitPlatformSupport::idbFactory() {
119 "IndexedDB cannot be tested with in-process harnesses.";
123 blink::WebURLLoader
* TestWebKitPlatformSupport::createURLLoader() {
124 return url_loader_factory_
->CreateURLLoader(
125 BlinkPlatformImpl::createURLLoader());
128 blink::WebString
TestWebKitPlatformSupport::userAgent() {
129 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
132 blink::WebData
TestWebKitPlatformSupport::loadResource(const char* name
) {
133 if (!strcmp(name
, "deleteButton")) {
134 // Create a red 30x30 square.
135 const char red_square
[] =
136 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
137 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
138 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
139 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
140 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
141 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
142 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
143 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
144 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
145 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
147 return blink::WebData(red_square
, arraysize(red_square
));
149 return blink::WebData();
152 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
153 blink::WebLocalizedString::Name name
) {
154 // Returns placeholder strings to check if they are correctly localized.
156 case blink::WebLocalizedString::OtherDateLabel
:
157 return base::ASCIIToUTF16("<<OtherDateLabel>>");
158 case blink::WebLocalizedString::OtherMonthLabel
:
159 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
160 case blink::WebLocalizedString::OtherTimeLabel
:
161 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
162 case blink::WebLocalizedString::OtherWeekLabel
:
163 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
164 case blink::WebLocalizedString::CalendarClear
:
165 return base::ASCIIToUTF16("<<CalendarClear>>");
166 case blink::WebLocalizedString::CalendarToday
:
167 return base::ASCIIToUTF16("<<CalendarToday>>");
168 case blink::WebLocalizedString::ThisMonthButtonLabel
:
169 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
170 case blink::WebLocalizedString::ThisWeekButtonLabel
:
171 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
172 case blink::WebLocalizedString::WeekFormatTemplate
:
173 return base::ASCIIToUTF16("Week $2, $1");
175 return blink::WebString();
179 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
180 blink::WebLocalizedString::Name name
,
181 const blink::WebString
& value
) {
182 if (name
== blink::WebLocalizedString::ValidationRangeUnderflow
)
183 return base::ASCIIToUTF16("range underflow");
184 if (name
== blink::WebLocalizedString::ValidationRangeOverflow
)
185 return base::ASCIIToUTF16("range overflow");
186 return BlinkPlatformImpl::queryLocalizedString(name
, value
);
189 blink::WebString
TestWebKitPlatformSupport::queryLocalizedString(
190 blink::WebLocalizedString::Name name
,
191 const blink::WebString
& value1
,
192 const blink::WebString
& value2
) {
193 if (name
== blink::WebLocalizedString::ValidationTooLong
)
194 return base::ASCIIToUTF16("too long");
195 if (name
== blink::WebLocalizedString::ValidationStepMismatch
)
196 return base::ASCIIToUTF16("step mismatch");
197 return BlinkPlatformImpl::queryLocalizedString(name
, value1
, value2
);
200 blink::WebString
TestWebKitPlatformSupport::defaultLocale() {
201 return base::ASCIIToUTF16("en-US");
204 #if defined(OS_WIN) || defined(OS_MACOSX)
205 void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine
* engine
) {
206 active_theme_engine_
= engine
? engine
: BlinkPlatformImpl::themeEngine();
209 blink::WebThemeEngine
* TestWebKitPlatformSupport::themeEngine() {
210 return active_theme_engine_
;
214 blink::WebCompositorSupport
* TestWebKitPlatformSupport::compositorSupport() {
215 return &compositor_support_
;
218 blink::WebGestureCurve
* TestWebKitPlatformSupport::createFlingAnimationCurve(
220 const blink::WebFloatPoint
& velocity
,
221 const blink::WebSize
& cumulative_scroll
) {
222 // Caller will retain and release.
223 return new WebGestureCurveMock(velocity
, cumulative_scroll
);
226 blink::WebUnitTestSupport
* TestWebKitPlatformSupport::unitTestSupport() {
230 void TestWebKitPlatformSupport::registerMockedURL(
231 const blink::WebURL
& url
,
232 const blink::WebURLResponse
& response
,
233 const blink::WebString
& file_path
) {
234 url_loader_factory_
->RegisterURL(url
, response
, file_path
);
237 void TestWebKitPlatformSupport::registerMockedErrorURL(
238 const blink::WebURL
& url
,
239 const blink::WebURLResponse
& response
,
240 const blink::WebURLError
& error
) {
241 url_loader_factory_
->RegisterErrorURL(url
, response
, error
);
244 void TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL
& url
) {
245 url_loader_factory_
->UnregisterURL(url
);
248 void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
249 url_loader_factory_
->UnregisterAllURLs();
252 void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
253 url_loader_factory_
->ServeAsynchronousRequests();
256 blink::WebString
TestWebKitPlatformSupport::webKitRootDir() {
258 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
259 path
= path
.Append(FILE_PATH_LITERAL("third_party/WebKit"));
260 path
= base::MakeAbsoluteFilePath(path
);
261 CHECK(!path
.empty());
262 std::string path_ascii
= path
.MaybeAsASCII();
263 CHECK(!path_ascii
.empty());
264 return blink::WebString::fromUTF8(path_ascii
.c_str());
267 blink::WebLayerTreeView
*
268 TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
269 scoped_ptr
<WebLayerTreeViewImplForTesting
> view(
270 new WebLayerTreeViewImplForTesting());
273 return view
.release();
276 blink::WebLayerTreeView
*
277 TestWebKitPlatformSupport::createLayerTreeViewForTesting(TestViewType type
) {
278 DCHECK_EQ(TestViewTypeUnitTest
, type
);
279 return createLayerTreeViewForTesting();
282 blink::WebData
TestWebKitPlatformSupport::readFromFile(
283 const blink::WebString
& path
) {
284 base::FilePath file_path
= base::FilePath::FromUTF16Unsafe(path
);
287 base::ReadFileToString(file_path
, &buffer
);
289 return blink::WebData(buffer
.data(), buffer
.size());
292 } // namespace content