| 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 "extensions/renderer/module_system_test.h" |
| 6 |
| 7 #include <map> |
| 8 #include <string> |
| 6 | 9 |
| 7 #include "base/callback.h" | 10 #include "base/callback.h" |
| 8 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 10 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 13 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 14 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
| 15 #include "chrome/common/chrome_paths.h" | 18 #include "extensions/common/extension_paths.h" |
| 16 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
| 17 #include "extensions/renderer/logging_native_handler.h" | 19 #include "extensions/renderer/logging_native_handler.h" |
| 18 #include "extensions/renderer/object_backed_native_handler.h" | 20 #include "extensions/renderer/object_backed_native_handler.h" |
| 19 #include "extensions/renderer/safe_builtins.h" | 21 #include "extensions/renderer/safe_builtins.h" |
| 20 #include "extensions/renderer/utils_native_handler.h" | 22 #include "extensions/renderer/utils_native_handler.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 22 | 24 |
| 23 #include <map> | 25 namespace extensions { |
| 24 #include <string> | |
| 25 | |
| 26 using extensions::ModuleSystem; | |
| 27 using extensions::NativeHandler; | |
| 28 using extensions::ObjectBackedNativeHandler; | |
| 29 | |
| 30 namespace { | 26 namespace { |
| 31 | 27 |
| 32 class FailsOnException : public ModuleSystem::ExceptionHandler { | 28 class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 33 public: | 29 public: |
| 34 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { | 30 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { |
| 35 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); | 31 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); |
| 36 } | 32 } |
| 37 }; | 33 }; |
| 38 | 34 |
| 39 class V8ExtensionConfigurator { | 35 class V8ExtensionConfigurator { |
| 40 public: | 36 public: |
| 41 V8ExtensionConfigurator() | 37 V8ExtensionConfigurator() |
| 42 : safe_builtins_(extensions::SafeBuiltins::CreateV8Extension()), | 38 : safe_builtins_(SafeBuiltins::CreateV8Extension()), |
| 43 names_(1, safe_builtins_->name()), | 39 names_(1, safe_builtins_->name()), |
| 44 configuration_(new v8::ExtensionConfiguration( | 40 configuration_( |
| 45 names_.size(), vector_as_array(&names_))) { | 41 new v8::ExtensionConfiguration(static_cast<int>(names_.size()), |
| 42 vector_as_array(&names_))) { |
| 46 v8::RegisterExtension(safe_builtins_.get()); | 43 v8::RegisterExtension(safe_builtins_.get()); |
| 47 } | 44 } |
| 48 | 45 |
| 49 v8::ExtensionConfiguration* GetConfiguration() { | 46 v8::ExtensionConfiguration* GetConfiguration() { |
| 50 return configuration_.get(); | 47 return configuration_.get(); |
| 51 } | 48 } |
| 52 | 49 |
| 53 private: | 50 private: |
| 54 scoped_ptr<v8::Extension> safe_builtins_; | 51 scoped_ptr<v8::Extension> safe_builtins_; |
| 55 std::vector<const char*> names_; | 52 std::vector<const char*> names_; |
| 56 scoped_ptr<v8::ExtensionConfiguration> configuration_; | 53 scoped_ptr<v8::ExtensionConfiguration> configuration_; |
| 57 }; | 54 }; |
| 58 | 55 |
| 59 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = | 56 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = |
| 60 LAZY_INSTANCE_INITIALIZER; | 57 LAZY_INSTANCE_INITIALIZER; |
| 61 | 58 |
| 62 } // namespace | 59 } // namespace |
| 63 | 60 |
| 64 // Native JS functions for doing asserts. | 61 // Native JS functions for doing asserts. |
| 65 class ModuleSystemTestEnvironment::AssertNatives | 62 class ModuleSystemTestEnvironment::AssertNatives |
| 66 : public ObjectBackedNativeHandler { | 63 : public ObjectBackedNativeHandler { |
| 67 public: | 64 public: |
| 68 explicit AssertNatives(extensions::ChromeV8Context* context) | 65 explicit AssertNatives(ScriptContext* context) |
| 69 : ObjectBackedNativeHandler(context), | 66 : ObjectBackedNativeHandler(context), |
| 70 assertion_made_(false), | 67 assertion_made_(false), |
| 71 failed_(false) { | 68 failed_(false) { |
| 72 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, | 69 RouteFunction( |
| 73 base::Unretained(this))); | 70 "AssertTrue", |
| 74 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, | 71 base::Bind(&AssertNatives::AssertTrue, base::Unretained(this))); |
| 75 base::Unretained(this))); | 72 RouteFunction( |
| 73 "AssertFalse", |
| 74 base::Bind(&AssertNatives::AssertFalse, base::Unretained(this))); |
| 76 } | 75 } |
| 77 | 76 |
| 78 bool assertion_made() { return assertion_made_; } | 77 bool assertion_made() { return assertion_made_; } |
| 79 bool failed() { return failed_; } | 78 bool failed() { return failed_; } |
| 80 | 79 |
| 81 void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) { | 80 void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 82 CHECK_EQ(1, args.Length()); | 81 CHECK_EQ(1, args.Length()); |
| 83 assertion_made_ = true; | 82 assertion_made_ = true; |
| 84 failed_ = failed_ || !args[0]->ToBoolean()->Value(); | 83 failed_ = failed_ || !args[0]->ToBoolean()->Value(); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) { | 86 void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 88 CHECK_EQ(1, args.Length()); | 87 CHECK_EQ(1, args.Length()); |
| 89 assertion_made_ = true; | 88 assertion_made_ = true; |
| 90 failed_ = failed_ || args[0]->ToBoolean()->Value(); | 89 failed_ = failed_ || args[0]->ToBoolean()->Value(); |
| 91 } | 90 } |
| 92 | 91 |
| 93 private: | 92 private: |
| 94 bool assertion_made_; | 93 bool assertion_made_; |
| 95 bool failed_; | 94 bool failed_; |
| 96 }; | 95 }; |
| 97 | 96 |
| 98 // Source map that operates on std::strings. | 97 // Source map that operates on std::strings. |
| 99 class ModuleSystemTestEnvironment::StringSourceMap | 98 class ModuleSystemTestEnvironment::StringSourceMap |
| 100 : public extensions::ModuleSystem::SourceMap { | 99 : public ModuleSystem::SourceMap { |
| 101 public: | 100 public: |
| 102 StringSourceMap() {} | 101 StringSourceMap() {} |
| 103 virtual ~StringSourceMap() {} | 102 virtual ~StringSourceMap() {} |
| 104 | 103 |
| 105 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, | 104 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, |
| 106 const std::string& name) OVERRIDE { | 105 const std::string& name) OVERRIDE { |
| 107 if (source_map_.count(name) == 0) | 106 if (source_map_.count(name) == 0) |
| 108 return v8::Undefined(isolate); | 107 return v8::Undefined(isolate); |
| 109 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str()); | 108 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str()); |
| 110 } | 109 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 124 | 123 |
| 125 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment( | 124 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment( |
| 126 gin::IsolateHolder* isolate_holder) | 125 gin::IsolateHolder* isolate_holder) |
| 127 : isolate_holder_(isolate_holder), | 126 : isolate_holder_(isolate_holder), |
| 128 context_holder_(new gin::ContextHolder(isolate_holder_->isolate())), | 127 context_holder_(new gin::ContextHolder(isolate_holder_->isolate())), |
| 129 handle_scope_(isolate_holder_->isolate()), | 128 handle_scope_(isolate_holder_->isolate()), |
| 130 source_map_(new StringSourceMap()) { | 129 source_map_(new StringSourceMap()) { |
| 131 context_holder_->SetContext( | 130 context_holder_->SetContext( |
| 132 v8::Context::New(isolate_holder->isolate(), | 131 v8::Context::New(isolate_holder->isolate(), |
| 133 g_v8_extension_configurator.Get().GetConfiguration())); | 132 g_v8_extension_configurator.Get().GetConfiguration())); |
| 134 context_.reset(new extensions::ChromeV8Context( | 133 context_.reset(new ScriptContext(context_holder_->context(), |
| 135 context_holder_->context(), | 134 NULL, // WebFrame |
| 136 NULL, // WebFrame | 135 NULL, // Extension |
| 137 NULL, // Extension | 136 Feature::UNSPECIFIED_CONTEXT)); |
| 138 extensions::Feature::UNSPECIFIED_CONTEXT)); | |
| 139 context_->v8_context()->Enter(); | 137 context_->v8_context()->Enter(); |
| 140 assert_natives_ = new AssertNatives(context_.get()); | 138 assert_natives_ = new AssertNatives(context_.get()); |
| 141 | 139 |
| 142 { | 140 { |
| 143 scoped_ptr<ModuleSystem> module_system( | 141 scoped_ptr<ModuleSystem> module_system( |
| 144 new ModuleSystem(context_.get(), source_map_.get())); | 142 new ModuleSystem(context_.get(), source_map_.get())); |
| 145 context_->set_module_system(module_system.Pass()); | 143 context_->set_module_system(module_system.Pass()); |
| 146 } | 144 } |
| 147 ModuleSystem* module_system = context_->module_system(); | 145 ModuleSystem* module_system = context_->module_system(); |
| 148 module_system->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( | 146 module_system->RegisterNativeHandler( |
| 149 assert_natives_)); | 147 "assert", scoped_ptr<NativeHandler>(assert_natives_)); |
| 150 module_system->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>( | 148 module_system->RegisterNativeHandler( |
| 151 new extensions::LoggingNativeHandler(context_.get()))); | 149 "logging", |
| 152 module_system->RegisterNativeHandler("utils", scoped_ptr<NativeHandler>( | 150 scoped_ptr<NativeHandler>(new LoggingNativeHandler(context_.get()))); |
| 153 new extensions::UtilsNativeHandler(context_.get()))); | 151 module_system->RegisterNativeHandler( |
| 152 "utils", |
| 153 scoped_ptr<NativeHandler>(new UtilsNativeHandler(context_.get()))); |
| 154 module_system->SetExceptionHandlerForTest( | 154 module_system->SetExceptionHandlerForTest( |
| 155 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); | 155 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
| 156 } | 156 } |
| 157 | 157 |
| 158 ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() { | 158 ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() { |
| 159 if (context_) | 159 if (context_) |
| 160 context_->v8_context()->Exit(); | 160 context_->v8_context()->Exit(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, | 163 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
| 164 const std::string& code) { | 164 const std::string& code) { |
| 165 source_map_->RegisterModule(name, code); | 165 source_map_->RegisterModule(name, code); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, | 168 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name, |
| 169 int resource_id) { | 169 int resource_id) { |
| 170 const std::string& code = ResourceBundle::GetSharedInstance(). | 170 const std::string& code = ResourceBundle::GetSharedInstance() |
| 171 GetRawDataResource(resource_id).as_string(); | 171 .GetRawDataResource(resource_id) |
| 172 .as_string(); |
| 172 source_map_->RegisterModule(name, code); | 173 source_map_->RegisterModule(name, code); |
| 173 } | 174 } |
| 174 | 175 |
| 175 void ModuleSystemTestEnvironment::OverrideNativeHandler( | 176 void ModuleSystemTestEnvironment::OverrideNativeHandler( |
| 176 const std::string& name, | 177 const std::string& name, |
| 177 const std::string& code) { | 178 const std::string& code) { |
| 178 RegisterModule(name, code); | 179 RegisterModule(name, code); |
| 179 context_->module_system()->OverrideNativeHandlerForTest(name); | 180 context_->module_system()->OverrideNativeHandlerForTest(name); |
| 180 } | 181 } |
| 181 | 182 |
| 182 void ModuleSystemTestEnvironment::RegisterTestFile( | 183 void ModuleSystemTestEnvironment::RegisterTestFile( |
| 183 const std::string& module_name, | 184 const std::string& module_name, |
| 184 const std::string& file_name) { | 185 const std::string& file_name) { |
| 185 base::FilePath test_js_file_path; | 186 base::FilePath test_js_file_path; |
| 186 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); | 187 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &test_js_file_path)); |
| 187 test_js_file_path = test_js_file_path.AppendASCII("extensions") | 188 test_js_file_path = test_js_file_path.AppendASCII(file_name); |
| 188 .AppendASCII(file_name); | |
| 189 std::string test_js; | 189 std::string test_js; |
| 190 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); | 190 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); |
| 191 source_map_->RegisterModule(module_name, test_js); | 191 source_map_->RegisterModule(module_name, test_js); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void ModuleSystemTestEnvironment::ShutdownGin() { | 194 void ModuleSystemTestEnvironment::ShutdownGin() { |
| 195 context_holder_.reset(); | 195 context_holder_.reset(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 void ModuleSystemTestEnvironment::ShutdownModuleSystem() { | 198 void ModuleSystemTestEnvironment::ShutdownModuleSystem() { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return make_scoped_ptr(new ModuleSystemTestEnvironment(&isolate_holder_)); | 230 return make_scoped_ptr(new ModuleSystemTestEnvironment(&isolate_holder_)); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void ModuleSystemTest::ExpectNoAssertionsMade() { | 233 void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 234 should_assertions_be_made_ = false; | 234 should_assertions_be_made_ = false; |
| 235 } | 235 } |
| 236 | 236 |
| 237 void ModuleSystemTest::RunResolvedPromises() { | 237 void ModuleSystemTest::RunResolvedPromises() { |
| 238 isolate_holder_.isolate()->RunMicrotasks(); | 238 isolate_holder_.isolate()->RunMicrotasks(); |
| 239 } | 239 } |
| 240 |
| 241 } // namespace extensions |
| OLD | NEW |