[Smart Lock] Smart Lock branding in save password infobar should available only for...
[chromium-blink-merge.git] / content / test / test_blink_web_unit_test_support.cc
blob8aa66187af66a49145d8a2c25588f18685f18ad4
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.h"
16 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
17 #include "content/test/mock_webclipboard_impl.h"
18 #include "content/test/web_gesture_curve_mock.h"
19 #include "content/test/web_layer_tree_view_impl_for_testing.h"
20 #include "content/test/weburl_loader_mock_factory.h"
21 #include "media/base/media.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/test/spawned_test_server/spawned_test_server.h"
24 #include "storage/browser/database/vfs_backend.h"
25 #include "third_party/WebKit/public/platform/WebData.h"
26 #include "third_party/WebKit/public/platform/WebFileSystem.h"
27 #include "third_party/WebKit/public/platform/WebStorageArea.h"
28 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
29 #include "third_party/WebKit/public/platform/WebString.h"
30 #include "third_party/WebKit/public/platform/WebURL.h"
31 #include "third_party/WebKit/public/web/WebDatabase.h"
32 #include "third_party/WebKit/public/web/WebKit.h"
33 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
34 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
35 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
36 #include "v8/include/v8.h"
38 #if defined(OS_MACOSX)
39 #include "base/mac/foundation_util.h"
40 #include "base/mac/scoped_nsautorelease_pool.h"
41 #endif
43 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
44 #include "gin/v8_initializer.h"
45 #endif
47 namespace {
49 class DummyTaskRunner : public base::SingleThreadTaskRunner {
50 public:
51 DummyTaskRunner() : thread_id_(base::PlatformThread::CurrentId()) {}
53 bool PostDelayedTask(const tracked_objects::Location& from_here,
54 const base::Closure& task,
55 base::TimeDelta delay) override {
56 NOTREACHED();
57 return false;
60 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
61 const base::Closure& task,
62 base::TimeDelta delay) override {
63 NOTREACHED();
64 return false;
67 bool RunsTasksOnCurrentThread() const override {
68 return thread_id_ == base::PlatformThread::CurrentId();
71 protected:
72 ~DummyTaskRunner() override {}
74 base::PlatformThreadId thread_id_;
76 DISALLOW_COPY_AND_ASSIGN(DummyTaskRunner);
79 } // namespace
81 namespace content {
83 TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
84 #if defined(OS_MACOSX)
85 base::mac::ScopedNSAutoreleasePool autorelease_pool;
86 #endif
88 url_loader_factory_.reset(new WebURLLoaderMockFactory());
89 mock_clipboard_.reset(new MockWebClipboardImpl());
91 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
92 gin::V8Initializer::LoadV8Snapshot();
93 #endif
95 scoped_refptr<base::SingleThreadTaskRunner> dummy_task_runner;
96 scoped_ptr<base::ThreadTaskRunnerHandle> dummy_task_runner_handle;
97 if (base::MessageLoopProxy::current()) {
98 renderer_scheduler_ = scheduler::RendererScheduler::Create();
99 web_thread_.reset(new scheduler::WebThreadImplForRendererScheduler(
100 renderer_scheduler_.get()));
101 } else {
102 // Dummy task runner is initialized here because the blink::initialize
103 // creates IsolateHolder which needs the current task runner handle. There
104 // should be no task posted to this task runner. The message loop is not
105 // created before this initialization because some tests need specific kinds
106 // of message loops, and their types are not known upfront. Some tests also
107 // create their own thread bundles or message loops, and doing the same in
108 // TestBlinkWebUnitTestSupport would introduce a conflict.
109 dummy_task_runner = make_scoped_refptr(new DummyTaskRunner());
110 dummy_task_runner_handle.reset(
111 new base::ThreadTaskRunnerHandle(dummy_task_runner));
114 blink::initialize(this);
115 blink::setLayoutTestMode(true);
116 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
117 blink::WebString::fromUTF8("test-shell-resource"));
118 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
119 blink::WebString::fromUTF8("test-shell-resource"));
120 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
121 blink::WebString::fromUTF8("test-shell-resource"));
122 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
123 blink::WebString::fromUTF8("test-shell-resource"));
124 blink::WebRuntimeFeatures::enableApplicationCache(true);
125 blink::WebRuntimeFeatures::enableDatabase(true);
126 blink::WebRuntimeFeatures::enableNotifications(true);
127 blink::WebRuntimeFeatures::enableTouch(true);
129 // Load libraries for media and enable the media player.
130 bool enable_media = false;
131 base::FilePath module_path;
132 if (PathService::Get(base::DIR_MODULE, &module_path)) {
133 #if defined(OS_MACOSX)
134 if (base::mac::AmIBundled())
135 module_path = module_path.DirName().DirName().DirName();
136 #endif
137 if (media::InitializeMediaLibrary(module_path))
138 enable_media = true;
140 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media);
141 LOG_IF(WARNING, !enable_media) << "Failed to initialize the media library.\n";
143 file_utilities_.set_sandbox_enabled(false);
145 if (!file_system_root_.CreateUniqueTempDir()) {
146 LOG(WARNING) << "Failed to create a temp dir for the filesystem."
147 "FileSystem feature will be disabled.";
148 DCHECK(file_system_root_.path().empty());
151 #if defined(OS_WIN)
152 // Ensure we pick up the default theme engine.
153 SetThemeEngine(NULL);
154 #endif
156 // Test shell always exposes the GC.
157 std::string flags("--expose-gc");
158 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
161 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
162 url_loader_factory_.reset();
163 mock_clipboard_.reset();
164 if (renderer_scheduler_)
165 renderer_scheduler_->Shutdown();
166 blink::shutdown();
169 blink::WebBlobRegistry* TestBlinkWebUnitTestSupport::blobRegistry() {
170 return &blob_registry_;
173 blink::WebClipboard* TestBlinkWebUnitTestSupport::clipboard() {
174 // Mock out clipboard calls so that tests don't mess
175 // with each other's copies/pastes when running in parallel.
176 return mock_clipboard_.get();
179 blink::WebFileUtilities* TestBlinkWebUnitTestSupport::fileUtilities() {
180 return &file_utilities_;
183 blink::WebIDBFactory* TestBlinkWebUnitTestSupport::idbFactory() {
184 NOTREACHED() <<
185 "IndexedDB cannot be tested with in-process harnesses.";
186 return NULL;
189 blink::WebMimeRegistry* TestBlinkWebUnitTestSupport::mimeRegistry() {
190 return &mime_registry_;
193 blink::WebURLLoader* TestBlinkWebUnitTestSupport::createURLLoader() {
194 return url_loader_factory_->CreateURLLoader(
195 BlinkPlatformImpl::createURLLoader());
198 blink::WebString TestBlinkWebUnitTestSupport::userAgent() {
199 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
202 blink::WebData TestBlinkWebUnitTestSupport::loadResource(const char* name) {
203 if (!strcmp(name, "deleteButton")) {
204 // Create a red 30x30 square.
205 const char red_square[] =
206 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
207 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
208 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
209 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
210 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
211 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
212 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
213 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
214 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
215 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
216 "\x82";
217 return blink::WebData(red_square, arraysize(red_square));
219 return BlinkPlatformImpl::loadResource(name);
222 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
223 blink::WebLocalizedString::Name name) {
224 // Returns placeholder strings to check if they are correctly localized.
225 switch (name) {
226 case blink::WebLocalizedString::OtherDateLabel:
227 return base::ASCIIToUTF16("<<OtherDateLabel>>");
228 case blink::WebLocalizedString::OtherMonthLabel:
229 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
230 case blink::WebLocalizedString::OtherTimeLabel:
231 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
232 case blink::WebLocalizedString::OtherWeekLabel:
233 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
234 case blink::WebLocalizedString::CalendarClear:
235 return base::ASCIIToUTF16("<<CalendarClear>>");
236 case blink::WebLocalizedString::CalendarToday:
237 return base::ASCIIToUTF16("<<CalendarToday>>");
238 case blink::WebLocalizedString::ThisMonthButtonLabel:
239 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
240 case blink::WebLocalizedString::ThisWeekButtonLabel:
241 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
242 case blink::WebLocalizedString::WeekFormatTemplate:
243 return base::ASCIIToUTF16("Week $2, $1");
244 default:
245 return blink::WebString();
249 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
250 blink::WebLocalizedString::Name name,
251 const blink::WebString& value) {
252 if (name == blink::WebLocalizedString::ValidationRangeUnderflow)
253 return base::ASCIIToUTF16("range underflow");
254 if (name == blink::WebLocalizedString::ValidationRangeOverflow)
255 return base::ASCIIToUTF16("range overflow");
256 if (name == blink::WebLocalizedString::SelectMenuListText)
257 return base::ASCIIToUTF16("$1 selected");
258 return BlinkPlatformImpl::queryLocalizedString(name, value);
261 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
262 blink::WebLocalizedString::Name name,
263 const blink::WebString& value1,
264 const blink::WebString& value2) {
265 if (name == blink::WebLocalizedString::ValidationTooLong)
266 return base::ASCIIToUTF16("too long");
267 if (name == blink::WebLocalizedString::ValidationStepMismatch)
268 return base::ASCIIToUTF16("step mismatch");
269 return BlinkPlatformImpl::queryLocalizedString(name, value1, value2);
272 blink::WebString TestBlinkWebUnitTestSupport::defaultLocale() {
273 return base::ASCIIToUTF16("en-US");
276 #if defined(OS_WIN) || defined(OS_MACOSX)
277 void TestBlinkWebUnitTestSupport::SetThemeEngine(
278 blink::WebThemeEngine* engine) {
279 active_theme_engine_ = engine ? engine : BlinkPlatformImpl::themeEngine();
282 blink::WebThemeEngine* TestBlinkWebUnitTestSupport::themeEngine() {
283 return active_theme_engine_;
285 #endif
287 blink::WebCompositorSupport* TestBlinkWebUnitTestSupport::compositorSupport() {
288 return &compositor_support_;
291 blink::WebGestureCurve* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
292 blink::WebGestureDevice device_source,
293 const blink::WebFloatPoint& velocity,
294 const blink::WebSize& cumulative_scroll) {
295 // Caller will retain and release.
296 return new WebGestureCurveMock(velocity, cumulative_scroll);
299 blink::WebUnitTestSupport* TestBlinkWebUnitTestSupport::unitTestSupport() {
300 return this;
303 void TestBlinkWebUnitTestSupport::registerMockedURL(
304 const blink::WebURL& url,
305 const blink::WebURLResponse& response,
306 const blink::WebString& file_path) {
307 url_loader_factory_->RegisterURL(url, response, file_path);
310 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
311 const blink::WebURL& url,
312 const blink::WebURLResponse& response,
313 const blink::WebURLError& error) {
314 url_loader_factory_->RegisterErrorURL(url, response, error);
317 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
318 const blink::WebURL& url) {
319 url_loader_factory_->UnregisterURL(url);
322 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
323 url_loader_factory_->UnregisterAllURLs();
326 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
327 url_loader_factory_->ServeAsynchronousRequests();
330 blink::WebString TestBlinkWebUnitTestSupport::webKitRootDir() {
331 base::FilePath path;
332 PathService::Get(base::DIR_SOURCE_ROOT, &path);
333 path = path.Append(FILE_PATH_LITERAL("third_party/WebKit"));
334 path = base::MakeAbsoluteFilePath(path);
335 CHECK(!path.empty());
336 std::string path_ascii = path.MaybeAsASCII();
337 CHECK(!path_ascii.empty());
338 return blink::WebString::fromUTF8(path_ascii.c_str());
341 blink::WebLayerTreeView*
342 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
343 scoped_ptr<WebLayerTreeViewImplForTesting> view(
344 new WebLayerTreeViewImplForTesting());
346 view->Initialize();
347 return view.release();
350 blink::WebData TestBlinkWebUnitTestSupport::readFromFile(
351 const blink::WebString& path) {
352 base::FilePath file_path = base::FilePath::FromUTF16Unsafe(path);
354 std::string buffer;
355 base::ReadFileToString(file_path, &buffer);
357 return blink::WebData(buffer.data(), buffer.size());
360 bool TestBlinkWebUnitTestSupport::getBlobItems(
361 const blink::WebString& uuid,
362 blink::WebVector<blink::WebBlobData::Item*>* items) {
363 return blob_registry_.GetBlobItems(uuid, items);
366 blink::WebThread* TestBlinkWebUnitTestSupport::currentThread() {
367 if (web_thread_ && web_thread_->isCurrentThread())
368 return web_thread_.get();
369 return BlinkPlatformImpl::currentThread();
372 void TestBlinkWebUnitTestSupport::enterRunLoop() {
373 DCHECK(base::MessageLoop::current());
374 DCHECK(!base::MessageLoop::current()->is_running());
375 base::MessageLoop::current()->Run();
378 void TestBlinkWebUnitTestSupport::exitRunLoop() {
379 base::MessageLoop::current()->Quit();
382 } // namespace content