Extension window.create API accepts state.
[chromium-blink-merge.git] / content / test / test_blink_web_unit_test_support.cc
blob26c784d49f1b8d3d38119b057a256d90286ad49a
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/strings/utf_string_conversions.h"
12 #include "content/child/scheduler/web_scheduler_impl.h"
13 #include "content/renderer/scheduler/renderer_scheduler.h"
14 #include "content/renderer/scheduler/webthread_impl_for_renderer_scheduler.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/v8_initializer.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 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
56 gin::V8Initializer::LoadV8Snapshot();
57 #endif
59 if (base::MessageLoopProxy::current()) {
60 renderer_scheduler_ = RendererScheduler::Create();
61 web_scheduler_.reset(new WebSchedulerImpl(
62 renderer_scheduler_.get(), renderer_scheduler_->IdleTaskRunner(),
63 renderer_scheduler_->LoadingTaskRunner(),
64 renderer_scheduler_->TimerTaskRunner()));
65 web_thread_.reset(
66 new WebThreadImplForRendererScheduler(renderer_scheduler_.get()));
69 blink::initialize(this);
70 blink::setLayoutTestMode(true);
71 blink::WebSecurityPolicy::registerURLSchemeAsLocal(
72 blink::WebString::fromUTF8("test-shell-resource"));
73 blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
74 blink::WebString::fromUTF8("test-shell-resource"));
75 blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
76 blink::WebString::fromUTF8("test-shell-resource"));
77 blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
78 blink::WebString::fromUTF8("test-shell-resource"));
79 blink::WebRuntimeFeatures::enableApplicationCache(true);
80 blink::WebRuntimeFeatures::enableDatabase(true);
81 blink::WebRuntimeFeatures::enableNotifications(true);
82 blink::WebRuntimeFeatures::enableTouch(true);
84 // Load libraries for media and enable the media player.
85 bool enable_media = false;
86 base::FilePath module_path;
87 if (PathService::Get(base::DIR_MODULE, &module_path)) {
88 #if defined(OS_MACOSX)
89 if (base::mac::AmIBundled())
90 module_path = module_path.DirName().DirName().DirName();
91 #endif
92 if (media::InitializeMediaLibrary(module_path))
93 enable_media = true;
95 blink::WebRuntimeFeatures::enableMediaPlayer(enable_media);
96 LOG_IF(WARNING, !enable_media) << "Failed to initialize the media library.\n";
98 file_utilities_.set_sandbox_enabled(false);
100 if (!file_system_root_.CreateUniqueTempDir()) {
101 LOG(WARNING) << "Failed to create a temp dir for the filesystem."
102 "FileSystem feature will be disabled.";
103 DCHECK(file_system_root_.path().empty());
106 #if defined(OS_WIN)
107 // Ensure we pick up the default theme engine.
108 SetThemeEngine(NULL);
109 #endif
111 // Test shell always exposes the GC.
112 std::string flags("--expose-gc");
113 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
116 TestBlinkWebUnitTestSupport::~TestBlinkWebUnitTestSupport() {
117 url_loader_factory_.reset();
118 mock_clipboard_.reset();
119 if (renderer_scheduler_)
120 renderer_scheduler_->Shutdown();
121 blink::shutdown();
124 blink::WebBlobRegistry* TestBlinkWebUnitTestSupport::blobRegistry() {
125 return &blob_registry_;
128 blink::WebClipboard* TestBlinkWebUnitTestSupport::clipboard() {
129 // Mock out clipboard calls so that tests don't mess
130 // with each other's copies/pastes when running in parallel.
131 return mock_clipboard_.get();
134 blink::WebFileUtilities* TestBlinkWebUnitTestSupport::fileUtilities() {
135 return &file_utilities_;
138 blink::WebIDBFactory* TestBlinkWebUnitTestSupport::idbFactory() {
139 NOTREACHED() <<
140 "IndexedDB cannot be tested with in-process harnesses.";
141 return NULL;
144 blink::WebMimeRegistry* TestBlinkWebUnitTestSupport::mimeRegistry() {
145 return &mime_registry_;
148 blink::WebURLLoader* TestBlinkWebUnitTestSupport::createURLLoader() {
149 return url_loader_factory_->CreateURLLoader(
150 BlinkPlatformImpl::createURLLoader());
153 blink::WebString TestBlinkWebUnitTestSupport::userAgent() {
154 return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
157 blink::WebData TestBlinkWebUnitTestSupport::loadResource(const char* name) {
158 if (!strcmp(name, "deleteButton")) {
159 // Create a red 30x30 square.
160 const char red_square[] =
161 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
162 "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
163 "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
164 "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
165 "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
166 "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
167 "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
168 "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
169 "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
170 "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
171 "\x82";
172 return blink::WebData(red_square, arraysize(red_square));
174 return BlinkPlatformImpl::loadResource(name);
177 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
178 blink::WebLocalizedString::Name name) {
179 // Returns placeholder strings to check if they are correctly localized.
180 switch (name) {
181 case blink::WebLocalizedString::OtherDateLabel:
182 return base::ASCIIToUTF16("<<OtherDateLabel>>");
183 case blink::WebLocalizedString::OtherMonthLabel:
184 return base::ASCIIToUTF16("<<OtherMonthLabel>>");
185 case blink::WebLocalizedString::OtherTimeLabel:
186 return base::ASCIIToUTF16("<<OtherTimeLabel>>");
187 case blink::WebLocalizedString::OtherWeekLabel:
188 return base::ASCIIToUTF16("<<OtherWeekLabel>>");
189 case blink::WebLocalizedString::CalendarClear:
190 return base::ASCIIToUTF16("<<CalendarClear>>");
191 case blink::WebLocalizedString::CalendarToday:
192 return base::ASCIIToUTF16("<<CalendarToday>>");
193 case blink::WebLocalizedString::ThisMonthButtonLabel:
194 return base::ASCIIToUTF16("<<ThisMonthLabel>>");
195 case blink::WebLocalizedString::ThisWeekButtonLabel:
196 return base::ASCIIToUTF16("<<ThisWeekLabel>>");
197 case blink::WebLocalizedString::WeekFormatTemplate:
198 return base::ASCIIToUTF16("Week $2, $1");
199 default:
200 return blink::WebString();
204 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
205 blink::WebLocalizedString::Name name,
206 const blink::WebString& value) {
207 if (name == blink::WebLocalizedString::ValidationRangeUnderflow)
208 return base::ASCIIToUTF16("range underflow");
209 if (name == blink::WebLocalizedString::ValidationRangeOverflow)
210 return base::ASCIIToUTF16("range overflow");
211 if (name == blink::WebLocalizedString::SelectMenuListText)
212 return base::ASCIIToUTF16("$1 selected");
213 return BlinkPlatformImpl::queryLocalizedString(name, value);
216 blink::WebString TestBlinkWebUnitTestSupport::queryLocalizedString(
217 blink::WebLocalizedString::Name name,
218 const blink::WebString& value1,
219 const blink::WebString& value2) {
220 if (name == blink::WebLocalizedString::ValidationTooLong)
221 return base::ASCIIToUTF16("too long");
222 if (name == blink::WebLocalizedString::ValidationStepMismatch)
223 return base::ASCIIToUTF16("step mismatch");
224 return BlinkPlatformImpl::queryLocalizedString(name, value1, value2);
227 blink::WebString TestBlinkWebUnitTestSupport::defaultLocale() {
228 return base::ASCIIToUTF16("en-US");
231 #if defined(OS_WIN) || defined(OS_MACOSX)
232 void TestBlinkWebUnitTestSupport::SetThemeEngine(
233 blink::WebThemeEngine* engine) {
234 active_theme_engine_ = engine ? engine : BlinkPlatformImpl::themeEngine();
237 blink::WebThemeEngine* TestBlinkWebUnitTestSupport::themeEngine() {
238 return active_theme_engine_;
240 #endif
242 blink::WebCompositorSupport* TestBlinkWebUnitTestSupport::compositorSupport() {
243 return &compositor_support_;
246 blink::WebGestureCurve* TestBlinkWebUnitTestSupport::createFlingAnimationCurve(
247 blink::WebGestureDevice device_source,
248 const blink::WebFloatPoint& velocity,
249 const blink::WebSize& cumulative_scroll) {
250 // Caller will retain and release.
251 return new WebGestureCurveMock(velocity, cumulative_scroll);
254 blink::WebUnitTestSupport* TestBlinkWebUnitTestSupport::unitTestSupport() {
255 return this;
258 void TestBlinkWebUnitTestSupport::registerMockedURL(
259 const blink::WebURL& url,
260 const blink::WebURLResponse& response,
261 const blink::WebString& file_path) {
262 url_loader_factory_->RegisterURL(url, response, file_path);
265 void TestBlinkWebUnitTestSupport::registerMockedErrorURL(
266 const blink::WebURL& url,
267 const blink::WebURLResponse& response,
268 const blink::WebURLError& error) {
269 url_loader_factory_->RegisterErrorURL(url, response, error);
272 void TestBlinkWebUnitTestSupport::unregisterMockedURL(
273 const blink::WebURL& url) {
274 url_loader_factory_->UnregisterURL(url);
277 void TestBlinkWebUnitTestSupport::unregisterAllMockedURLs() {
278 url_loader_factory_->UnregisterAllURLs();
281 void TestBlinkWebUnitTestSupport::serveAsynchronousMockedRequests() {
282 url_loader_factory_->ServeAsynchronousRequests();
285 blink::WebString TestBlinkWebUnitTestSupport::webKitRootDir() {
286 base::FilePath path;
287 PathService::Get(base::DIR_SOURCE_ROOT, &path);
288 path = path.Append(FILE_PATH_LITERAL("third_party/WebKit"));
289 path = base::MakeAbsoluteFilePath(path);
290 CHECK(!path.empty());
291 std::string path_ascii = path.MaybeAsASCII();
292 CHECK(!path_ascii.empty());
293 return blink::WebString::fromUTF8(path_ascii.c_str());
296 blink::WebLayerTreeView*
297 TestBlinkWebUnitTestSupport::createLayerTreeViewForTesting() {
298 scoped_ptr<WebLayerTreeViewImplForTesting> view(
299 new WebLayerTreeViewImplForTesting());
301 view->Initialize();
302 return view.release();
305 blink::WebData TestBlinkWebUnitTestSupport::readFromFile(
306 const blink::WebString& path) {
307 base::FilePath file_path = base::FilePath::FromUTF16Unsafe(path);
309 std::string buffer;
310 base::ReadFileToString(file_path, &buffer);
312 return blink::WebData(buffer.data(), buffer.size());
315 bool TestBlinkWebUnitTestSupport::getBlobItems(
316 const blink::WebString& uuid,
317 blink::WebVector<blink::WebBlobData::Item*>* items) {
318 return blob_registry_.GetBlobItems(uuid, items);
321 blink::WebScheduler* TestBlinkWebUnitTestSupport::scheduler() {
322 return web_scheduler_.get();
325 blink::WebThread* TestBlinkWebUnitTestSupport::currentThread() {
326 if (web_thread_ && web_thread_->isCurrentThread())
327 return web_thread_.get();
328 return BlinkPlatformImpl::currentThread();
331 } // namespace content