| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "extensions/renderer/api_test_base.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/location.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "extensions/common/extension_urls.h" | |
| 15 #include "extensions/renderer/dispatcher.h" | |
| 16 #include "extensions/renderer/process_info_native_handler.h" | |
| 17 #include "gin/converter.h" | |
| 18 #include "gin/dictionary.h" | |
| 19 #include "mojo/edk/js/core.h" | |
| 20 #include "mojo/edk/js/handle.h" | |
| 21 #include "mojo/edk/js/support.h" | |
| 22 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 23 #include "mojo/public/cpp/system/core.h" | |
| 24 | |
| 25 namespace extensions { | |
| 26 namespace { | |
| 27 | |
| 28 // Natives for the implementation of the unit test version of chrome.test. Calls | |
| 29 // the provided |quit_closure| when either notifyPass or notifyFail is called. | |
| 30 class TestNatives : public gin::Wrappable<TestNatives> { | |
| 31 public: | |
| 32 static gin::Handle<TestNatives> Create(v8::Isolate* isolate, | |
| 33 const base::Closure& quit_closure) { | |
| 34 return gin::CreateHandle(isolate, new TestNatives(quit_closure)); | |
| 35 } | |
| 36 | |
| 37 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
| 38 v8::Isolate* isolate) override { | |
| 39 return Wrappable<TestNatives>::GetObjectTemplateBuilder(isolate) | |
| 40 .SetMethod("Log", &TestNatives::Log) | |
| 41 .SetMethod("NotifyPass", &TestNatives::NotifyPass) | |
| 42 .SetMethod("NotifyFail", &TestNatives::NotifyFail); | |
| 43 } | |
| 44 | |
| 45 void Log(const std::string& value) { logs_ += value + "\n"; } | |
| 46 void NotifyPass() { FinishTesting(); } | |
| 47 | |
| 48 void NotifyFail(const std::string& message) { | |
| 49 FinishTesting(); | |
| 50 FAIL() << logs_ << message; | |
| 51 } | |
| 52 | |
| 53 void FinishTesting() { | |
| 54 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | |
| 55 } | |
| 56 | |
| 57 static gin::WrapperInfo kWrapperInfo; | |
| 58 | |
| 59 private: | |
| 60 explicit TestNatives(const base::Closure& quit_closure) | |
| 61 : quit_closure_(quit_closure) {} | |
| 62 | |
| 63 const base::Closure quit_closure_; | |
| 64 std::string logs_; | |
| 65 }; | |
| 66 | |
| 67 gin::WrapperInfo TestNatives::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 gin::WrapperInfo TestInterfaceProvider::kWrapperInfo = | |
| 72 {gin::kEmbedderNativeGin}; | |
| 73 | |
| 74 gin::Handle<TestInterfaceProvider> TestInterfaceProvider::Create( | |
| 75 v8::Isolate* isolate) { | |
| 76 return gin::CreateHandle(isolate, new TestInterfaceProvider()); | |
| 77 } | |
| 78 | |
| 79 TestInterfaceProvider::~TestInterfaceProvider() { | |
| 80 } | |
| 81 | |
| 82 gin::ObjectTemplateBuilder TestInterfaceProvider::GetObjectTemplateBuilder( | |
| 83 v8::Isolate* isolate) { | |
| 84 return Wrappable<TestInterfaceProvider>::GetObjectTemplateBuilder(isolate) | |
| 85 .SetMethod("getInterface", &TestInterfaceProvider::GetInterface); | |
| 86 } | |
| 87 | |
| 88 mojo::Handle TestInterfaceProvider::GetInterface( | |
| 89 const std::string& interface_name) { | |
| 90 EXPECT_EQ(1u, factories_.count(interface_name)) | |
| 91 << "Unregistered interface " << interface_name << " requested."; | |
| 92 mojo::MessagePipe pipe; | |
| 93 std::map<std::string, | |
| 94 base::Callback<void(mojo::ScopedMessagePipeHandle)> >::iterator it = | |
| 95 factories_.find(interface_name); | |
| 96 if (it != factories_.end()) | |
| 97 it->second.Run(std::move(pipe.handle0)); | |
| 98 return pipe.handle1.release(); | |
| 99 } | |
| 100 | |
| 101 TestInterfaceProvider::TestInterfaceProvider() { | |
| 102 } | |
| 103 | |
| 104 // static | |
| 105 void TestInterfaceProvider::IgnoreHandle(mojo::ScopedMessagePipeHandle handle) { | |
| 106 } | |
| 107 | |
| 108 ApiTestEnvironment::ApiTestEnvironment( | |
| 109 ModuleSystemTestEnvironment* environment) { | |
| 110 env_ = environment; | |
| 111 InitializeEnvironment(); | |
| 112 RegisterModules(); | |
| 113 } | |
| 114 | |
| 115 ApiTestEnvironment::~ApiTestEnvironment() { | |
| 116 } | |
| 117 | |
| 118 void ApiTestEnvironment::RegisterModules() { | |
| 119 v8_schema_registry_.reset(new V8SchemaRegistry); | |
| 120 const std::vector<std::pair<std::string, int> > resources = | |
| 121 Dispatcher::GetJsResources(); | |
| 122 for (std::vector<std::pair<std::string, int> >::const_iterator resource = | |
| 123 resources.begin(); | |
| 124 resource != resources.end(); | |
| 125 ++resource) { | |
| 126 if (resource->first != "test_environment_specific_bindings") | |
| 127 env()->RegisterModule(resource->first, resource->second); | |
| 128 } | |
| 129 Dispatcher::RegisterNativeHandlers(env()->module_system(), | |
| 130 env()->context(), | |
| 131 NULL, | |
| 132 NULL, | |
| 133 v8_schema_registry_.get()); | |
| 134 env()->module_system()->RegisterNativeHandler( | |
| 135 "process", std::unique_ptr<NativeHandler>(new ProcessInfoNativeHandler( | |
| 136 env()->context(), env()->context()->GetExtensionID(), | |
| 137 env()->context()->GetContextTypeDescription(), false, | |
| 138 false, 2, false))); | |
| 139 env()->RegisterTestFile("test_environment_specific_bindings", | |
| 140 "unit_test_environment_specific_bindings.js"); | |
| 141 | |
| 142 env()->OverrideNativeHandler("activityLogger", | |
| 143 "exports.$set('LogAPICall', function() {});"); | |
| 144 env()->OverrideNativeHandler( | |
| 145 "apiDefinitions", | |
| 146 "exports.$set('GetExtensionAPIDefinitionsForTest'," | |
| 147 "function() { return [] });"); | |
| 148 env()->OverrideNativeHandler( | |
| 149 "event_natives", | |
| 150 "exports.$set('AttachEvent', function() {});" | |
| 151 "exports.$set('DetachEvent', function() {});" | |
| 152 "exports.$set('AttachFilteredEvent', function() {});" | |
| 153 "exports.$set('AttachFilteredEvent', function() {});" | |
| 154 "exports.$set('MatchAgainstEventFilter', function() { return [] });"); | |
| 155 | |
| 156 gin::ModuleRegistry::From(env()->context()->v8_context()) | |
| 157 ->AddBuiltinModule(env()->isolate(), mojo::edk::js::Core::kModuleName, | |
| 158 mojo::edk::js::Core::GetModule(env()->isolate())); | |
| 159 gin::ModuleRegistry::From(env()->context()->v8_context()) | |
| 160 ->AddBuiltinModule(env()->isolate(), mojo::edk::js::Support::kModuleName, | |
| 161 mojo::edk::js::Support::GetModule(env()->isolate())); | |
| 162 gin::Handle<TestInterfaceProvider> interface_provider = | |
| 163 TestInterfaceProvider::Create(env()->isolate()); | |
| 164 interface_provider_ = interface_provider.get(); | |
| 165 gin::ModuleRegistry::From(env()->context()->v8_context()) | |
| 166 ->AddBuiltinModule(env()->isolate(), | |
| 167 "content/public/renderer/frame_interfaces", | |
| 168 interface_provider.ToV8()); | |
| 169 } | |
| 170 | |
| 171 void ApiTestEnvironment::InitializeEnvironment() { | |
| 172 gin::Dictionary global(env()->isolate(), | |
| 173 env()->context()->v8_context()->Global()); | |
| 174 gin::Dictionary navigator(gin::Dictionary::CreateEmpty(env()->isolate())); | |
| 175 navigator.Set("appVersion", base::StringPiece("")); | |
| 176 global.Set("navigator", navigator); | |
| 177 gin::Dictionary chrome(gin::Dictionary::CreateEmpty(env()->isolate())); | |
| 178 global.Set("chrome", chrome); | |
| 179 gin::Dictionary extension(gin::Dictionary::CreateEmpty(env()->isolate())); | |
| 180 chrome.Set("extension", extension); | |
| 181 gin::Dictionary runtime(gin::Dictionary::CreateEmpty(env()->isolate())); | |
| 182 chrome.Set("runtime", runtime); | |
| 183 } | |
| 184 | |
| 185 void ApiTestEnvironment::RunTest(const std::string& file_name, | |
| 186 const std::string& test_name) { | |
| 187 env()->RegisterTestFile("testBody", file_name); | |
| 188 base::RunLoop run_loop; | |
| 189 gin::ModuleRegistry::From(env()->context()->v8_context())->AddBuiltinModule( | |
| 190 env()->isolate(), | |
| 191 "testNatives", | |
| 192 TestNatives::Create(env()->isolate(), run_loop.QuitClosure()).ToV8()); | |
| 193 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 194 FROM_HERE, | |
| 195 base::Bind(&ApiTestEnvironment::RunTestInner, base::Unretained(this), | |
| 196 test_name, run_loop.QuitClosure())); | |
| 197 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 198 FROM_HERE, base::Bind(&ApiTestEnvironment::RunPromisesAgain, | |
| 199 base::Unretained(this))); | |
| 200 run_loop.Run(); | |
| 201 } | |
| 202 | |
| 203 void ApiTestEnvironment::RunTestInner(const std::string& test_name, | |
| 204 const base::Closure& quit_closure) { | |
| 205 v8::HandleScope scope(env()->isolate()); | |
| 206 ModuleSystem::NativesEnabledScope natives_enabled(env()->module_system()); | |
| 207 v8::Local<v8::Value> result = | |
| 208 env()->module_system()->CallModuleMethod("testBody", test_name); | |
| 209 if (!result->IsTrue()) { | |
| 210 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure); | |
| 211 FAIL() << "Failed to run test \"" << test_name << "\""; | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 void ApiTestEnvironment::RunPromisesAgain() { | |
| 216 v8::MicrotasksScope::PerformCheckpoint(env()->isolate()); | |
| 217 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 218 FROM_HERE, base::Bind(&ApiTestEnvironment::RunPromisesAgain, | |
| 219 base::Unretained(this))); | |
| 220 } | |
| 221 | |
| 222 ApiTestBase::ApiTestBase() { | |
| 223 } | |
| 224 | |
| 225 ApiTestBase::~ApiTestBase() { | |
| 226 } | |
| 227 | |
| 228 void ApiTestBase::SetUp() { | |
| 229 ModuleSystemTest::SetUp(); | |
| 230 test_env_.reset(new ApiTestEnvironment(env())); | |
| 231 } | |
| 232 | |
| 233 void ApiTestBase::RunTest(const std::string& file_name, | |
| 234 const std::string& test_name) { | |
| 235 ExpectNoAssertionsMade(); | |
| 236 test_env_->RunTest(file_name, test_name); | |
| 237 } | |
| 238 | |
| 239 } // namespace extensions | |
| OLD | NEW |