test_webkit_platform_support.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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/renderer/compositor_bindings/web_compositor_support_impl.h"
36
37#if defined(OS_MACOSX)
38#include "base/mac/mac_util.h"
39#endif
40
41namespace content {
42
43TestWebKitPlatformSupport::TestWebKitPlatformSupport() {
44  url_loader_factory_.reset(new WebURLLoaderMockFactory());
45  mock_clipboard_.reset(new MockWebClipboardImpl());
46  v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
47
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);
62
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();
70#endif
71    if (media::InitializeMediaLibrary(module_path))
72      enable_media = true;
73  }
74  blink::WebRuntimeFeatures::enableMediaPlayer(enable_media);
75  LOG_IF(WARNING, !enable_media) << "Failed to initialize the media library.\n";
76
77  file_utilities_.set_sandbox_enabled(false);
78
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());
83  }
84
85#if defined(OS_WIN)
86  // Ensure we pick up the default theme engine.
87  SetThemeEngine(NULL);
88#endif
89
90  CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableFileCookies);
91
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()));
95}
96
97TestWebKitPlatformSupport::~TestWebKitPlatformSupport() {
98  url_loader_factory_.reset();
99  mock_clipboard_.reset();
100  blink::shutdown();
101}
102
103blink::WebMimeRegistry* TestWebKitPlatformSupport::mimeRegistry() {
104  return &mime_registry_;
105}
106
107blink::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();
111}
112
113blink::WebFileUtilities* TestWebKitPlatformSupport::fileUtilities() {
114  return &file_utilities_;
115}
116
117blink::WebIDBFactory* TestWebKitPlatformSupport::idbFactory() {
118  NOTREACHED() <<
119      "IndexedDB cannot be tested with in-process harnesses.";
120  return NULL;
121}
122
123blink::WebURLLoader* TestWebKitPlatformSupport::createURLLoader() {
124  return url_loader_factory_->CreateURLLoader(
125      BlinkPlatformImpl::createURLLoader());
126}
127
128blink::WebString TestWebKitPlatformSupport::userAgent() {
129  return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
130}
131
132blink::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"
146        "\x82";
147    return blink::WebData(red_square, arraysize(red_square));
148  }
149  return blink::WebData();
150}
151
152blink::WebString TestWebKitPlatformSupport::queryLocalizedString(
153    blink::WebLocalizedString::Name name) {
154  // Returns placeholder strings to check if they are correctly localized.
155  switch (name) {
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");
174    default:
175      return blink::WebString();
176  }
177}
178
179blink::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);
187}
188
189blink::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);
198}
199
200blink::WebString TestWebKitPlatformSupport::defaultLocale() {
201  return base::ASCIIToUTF16("en-US");
202}
203
204#if defined(OS_WIN) || defined(OS_MACOSX)
205void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine* engine) {
206  active_theme_engine_ = engine ? engine : BlinkPlatformImpl::themeEngine();
207}
208
209blink::WebThemeEngine* TestWebKitPlatformSupport::themeEngine() {
210  return active_theme_engine_;
211}
212#endif
213
214blink::WebCompositorSupport* TestWebKitPlatformSupport::compositorSupport() {
215  return &compositor_support_;
216}
217
218blink::WebGestureCurve* TestWebKitPlatformSupport::createFlingAnimationCurve(
219    blink::WebGestureDevice device_source,
220    const blink::WebFloatPoint& velocity,
221    const blink::WebSize& cumulative_scroll) {
222  // Caller will retain and release.
223  return new WebGestureCurveMock(velocity, cumulative_scroll);
224}
225
226blink::WebUnitTestSupport* TestWebKitPlatformSupport::unitTestSupport() {
227  return this;
228}
229
230void 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);
235}
236
237void 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);
242}
243
244void TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL& url) {
245  url_loader_factory_->UnregisterURL(url);
246}
247
248void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
249  url_loader_factory_->UnregisterAllURLs();
250}
251
252void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
253  url_loader_factory_->ServeAsynchronousRequests();
254}
255
256blink::WebString TestWebKitPlatformSupport::webKitRootDir() {
257  base::FilePath path;
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());
265}
266
267blink::WebLayerTreeView*
268TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
269  scoped_ptr<WebLayerTreeViewImplForTesting> view(
270      new WebLayerTreeViewImplForTesting());
271
272  view->Initialize();
273  return view.release();
274}
275
276blink::WebData TestWebKitPlatformSupport::readFromFile(
277    const blink::WebString& path) {
278  base::FilePath file_path = base::FilePath::FromUTF16Unsafe(path);
279
280  std::string buffer;
281  base::ReadFileToString(file_path, &buffer);
282
283  return blink::WebData(buffer.data(), buffer.size());
284}
285
286}  // namespace content
287