Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / renderer / module_system_test.cc
blob4f4675e625610be86972476ac2dca54fd4e68d07
1 // Copyright 2014 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.
5 #include "extensions/renderer/module_system_test.h"
7 #include <map>
8 #include <string>
10 #include "base/callback.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16 #include "base/stl_util.h"
17 #include "base/strings/string_piece.h"
18 #include "extensions/common/extension_paths.h"
19 #include "extensions/renderer/logging_native_handler.h"
20 #include "extensions/renderer/object_backed_native_handler.h"
21 #include "extensions/renderer/safe_builtins.h"
22 #include "extensions/renderer/utils_native_handler.h"
23 #include "ui/base/resource/resource_bundle.h"
25 namespace extensions {
26 namespace {
28 class FailsOnException : public ModuleSystem::ExceptionHandler {
29 public:
30 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE {
31 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch);
35 class V8ExtensionConfigurator {
36 public:
37 V8ExtensionConfigurator()
38 : safe_builtins_(SafeBuiltins::CreateV8Extension()),
39 names_(1, safe_builtins_->name()),
40 configuration_(
41 new v8::ExtensionConfiguration(static_cast<int>(names_.size()),
42 vector_as_array(&names_))) {
43 v8::RegisterExtension(safe_builtins_.get());
46 v8::ExtensionConfiguration* GetConfiguration() {
47 return configuration_.get();
50 private:
51 scoped_ptr<v8::Extension> safe_builtins_;
52 std::vector<const char*> names_;
53 scoped_ptr<v8::ExtensionConfiguration> configuration_;
56 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator =
57 LAZY_INSTANCE_INITIALIZER;
59 } // namespace
61 // Native JS functions for doing asserts.
62 class ModuleSystemTestEnvironment::AssertNatives
63 : public ObjectBackedNativeHandler {
64 public:
65 explicit AssertNatives(ScriptContext* context)
66 : ObjectBackedNativeHandler(context),
67 assertion_made_(false),
68 failed_(false) {
69 RouteFunction(
70 "AssertTrue",
71 base::Bind(&AssertNatives::AssertTrue, base::Unretained(this)));
72 RouteFunction(
73 "AssertFalse",
74 base::Bind(&AssertNatives::AssertFalse, base::Unretained(this)));
77 bool assertion_made() { return assertion_made_; }
78 bool failed() { return failed_; }
80 void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) {
81 CHECK_EQ(1, args.Length());
82 assertion_made_ = true;
83 failed_ = failed_ || !args[0]->ToBoolean()->Value();
86 void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) {
87 CHECK_EQ(1, args.Length());
88 assertion_made_ = true;
89 failed_ = failed_ || args[0]->ToBoolean()->Value();
92 private:
93 bool assertion_made_;
94 bool failed_;
97 // Source map that operates on std::strings.
98 class ModuleSystemTestEnvironment::StringSourceMap
99 : public ModuleSystem::SourceMap {
100 public:
101 StringSourceMap() {}
102 virtual ~StringSourceMap() {}
104 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate,
105 const std::string& name) OVERRIDE {
106 if (source_map_.count(name) == 0)
107 return v8::Undefined(isolate);
108 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str());
111 virtual bool Contains(const std::string& name) OVERRIDE {
112 return source_map_.count(name);
115 void RegisterModule(const std::string& name, const std::string& source) {
116 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
117 source_map_[name] = source;
120 private:
121 std::map<std::string, std::string> source_map_;
124 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(v8::Isolate* isolate)
125 : isolate_(isolate),
126 context_holder_(new gin::ContextHolder(isolate_)),
127 handle_scope_(isolate_),
128 source_map_(new StringSourceMap()) {
129 context_holder_->SetContext(v8::Context::New(
130 isolate, g_v8_extension_configurator.Get().GetConfiguration()));
131 context_.reset(new ScriptContext(context_holder_->context(),
132 NULL, // WebFrame
133 NULL, // Extension
134 Feature::BLESSED_EXTENSION_CONTEXT));
135 context_->v8_context()->Enter();
136 assert_natives_ = new AssertNatives(context_.get());
139 scoped_ptr<ModuleSystem> module_system(
140 new ModuleSystem(context_.get(), source_map_.get()));
141 context_->set_module_system(module_system.Pass());
143 ModuleSystem* module_system = context_->module_system();
144 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())));
152 module_system->SetExceptionHandlerForTest(
153 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
156 ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() {
157 if (context_)
158 context_->v8_context()->Exit();
161 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
162 const std::string& code) {
163 source_map_->RegisterModule(name, code);
166 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
167 int resource_id) {
168 const std::string& code = ResourceBundle::GetSharedInstance()
169 .GetRawDataResource(resource_id)
170 .as_string();
171 source_map_->RegisterModule(name, code);
174 void ModuleSystemTestEnvironment::OverrideNativeHandler(
175 const std::string& name,
176 const std::string& code) {
177 RegisterModule(name, code);
178 context_->module_system()->OverrideNativeHandlerForTest(name);
181 void ModuleSystemTestEnvironment::RegisterTestFile(
182 const std::string& module_name,
183 const std::string& file_name) {
184 base::FilePath test_js_file_path;
185 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &test_js_file_path));
186 test_js_file_path = test_js_file_path.AppendASCII(file_name);
187 std::string test_js;
188 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
189 source_map_->RegisterModule(module_name, test_js);
192 void ModuleSystemTestEnvironment::ShutdownGin() {
193 context_holder_.reset();
196 void ModuleSystemTestEnvironment::ShutdownModuleSystem() {
197 context_->v8_context()->Exit();
198 context_.reset();
201 v8::Handle<v8::Object> ModuleSystemTestEnvironment::CreateGlobal(
202 const std::string& name) {
203 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);
207 return handle_scope.Escape(object);
210 ModuleSystemTest::ModuleSystemTest()
211 : isolate_(v8::Isolate::GetCurrent()),
212 env_(CreateEnvironment()),
213 should_assertions_be_made_(true) {
216 ModuleSystemTest::~ModuleSystemTest() {
219 void ModuleSystemTest::TearDown() {
220 // All tests must assert at least once unless otherwise specified.
221 EXPECT_EQ(should_assertions_be_made_,
222 env_->assert_natives()->assertion_made());
223 EXPECT_FALSE(env_->assert_natives()->failed());
226 scoped_ptr<ModuleSystemTestEnvironment> ModuleSystemTest::CreateEnvironment() {
227 return make_scoped_ptr(new ModuleSystemTestEnvironment(isolate_));
230 void ModuleSystemTest::ExpectNoAssertionsMade() {
231 should_assertions_be_made_ = false;
234 void ModuleSystemTest::RunResolvedPromises() {
235 isolate_->RunMicrotasks();
238 } // namespace extensions