blob: dd4ca2a8cd15db08a5d799cc7ae823c06362e3d7 [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]4f1633f2013-03-09 14:26:2418#include "chrome/renderer/extensions/object_backed_native_handler.h"
[email protected]295890bd2013-06-15 10:52:4519#include "chrome/renderer/extensions/safe_builtins.h"
[email protected]11844fa2012-05-10 00:35:5920#include "ui/base/resource/resource_bundle.h"
[email protected]1164b862012-05-09 22:38:3721
22#include <map>
23#include <string>
24
[email protected]3c6babd2012-08-28 03:17:2925using extensions::ModuleSystem;
26using extensions::NativeHandler;
[email protected]4f1633f2013-03-09 14:26:2427using extensions::ObjectBackedNativeHandler;
[email protected]3c6babd2012-08-28 03:17:2928
[email protected]295890bd2013-06-15 10:52:4529namespace {
30
31class FailsOnException : public ModuleSystem::ExceptionHandler {
32 public:
33 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE {
34 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch);
35 }
36};
37
38class V8ExtensionConfigurator {
39 public:
40 V8ExtensionConfigurator()
[email protected]5380451c2013-06-18 05:16:2541 : safe_builtins_(extensions::SafeBuiltins::CreateV8Extension()),
42 names_(1, safe_builtins_->name()),
43 configuration_(new v8::ExtensionConfiguration(
44 names_.size(), vector_as_array(&names_))) {
45 v8::RegisterExtension(safe_builtins_.get());
[email protected]295890bd2013-06-15 10:52:4546 }
47
[email protected]5380451c2013-06-18 05:16:2548 v8::ExtensionConfiguration* GetConfiguration() {
49 return configuration_.get();
[email protected]295890bd2013-06-15 10:52:4550 }
51
52 private:
[email protected]5380451c2013-06-18 05:16:2553 scoped_ptr<v8::Extension> safe_builtins_;
[email protected]295890bd2013-06-15 10:52:4554 std::vector<const char*> names_;
[email protected]5380451c2013-06-18 05:16:2555 scoped_ptr<v8::ExtensionConfiguration> configuration_;
[email protected]295890bd2013-06-15 10:52:4556};
57
[email protected]5380451c2013-06-18 05:16:2558base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator =
[email protected]295890bd2013-06-15 10:52:4559 LAZY_INSTANCE_INITIALIZER;
60
61} // namespace
62
[email protected]1164b862012-05-09 22:38:3763// Native JS functions for doing asserts.
[email protected]295890bd2013-06-15 10:52:4564class ModuleSystemTest::AssertNatives : public ObjectBackedNativeHandler {
[email protected]1164b862012-05-09 22:38:3765 public:
[email protected]9a598442013-06-04 16:39:1266 explicit AssertNatives(extensions::ChromeV8Context* 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) {
70 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue,
71 base::Unretained(this)));
72 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse,
73 base::Unretained(this)));
74 }
75
76 bool assertion_made() { return assertion_made_; }
77 bool failed() { return failed_; }
78
[email protected]d8c5fbb2013-06-14 11:35:2579 void AssertTrue(const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]1164b862012-05-09 22:38:3780 CHECK_EQ(1, args.Length());
81 assertion_made_ = true;
82 failed_ = failed_ || !args[0]->ToBoolean()->Value();
[email protected]1164b862012-05-09 22:38:3783 }
84
[email protected]d8c5fbb2013-06-14 11:35:2585 void AssertFalse(const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]1164b862012-05-09 22:38:3786 CHECK_EQ(1, args.Length());
87 assertion_made_ = true;
88 failed_ = failed_ || args[0]->ToBoolean()->Value();
[email protected]1164b862012-05-09 22:38:3789 }
90
91 private:
92 bool assertion_made_;
93 bool failed_;
94};
95
96// Source map that operates on std::strings.
[email protected]295890bd2013-06-15 10:52:4597class ModuleSystemTest::StringSourceMap : public ModuleSystem::SourceMap {
[email protected]1164b862012-05-09 22:38:3798 public:
99 StringSourceMap() {}
100 virtual ~StringSourceMap() {}
101
[email protected]6f59d3b2013-12-02 12:50:50102 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate,
103 const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:37104 if (source_map_.count(name) == 0)
[email protected]6f59d3b2013-12-02 12:50:50105 return v8::Undefined(isolate);
106 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str());
[email protected]1164b862012-05-09 22:38:37107 }
108
[email protected]be9826e62013-02-07 02:00:58109 virtual bool Contains(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:37110 return source_map_.count(name);
111 }
112
113 void RegisterModule(const std::string& name, const std::string& source) {
[email protected]68e63ea12013-06-05 05:00:54114 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
[email protected]1164b862012-05-09 22:38:37115 source_map_[name] = source;
116 }
117
118 private:
119 std::map<std::string, std::string> source_map_;
120};
121
122ModuleSystemTest::ModuleSystemTest()
[email protected]48002af2013-05-08 23:06:24123 : isolate_(v8::Isolate::GetCurrent()),
124 handle_scope_(isolate_),
[email protected]9a598442013-06-04 16:39:12125 context_(
126 new extensions::ChromeV8Context(
[email protected]295890bd2013-06-15 10:52:45127 v8::Context::New(
128 isolate_,
[email protected]5380451c2013-06-18 05:16:25129 g_v8_extension_configurator.Get().GetConfiguration()),
[email protected]68e63ea12013-06-05 05:00:54130 NULL, // WebFrame
131 NULL, // Extension
[email protected]9a598442013-06-04 16:39:12132 extensions::Feature::UNSPECIFIED_CONTEXT)),
[email protected]1164b862012-05-09 22:38:37133 source_map_(new StringSourceMap()),
134 should_assertions_be_made_(true) {
[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());
137 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get()));
[email protected]1164b862012-05-09 22:38:37138 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
139 assert_natives_));
[email protected]f8d87d32013-06-06 02:51:29140 module_system_->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>(
141 new extensions::LoggingNativeHandler(context_.get())));
[email protected]95ee77da2013-03-19 21:11:11142 module_system_->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23143 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37144}
145
146ModuleSystemTest::~ModuleSystemTest() {
147 module_system_.reset();
[email protected]9a598442013-06-04 16:39:12148 context_->v8_context()->Exit();
[email protected]1164b862012-05-09 22:38:37149}
150
151void ModuleSystemTest::RegisterModule(const std::string& name,
152 const std::string& code) {
153 source_map_->RegisterModule(name, code);
154}
155
[email protected]11844fa2012-05-10 00:35:59156void ModuleSystemTest::RegisterModule(const std::string& name,
157 int resource_id) {
158 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]4d8bb1a92012-11-01 21:12:40159 GetRawDataResource(resource_id).as_string();
[email protected]11844fa2012-05-10 00:35:59160 source_map_->RegisterModule(name, code);
161}
162
163void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
164 const std::string& code) {
165 RegisterModule(name, code);
[email protected]95ee77da2013-03-19 21:11:11166 module_system_->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59167}
168
[email protected]f8d87d32013-06-06 02:51:29169void ModuleSystemTest::RegisterTestFile(const std::string& module_name,
170 const std::string& file_name) {
171 base::FilePath test_js_file_path;
172 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path));
173 test_js_file_path = test_js_file_path.AppendASCII("extensions")
174 .AppendASCII(file_name);
175 std::string test_js;
[email protected]82f84b92013-08-30 18:23:50176 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
[email protected]f8d87d32013-06-06 02:51:29177 source_map_->RegisterModule(module_name, test_js);
178}
179
[email protected]1164b862012-05-09 22:38:37180void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37181 // All tests must assert at least once unless otherwise specified.
182 EXPECT_EQ(should_assertions_be_made_,
183 assert_natives_->assertion_made());
184 EXPECT_FALSE(assert_natives_->failed());
185}
186
187void ModuleSystemTest::ExpectNoAssertionsMade() {
188 should_assertions_be_made_ = false;
189}
190
191v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) {
[email protected]6f59d3b2013-12-02 12:50:50192 v8::Isolate* isolate = v8::Isolate::GetCurrent();
193 v8::EscapableHandleScope handle_scope(isolate);
194 v8::Local<v8::Object> object = v8::Object::New(isolate);
195 isolate->GetCurrentContext()->Global()->Set(
196 v8::String::NewFromUtf8(isolate, name.c_str()), object);
197 return handle_scope.Escape(object);
[email protected]1164b862012-05-09 22:38:37198}