blob: d9629a73ee1fe5e4550cad2818ad6870222342c6 [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]be9826e62013-02-07 02:00:58102 virtual v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:37103 if (source_map_.count(name) == 0)
104 return v8::Undefined();
105 return v8::String::New(source_map_[name].c_str());
106 }
107
[email protected]be9826e62013-02-07 02:00:58108 virtual bool Contains(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:37109 return source_map_.count(name);
110 }
111
112 void RegisterModule(const std::string& name, const std::string& source) {
[email protected]68e63ea12013-06-05 05:00:54113 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
[email protected]1164b862012-05-09 22:38:37114 source_map_[name] = source;
115 }
116
117 private:
118 std::map<std::string, std::string> source_map_;
119};
120
121ModuleSystemTest::ModuleSystemTest()
[email protected]48002af2013-05-08 23:06:24122 : isolate_(v8::Isolate::GetCurrent()),
123 handle_scope_(isolate_),
[email protected]9a598442013-06-04 16:39:12124 context_(
125 new extensions::ChromeV8Context(
[email protected]295890bd2013-06-15 10:52:45126 v8::Context::New(
127 isolate_,
[email protected]5380451c2013-06-18 05:16:25128 g_v8_extension_configurator.Get().GetConfiguration()),
[email protected]68e63ea12013-06-05 05:00:54129 NULL, // WebFrame
130 NULL, // Extension
[email protected]9a598442013-06-04 16:39:12131 extensions::Feature::UNSPECIFIED_CONTEXT)),
[email protected]1164b862012-05-09 22:38:37132 source_map_(new StringSourceMap()),
133 should_assertions_be_made_(true) {
[email protected]9a598442013-06-04 16:39:12134 context_->v8_context()->Enter();
[email protected]4f1633f2013-03-09 14:26:24135 assert_natives_ = new AssertNatives(context_.get());
136 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get()));
[email protected]1164b862012-05-09 22:38:37137 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
138 assert_natives_));
[email protected]f8d87d32013-06-06 02:51:29139 module_system_->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>(
140 new extensions::LoggingNativeHandler(context_.get())));
[email protected]95ee77da2013-03-19 21:11:11141 module_system_->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23142 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37143}
144
145ModuleSystemTest::~ModuleSystemTest() {
146 module_system_.reset();
[email protected]9a598442013-06-04 16:39:12147 context_->v8_context()->Exit();
[email protected]1164b862012-05-09 22:38:37148}
149
150void ModuleSystemTest::RegisterModule(const std::string& name,
151 const std::string& code) {
152 source_map_->RegisterModule(name, code);
153}
154
[email protected]11844fa2012-05-10 00:35:59155void ModuleSystemTest::RegisterModule(const std::string& name,
156 int resource_id) {
157 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]4d8bb1a92012-11-01 21:12:40158 GetRawDataResource(resource_id).as_string();
[email protected]11844fa2012-05-10 00:35:59159 source_map_->RegisterModule(name, code);
160}
161
162void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
163 const std::string& code) {
164 RegisterModule(name, code);
[email protected]95ee77da2013-03-19 21:11:11165 module_system_->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59166}
167
[email protected]f8d87d32013-06-06 02:51:29168void ModuleSystemTest::RegisterTestFile(const std::string& module_name,
169 const std::string& file_name) {
170 base::FilePath test_js_file_path;
171 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path));
172 test_js_file_path = test_js_file_path.AppendASCII("extensions")
173 .AppendASCII(file_name);
174 std::string test_js;
[email protected]82f84b92013-08-30 18:23:50175 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
[email protected]f8d87d32013-06-06 02:51:29176 source_map_->RegisterModule(module_name, test_js);
177}
178
[email protected]1164b862012-05-09 22:38:37179void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37180 // All tests must assert at least once unless otherwise specified.
181 EXPECT_EQ(should_assertions_be_made_,
182 assert_natives_->assertion_made());
183 EXPECT_FALSE(assert_natives_->failed());
184}
185
186void ModuleSystemTest::ExpectNoAssertionsMade() {
187 should_assertions_be_made_ = false;
188}
189
190v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) {
191 v8::HandleScope handle_scope;
192 v8::Handle<v8::Object> object = v8::Object::New();
193 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()),
194 object);
195 return handle_scope.Close(object);
196}