| 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> | |
| 24 #include <string> | |
| 25 | |
| 26 using extensions::ModuleSystem; | 25 using extensions::ModuleSystem; |
| 27 using extensions::NativeHandler; | 26 using extensions::NativeHandler; |
| 28 using extensions::ObjectBackedNativeHandler; | 27 using extensions::ObjectBackedNativeHandler; |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 class FailsOnException : public ModuleSystem::ExceptionHandler { | 31 class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 33 public: | 32 public: |
| 34 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { | 33 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { |
| 35 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); | 34 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); |
| 36 } | 35 } |
| 37 }; | 36 }; |
| 38 | 37 |
| 39 class V8ExtensionConfigurator { | 38 class V8ExtensionConfigurator { |
| 40 public: | 39 public: |
| 41 V8ExtensionConfigurator() | 40 V8ExtensionConfigurator() |
| 42 : safe_builtins_(extensions::SafeBuiltins::CreateV8Extension()), | 41 : safe_builtins_(extensions::SafeBuiltins::CreateV8Extension()), |
| 43 names_(1, safe_builtins_->name()), | 42 names_(1, safe_builtins_->name()), |
| 44 configuration_(new v8::ExtensionConfiguration( | 43 configuration_(new v8::ExtensionConfiguration( |
| 45 names_.size(), vector_as_array(&names_))) { | 44 static_cast<int>(names_.size()), vector_as_array(&names_))) { |
| 46 v8::RegisterExtension(safe_builtins_.get()); | 45 v8::RegisterExtension(safe_builtins_.get()); |
| 47 } | 46 } |
| 48 | 47 |
| 49 v8::ExtensionConfiguration* GetConfiguration() { | 48 v8::ExtensionConfiguration* GetConfiguration() { |
| 50 return configuration_.get(); | 49 return configuration_.get(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 scoped_ptr<v8::Extension> safe_builtins_; | 53 scoped_ptr<v8::Extension> safe_builtins_; |
| 55 std::vector<const char*> names_; | 54 std::vector<const char*> names_; |
| 56 scoped_ptr<v8::ExtensionConfiguration> configuration_; | 55 scoped_ptr<v8::ExtensionConfiguration> configuration_; |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = | 58 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = |
| 60 LAZY_INSTANCE_INITIALIZER; | 59 LAZY_INSTANCE_INITIALIZER; |
| 61 | 60 |
| 62 } // namespace | 61 } // namespace |
| 63 | 62 |
| 64 // Native JS functions for doing asserts. | 63 // Native JS functions for doing asserts. |
| 65 class ModuleSystemTestEnvironment::AssertNatives | 64 class ModuleSystemTestEnvironment::AssertNatives |
| 66 : public ObjectBackedNativeHandler { | 65 : public ObjectBackedNativeHandler { |
| 67 public: | 66 public: |
| 68 explicit AssertNatives(extensions::ChromeV8Context* context) | 67 explicit AssertNatives(extensions::ScriptContext* context) |
| 69 : ObjectBackedNativeHandler(context), | 68 : ObjectBackedNativeHandler(context), |
| 70 assertion_made_(false), | 69 assertion_made_(false), |
| 71 failed_(false) { | 70 failed_(false) { |
| 72 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, | 71 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, |
| 73 base::Unretained(this))); | 72 base::Unretained(this))); |
| 74 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, | 73 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, |
| 75 base::Unretained(this))); | 74 base::Unretained(this))); |
| 76 } | 75 } |
| 77 | 76 |
| 78 bool assertion_made() { return assertion_made_; } | 77 bool assertion_made() { return assertion_made_; } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 extensions::ScriptContext( |
| 135 context_holder_->context(), | 134 context_holder_->context(), |
| 136 NULL, // WebFrame | 135 NULL, // WebFrame |
| 137 NULL, // Extension | 136 NULL, // Extension |
| 138 extensions::Feature::UNSPECIFIED_CONTEXT)); | 137 extensions::Feature::UNSPECIFIED_CONTEXT)); |
| 139 context_->v8_context()->Enter(); | 138 context_->v8_context()->Enter(); |
| 140 assert_natives_ = new AssertNatives(context_.get()); | 139 assert_natives_ = new AssertNatives(context_.get()); |
| 141 | 140 |
| 142 { | 141 { |
| 143 scoped_ptr<ModuleSystem> module_system( | 142 scoped_ptr<ModuleSystem> module_system( |
| 144 new ModuleSystem(context_.get(), source_map_.get())); | 143 new ModuleSystem(context_.get(), source_map_.get())); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 const std::string& name, | 175 const std::string& name, |
| 177 const std::string& code) { | 176 const std::string& code) { |
| 178 RegisterModule(name, code); | 177 RegisterModule(name, code); |
| 179 context_->module_system()->OverrideNativeHandlerForTest(name); | 178 context_->module_system()->OverrideNativeHandlerForTest(name); |
| 180 } | 179 } |
| 181 | 180 |
| 182 void ModuleSystemTestEnvironment::RegisterTestFile( | 181 void ModuleSystemTestEnvironment::RegisterTestFile( |
| 183 const std::string& module_name, | 182 const std::string& module_name, |
| 184 const std::string& file_name) { | 183 const std::string& file_name) { |
| 185 base::FilePath test_js_file_path; | 184 base::FilePath test_js_file_path; |
| 186 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); | 185 ASSERT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &test_js_file_path)); |
| 187 test_js_file_path = test_js_file_path.AppendASCII("extensions") | 186 test_js_file_path = test_js_file_path.AppendASCII(file_name); |
| 188 .AppendASCII(file_name); | |
| 189 std::string test_js; | 187 std::string test_js; |
| 190 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); | 188 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); |
| 191 source_map_->RegisterModule(module_name, test_js); | 189 source_map_->RegisterModule(module_name, test_js); |
| 192 } | 190 } |
| 193 | 191 |
| 194 void ModuleSystemTestEnvironment::ShutdownGin() { | 192 void ModuleSystemTestEnvironment::ShutdownGin() { |
| 195 context_holder_.reset(); | 193 context_holder_.reset(); |
| 196 } | 194 } |
| 197 | 195 |
| 198 void ModuleSystemTestEnvironment::ShutdownModuleSystem() { | 196 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_)); | 228 return make_scoped_ptr(new ModuleSystemTestEnvironment(&isolate_holder_)); |
| 231 } | 229 } |
| 232 | 230 |
| 233 void ModuleSystemTest::ExpectNoAssertionsMade() { | 231 void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 234 should_assertions_be_made_ = false; | 232 should_assertions_be_made_ = false; |
| 235 } | 233 } |
| 236 | 234 |
| 237 void ModuleSystemTest::RunResolvedPromises() { | 235 void ModuleSystemTest::RunResolvedPromises() { |
| 238 isolate_holder_.isolate()->RunMicrotasks(); | 236 isolate_holder_.isolate()->RunMicrotasks(); |
| 239 } | 237 } |
| OLD | NEW |