blob: 69976bae1731919789d59539a33261a0dd6fc80f [file] [log] [blame]
[email protected]1164b862012-05-09 22:38:371// 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]f8d87d32013-06-06 02:51:298#include "base/file_util.h"
9#include "base/files/file_path.h"
[email protected]295890bd2013-06-15 10:52:4510#include "base/lazy_instance.h"
[email protected]1164b862012-05-09 22:38:3711#include "base/memory/scoped_ptr.h"
[email protected]f8d87d32013-06-06 02:51:2912#include "base/path_service.h"
[email protected]295890bd2013-06-15 10:52:4513#include "base/stl_util.h"
[email protected]4570a252013-03-31 00:35:4314#include "base/strings/string_piece.h"
[email protected]f8d87d32013-06-06 02:51:2915#include "chrome/common/chrome_paths.h"
[email protected]68e63ea12013-06-05 05:00:5416#include "chrome/renderer/extensions/chrome_v8_context.h"
[email protected]f8d87d32013-06-06 02:51:2917#include "chrome/renderer/extensions/logging_native_handler.h"
[email protected]2a356872014-02-21 23:18:5218#include "chrome/renderer/extensions/utils_native_handler.h"
[email protected]f55c90ee62014-04-12 00:50:0319#include "extensions/renderer/object_backed_native_handler.h"
20#include "extensions/renderer/safe_builtins.h"
[email protected]11844fa2012-05-10 00:35:5921#include "ui/base/resource/resource_bundle.h"
[email protected]1164b862012-05-09 22:38:3722
23#include <map>
24#include <string>
25
[email protected]3c6babd2012-08-28 03:17:2926using extensions::ModuleSystem;
27using extensions::NativeHandler;
[email protected]4f1633f2013-03-09 14:26:2428using extensions::ObjectBackedNativeHandler;
[email protected]3c6babd2012-08-28 03:17:2929
[email protected]295890bd2013-06-15 10:52:4530namespace {
31
32class FailsOnException : public ModuleSystem::ExceptionHandler {
33 public:
34 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE {
35 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch);
36 }
37};
38
39class V8ExtensionConfigurator {
40 public:
41 V8ExtensionConfigurator()
[email protected]5380451c2013-06-18 05:16:2542 : safe_builtins_(extensions::SafeBuiltins::CreateV8Extension()),
43 names_(1, safe_builtins_->name()),
44 configuration_(new v8::ExtensionConfiguration(
45 names_.size(), vector_as_array(&names_))) {
46 v8::RegisterExtension(safe_builtins_.get());
[email protected]295890bd2013-06-15 10:52:4547 }
48
[email protected]5380451c2013-06-18 05:16:2549 v8::ExtensionConfiguration* GetConfiguration() {
50 return configuration_.get();
[email protected]295890bd2013-06-15 10:52:4551 }
52
53 private:
[email protected]5380451c2013-06-18 05:16:2554 scoped_ptr<v8::Extension> safe_builtins_;
[email protected]295890bd2013-06-15 10:52:4555 std::vector<const char*> names_;
[email protected]5380451c2013-06-18 05:16:2556 scoped_ptr<v8::ExtensionConfiguration> configuration_;
[email protected]295890bd2013-06-15 10:52:4557};
58
[email protected]5380451c2013-06-18 05:16:2559base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator =
[email protected]295890bd2013-06-15 10:52:4560 LAZY_INSTANCE_INITIALIZER;
61
62} // namespace
63
[email protected]1164b862012-05-09 22:38:3764// Native JS functions for doing asserts.
[email protected]295890bd2013-06-15 10:52:4565class ModuleSystemTest::AssertNatives : public ObjectBackedNativeHandler {
[email protected]1164b862012-05-09 22:38:3766 public:
[email protected]9a598442013-06-04 16:39:1267 explicit AssertNatives(extensions::ChromeV8Context* context)
[email protected]4f1633f2013-03-09 14:26:2468 : ObjectBackedNativeHandler(context),
[email protected]2e0e0bc2013-02-04 10:30:3469 assertion_made_(false),
[email protected]1164b862012-05-09 22:38:3770 failed_(false) {
71 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue,
72 base::Unretained(this)));
73 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse,
74 base::Unretained(this)));
75 }
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]295890bd2013-06-15 10:52:4598class ModuleSystemTest::StringSourceMap : public ModuleSystem::SourceMap {
[email protected]1164b862012-05-09 22:38:3799 public:
100 StringSourceMap() {}
101 virtual ~StringSourceMap() {}
102
[email protected]6f59d3b2013-12-02 12:50:50103 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate,
104 const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:37105 if (source_map_.count(name) == 0)
[email protected]6f59d3b2013-12-02 12:50:50106 return v8::Undefined(isolate);
107 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str());
[email protected]1164b862012-05-09 22:38:37108 }
109
[email protected]be9826e62013-02-07 02:00:58110 virtual bool Contains(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:37111 return source_map_.count(name);
112 }
113
114 void RegisterModule(const std::string& name, const std::string& source) {
[email protected]68e63ea12013-06-05 05:00:54115 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
[email protected]1164b862012-05-09 22:38:37116 source_map_[name] = source;
117 }
118
119 private:
120 std::map<std::string, std::string> source_map_;
121};
122
123ModuleSystemTest::ModuleSystemTest()
[email protected]48002af2013-05-08 23:06:24124 : isolate_(v8::Isolate::GetCurrent()),
125 handle_scope_(isolate_),
[email protected]9a598442013-06-04 16:39:12126 context_(
127 new extensions::ChromeV8Context(
[email protected]295890bd2013-06-15 10:52:45128 v8::Context::New(
129 isolate_,
[email protected]5380451c2013-06-18 05:16:25130 g_v8_extension_configurator.Get().GetConfiguration()),
[email protected]68e63ea12013-06-05 05:00:54131 NULL, // WebFrame
132 NULL, // Extension
[email protected]9a598442013-06-04 16:39:12133 extensions::Feature::UNSPECIFIED_CONTEXT)),
[email protected]1164b862012-05-09 22:38:37134 source_map_(new StringSourceMap()),
135 should_assertions_be_made_(true) {
[email protected]9a598442013-06-04 16:39:12136 context_->v8_context()->Enter();
[email protected]4f1633f2013-03-09 14:26:24137 assert_natives_ = new AssertNatives(context_.get());
[email protected]2a356872014-02-21 23:18:52138
139 {
140 scoped_ptr<ModuleSystem> module_system(
141 new ModuleSystem(context_.get(), source_map_.get()));
142 context_->set_module_system(module_system.Pass());
143 }
144 ModuleSystem* module_system = context_->module_system();
145 module_system->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
[email protected]1164b862012-05-09 22:38:37146 assert_natives_));
[email protected]2a356872014-02-21 23:18:52147 module_system->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>(
[email protected]f8d87d32013-06-06 02:51:29148 new extensions::LoggingNativeHandler(context_.get())));
[email protected]2a356872014-02-21 23:18:52149 module_system->RegisterNativeHandler("utils", scoped_ptr<NativeHandler>(
150 new extensions::UtilsNativeHandler(context_.get())));
151 module_system->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23152 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37153}
154
155ModuleSystemTest::~ModuleSystemTest() {
[email protected]9a598442013-06-04 16:39:12156 context_->v8_context()->Exit();
[email protected]1164b862012-05-09 22:38:37157}
158
159void ModuleSystemTest::RegisterModule(const std::string& name,
160 const std::string& code) {
161 source_map_->RegisterModule(name, code);
162}
163
[email protected]11844fa2012-05-10 00:35:59164void ModuleSystemTest::RegisterModule(const std::string& name,
165 int resource_id) {
166 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]4d8bb1a92012-11-01 21:12:40167 GetRawDataResource(resource_id).as_string();
[email protected]11844fa2012-05-10 00:35:59168 source_map_->RegisterModule(name, code);
169}
170
171void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
172 const std::string& code) {
173 RegisterModule(name, code);
[email protected]2a356872014-02-21 23:18:52174 context_->module_system()->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59175}
176
[email protected]f8d87d32013-06-06 02:51:29177void ModuleSystemTest::RegisterTestFile(const std::string& module_name,
178 const std::string& file_name) {
179 base::FilePath test_js_file_path;
180 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path));
181 test_js_file_path = test_js_file_path.AppendASCII("extensions")
182 .AppendASCII(file_name);
183 std::string test_js;
[email protected]82f84b92013-08-30 18:23:50184 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
[email protected]f8d87d32013-06-06 02:51:29185 source_map_->RegisterModule(module_name, test_js);
186}
187
[email protected]1164b862012-05-09 22:38:37188void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37189 // All tests must assert at least once unless otherwise specified.
190 EXPECT_EQ(should_assertions_be_made_,
191 assert_natives_->assertion_made());
192 EXPECT_FALSE(assert_natives_->failed());
193}
194
195void ModuleSystemTest::ExpectNoAssertionsMade() {
196 should_assertions_be_made_ = false;
197}
198
199v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) {
[email protected]6f59d3b2013-12-02 12:50:50200 v8::Isolate* isolate = v8::Isolate::GetCurrent();
201 v8::EscapableHandleScope handle_scope(isolate);
202 v8::Local<v8::Object> object = v8::Object::New(isolate);
203 isolate->GetCurrentContext()->Global()->Set(
204 v8::String::NewFromUtf8(isolate, name.c_str()), object);
205 return handle_scope.Escape(object);
[email protected]1164b862012-05-09 22:38:37206}