base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / content / test / test_blink_web_unit_test_support.cc
blobb358a0e5c1d6a44f557aa10c690866beb6925f61
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_blink_web_unit_test_support.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.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 "storage/browser/database/vfs_backend.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"
36 #if defined(OS_MACOSX)
37 #include "base/mac/foundation_util.h"
38 #include "base/mac/scoped_nsautorelease_pool.h"
39 #endif
41 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
42 #include "gin/public/isolate_holder.h"
43 #endif
45 namespace content {
47 TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
48 #if defined(OS_MACOSX)
49 base::mac::ScopedNSAutoreleasePool autorelease_pool;
50 #endif
52 url_loader_factory_.reset(new WebURLLoaderMockFactory());
53 mock_clipboard_.reset(new MockWebClipboardImpl());
55 // Create an anonymous stats table since we don't need to share between
56 // processes.
57 stats_table_.reset(
58 new base::StatsTable(base::StatsTable::TableIdentifier(), 20, 200));
59 base::StatsTable::set_current(stats_table_.get());
61 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
62 gin::IsolateHolder::LoadV8Snapshot();
63 #endif
65 blink::initialize(this);
66 blink::mainThreadIsolate()->SetCounterFunction(
67 base::StatsTable::FindLocation);
68 blink::setLayoutTestMode(true);
69 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
70 blink::WebString::fromUTF8("test-shell-resource"));
71 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
72 blink::WebString::fromUTF8("test-shell-resource"));
73 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
74 blink::WebString::fromUTF8("test-shell-resource"));
75 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
76 blink::WebString::fromUTF8("test-shell-resource"));
77 blink::WebRuntimeFeatures::enableApplicationCache(true);
78 blink::WebRuntimeFeatures::enableDatabase(true);
79 blink::WebRuntimeFeatures::enableNotifications(true);
80 blink::WebRuntimeFeatures::enableTouch(true);
82 // Load libraries for media and enable the media player.
83 bool enable_media = false;
84 base::FilePath module_path;
85 if (PathService::Get(base::DIR_MODULE, &module_path)) {
86 #if defined(OS_MACOSX)
87 if (base::mac::AmIBundled())
88 module_path = module_path.DirName().DirName().DirName();
89 #endif
90 if (media::InitializeMediaLibrary(module_path))
91 enable_media = true;
93 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media);
94 LOG_IF(WARNING, !enable_media) << "Failed to initialize the media library.\n";
96 file_utilities_.set_sandbox_enabled(false);
98 if (!file_system_root_.CreateUniqueTempDir()) {
99 LOG(WARNING) << "Failed to create a temp dir for the filesystem."
100 "FileSystem feature will be disabled.";
101 DCHECK(file_system_root_.path().empty());
104 #if defined(OS_WIN)
105 // Ensure we pick up the default theme engine.
106 SetThemeEngine(NULL);
107 #endif
109 base::CommandLine::ForCurrentProcess()->AppendSwitch(
110 switches::kEnableFileCookies);
112 // Test shell always exposes the GC.
113 std::string flags("--expose-gc");
114 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
117 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
118 url_loader_factory_.reset();
119 mock_clipboard_.reset();
120 blink::shutdown();
121 base::StatsTable::set_current(NULL);
122 stats_table_.reset();
125 blink::WebBlobRegistry* TestBlinkWebUnitTestSupport::blobRegistry() {
126 return &blob_registry_;
129 blink::WebClipboard* TestBlinkWebUnitTestSupport::clipboard() {
130 // Mock out clipboard calls so that tests don't mess
131 // with each other's copies/pastes when running in parallel.
132 return mock_clipboard_.get();
135 blink::WebFileUtilities* TestBlinkWebUnitTestSupport::fileUtilities() {
136 return &file_utilities_;
139 blink::WebIDBFactory* TestBlinkWebUnitTestSupport::idbFactory() {
140 NOTREACHED() <<
141 "IndexedDB cannot be tested with in-process harnesses.";
142 return NULL;
145 blink::WebMimeRegistry* TestBlinkWebUnitTestSupport::mimeRegistry() {
146 return &mime_registry_;
149 blink::WebURLLoader* TestBlinkWebUnitTestSupport::createURLLoader() {
150 return url_loader_factory_->CreateURLLoader(
151 BlinkPlatformImpl::createURLLoader());
154 blink::WebString TestBlinkWebUnitTestSupport::userAgent() {
155 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
158 blink::WebData TestBlinkWebUnitTestSupport::loadResource(const char* name) {
159 if (!strcmp(name, "deleteButton")) {
160 // Create a red 30x30 square.
161 const char red_square[] =
162 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
163 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
164 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
165 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
166 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
167 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
168 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
169 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
170 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
171 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
172 "\x82";
173 return blink::WebData(red_square, arraysize(red_square));
175 return BlinkPlatformImpl::loadResource(name);
178 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
179 blink::WebLocalizedString::Name name) {
180 // Returns placeholder strings to check if they are correctly localized.
181 switch (name) {
182 case blink::WebLocalizedString::OtherDateLabel:
183 return base::ASCIIToUTF16("<<OtherDateLabel>>");
184 case blink::WebLocalizedString::OtherMonthLabel:
185 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
186 case blink::WebLocalizedString::OtherTimeLabel:
187 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
188 case blink::WebLocalizedString::OtherWeekLabel:
189 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
190 case blink::WebLocalizedString::CalendarClear:
191 return base::ASCIIToUTF16("<<CalendarClear>>");
192 case blink::WebLocalizedString::CalendarToday:
193 return base::ASCIIToUTF16("<<CalendarToday>>");
194 case blink::WebLocalizedString::ThisMonthButtonLabel:
195 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
196 case blink::WebLocalizedString::ThisWeekButtonLabel:
197 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
198 case blink::WebLocalizedString::WeekFormatTemplate:
199 return base::ASCIIToUTF16("Week $2, $1");
200 default:
201 return blink::WebString();
205 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
206 blink::WebLocalizedString::Name name,
207 const blink::WebString& value) {
208 if (name == blink::WebLocalizedString::ValidationRangeUnderflow)
209 return base::ASCIIToUTF16("range underflow");
210 if (name == blink::WebLocalizedString::ValidationRangeOverflow)
211 return base::ASCIIToUTF16("range overflow");
212 if (name == blink::WebLocalizedString::SelectMenuListText)
213 return base::ASCIIToUTF16("$1 selected");
214 return BlinkPlatformImpl::queryLocalizedString(name, value);
217 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
218 blink::WebLocalizedString::Name name,
219 const blink::WebString& value1,
220 const blink::WebString& value2) {
221 if (name == blink::WebLocalizedString::ValidationTooLong)
222 return base::ASCIIToUTF16("too long");
223 if (name == blink::WebLocalizedString::ValidationStepMismatch)
224 return base::ASCIIToUTF16("step mismatch");
225 return BlinkPlatformImpl::queryLocalizedString(name, value1, value2);
228 blink::WebString TestBlinkWebUnitTestSupport::defaultLocale() {
229 return base::ASCIIToUTF16("en-US");
232 #if defined(OS_WIN) || defined(OS_MACOSX)
233 void TestBlinkWebUnitTestSupport::SetThemeEngine(
234 blink::WebThemeEngine* engine) {
235 active_theme_engine_ = engine ? engine : BlinkPlatformImpl::themeEngine();
238 blink::WebThemeEngine* TestBlinkWebUnitTestSupport::themeEngine() {
239 return active_theme_engine_;
241 #endif
243 blink::WebCompositorSupport* TestBlinkWebUnitTestSupport::compositorSupport() {
244 return &compositor_support_;
247 blink::WebGestureCurve* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
248 blink::WebGestureDevice device_source,
249 const blink::WebFloatPoint& velocity,
250 const blink::WebSize& cumulative_scroll) {
251 // Caller will retain and release.
252 return new WebGestureCurveMock(velocity, cumulative_scroll);
255 blink::WebUnitTestSupport* TestBlinkWebUnitTestSupport::unitTestSupport() {
256 return this;
259 void TestBlinkWebUnitTestSupport::registerMockedURL(
260 const blink::WebURL& url,
261 const blink::WebURLResponse& response,
262 const blink::WebString& file_path) {
263 url_loader_factory_->RegisterURL(url, response, file_path);
266 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
267 const blink::WebURL& url,
268 const blink::WebURLResponse& response,
269 const blink::WebURLError& error) {
270 url_loader_factory_->RegisterErrorURL(url, response, error);
273 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
274 const blink::WebURL& url) {
275 url_loader_factory_->UnregisterURL(url);
278 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
279 url_loader_factory_->UnregisterAllURLs();
282 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
283 url_loader_factory_->ServeAsynchronousRequests();
286 blink::WebString TestBlinkWebUnitTestSupport::webKitRootDir() {
287 base::FilePath path;
288 PathService::Get(base::DIR_SOURCE_ROOT, &path);
289 path = path.Append(FILE_PATH_LITERAL("third_party/WebKit"));
290 path = base::MakeAbsoluteFilePath(path);
291 CHECK(!path.empty());
292 std::string path_ascii = path.MaybeAsASCII();
293 CHECK(!path_ascii.empty());
294 return blink::WebString::fromUTF8(path_ascii.c_str());
297 blink::WebLayerTreeView*
298 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
299 scoped_ptr<WebLayerTreeViewImplForTesting> view(
300 new WebLayerTreeViewImplForTesting());
302 view->Initialize();
303 return view.release();
306 blink::WebData TestBlinkWebUnitTestSupport::readFromFile(
307 const blink::WebString& path) {
308 base::FilePath file_path = base::FilePath::FromUTF16Unsafe(path);
310 std::string buffer;
311 base::ReadFileToString(file_path, &buffer);
313 return blink::WebData(buffer.data(), buffer.size());
316 bool TestBlinkWebUnitTestSupport::getBlobItems(
317 const blink::WebString& uuid,
318 blink::WebVector<blink::WebBlobData::Item*>* items) {
319 return blob_registry_.GetBlobItems(uuid, items);
322 } // namespace content