blob: 72f1dbb55531c60fda1b973b80993d70feeb8690 [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/file_util.h"
12#include "base/files/file_path.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:
30 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE {
31 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;
83 failed_ = failed_ || !args[0]->ToBoolean()->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;
89 failed_ = failed_ || args[0]->ToBoolean()->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() {}
102 virtual ~StringSourceMap() {}
103
[email protected]6f59d3b2013-12-02 12:50:50104 virtual 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
[email protected]be9826e62013-02-07 02:00:58111 virtual 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
134 Feature::UNSPECIFIED_CONTEXT));
[email protected]9a598442013-06-04 16:39:12135 context_->v8_context()->Enter();
[email protected]4f1633f2013-03-09 14:26:24136 assert_natives_ = new AssertNatives(context_.get());
[email protected]2a356872014-02-21 23:18:52137
138 {
139 scoped_ptr<ModuleSystem> module_system(
140 new ModuleSystem(context_.get(), source_map_.get()));
141 context_->set_module_system(module_system.Pass());
142 }
143 ModuleSystem* module_system = context_->module_system();
[email protected]582f6e92014-07-16 23:39:15144 module_system->RegisterNativeHandler(
145 "assert", scoped_ptr<NativeHandler>(assert_natives_));
146 module_system->RegisterNativeHandler(
147 "logging",
148 scoped_ptr<NativeHandler>(new LoggingNativeHandler(context_.get())));
149 module_system->RegisterNativeHandler(
150 "utils",
151 scoped_ptr<NativeHandler>(new UtilsNativeHandler(context_.get())));
[email protected]2a356872014-02-21 23:18:52152 module_system->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23153 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37154}
155
[email protected]d9f51dad2014-07-09 05:39:38156ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() {
157 if (context_)
158 context_->v8_context()->Exit();
[email protected]1164b862012-05-09 22:38:37159}
160
[email protected]d9f51dad2014-07-09 05:39:38161void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
162 const std::string& code) {
[email protected]1164b862012-05-09 22:38:37163 source_map_->RegisterModule(name, code);
164}
165
[email protected]d9f51dad2014-07-09 05:39:38166void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
167 int resource_id) {
[email protected]582f6e92014-07-16 23:39:15168 const std::string& code = ResourceBundle::GetSharedInstance()
169 .GetRawDataResource(resource_id)
170 .as_string();
[email protected]11844fa2012-05-10 00:35:59171 source_map_->RegisterModule(name, code);
172}
173
[email protected]d9f51dad2014-07-09 05:39:38174void ModuleSystemTestEnvironment::OverrideNativeHandler(
175 const std::string& name,
176 const std::string& code) {
[email protected]11844fa2012-05-10 00:35:59177 RegisterModule(name, code);
[email protected]2a356872014-02-21 23:18:52178 context_->module_system()->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59179}
180
[email protected]d9f51dad2014-07-09 05:39:38181void ModuleSystemTestEnvironment::RegisterTestFile(
182 const std::string& module_name,
183 const std::string& file_name) {
[email protected]f8d87d32013-06-06 02:51:29184 base::FilePath test_js_file_path;
[email protected]582f6e92014-07-16 23:39:15185 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &test_js_file_path));
186 test_js_file_path = test_js_file_path.AppendASCII(file_name);
[email protected]f8d87d32013-06-06 02:51:29187 std::string test_js;
[email protected]82f84b92013-08-30 18:23:50188 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
[email protected]f8d87d32013-06-06 02:51:29189 source_map_->RegisterModule(module_name, test_js);
190}
191
[email protected]d9f51dad2014-07-09 05:39:38192void ModuleSystemTestEnvironment::ShutdownGin() {
193 context_holder_.reset();
194}
195
196void ModuleSystemTestEnvironment::ShutdownModuleSystem() {
197 context_->v8_context()->Exit();
198 context_.reset();
199}
200
201v8::Handle<v8::Object> ModuleSystemTestEnvironment::CreateGlobal(
202 const std::string& name) {
[email protected]99ea16b2014-07-17 22:15:56203 v8::EscapableHandleScope handle_scope(isolate_);
204 v8::Local<v8::Object> object = v8::Object::New(isolate_);
205 isolate_->GetCurrentContext()->Global()->Set(
206 v8::String::NewFromUtf8(isolate_, name.c_str()), object);
[email protected]d9f51dad2014-07-09 05:39:38207 return handle_scope.Escape(object);
208}
209
210ModuleSystemTest::ModuleSystemTest()
[email protected]99ea16b2014-07-17 22:15:56211 : isolate_(v8::Isolate::GetCurrent()),
[email protected]d9f51dad2014-07-09 05:39:38212 env_(CreateEnvironment()),
213 should_assertions_be_made_(true) {
214}
215
216ModuleSystemTest::~ModuleSystemTest() {
217}
218
[email protected]1164b862012-05-09 22:38:37219void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37220 // All tests must assert at least once unless otherwise specified.
221 EXPECT_EQ(should_assertions_be_made_,
[email protected]d9f51dad2014-07-09 05:39:38222 env_->assert_natives()->assertion_made());
223 EXPECT_FALSE(env_->assert_natives()->failed());
224}
225
226scoped_ptr<ModuleSystemTestEnvironment> ModuleSystemTest::CreateEnvironment() {
[email protected]99ea16b2014-07-17 22:15:56227 return make_scoped_ptr(new ModuleSystemTestEnvironment(isolate_));
[email protected]1164b862012-05-09 22:38:37228}
229
230void ModuleSystemTest::ExpectNoAssertionsMade() {
231 should_assertions_be_made_ = false;
232}
233
[email protected]d9f51dad2014-07-09 05:39:38234void ModuleSystemTest::RunResolvedPromises() {
[email protected]99ea16b2014-07-17 22:15:56235 isolate_->RunMicrotasks();
[email protected]1164b862012-05-09 22:38:37236}
[email protected]582f6e92014-07-16 23:39:15237
238} // namespace extensions