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