Update broken references to image assets
[chromium-blink-merge.git] / content / test / test_blink_web_unit_test_support.cc
blob525b25f20322083d95ca0f538f6b851e4822cb2b
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/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/path_service.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/platform_thread.h"
15 #include "components/scheduler/renderer/renderer_scheduler_impl.h"
16 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
17 #include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h"
18 #include "content/test/mock_webclipboard_impl.h"
19 #include "content/test/web_gesture_curve_mock.h"
20 #include "content/test/web_layer_tree_view_impl_for_testing.h"
21 #include "content/test/weburl_loader_mock_factory.h"
22 #include "media/base/media.h"
23 #include "net/cookies/cookie_monster.h"
24 #include "net/test/spawned_test_server/spawned_test_server.h"
25 #include "storage/browser/database/vfs_backend.h"
26 #include "third_party/WebKit/public/platform/WebData.h"
27 #include "third_party/WebKit/public/platform/WebFileSystem.h"
28 #include "third_party/WebKit/public/platform/WebPluginListBuilder.h"
29 #include "third_party/WebKit/public/platform/WebStorageArea.h"
30 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
31 #include "third_party/WebKit/public/platform/WebString.h"
32 #include "third_party/WebKit/public/platform/WebURL.h"
33 #include "third_party/WebKit/public/web/WebDatabase.h"
34 #include "third_party/WebKit/public/web/WebKit.h"
35 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
36 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
37 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
38 #include "v8/include/v8.h"
40 #if defined(OS_MACOSX)
41 #include "base/mac/foundation_util.h"
42 #include "base/mac/scoped_nsautorelease_pool.h"
43 #endif
45 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
46 #include "gin/v8_initializer.h"
47 #endif
49 namespace {
51 class DummyTaskRunner : public base::SingleThreadTaskRunner {
52 public:
53 DummyTaskRunner() : thread_id_(base::PlatformThread::CurrentId()) {}
55 bool PostDelayedTask(const tracked_objects::Location& from_here,
56 const base::Closure& task,
57 base::TimeDelta delay) override {
58 // Drop the delayed task.
59 return false;
62 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
63 const base::Closure& task,
64 base::TimeDelta delay) override {
65 // Drop the delayed task.
66 return false;
69 bool RunsTasksOnCurrentThread() const override {
70 return thread_id_ == base::PlatformThread::CurrentId();
73 protected:
74 ~DummyTaskRunner() override {}
76 base::PlatformThreadId thread_id_;
78 DISALLOW_COPY_AND_ASSIGN(DummyTaskRunner);
81 } // namespace
83 namespace content {
85 TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
86 #if defined(OS_MACOSX)
87 base::mac::ScopedNSAutoreleasePool autorelease_pool;
88 #endif
90 url_loader_factory_.reset(new WebURLLoaderMockFactory());
91 mock_clipboard_.reset(new MockWebClipboardImpl());
93 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
94 gin::V8Initializer::LoadV8Snapshot();
95 gin::V8Initializer::LoadV8Natives();
96 #endif
98 scoped_refptr<base::SingleThreadTaskRunner> dummy_task_runner;
99 scoped_ptr<base::ThreadTaskRunnerHandle> dummy_task_runner_handle;
100 if (!base::ThreadTaskRunnerHandle::IsSet()) {
101 // Dummy task runner is initialized here because the blink::initialize
102 // creates IsolateHolder which needs the current task runner handle. There
103 // should be no task posted to this task runner. The message loop is not
104 // created before this initialization because some tests need specific kinds
105 // of message loops, and their types are not known upfront. Some tests also
106 // create their own thread bundles or message loops, and doing the same in
107 // TestBlinkWebUnitTestSupport would introduce a conflict.
108 dummy_task_runner = make_scoped_refptr(new DummyTaskRunner());
109 dummy_task_runner_handle.reset(
110 new base::ThreadTaskRunnerHandle(dummy_task_runner));
112 renderer_scheduler_ = make_scoped_ptr(new scheduler::RendererSchedulerImpl(
113 scheduler::LazySchedulerMessageLoopDelegateForTests::Create()));
114 web_thread_.reset(new scheduler::WebThreadImplForRendererScheduler(
115 renderer_scheduler_.get()));
117 blink::initialize(this);
118 blink::setLayoutTestMode(true);
119 blink::WebRuntimeFeatures::enableApplicationCache(true);
120 blink::WebRuntimeFeatures::enableDatabase(true);
121 blink::WebRuntimeFeatures::enableNotifications(true);
122 blink::WebRuntimeFeatures::enableTouch(true);
124 // Initialize libraries for media and enable the media player.
125 media::InitializeMediaLibrary();
126 blink::WebRuntimeFeatures::enableMediaPlayer(true);
128 file_utilities_.set_sandbox_enabled(false);
130 if (!file_system_root_.CreateUniqueTempDir()) {
131 LOG(WARNING) << "Failed to create a temp dir for the filesystem."
132 "FileSystem feature will be disabled.";
133 DCHECK(file_system_root_.path().empty());
136 #if defined(OS_WIN)
137 // Ensure we pick up the default theme engine.
138 SetThemeEngine(NULL);
139 #endif
141 // Test shell always exposes the GC.
142 std::string flags("--expose-gc");
143 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
146 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
147 url_loader_factory_.reset();
148 mock_clipboard_.reset();
149 if (renderer_scheduler_)
150 renderer_scheduler_->Shutdown();
151 blink::shutdown();
154 blink::WebBlobRegistry* TestBlinkWebUnitTestSupport::blobRegistry() {
155 return &blob_registry_;
158 blink::WebClipboard* TestBlinkWebUnitTestSupport::clipboard() {
159 // Mock out clipboard calls so that tests don't mess
160 // with each other's copies/pastes when running in parallel.
161 return mock_clipboard_.get();
164 blink::WebFileUtilities* TestBlinkWebUnitTestSupport::fileUtilities() {
165 return &file_utilities_;
168 blink::WebIDBFactory* TestBlinkWebUnitTestSupport::idbFactory() {
169 NOTREACHED() <<
170 "IndexedDB cannot be tested with in-process harnesses.";
171 return NULL;
174 blink::WebMimeRegistry* TestBlinkWebUnitTestSupport::mimeRegistry() {
175 return &mime_registry_;
178 blink::WebURLLoader* TestBlinkWebUnitTestSupport::createURLLoader() {
179 return url_loader_factory_->CreateURLLoader(
180 BlinkPlatformImpl::createURLLoader());
183 blink::WebString TestBlinkWebUnitTestSupport::userAgent() {
184 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
187 blink::WebData TestBlinkWebUnitTestSupport::loadResource(const char* name) {
188 if (!strcmp(name, "deleteButton")) {
189 // Create a red 30x30 square.
190 const char red_square[] =
191 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
192 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
193 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
194 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
195 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
196 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
197 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
198 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
199 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
200 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
201 "\x82";
202 return blink::WebData(red_square, arraysize(red_square));
204 return BlinkPlatformImpl::loadResource(name);
207 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
208 blink::WebLocalizedString::Name name) {
209 // Returns placeholder strings to check if they are correctly localized.
210 switch (name) {
211 case blink::WebLocalizedString::OtherDateLabel:
212 return base::ASCIIToUTF16("<<OtherDateLabel>>");
213 case blink::WebLocalizedString::OtherMonthLabel:
214 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
215 case blink::WebLocalizedString::OtherTimeLabel:
216 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
217 case blink::WebLocalizedString::OtherWeekLabel:
218 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
219 case blink::WebLocalizedString::CalendarClear:
220 return base::ASCIIToUTF16("<<CalendarClear>>");
221 case blink::WebLocalizedString::CalendarToday:
222 return base::ASCIIToUTF16("<<CalendarToday>>");
223 case blink::WebLocalizedString::ThisMonthButtonLabel:
224 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
225 case blink::WebLocalizedString::ThisWeekButtonLabel:
226 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
227 case blink::WebLocalizedString::WeekFormatTemplate:
228 return base::ASCIIToUTF16("Week $2, $1");
229 default:
230 return blink::WebString();
234 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
235 blink::WebLocalizedString::Name name,
236 const blink::WebString& value) {
237 if (name == blink::WebLocalizedString::ValidationRangeUnderflow)
238 return base::ASCIIToUTF16("range underflow");
239 if (name == blink::WebLocalizedString::ValidationRangeOverflow)
240 return base::ASCIIToUTF16("range overflow");
241 if (name == blink::WebLocalizedString::SelectMenuListText)
242 return base::ASCIIToUTF16("$1 selected");
243 return BlinkPlatformImpl::queryLocalizedString(name, value);
246 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
247 blink::WebLocalizedString::Name name,
248 const blink::WebString& value1,
249 const blink::WebString& value2) {
250 if (name == blink::WebLocalizedString::ValidationTooLong)
251 return base::ASCIIToUTF16("too long");
252 if (name == blink::WebLocalizedString::ValidationStepMismatch)
253 return base::ASCIIToUTF16("step mismatch");
254 return BlinkPlatformImpl::queryLocalizedString(name, value1, value2);
257 blink::WebString TestBlinkWebUnitTestSupport::defaultLocale() {
258 return base::ASCIIToUTF16("en-US");
261 #if defined(OS_WIN) || defined(OS_MACOSX)
262 void TestBlinkWebUnitTestSupport::SetThemeEngine(
263 blink::WebThemeEngine* engine) {
264 active_theme_engine_ = engine ? engine : BlinkPlatformImpl::themeEngine();
267 blink::WebThemeEngine* TestBlinkWebUnitTestSupport::themeEngine() {
268 return active_theme_engine_;
270 #endif
272 blink::WebCompositorSupport* TestBlinkWebUnitTestSupport::compositorSupport() {
273 return &compositor_support_;
276 blink::WebGestureCurve* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
277 blink::WebGestureDevice device_source,
278 const blink::WebFloatPoint& velocity,
279 const blink::WebSize& cumulative_scroll) {
280 // Caller will retain and release.
281 return new WebGestureCurveMock(velocity, cumulative_scroll);
284 blink::WebUnitTestSupport* TestBlinkWebUnitTestSupport::unitTestSupport() {
285 return this;
288 void TestBlinkWebUnitTestSupport::registerMockedURL(
289 const blink::WebURL& url,
290 const blink::WebURLResponse& response,
291 const blink::WebString& file_path) {
292 url_loader_factory_->RegisterURL(url, response, file_path);
295 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
296 const blink::WebURL& url,
297 const blink::WebURLResponse& response,
298 const blink::WebURLError& error) {
299 url_loader_factory_->RegisterErrorURL(url, response, error);
302 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
303 const blink::WebURL& url) {
304 url_loader_factory_->UnregisterURL(url);
307 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
308 url_loader_factory_->UnregisterAllURLs();
311 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
312 url_loader_factory_->ServeAsynchronousRequests();
315 void TestBlinkWebUnitTestSupport::setLoaderDelegate(
316 blink::WebURLLoaderTestDelegate* delegate) {
317 url_loader_factory_->set_delegate(delegate);
320 blink::WebString TestBlinkWebUnitTestSupport::webKitRootDir() {
321 base::FilePath path;
322 PathService::Get(base::DIR_SOURCE_ROOT, &path);
323 path = path.Append(FILE_PATH_LITERAL("third_party/WebKit"));
324 path = base::MakeAbsoluteFilePath(path);
325 CHECK(!path.empty());
326 std::string path_ascii = path.MaybeAsASCII();
327 CHECK(!path_ascii.empty());
328 return blink::WebString::fromUTF8(path_ascii.c_str());
331 blink::WebLayerTreeView*
332 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
333 scoped_ptr<WebLayerTreeViewImplForTesting> view(
334 new WebLayerTreeViewImplForTesting());
336 view->Initialize();
337 return view.release();
340 blink::WebData TestBlinkWebUnitTestSupport::readFromFile(
341 const blink::WebString& path) {
342 base::FilePath file_path = base::FilePath::FromUTF16Unsafe(path);
344 std::string buffer;
345 base::ReadFileToString(file_path, &buffer);
347 return blink::WebData(buffer.data(), buffer.size());
350 bool TestBlinkWebUnitTestSupport::getBlobItems(
351 const blink::WebString& uuid,
352 blink::WebVector<blink::WebBlobData::Item*>* items) {
353 return blob_registry_.GetBlobItems(uuid, items);
356 blink::WebThread* TestBlinkWebUnitTestSupport::currentThread() {
357 if (web_thread_ && web_thread_->isCurrentThread())
358 return web_thread_.get();
359 return BlinkPlatformImpl::currentThread();
362 void TestBlinkWebUnitTestSupport::enterRunLoop() {
363 DCHECK(base::MessageLoop::current());
364 DCHECK(!base::MessageLoop::current()->is_running());
365 base::MessageLoop::current()->Run();
368 void TestBlinkWebUnitTestSupport::exitRunLoop() {
369 base::MessageLoop::current()->Quit();
372 void TestBlinkWebUnitTestSupport::getPluginList(
373 bool refresh, blink::WebPluginListBuilder* builder) {
374 builder->addPlugin("pdf", "pdf", "pdf-files");
375 builder->addMediaTypeToLastPlugin("application/pdf", "pdf");
378 } // namespace content