Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / test / test_webkit_platform_support.cc
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.
4
5 #include "content/test/test_webkit_platform_support.h"
6
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/child/webkitplatformsupport_impl.h"
36 #include "webkit/renderer/compositor_bindings/web_compositor_support_impl.h"
37
38 #if defined(OS_MACOSX)
39 #include "base/mac/mac_util.h"
40 #endif
41
42 namespace content {
43
44 TestWebKitPlatformSupport::TestWebKitPlatformSupport() {
45   url_loader_factory_.reset(new WebURLLoaderMockFactory());
46   mock_clipboard_.reset(new MockWebClipboardImpl());
47   v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
48
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);
63
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();
71 #endif
72     if (media::InitializeMediaLibrary(module_path))
73       enable_media = true;
74   }
75   blink::WebRuntimeFeatures::enableMediaPlayer(enable_media);
76   LOG_IF(WARNING, !enable_media) << "Failed to initialize the media library.\n";
77
78   file_utilities_.set_sandbox_enabled(false);
79
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());
84   }
85
86 #if defined(OS_WIN)
87   // Ensure we pick up the default theme engine.
88   SetThemeEngine(NULL);
89 #endif
90
91   CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableFileCookies);
92
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()));
96 }
97
98 TestWebKitPlatformSupport::~TestWebKitPlatformSupport() {
99   url_loader_factory_.reset();
100   mock_clipboard_.reset();
101   blink::shutdown();
102 }
103
104 blink::WebMimeRegistry* TestWebKitPlatformSupport::mimeRegistry() {
105   return &mime_registry_;
106 }
107
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();
112 }
113
114 blink::WebFileUtilities* TestWebKitPlatformSupport::fileUtilities() {
115   return &file_utilities_;
116 }
117
118 blink::WebIDBFactory* TestWebKitPlatformSupport::idbFactory() {
119   NOTREACHED() <<
120       "IndexedDB cannot be tested with in-process harnesses.";
121   return NULL;
122 }
123
124 blink::WebURLLoader* TestWebKitPlatformSupport::createURLLoader() {
125   return url_loader_factory_->CreateURLLoader(
126       webkit_glue::WebKitPlatformSupportImpl::createURLLoader());
127 }
128
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"
143         "\x82";
144     return blink::WebData(red_square, arraysize(red_square));
145   }
146   return webkit_glue::WebKitPlatformSupportImpl::loadResource(name);
147 }
148
149 blink::WebString TestWebKitPlatformSupport::queryLocalizedString(
150     blink::WebLocalizedString::Name name) {
151   // Returns placeholder strings to check if they are correctly localized.
152   switch (name) {
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");
171     default:
172       return WebKitPlatformSupportImpl::queryLocalizedString(name);
173   }
174 }
175
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 WebKitPlatformSupportImpl::queryLocalizedString(name, value);
184 }
185
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 WebKitPlatformSupportImpl::queryLocalizedString(name, value1, value2);
195 }
196
197 blink::WebString TestWebKitPlatformSupport::defaultLocale() {
198   return base::ASCIIToUTF16("en-US");
199 }
200
201 #if defined(OS_WIN) || defined(OS_MACOSX)
202 void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine* engine) {
203   active_theme_engine_ = engine ?
204       engine : WebKitPlatformSupportChildImpl::themeEngine();
205 }
206
207 blink::WebThemeEngine* TestWebKitPlatformSupport::themeEngine() {
208   return active_theme_engine_;
209 }
210 #endif
211
212 blink::WebCompositorSupport* TestWebKitPlatformSupport::compositorSupport() {
213   return &compositor_support_;
214 }
215
216 base::string16 TestWebKitPlatformSupport::GetLocalizedString(int message_id) {
217   return base::string16();
218 }
219
220 base::StringPiece TestWebKitPlatformSupport::GetDataResource(
221     int resource_id,
222     ui::ScaleFactor scale_factor) {
223   return base::StringPiece();
224 }
225
226 webkit_glue::ResourceLoaderBridge*
227 TestWebKitPlatformSupport::CreateResourceLoader(
228     const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info) {
229   NOTREACHED();
230   return NULL;
231 }
232
233 webkit_glue::WebSocketStreamHandleBridge*
234 TestWebKitPlatformSupport::CreateWebSocketStreamBridge(
235     blink::WebSocketStreamHandle* handle,
236     webkit_glue::WebSocketStreamHandleDelegate* delegate) {
237   NOTREACHED();
238   return NULL;
239 }
240
241 blink::WebGestureCurve* TestWebKitPlatformSupport::createFlingAnimationCurve(
242     int device_source,
243     const blink::WebFloatPoint& velocity,
244     const blink::WebSize& cumulative_scroll) {
245   // Caller will retain and release.
246   return new WebGestureCurveMock(velocity, cumulative_scroll);
247 }
248
249 blink::WebUnitTestSupport* TestWebKitPlatformSupport::unitTestSupport() {
250   return this;
251 }
252
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);
258 }
259
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);
265 }
266
267 void TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL& url) {
268   url_loader_factory_->UnregisterURL(url);
269 }
270
271 void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
272   url_loader_factory_->UnregisterAllURLs();
273 }
274
275 void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
276   url_loader_factory_->ServeAsynchronousRequests();
277 }
278
279 blink::WebString TestWebKitPlatformSupport::webKitRootDir() {
280   base::FilePath path;
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());
288 }
289
290 blink::WebLayerTreeView*
291 TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
292   scoped_ptr<WebLayerTreeViewImplForTesting> view(
293       new WebLayerTreeViewImplForTesting());
294
295   view->Initialize();
296   return view.release();
297 }
298
299 blink::WebLayerTreeView*
300 TestWebKitPlatformSupport::createLayerTreeViewForTesting(TestViewType type) {
301   DCHECK_EQ(TestViewTypeUnitTest, type);
302   return createLayerTreeViewForTesting();
303 }
304
305 blink::WebData TestWebKitPlatformSupport::readFromFile(
306     const blink::WebString& path) {
307   base::FilePath file_path = base::FilePath::FromUTF16Unsafe(path);
308
309   std::string buffer;
310   base::ReadFileToString(file_path, &buffer);
311
312   return blink::WebData(buffer.data(), buffer.size());
313 }
314
315 }  // namespace content