blob: cc4cbfbaed05f09498bc5a19712092275252a215 [file] [log] [blame]
[email protected]582f6e92014-07-16 23:39:151// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]1164b862012-05-09 22:38:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]582f6e92014-07-16 23:39:155#include "extensions/renderer/module_system_test.h"
6
7#include <map>
8#include <string>
[email protected]1164b862012-05-09 22:38:379
10#include "base/callback.h"
[email protected]f8d87d32013-06-06 02:51:2911#include "base/files/file_path.h"
thestig94712702014-09-10 07:46:5912#include "base/files/file_util.h"
[email protected]295890bd2013-06-15 10:52:4513#include "base/lazy_instance.h"
[email protected]1164b862012-05-09 22:38:3714#include "base/memory/scoped_ptr.h"
[email protected]f8d87d32013-06-06 02:51:2915#include "base/path_service.h"
[email protected]295890bd2013-06-15 10:52:4516#include "base/stl_util.h"
[email protected]4570a252013-03-31 00:35:4317#include "base/strings/string_piece.h"
[email protected]582f6e92014-07-16 23:39:1518#include "extensions/common/extension_paths.h"
[email protected]701a94e2014-04-17 04:37:3719#include "extensions/renderer/logging_native_handler.h"
[email protected]f55c90ee62014-04-12 00:50:0320#include "extensions/renderer/object_backed_native_handler.h"
21#include "extensions/renderer/safe_builtins.h"
[email protected]701a94e2014-04-17 04:37:3722#include "extensions/renderer/utils_native_handler.h"
[email protected]11844fa2012-05-10 00:35:5923#include "ui/base/resource/resource_bundle.h"
[email protected]1164b862012-05-09 22:38:3724
[email protected]582f6e92014-07-16 23:39:1525namespace extensions {
[email protected]295890bd2013-06-15 10:52:4526namespace {
27
28class FailsOnException : public ModuleSystem::ExceptionHandler {
29 public:
dcheng9168b2f2014-10-21 12:38:2430 void HandleUncaughtException(const v8::TryCatch& try_catch) override {
[email protected]295890bd2013-06-15 10:52:4531 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch);
32 }
33};
34
35class V8ExtensionConfigurator {
36 public:
37 V8ExtensionConfigurator()
[email protected]582f6e92014-07-16 23:39:1538 : safe_builtins_(SafeBuiltins::CreateV8Extension()),
[email protected]5380451c2013-06-18 05:16:2539 names_(1, safe_builtins_->name()),
[email protected]582f6e92014-07-16 23:39:1540 configuration_(
41 new v8::ExtensionConfiguration(static_cast<int>(names_.size()),
42 vector_as_array(&names_))) {
[email protected]5380451c2013-06-18 05:16:2543 v8::RegisterExtension(safe_builtins_.get());
[email protected]295890bd2013-06-15 10:52:4544 }
45
[email protected]5380451c2013-06-18 05:16:2546 v8::ExtensionConfiguration* GetConfiguration() {
47 return configuration_.get();
[email protected]295890bd2013-06-15 10:52:4548 }
49
50 private:
[email protected]5380451c2013-06-18 05:16:2551 scoped_ptr<v8::Extension> safe_builtins_;
[email protected]295890bd2013-06-15 10:52:4552 std::vector<const char*> names_;
[email protected]5380451c2013-06-18 05:16:2553 scoped_ptr<v8::ExtensionConfiguration> configuration_;
[email protected]295890bd2013-06-15 10:52:4554};
55
[email protected]5380451c2013-06-18 05:16:2556base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator =
[email protected]295890bd2013-06-15 10:52:4557 LAZY_INSTANCE_INITIALIZER;
58
59} // namespace
60
[email protected]1164b862012-05-09 22:38:3761// Native JS functions for doing asserts.
[email protected]d9f51dad2014-07-09 05:39:3862class ModuleSystemTestEnvironment::AssertNatives
63 : public ObjectBackedNativeHandler {
[email protected]1164b862012-05-09 22:38:3764 public:
[email protected]582f6e92014-07-16 23:39:1565 explicit AssertNatives(ScriptContext* context)
[email protected]4f1633f2013-03-09 14:26:2466 : ObjectBackedNativeHandler(context),
[email protected]2e0e0bc2013-02-04 10:30:3467 assertion_made_(false),
[email protected]1164b862012-05-09 22:38:3768 failed_(false) {
[email protected]582f6e92014-07-16 23:39:1569 RouteFunction(
70 "AssertTrue",
71 base::Bind(&AssertNatives::AssertTrue, base::Unretained(this)));
72 RouteFunction(
73 "AssertFalse",
74 base::Bind(&AssertNatives::AssertFalse, base::Unretained(this)));
[email protected]1164b862012-05-09 22:38:3775 }
76
77 bool assertion_made() { return assertion_made_; }
78 bool failed() { return failed_; }
79
[email protected]d8c5fbb2013-06-14 11:35:2580 void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]1164b862012-05-09 22:38:3781 CHECK_EQ(1, args.Length());
82 assertion_made_ = true;
dcarney04cd9642014-11-21 13:58:1183 failed_ = failed_ || !args[0]->ToBoolean(args.GetIsolate())->Value();
[email protected]1164b862012-05-09 22:38:3784 }
85
[email protected]d8c5fbb2013-06-14 11:35:2586 void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]1164b862012-05-09 22:38:3787 CHECK_EQ(1, args.Length());
88 assertion_made_ = true;
dcarney04cd9642014-11-21 13:58:1189 failed_ = failed_ || args[0]->ToBoolean(args.GetIsolate())->Value();
[email protected]1164b862012-05-09 22:38:3790 }
91
92 private:
93 bool assertion_made_;
94 bool failed_;
95};
96
97// Source map that operates on std::strings.
[email protected]d9f51dad2014-07-09 05:39:3898class ModuleSystemTestEnvironment::StringSourceMap
[email protected]582f6e92014-07-16 23:39:1599 : public ModuleSystem::SourceMap {
[email protected]1164b862012-05-09 22:38:37100 public:
101 StringSourceMap() {}
dcheng9168b2f2014-10-21 12:38:24102 ~StringSourceMap() override {}
[email protected]1164b862012-05-09 22:38:37103
dcheng9168b2f2014-10-21 12:38:24104 v8::Handle<v8::Value> GetSource(v8::Isolate* isolate,
105 const std::string& name) override {
[email protected]1164b862012-05-09 22:38:37106 if (source_map_.count(name) == 0)
[email protected]6f59d3b2013-12-02 12:50:50107 return v8::Undefined(isolate);
108 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str());
[email protected]1164b862012-05-09 22:38:37109 }
110
dcheng9168b2f2014-10-21 12:38:24111 bool Contains(const std::string& name) override {
[email protected]1164b862012-05-09 22:38:37112 return source_map_.count(name);
113 }
114
115 void RegisterModule(const std::string& name, const std::string& source) {
[email protected]68e63ea12013-06-05 05:00:54116 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
[email protected]1164b862012-05-09 22:38:37117 source_map_[name] = source;
118 }
119
120 private:
121 std::map<std::string, std::string> source_map_;
122};
123
[email protected]99ea16b2014-07-17 22:15:56124ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(v8::Isolate* isolate)
125 : isolate_(isolate),
126 context_holder_(new gin::ContextHolder(isolate_)),
127 handle_scope_(isolate_),
[email protected]d9f51dad2014-07-09 05:39:38128 source_map_(new StringSourceMap()) {
[email protected]99ea16b2014-07-17 22:15:56129 context_holder_->SetContext(v8::Context::New(
130 isolate, g_v8_extension_configurator.Get().GetConfiguration()));
[email protected]582f6e92014-07-16 23:39:15131 context_.reset(new ScriptContext(context_holder_->context(),
132 NULL, // WebFrame
133 NULL, // Extension
mek7e1d7452014-09-08 23:55:57134 Feature::BLESSED_EXTENSION_CONTEXT,
135 NULL, // Effective Extension
[email protected]f80685c32014-07-26 19:48:04136 Feature::BLESSED_EXTENSION_CONTEXT));
[email protected]9a598442013-06-04 16:39:12137 context_->v8_context()->Enter();
[email protected]4f1633f2013-03-09 14:26:24138 assert_natives_ = new AssertNatives(context_.get());
[email protected]2a356872014-02-21 23:18:52139
140 {
141 scoped_ptr<ModuleSystem> module_system(
142 new ModuleSystem(context_.get(), source_map_.get()));
143 context_->set_module_system(module_system.Pass());
144 }
145 ModuleSystem* module_system = context_->module_system();
[email protected]582f6e92014-07-16 23:39:15146 module_system->RegisterNativeHandler(
147 "assert", scoped_ptr<NativeHandler>(assert_natives_));
148 module_system->RegisterNativeHandler(
149 "logging",
150 scoped_ptr<NativeHandler>(new LoggingNativeHandler(context_.get())));
151 module_system->RegisterNativeHandler(
152 "utils",
153 scoped_ptr<NativeHandler>(new UtilsNativeHandler(context_.get())));
[email protected]2a356872014-02-21 23:18:52154 module_system->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23155 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37156}
157
[email protected]d9f51dad2014-07-09 05:39:38158ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() {
sammcde54a47e2015-01-13 23:16:34159 if (context_->is_valid())
kalmanb0c1c502015-04-15 00:25:06160 ShutdownModuleSystem();
[email protected]1164b862012-05-09 22:38:37161}
162
[email protected]d9f51dad2014-07-09 05:39:38163void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
164 const std::string& code) {
[email protected]1164b862012-05-09 22:38:37165 source_map_->RegisterModule(name, code);
166}
167
[email protected]d9f51dad2014-07-09 05:39:38168void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
169 int resource_id) {
[email protected]582f6e92014-07-16 23:39:15170 const std::string& code = ResourceBundle::GetSharedInstance()
171 .GetRawDataResource(resource_id)
172 .as_string();
[email protected]11844fa2012-05-10 00:35:59173 source_map_->RegisterModule(name, code);
174}
175
[email protected]d9f51dad2014-07-09 05:39:38176void ModuleSystemTestEnvironment::OverrideNativeHandler(
177 const std::string& name,
178 const std::string& code) {
[email protected]11844fa2012-05-10 00:35:59179 RegisterModule(name, code);
[email protected]2a356872014-02-21 23:18:52180 context_->module_system()->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59181}
182
[email protected]d9f51dad2014-07-09 05:39:38183void ModuleSystemTestEnvironment::RegisterTestFile(
184 const std::string& module_name,
185 const std::string& file_name) {
[email protected]f8d87d32013-06-06 02:51:29186 base::FilePath test_js_file_path;
[email protected]582f6e92014-07-16 23:39:15187 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &test_js_file_path));
188 test_js_file_path = test_js_file_path.AppendASCII(file_name);
[email protected]f8d87d32013-06-06 02:51:29189 std::string test_js;
[email protected]82f84b92013-08-30 18:23:50190 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
[email protected]f8d87d32013-06-06 02:51:29191 source_map_->RegisterModule(module_name, test_js);
192}
193
[email protected]d9f51dad2014-07-09 05:39:38194void ModuleSystemTestEnvironment::ShutdownGin() {
195 context_holder_.reset();
196}
197
198void ModuleSystemTestEnvironment::ShutdownModuleSystem() {
kalmanb0c1c502015-04-15 00:25:06199 CHECK(context_->is_valid());
[email protected]d9f51dad2014-07-09 05:39:38200 context_->v8_context()->Exit();
sammcde54a47e2015-01-13 23:16:34201 context_->Invalidate();
[email protected]d9f51dad2014-07-09 05:39:38202}
203
204v8::Handle<v8::Object> ModuleSystemTestEnvironment::CreateGlobal(
205 const std::string& name) {
[email protected]99ea16b2014-07-17 22:15:56206 v8::EscapableHandleScope handle_scope(isolate_);
207 v8::Local<v8::Object> object = v8::Object::New(isolate_);
208 isolate_->GetCurrentContext()->Global()->Set(
209 v8::String::NewFromUtf8(isolate_, name.c_str()), object);
[email protected]d9f51dad2014-07-09 05:39:38210 return handle_scope.Escape(object);
211}
212
213ModuleSystemTest::ModuleSystemTest()
[email protected]99ea16b2014-07-17 22:15:56214 : isolate_(v8::Isolate::GetCurrent()),
[email protected]d9f51dad2014-07-09 05:39:38215 should_assertions_be_made_(true) {
216}
217
218ModuleSystemTest::~ModuleSystemTest() {
219}
220
sammc32a6fc7d2014-09-15 02:47:31221void ModuleSystemTest::SetUp() {
222 env_ = CreateEnvironment();
223}
224
[email protected]1164b862012-05-09 22:38:37225void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37226 // All tests must assert at least once unless otherwise specified.
227 EXPECT_EQ(should_assertions_be_made_,
[email protected]d9f51dad2014-07-09 05:39:38228 env_->assert_natives()->assertion_made());
229 EXPECT_FALSE(env_->assert_natives()->failed());
sammc32a6fc7d2014-09-15 02:47:31230 env_.reset();
231 v8::HeapStatistics stats;
232 isolate_->GetHeapStatistics(&stats);
233 size_t old_heap_size = 0;
234 // Run the GC until the heap size reaches a steady state to ensure that
235 // all the garbage is collected.
236 while (stats.used_heap_size() != old_heap_size) {
237 old_heap_size = stats.used_heap_size();
238 isolate_->RequestGarbageCollectionForTesting(
239 v8::Isolate::kFullGarbageCollection);
240 isolate_->GetHeapStatistics(&stats);
241 }
[email protected]d9f51dad2014-07-09 05:39:38242}
243
244scoped_ptr<ModuleSystemTestEnvironment> ModuleSystemTest::CreateEnvironment() {
[email protected]99ea16b2014-07-17 22:15:56245 return make_scoped_ptr(new ModuleSystemTestEnvironment(isolate_));
[email protected]1164b862012-05-09 22:38:37246}
247
248void ModuleSystemTest::ExpectNoAssertionsMade() {
249 should_assertions_be_made_ = false;
250}
251
[email protected]d9f51dad2014-07-09 05:39:38252void ModuleSystemTest::RunResolvedPromises() {
[email protected]99ea16b2014-07-17 22:15:56253 isolate_->RunMicrotasks();
[email protected]1164b862012-05-09 22:38:37254}
[email protected]582f6e92014-07-16 23:39:15255
256} // namespace extensions