Revert "Revert of Revert of Revert of ChromeOS NetworkScreenHandler should not call...
[chromium-blink-merge.git] / extensions / renderer / api_test_base.cc
blob46a6ac27bac5fb3b7734181be9139a16915537fb
1 // Copyright 2014 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 "extensions/renderer/api_test_base.h"
7 #include <vector>
9 #include "base/run_loop.h"
10 #include "extensions/common/extension_urls.h"
11 #include "extensions/renderer/dispatcher.h"
12 #include "extensions/renderer/process_info_native_handler.h"
13 #include "gin/converter.h"
14 #include "gin/dictionary.h"
15 #include "mojo/edk/js/core.h"
16 #include "mojo/edk/js/handle.h"
17 #include "mojo/edk/js/support.h"
18 #include "mojo/public/cpp/bindings/interface_request.h"
19 #include "mojo/public/cpp/system/core.h"
21 namespace extensions {
22 namespace {
24 // Natives for the implementation of the unit test version of chrome.test. Calls
25 // the provided |quit_closure| when either notifyPass or notifyFail is called.
26 class TestNatives : public gin::Wrappable<TestNatives> {
27 public:
28 static gin::Handle<TestNatives> Create(v8::Isolate* isolate,
29 const base::Closure& quit_closure) {
30 return gin::CreateHandle(isolate, new TestNatives(quit_closure));
33 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
34 v8::Isolate* isolate) override {
35 return Wrappable<TestNatives>::GetObjectTemplateBuilder(isolate)
36 .SetMethod("Log", &TestNatives::Log)
37 .SetMethod("NotifyPass", &TestNatives::NotifyPass)
38 .SetMethod("NotifyFail", &TestNatives::NotifyFail);
41 void Log(const std::string& value) { logs_ += value + "\n"; }
42 void NotifyPass() { FinishTesting(); }
44 void NotifyFail(const std::string& message) {
45 FinishTesting();
46 FAIL() << logs_ << message;
49 void FinishTesting() {
50 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
53 static gin::WrapperInfo kWrapperInfo;
55 private:
56 explicit TestNatives(const base::Closure& quit_closure)
57 : quit_closure_(quit_closure) {}
59 const base::Closure quit_closure_;
60 std::string logs_;
63 gin::WrapperInfo TestNatives::kWrapperInfo = {gin::kEmbedderNativeGin};
65 } // namespace
67 gin::WrapperInfo TestServiceProvider::kWrapperInfo = {gin::kEmbedderNativeGin};
69 gin::Handle<TestServiceProvider> TestServiceProvider::Create(
70 v8::Isolate* isolate) {
71 return gin::CreateHandle(isolate, new TestServiceProvider());
74 TestServiceProvider::~TestServiceProvider() {
77 gin::ObjectTemplateBuilder TestServiceProvider::GetObjectTemplateBuilder(
78 v8::Isolate* isolate) {
79 return Wrappable<TestServiceProvider>::GetObjectTemplateBuilder(isolate)
80 .SetMethod("connectToService", &TestServiceProvider::ConnectToService);
83 mojo::Handle TestServiceProvider::ConnectToService(
84 const std::string& service_name) {
85 EXPECT_EQ(1u, service_factories_.count(service_name))
86 << "Unregistered service " << service_name << " requested.";
87 mojo::MessagePipe pipe;
88 std::map<std::string,
89 base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it =
90 service_factories_.find(service_name);
91 if (it != service_factories_.end())
92 it->second.Run(pipe.handle0.Pass());
93 return pipe.handle1.release();
96 TestServiceProvider::TestServiceProvider() {
99 // static
100 void TestServiceProvider::IgnoreHandle(mojo::ScopedMessagePipeHandle handle) {
103 ApiTestBase::ApiTestBase() {
105 ApiTestBase::~ApiTestBase() {
108 void ApiTestBase::SetUp() {
109 ModuleSystemTest::SetUp();
110 InitializeEnvironment();
111 RegisterModules();
114 void ApiTestBase::RegisterModules() {
115 v8_schema_registry_.reset(new V8SchemaRegistry);
116 const std::vector<std::pair<std::string, int> > resources =
117 Dispatcher::GetJsResources();
118 for (std::vector<std::pair<std::string, int> >::const_iterator resource =
119 resources.begin();
120 resource != resources.end();
121 ++resource) {
122 if (resource->first != "test_environment_specific_bindings")
123 env()->RegisterModule(resource->first, resource->second);
125 Dispatcher::RegisterNativeHandlers(env()->module_system(),
126 env()->context(),
127 NULL,
128 NULL,
129 v8_schema_registry_.get());
130 env()->module_system()->RegisterNativeHandler(
131 "process",
132 scoped_ptr<NativeHandler>(new ProcessInfoNativeHandler(
133 env()->context(),
134 env()->context()->GetExtensionID(),
135 env()->context()->GetContextTypeDescription(),
136 false,
138 false)));
139 env()->RegisterTestFile("test_environment_specific_bindings",
140 "unit_test_environment_specific_bindings.js");
142 env()->OverrideNativeHandler("activityLogger",
143 "exports.LogAPICall = function() {};");
144 env()->OverrideNativeHandler(
145 "apiDefinitions",
146 "exports.GetExtensionAPIDefinitionsForTest = function() { return [] };");
147 env()->OverrideNativeHandler(
148 "event_natives",
149 "exports.AttachEvent = function() {};"
150 "exports.DetachEvent = function() {};"
151 "exports.AttachFilteredEvent = function() {};"
152 "exports.AttachFilteredEvent = function() {};"
153 "exports.MatchAgainstEventFilter = function() { return [] };");
155 gin::ModuleRegistry::From(env()->context()->v8_context())
156 ->AddBuiltinModule(env()->isolate(),
157 mojo::js::Core::kModuleName,
158 mojo::js::Core::GetModule(env()->isolate()));
159 gin::ModuleRegistry::From(env()->context()->v8_context())
160 ->AddBuiltinModule(env()->isolate(),
161 mojo::js::Support::kModuleName,
162 mojo::js::Support::GetModule(env()->isolate()));
163 gin::Handle<TestServiceProvider> service_provider =
164 TestServiceProvider::Create(env()->isolate());
165 service_provider_ = service_provider.get();
166 gin::ModuleRegistry::From(env()->context()->v8_context())
167 ->AddBuiltinModule(env()->isolate(),
168 "content/public/renderer/service_provider",
169 service_provider.ToV8());
172 void ApiTestBase::InitializeEnvironment() {
173 gin::Dictionary global(env()->isolate(),
174 env()->context()->v8_context()->Global());
175 gin::Dictionary navigator(gin::Dictionary::CreateEmpty(env()->isolate()));
176 navigator.Set("appVersion", base::StringPiece(""));
177 global.Set("navigator", navigator);
178 gin::Dictionary chrome(gin::Dictionary::CreateEmpty(env()->isolate()));
179 global.Set("chrome", chrome);
180 gin::Dictionary extension(gin::Dictionary::CreateEmpty(env()->isolate()));
181 chrome.Set("extension", extension);
182 gin::Dictionary runtime(gin::Dictionary::CreateEmpty(env()->isolate()));
183 chrome.Set("runtime", runtime);
186 void ApiTestBase::RunTest(const std::string& file_name,
187 const std::string& test_name) {
188 env()->RegisterTestFile("testBody", file_name);
189 ExpectNoAssertionsMade();
190 base::RunLoop run_loop;
191 gin::ModuleRegistry::From(env()->context()->v8_context())->AddBuiltinModule(
192 env()->isolate(),
193 "testNatives",
194 TestNatives::Create(env()->isolate(), run_loop.QuitClosure()).ToV8());
195 base::MessageLoop::current()->PostTask(FROM_HERE,
196 base::Bind(&ApiTestBase::RunTestInner,
197 base::Unretained(this),
198 test_name,
199 run_loop.QuitClosure()));
200 base::MessageLoop::current()->PostTask(
201 FROM_HERE,
202 base::Bind(&ApiTestBase::RunPromisesAgain, base::Unretained(this)));
203 run_loop.Run();
206 void ApiTestBase::RunTestInner(const std::string& test_name,
207 const base::Closure& quit_closure) {
208 v8::HandleScope scope(env()->isolate());
209 ModuleSystem::NativesEnabledScope natives_enabled(env()->module_system());
210 v8::Handle<v8::Value> result =
211 env()->module_system()->CallModuleMethod("testBody", test_name);
212 if (!result->IsTrue()) {
213 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure);
214 FAIL() << "Failed to run test \"" << test_name << "\"";
218 void ApiTestBase::RunPromisesAgain() {
219 RunResolvedPromises();
220 base::MessageLoop::current()->PostTask(
221 FROM_HERE,
222 base::Bind(&ApiTestBase::RunPromisesAgain, base::Unretained(this)));
225 } // namespace extensions