blob: 0db5495e06a9eb23963a98c17c7236644eecb5b2 [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>
dchenge59eca1602015-12-18 17:48:009#include <utility>
[email protected]1164b862012-05-09 22:38:3710
11#include "base/callback.h"
[email protected]f8d87d32013-06-06 02:51:2912#include "base/files/file_path.h"
thestig94712702014-09-10 07:46:5913#include "base/files/file_util.h"
[email protected]295890bd2013-06-15 10:52:4514#include "base/lazy_instance.h"
[email protected]1164b862012-05-09 22:38:3715#include "base/memory/scoped_ptr.h"
[email protected]f8d87d32013-06-06 02:51:2916#include "base/path_service.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"
kalman33076cb2015-08-11 19:12:0721#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:
bashi6a4854f2015-06-19 00:51:5130 FailsOnException() : ModuleSystem::ExceptionHandler(nullptr) {}
dcheng9168b2f2014-10-21 12:38:2431 void HandleUncaughtException(const v8::TryCatch& try_catch) override {
[email protected]295890bd2013-06-15 10:52:4532 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch);
33 }
34};
35
kalman33076cb2015-08-11 19:12:0736class V8ExtensionConfigurator {
37 public:
38 V8ExtensionConfigurator()
39 : safe_builtins_(SafeBuiltins::CreateV8Extension()),
40 names_(1, safe_builtins_->name()),
41 configuration_(
42 new v8::ExtensionConfiguration(static_cast<int>(names_.size()),
davidbenb50f00c2015-12-01 00:01:5043 names_.data())) {
kalman33076cb2015-08-11 19:12:0744 v8::RegisterExtension(safe_builtins_.get());
45 }
46
47 v8::ExtensionConfiguration* GetConfiguration() {
48 return configuration_.get();
49 }
50
51 private:
52 scoped_ptr<v8::Extension> safe_builtins_;
53 std::vector<const char*> names_;
54 scoped_ptr<v8::ExtensionConfiguration> configuration_;
55};
56
57base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator =
58 LAZY_INSTANCE_INITIALIZER;
59
[email protected]295890bd2013-06-15 10:52:4560} // namespace
61
[email protected]1164b862012-05-09 22:38:3762// Native JS functions for doing asserts.
[email protected]d9f51dad2014-07-09 05:39:3863class ModuleSystemTestEnvironment::AssertNatives
64 : public ObjectBackedNativeHandler {
[email protected]1164b862012-05-09 22:38:3765 public:
[email protected]582f6e92014-07-16 23:39:1566 explicit AssertNatives(ScriptContext* context)
[email protected]4f1633f2013-03-09 14:26:2467 : ObjectBackedNativeHandler(context),
[email protected]2e0e0bc2013-02-04 10:30:3468 assertion_made_(false),
[email protected]1164b862012-05-09 22:38:3769 failed_(false) {
[email protected]582f6e92014-07-16 23:39:1570 RouteFunction(
71 "AssertTrue",
72 base::Bind(&AssertNatives::AssertTrue, base::Unretained(this)));
73 RouteFunction(
74 "AssertFalse",
75 base::Bind(&AssertNatives::AssertFalse, base::Unretained(this)));
[email protected]1164b862012-05-09 22:38:3776 }
77
78 bool assertion_made() { return assertion_made_; }
79 bool failed() { return failed_; }
80
[email protected]d8c5fbb2013-06-14 11:35:2581 void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]1164b862012-05-09 22:38:3782 CHECK_EQ(1, args.Length());
83 assertion_made_ = true;
dcarney04cd9642014-11-21 13:58:1184 failed_ = failed_ || !args[0]->ToBoolean(args.GetIsolate())->Value();
[email protected]1164b862012-05-09 22:38:3785 }
86
[email protected]d8c5fbb2013-06-14 11:35:2587 void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]1164b862012-05-09 22:38:3788 CHECK_EQ(1, args.Length());
89 assertion_made_ = true;
dcarney04cd9642014-11-21 13:58:1190 failed_ = failed_ || args[0]->ToBoolean(args.GetIsolate())->Value();
[email protected]1164b862012-05-09 22:38:3791 }
92
93 private:
94 bool assertion_made_;
95 bool failed_;
96};
97
98// Source map that operates on std::strings.
[email protected]d9f51dad2014-07-09 05:39:3899class ModuleSystemTestEnvironment::StringSourceMap
[email protected]582f6e92014-07-16 23:39:15100 : public ModuleSystem::SourceMap {
[email protected]1164b862012-05-09 22:38:37101 public:
102 StringSourceMap() {}
dcheng9168b2f2014-10-21 12:38:24103 ~StringSourceMap() override {}
[email protected]1164b862012-05-09 22:38:37104
tfarinaf85316f2015-04-29 17:03:40105 v8::Local<v8::Value> GetSource(v8::Isolate* isolate,
106 const std::string& name) override {
[email protected]1164b862012-05-09 22:38:37107 if (source_map_.count(name) == 0)
[email protected]6f59d3b2013-12-02 12:50:50108 return v8::Undefined(isolate);
109 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str());
[email protected]1164b862012-05-09 22:38:37110 }
111
dcheng9168b2f2014-10-21 12:38:24112 bool Contains(const std::string& name) override {
[email protected]1164b862012-05-09 22:38:37113 return source_map_.count(name);
114 }
115
116 void RegisterModule(const std::string& name, const std::string& source) {
[email protected]68e63ea12013-06-05 05:00:54117 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
[email protected]1164b862012-05-09 22:38:37118 source_map_[name] = source;
119 }
120
121 private:
122 std::map<std::string, std::string> source_map_;
123};
124
[email protected]99ea16b2014-07-17 22:15:56125ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(v8::Isolate* isolate)
126 : isolate_(isolate),
127 context_holder_(new gin::ContextHolder(isolate_)),
128 handle_scope_(isolate_),
[email protected]d9f51dad2014-07-09 05:39:38129 source_map_(new StringSourceMap()) {
kalman33076cb2015-08-11 19:12:07130 context_holder_->SetContext(v8::Context::New(
131 isolate, g_v8_extension_configurator.Get().GetConfiguration()));
[email protected]582f6e92014-07-16 23:39:15132 context_.reset(new ScriptContext(context_holder_->context(),
bashi6a4854f2015-06-19 00:51:51133 nullptr, // WebFrame
134 nullptr, // Extension
mek7e1d7452014-09-08 23:55:57135 Feature::BLESSED_EXTENSION_CONTEXT,
bashi6a4854f2015-06-19 00:51:51136 nullptr, // Effective Extension
[email protected]f80685c32014-07-26 19:48:04137 Feature::BLESSED_EXTENSION_CONTEXT));
[email protected]9a598442013-06-04 16:39:12138 context_->v8_context()->Enter();
[email protected]4f1633f2013-03-09 14:26:24139 assert_natives_ = new AssertNatives(context_.get());
[email protected]2a356872014-02-21 23:18:52140
141 {
142 scoped_ptr<ModuleSystem> module_system(
143 new ModuleSystem(context_.get(), source_map_.get()));
dchenge59eca1602015-12-18 17:48:00144 context_->set_module_system(std::move(module_system));
[email protected]2a356872014-02-21 23:18:52145 }
146 ModuleSystem* module_system = context_->module_system();
[email protected]582f6e92014-07-16 23:39:15147 module_system->RegisterNativeHandler(
148 "assert", scoped_ptr<NativeHandler>(assert_natives_));
149 module_system->RegisterNativeHandler(
150 "logging",
151 scoped_ptr<NativeHandler>(new LoggingNativeHandler(context_.get())));
152 module_system->RegisterNativeHandler(
153 "utils",
154 scoped_ptr<NativeHandler>(new UtilsNativeHandler(context_.get())));
[email protected]2a356872014-02-21 23:18:52155 module_system->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23156 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37157}
158
[email protected]d9f51dad2014-07-09 05:39:38159ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() {
sammcde54a47e2015-01-13 23:16:34160 if (context_->is_valid())
kalmanb0c1c502015-04-15 00:25:06161 ShutdownModuleSystem();
[email protected]1164b862012-05-09 22:38:37162}
163
[email protected]d9f51dad2014-07-09 05:39:38164void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
165 const std::string& code) {
[email protected]1164b862012-05-09 22:38:37166 source_map_->RegisterModule(name, code);
167}
168
[email protected]d9f51dad2014-07-09 05:39:38169void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
170 int resource_id) {
[email protected]582f6e92014-07-16 23:39:15171 const std::string& code = ResourceBundle::GetSharedInstance()
172 .GetRawDataResource(resource_id)
173 .as_string();
[email protected]11844fa2012-05-10 00:35:59174 source_map_->RegisterModule(name, code);
175}
176
[email protected]d9f51dad2014-07-09 05:39:38177void ModuleSystemTestEnvironment::OverrideNativeHandler(
178 const std::string& name,
179 const std::string& code) {
[email protected]11844fa2012-05-10 00:35:59180 RegisterModule(name, code);
[email protected]2a356872014-02-21 23:18:52181 context_->module_system()->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59182}
183
[email protected]d9f51dad2014-07-09 05:39:38184void ModuleSystemTestEnvironment::RegisterTestFile(
185 const std::string& module_name,
186 const std::string& file_name) {
[email protected]f8d87d32013-06-06 02:51:29187 base::FilePath test_js_file_path;
[email protected]582f6e92014-07-16 23:39:15188 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &test_js_file_path));
189 test_js_file_path = test_js_file_path.AppendASCII(file_name);
[email protected]f8d87d32013-06-06 02:51:29190 std::string test_js;
[email protected]82f84b92013-08-30 18:23:50191 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
[email protected]f8d87d32013-06-06 02:51:29192 source_map_->RegisterModule(module_name, test_js);
193}
194
[email protected]d9f51dad2014-07-09 05:39:38195void ModuleSystemTestEnvironment::ShutdownGin() {
196 context_holder_.reset();
197}
198
199void ModuleSystemTestEnvironment::ShutdownModuleSystem() {
kalmanb0c1c502015-04-15 00:25:06200 CHECK(context_->is_valid());
[email protected]d9f51dad2014-07-09 05:39:38201 context_->v8_context()->Exit();
sammcde54a47e2015-01-13 23:16:34202 context_->Invalidate();
[email protected]d9f51dad2014-07-09 05:39:38203}
204
tfarinaf85316f2015-04-29 17:03:40205v8::Local<v8::Object> ModuleSystemTestEnvironment::CreateGlobal(
[email protected]d9f51dad2014-07-09 05:39:38206 const std::string& name) {
[email protected]99ea16b2014-07-17 22:15:56207 v8::EscapableHandleScope handle_scope(isolate_);
208 v8::Local<v8::Object> object = v8::Object::New(isolate_);
209 isolate_->GetCurrentContext()->Global()->Set(
210 v8::String::NewFromUtf8(isolate_, name.c_str()), object);
[email protected]d9f51dad2014-07-09 05:39:38211 return handle_scope.Escape(object);
212}
213
214ModuleSystemTest::ModuleSystemTest()
[email protected]99ea16b2014-07-17 22:15:56215 : isolate_(v8::Isolate::GetCurrent()),
[email protected]d9f51dad2014-07-09 05:39:38216 should_assertions_be_made_(true) {
217}
218
219ModuleSystemTest::~ModuleSystemTest() {
220}
221
sammc32a6fc7d2014-09-15 02:47:31222void ModuleSystemTest::SetUp() {
223 env_ = CreateEnvironment();
224}
225
[email protected]1164b862012-05-09 22:38:37226void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37227 // All tests must assert at least once unless otherwise specified.
228 EXPECT_EQ(should_assertions_be_made_,
[email protected]d9f51dad2014-07-09 05:39:38229 env_->assert_natives()->assertion_made());
230 EXPECT_FALSE(env_->assert_natives()->failed());
sammc32a6fc7d2014-09-15 02:47:31231 env_.reset();
232 v8::HeapStatistics stats;
233 isolate_->GetHeapStatistics(&stats);
234 size_t old_heap_size = 0;
235 // Run the GC until the heap size reaches a steady state to ensure that
236 // all the garbage is collected.
237 while (stats.used_heap_size() != old_heap_size) {
238 old_heap_size = stats.used_heap_size();
239 isolate_->RequestGarbageCollectionForTesting(
240 v8::Isolate::kFullGarbageCollection);
241 isolate_->GetHeapStatistics(&stats);
242 }
[email protected]d9f51dad2014-07-09 05:39:38243}
244
245scoped_ptr<ModuleSystemTestEnvironment> ModuleSystemTest::CreateEnvironment() {
[email protected]99ea16b2014-07-17 22:15:56246 return make_scoped_ptr(new ModuleSystemTestEnvironment(isolate_));
[email protected]1164b862012-05-09 22:38:37247}
248
249void ModuleSystemTest::ExpectNoAssertionsMade() {
250 should_assertions_be_made_ = false;
251}
252
[email protected]d9f51dad2014-07-09 05:39:38253void ModuleSystemTest::RunResolvedPromises() {
[email protected]99ea16b2014-07-17 22:15:56254 isolate_->RunMicrotasks();
[email protected]1164b862012-05-09 22:38:37255}
[email protected]582f6e92014-07-16 23:39:15256
257} // namespace extensions