| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/base/module_system_test.h" | 5 #include "chrome/test/base/module_system_test.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 std::vector<const char*> names_; | 55 std::vector<const char*> names_; |
| 56 scoped_ptr<v8::ExtensionConfiguration> configuration_; | 56 scoped_ptr<v8::ExtensionConfiguration> configuration_; |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = | 59 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = |
| 60 LAZY_INSTANCE_INITIALIZER; | 60 LAZY_INSTANCE_INITIALIZER; |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 // Native JS functions for doing asserts. | 64 // Native JS functions for doing asserts. |
| 65 class ModuleSystemTest::AssertNatives : public ObjectBackedNativeHandler { | 65 class ModuleSystemTestEnvironment::AssertNatives |
| 66 : public ObjectBackedNativeHandler { |
| 66 public: | 67 public: |
| 67 explicit AssertNatives(extensions::ChromeV8Context* context) | 68 explicit AssertNatives(extensions::ChromeV8Context* context) |
| 68 : ObjectBackedNativeHandler(context), | 69 : ObjectBackedNativeHandler(context), |
| 69 assertion_made_(false), | 70 assertion_made_(false), |
| 70 failed_(false) { | 71 failed_(false) { |
| 71 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, | 72 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, |
| 72 base::Unretained(this))); | 73 base::Unretained(this))); |
| 73 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, | 74 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, |
| 74 base::Unretained(this))); | 75 base::Unretained(this))); |
| 75 } | 76 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 88 assertion_made_ = true; | 89 assertion_made_ = true; |
| 89 failed_ = failed_ || args[0]->ToBoolean()->Value(); | 90 failed_ = failed_ || args[0]->ToBoolean()->Value(); |
| 90 } | 91 } |
| 91 | 92 |
| 92 private: | 93 private: |
| 93 bool assertion_made_; | 94 bool assertion_made_; |
| 94 bool failed_; | 95 bool failed_; |
| 95 }; | 96 }; |
| 96 | 97 |
| 97 // Source map that operates on std::strings. | 98 // Source map that operates on std::strings. |
| 98 class ModuleSystemTest::StringSourceMap : public ModuleSystem::SourceMap { | 99 class ModuleSystemTestEnvironment::StringSourceMap |
| 100 : public extensions::ModuleSystem::SourceMap { |
| 99 public: | 101 public: |
| 100 StringSourceMap() {} | 102 StringSourceMap() {} |
| 101 virtual ~StringSourceMap() {} | 103 virtual ~StringSourceMap() {} |
| 102 | 104 |
| 103 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, | 105 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, |
| 104 const std::string& name) OVERRIDE { | 106 const std::string& name) OVERRIDE { |
| 105 if (source_map_.count(name) == 0) | 107 if (source_map_.count(name) == 0) |
| 106 return v8::Undefined(isolate); | 108 return v8::Undefined(isolate); |
| 107 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str()); | 109 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str()); |
| 108 } | 110 } |
| 109 | 111 |
| 110 virtual bool Contains(const std::string& name) OVERRIDE { | 112 virtual bool Contains(const std::string& name) OVERRIDE { |
| 111 return source_map_.count(name); | 113 return source_map_.count(name); |
| 112 } | 114 } |
| 113 | 115 |
| 114 void RegisterModule(const std::string& name, const std::string& source) { | 116 void RegisterModule(const std::string& name, const std::string& source) { |
| 115 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found"; | 117 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found"; |
| 116 source_map_[name] = source; | 118 source_map_[name] = source; |
| 117 } | 119 } |
| 118 | 120 |
| 119 private: | 121 private: |
| 120 std::map<std::string, std::string> source_map_; | 122 std::map<std::string, std::string> source_map_; |
| 121 }; | 123 }; |
| 122 | 124 |
| 123 ModuleSystemTest::ModuleSystemTest() | 125 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment( |
| 124 : isolate_(v8::Isolate::GetCurrent()), | 126 gin::IsolateHolder* isolate_holder) |
| 125 handle_scope_(isolate_), | 127 : context_holder_(new gin::ContextHolder(isolate_holder->isolate())), |
| 126 context_( | 128 handle_scope_(isolate_holder->isolate()), |
| 127 new extensions::ChromeV8Context( | 129 source_map_(new StringSourceMap()) { |
| 128 v8::Context::New( | 130 context_holder_->SetContext( |
| 129 isolate_, | 131 v8::Context::New(isolate_holder->isolate(), |
| 130 g_v8_extension_configurator.Get().GetConfiguration()), | 132 g_v8_extension_configurator.Get().GetConfiguration())); |
| 131 NULL, // WebFrame | 133 context_.reset(new extensions::ChromeV8Context( |
| 132 NULL, // Extension | 134 context_holder_->context(), |
| 133 extensions::Feature::UNSPECIFIED_CONTEXT)), | 135 NULL, // WebFrame |
| 134 source_map_(new StringSourceMap()), | 136 NULL, // Extension |
| 135 should_assertions_be_made_(true) { | 137 extensions::Feature::UNSPECIFIED_CONTEXT)); |
| 136 context_->v8_context()->Enter(); | 138 context_->v8_context()->Enter(); |
| 137 assert_natives_ = new AssertNatives(context_.get()); | 139 assert_natives_ = new AssertNatives(context_.get()); |
| 138 | 140 |
| 139 { | 141 { |
| 140 scoped_ptr<ModuleSystem> module_system( | 142 scoped_ptr<ModuleSystem> module_system( |
| 141 new ModuleSystem(context_.get(), source_map_.get())); | 143 new ModuleSystem(context_.get(), source_map_.get())); |
| 142 context_->set_module_system(module_system.Pass()); | 144 context_->set_module_system(module_system.Pass()); |
| 143 } | 145 } |
| 144 ModuleSystem* module_system = context_->module_system(); | 146 ModuleSystem* module_system = context_->module_system(); |
| 145 module_system->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( | 147 module_system->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( |
| 146 assert_natives_)); | 148 assert_natives_)); |
| 147 module_system->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>( | 149 module_system->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>( |
| 148 new extensions::LoggingNativeHandler(context_.get()))); | 150 new extensions::LoggingNativeHandler(context_.get()))); |
| 149 module_system->RegisterNativeHandler("utils", scoped_ptr<NativeHandler>( | 151 module_system->RegisterNativeHandler("utils", scoped_ptr<NativeHandler>( |
| 150 new extensions::UtilsNativeHandler(context_.get()))); | 152 new extensions::UtilsNativeHandler(context_.get()))); |
| 151 module_system->SetExceptionHandlerForTest( | 153 module_system->SetExceptionHandlerForTest( |
| 152 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); | 154 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
| 153 } | 155 } |
| 154 | 156 |
| 155 ModuleSystemTest::~ModuleSystemTest() { | 157 ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() { |
| 156 context_->v8_context()->Exit(); | 158 if (context_) |
| 159 context_->v8_context()->Exit(); |
| 157 } | 160 } |
| 158 | 161 |
| 159 void ModuleSystemTest::RegisterModule(const std::string& name, | 162 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
| 160 const std::string& code) { | 163 const std::string& code) { |
| 161 source_map_->RegisterModule(name, code); | 164 source_map_->RegisterModule(name, code); |
| 162 } | 165 } |
| 163 | 166 |
| 164 void ModuleSystemTest::RegisterModule(const std::string& name, | 167 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
| 165 int resource_id) { | 168 int resource_id) { |
| 166 const std::string& code = ResourceBundle::GetSharedInstance(). | 169 const std::string& code = ResourceBundle::GetSharedInstance(). |
| 167 GetRawDataResource(resource_id).as_string(); | 170 GetRawDataResource(resource_id).as_string(); |
| 168 source_map_->RegisterModule(name, code); | 171 source_map_->RegisterModule(name, code); |
| 169 } | 172 } |
| 170 | 173 |
| 171 void ModuleSystemTest::OverrideNativeHandler(const std::string& name, | 174 void ModuleSystemTestEnvironment::OverrideNativeHandler( |
| 172 const std::string& code) { | 175 const std::string& name, |
| 176 const std::string& code) { |
| 173 RegisterModule(name, code); | 177 RegisterModule(name, code); |
| 174 context_->module_system()->OverrideNativeHandlerForTest(name); | 178 context_->module_system()->OverrideNativeHandlerForTest(name); |
| 175 } | 179 } |
| 176 | 180 |
| 177 void ModuleSystemTest::RegisterTestFile(const std::string& module_name, | 181 void ModuleSystemTestEnvironment::RegisterTestFile( |
| 178 const std::string& file_name) { | 182 const std::string& module_name, |
| 183 const std::string& file_name) { |
| 179 base::FilePath test_js_file_path; | 184 base::FilePath test_js_file_path; |
| 180 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); | 185 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); |
| 181 test_js_file_path = test_js_file_path.AppendASCII("extensions") | 186 test_js_file_path = test_js_file_path.AppendASCII("extensions") |
| 182 .AppendASCII(file_name); | 187 .AppendASCII(file_name); |
| 183 std::string test_js; | 188 std::string test_js; |
| 184 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); | 189 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); |
| 185 source_map_->RegisterModule(module_name, test_js); | 190 source_map_->RegisterModule(module_name, test_js); |
| 186 } | 191 } |
| 187 | 192 |
| 188 void ModuleSystemTest::TearDown() { | 193 void ModuleSystemTestEnvironment::DeleteContextHolder() { |
| 189 // All tests must assert at least once unless otherwise specified. | 194 context_holder_.reset(); |
| 190 EXPECT_EQ(should_assertions_be_made_, | |
| 191 assert_natives_->assertion_made()); | |
| 192 EXPECT_FALSE(assert_natives_->failed()); | |
| 193 } | 195 } |
| 194 | 196 |
| 195 void ModuleSystemTest::ExpectNoAssertionsMade() { | 197 void ModuleSystemTestEnvironment::DeleteModuleSystem() { |
| 196 should_assertions_be_made_ = false; | 198 context_->v8_context()->Exit(); |
| 199 context_.reset(); |
| 197 } | 200 } |
| 198 | 201 |
| 199 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { | 202 v8::Handle<v8::Object> ModuleSystemTestEnvironment::CreateGlobal( |
| 203 const std::string& name) { |
| 200 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 204 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 201 v8::EscapableHandleScope handle_scope(isolate); | 205 v8::EscapableHandleScope handle_scope(isolate); |
| 202 v8::Local<v8::Object> object = v8::Object::New(isolate); | 206 v8::Local<v8::Object> object = v8::Object::New(isolate); |
| 203 isolate->GetCurrentContext()->Global()->Set( | 207 isolate->GetCurrentContext()->Global()->Set( |
| 204 v8::String::NewFromUtf8(isolate, name.c_str()), object); | 208 v8::String::NewFromUtf8(isolate, name.c_str()), object); |
| 205 return handle_scope.Escape(object); | 209 return handle_scope.Escape(object); |
| 206 } | 210 } |
| 211 |
| 212 ModuleSystemTest::ModuleSystemTest() |
| 213 : isolate_holder_(v8::Isolate::GetCurrent(), NULL), |
| 214 env_(&isolate_holder_), |
| 215 should_assertions_be_made_(true) { |
| 216 } |
| 217 |
| 218 ModuleSystemTest::~ModuleSystemTest() { |
| 219 } |
| 220 |
| 221 void ModuleSystemTest::TearDown() { |
| 222 // All tests must assert at least once unless otherwise specified. |
| 223 EXPECT_EQ(should_assertions_be_made_, |
| 224 env_.assert_natives()->assertion_made()); |
| 225 EXPECT_FALSE(env_.assert_natives()->failed()); |
| 226 } |
| 227 |
| 228 void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 229 should_assertions_be_made_ = false; |
| 230 } |
| 231 |
| 232 void ModuleSystemTest::RunResolvedPromises() { |
| 233 isolate_holder_.isolate()->RunMicrotasks(); |
| 234 } |
| OLD | NEW |