[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/test/base/module_system_test.h" |
| 6 | |
| 7 | #include "base/callback.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 8 | #include "base/file_util.h" |
| 9 | #include "base/files/file_path.h" |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 12 | #include "base/path_service.h" |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | 4570a25 | 2013-03-31 00:35:43 | [diff] [blame] | 14 | #include "base/strings/string_piece.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 15 | #include "chrome/common/chrome_paths.h" |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 16 | #include "chrome/renderer/extensions/chrome_v8_context.h" |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 17 | #include "chrome/renderer/extensions/logging_native_handler.h" |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 18 | #include "chrome/renderer/extensions/utils_native_handler.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame^] | 19 | #include "extensions/renderer/object_backed_native_handler.h" |
| 20 | #include "extensions/renderer/safe_builtins.h" |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 21 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 22 | |
| 23 | #include <map> |
| 24 | #include <string> |
| 25 | |
[email protected] | 3c6babd | 2012-08-28 03:17:29 | [diff] [blame] | 26 | using extensions::ModuleSystem; |
| 27 | using extensions::NativeHandler; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 28 | using extensions::ObjectBackedNativeHandler; |
[email protected] | 3c6babd | 2012-08-28 03:17:29 | [diff] [blame] | 29 | |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 33 | public: |
| 34 | virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { |
| 35 | FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | class V8ExtensionConfigurator { |
| 40 | public: |
| 41 | V8ExtensionConfigurator() |
[email protected] | 5380451c | 2013-06-18 05:16:25 | [diff] [blame] | 42 | : safe_builtins_(extensions::SafeBuiltins::CreateV8Extension()), |
| 43 | names_(1, safe_builtins_->name()), |
| 44 | configuration_(new v8::ExtensionConfiguration( |
| 45 | names_.size(), vector_as_array(&names_))) { |
| 46 | v8::RegisterExtension(safe_builtins_.get()); |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | 5380451c | 2013-06-18 05:16:25 | [diff] [blame] | 49 | v8::ExtensionConfiguration* GetConfiguration() { |
| 50 | return configuration_.get(); |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | private: |
[email protected] | 5380451c | 2013-06-18 05:16:25 | [diff] [blame] | 54 | scoped_ptr<v8::Extension> safe_builtins_; |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 55 | std::vector<const char*> names_; |
[email protected] | 5380451c | 2013-06-18 05:16:25 | [diff] [blame] | 56 | scoped_ptr<v8::ExtensionConfiguration> configuration_; |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 57 | }; |
| 58 | |
[email protected] | 5380451c | 2013-06-18 05:16:25 | [diff] [blame] | 59 | base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 60 | LAZY_INSTANCE_INITIALIZER; |
| 61 | |
| 62 | } // namespace |
| 63 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 64 | // Native JS functions for doing asserts. |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 65 | class ModuleSystemTest::AssertNatives : public ObjectBackedNativeHandler { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 66 | public: |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 67 | explicit AssertNatives(extensions::ChromeV8Context* context) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 68 | : ObjectBackedNativeHandler(context), |
[email protected] | 2e0e0bc | 2013-02-04 10:30:34 | [diff] [blame] | 69 | assertion_made_(false), |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 70 | failed_(false) { |
| 71 | RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, |
| 72 | base::Unretained(this))); |
| 73 | RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, |
| 74 | base::Unretained(this))); |
| 75 | } |
| 76 | |
| 77 | bool assertion_made() { return assertion_made_; } |
| 78 | bool failed() { return failed_; } |
| 79 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 80 | void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 81 | CHECK_EQ(1, args.Length()); |
| 82 | assertion_made_ = true; |
| 83 | failed_ = failed_ || !args[0]->ToBoolean()->Value(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 86 | void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 87 | CHECK_EQ(1, args.Length()); |
| 88 | assertion_made_ = true; |
| 89 | failed_ = failed_ || args[0]->ToBoolean()->Value(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private: |
| 93 | bool assertion_made_; |
| 94 | bool failed_; |
| 95 | }; |
| 96 | |
| 97 | // Source map that operates on std::strings. |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 98 | class ModuleSystemTest::StringSourceMap : public ModuleSystem::SourceMap { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 99 | public: |
| 100 | StringSourceMap() {} |
| 101 | virtual ~StringSourceMap() {} |
| 102 | |
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 103 | virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, |
| 104 | const std::string& name) OVERRIDE { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 105 | if (source_map_.count(name) == 0) |
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 106 | return v8::Undefined(isolate); |
| 107 | return v8::String::NewFromUtf8(isolate, source_map_[name].c_str()); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | be9826e6 | 2013-02-07 02:00:58 | [diff] [blame] | 110 | virtual bool Contains(const std::string& name) OVERRIDE { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 111 | return source_map_.count(name); |
| 112 | } |
| 113 | |
| 114 | void RegisterModule(const std::string& name, const std::string& source) { |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 115 | CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found"; |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 116 | source_map_[name] = source; |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | std::map<std::string, std::string> source_map_; |
| 121 | }; |
| 122 | |
| 123 | ModuleSystemTest::ModuleSystemTest() |
[email protected] | 48002af | 2013-05-08 23:06:24 | [diff] [blame] | 124 | : isolate_(v8::Isolate::GetCurrent()), |
| 125 | handle_scope_(isolate_), |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 126 | context_( |
| 127 | new extensions::ChromeV8Context( |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 128 | v8::Context::New( |
| 129 | isolate_, |
[email protected] | 5380451c | 2013-06-18 05:16:25 | [diff] [blame] | 130 | g_v8_extension_configurator.Get().GetConfiguration()), |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 131 | NULL, // WebFrame |
| 132 | NULL, // Extension |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 133 | extensions::Feature::UNSPECIFIED_CONTEXT)), |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 134 | source_map_(new StringSourceMap()), |
| 135 | should_assertions_be_made_(true) { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 136 | context_->v8_context()->Enter(); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 137 | assert_natives_ = new AssertNatives(context_.get()); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 138 | |
| 139 | { |
| 140 | scoped_ptr<ModuleSystem> module_system( |
| 141 | new ModuleSystem(context_.get(), source_map_.get())); |
| 142 | context_->set_module_system(module_system.Pass()); |
| 143 | } |
| 144 | ModuleSystem* module_system = context_->module_system(); |
| 145 | module_system->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 146 | assert_natives_)); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 147 | module_system->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>( |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 148 | new extensions::LoggingNativeHandler(context_.get()))); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 149 | module_system->RegisterNativeHandler("utils", scoped_ptr<NativeHandler>( |
| 150 | new extensions::UtilsNativeHandler(context_.get()))); |
| 151 | module_system->SetExceptionHandlerForTest( |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 152 | scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | ModuleSystemTest::~ModuleSystemTest() { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 156 | context_->v8_context()->Exit(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void ModuleSystemTest::RegisterModule(const std::string& name, |
| 160 | const std::string& code) { |
| 161 | source_map_->RegisterModule(name, code); |
| 162 | } |
| 163 | |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 164 | void ModuleSystemTest::RegisterModule(const std::string& name, |
| 165 | int resource_id) { |
| 166 | const std::string& code = ResourceBundle::GetSharedInstance(). |
[email protected] | 4d8bb1a9 | 2012-11-01 21:12:40 | [diff] [blame] | 167 | GetRawDataResource(resource_id).as_string(); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 168 | source_map_->RegisterModule(name, code); |
| 169 | } |
| 170 | |
| 171 | void ModuleSystemTest::OverrideNativeHandler(const std::string& name, |
| 172 | const std::string& code) { |
| 173 | RegisterModule(name, code); |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 174 | context_->module_system()->OverrideNativeHandlerForTest(name); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 177 | void ModuleSystemTest::RegisterTestFile(const std::string& module_name, |
| 178 | const std::string& file_name) { |
| 179 | base::FilePath test_js_file_path; |
| 180 | ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); |
| 181 | test_js_file_path = test_js_file_path.AppendASCII("extensions") |
| 182 | .AppendASCII(file_name); |
| 183 | std::string test_js; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 184 | ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 185 | source_map_->RegisterModule(module_name, test_js); |
| 186 | } |
| 187 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 188 | void ModuleSystemTest::TearDown() { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 189 | // All tests must assert at least once unless otherwise specified. |
| 190 | EXPECT_EQ(should_assertions_be_made_, |
| 191 | assert_natives_->assertion_made()); |
| 192 | EXPECT_FALSE(assert_natives_->failed()); |
| 193 | } |
| 194 | |
| 195 | void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 196 | should_assertions_be_made_ = false; |
| 197 | } |
| 198 | |
| 199 | v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { |
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 200 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 201 | v8::EscapableHandleScope handle_scope(isolate); |
| 202 | v8::Local<v8::Object> object = v8::Object::New(isolate); |
| 203 | isolate->GetCurrentContext()->Global()->Set( |
| 204 | v8::String::NewFromUtf8(isolate, name.c_str()), object); |
| 205 | return handle_scope.Escape(object); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 206 | } |