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"
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.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
{
28 class FailsOnException
: public ModuleSystem::ExceptionHandler
{
30 FailsOnException() : ModuleSystem::ExceptionHandler(nullptr) {}
31 void HandleUncaughtException(const v8::TryCatch
& try_catch
) override
{
32 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch
);
36 class V8ExtensionConfigurator
{
38 V8ExtensionConfigurator()
39 : safe_builtins_(SafeBuiltins::CreateV8Extension()),
40 names_(1, safe_builtins_
->name()),
42 new v8::ExtensionConfiguration(static_cast<int>(names_
.size()),
43 vector_as_array(&names_
))) {
44 v8::RegisterExtension(safe_builtins_
.get());
47 v8::ExtensionConfiguration
* GetConfiguration() {
48 return configuration_
.get();
52 scoped_ptr
<v8::Extension
> safe_builtins_
;
53 std::vector
<const char*> names_
;
54 scoped_ptr
<v8::ExtensionConfiguration
> configuration_
;
57 base::LazyInstance
<V8ExtensionConfigurator
>::Leaky g_v8_extension_configurator
=
58 LAZY_INSTANCE_INITIALIZER
;
62 // Native JS functions for doing asserts.
63 class ModuleSystemTestEnvironment::AssertNatives
64 : public ObjectBackedNativeHandler
{
66 explicit AssertNatives(ScriptContext
* context
)
67 : ObjectBackedNativeHandler(context
),
68 assertion_made_(false),
72 base::Bind(&AssertNatives::AssertTrue
, base::Unretained(this)));
75 base::Bind(&AssertNatives::AssertFalse
, base::Unretained(this)));
78 bool assertion_made() { return assertion_made_
; }
79 bool failed() { return failed_
; }
81 void AssertTrue(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
82 CHECK_EQ(1, args
.Length());
83 assertion_made_
= true;
84 failed_
= failed_
|| !args
[0]->ToBoolean(args
.GetIsolate())->Value();
87 void AssertFalse(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
88 CHECK_EQ(1, args
.Length());
89 assertion_made_
= true;
90 failed_
= failed_
|| args
[0]->ToBoolean(args
.GetIsolate())->Value();
98 // Source map that operates on std::strings.
99 class ModuleSystemTestEnvironment::StringSourceMap
100 : public ModuleSystem::SourceMap
{
103 ~StringSourceMap() override
{}
105 v8::Local
<v8::Value
> GetSource(v8::Isolate
* isolate
,
106 const std::string
& name
) override
{
107 if (source_map_
.count(name
) == 0)
108 return v8::Undefined(isolate
);
109 return v8::String::NewFromUtf8(isolate
, source_map_
[name
].c_str());
112 bool Contains(const std::string
& name
) override
{
113 return source_map_
.count(name
);
116 void RegisterModule(const std::string
& name
, const std::string
& source
) {
117 CHECK_EQ(0u, source_map_
.count(name
)) << "Module " << name
<< " not found";
118 source_map_
[name
] = source
;
122 std::map
<std::string
, std::string
> source_map_
;
125 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(v8::Isolate
* isolate
)
127 context_holder_(new gin::ContextHolder(isolate_
)),
128 handle_scope_(isolate_
),
129 source_map_(new StringSourceMap()) {
130 context_holder_
->SetContext(v8::Context::New(
131 isolate
, g_v8_extension_configurator
.Get().GetConfiguration()));
132 context_
.reset(new ScriptContext(context_holder_
->context(),
134 nullptr, // Extension
135 Feature::BLESSED_EXTENSION_CONTEXT
,
136 nullptr, // Effective Extension
137 Feature::BLESSED_EXTENSION_CONTEXT
));
138 context_
->v8_context()->Enter();
139 assert_natives_
= new AssertNatives(context_
.get());
142 scoped_ptr
<ModuleSystem
> module_system(
143 new ModuleSystem(context_
.get(), source_map_
.get()));
144 context_
->set_module_system(module_system
.Pass());
146 ModuleSystem
* module_system
= context_
->module_system();
147 module_system
->RegisterNativeHandler(
148 "assert", scoped_ptr
<NativeHandler
>(assert_natives_
));
149 module_system
->RegisterNativeHandler(
151 scoped_ptr
<NativeHandler
>(new LoggingNativeHandler(context_
.get())));
152 module_system
->RegisterNativeHandler(
154 scoped_ptr
<NativeHandler
>(new UtilsNativeHandler(context_
.get())));
155 module_system
->SetExceptionHandlerForTest(
156 scoped_ptr
<ModuleSystem::ExceptionHandler
>(new FailsOnException
));
159 ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() {
160 if (context_
->is_valid())
161 ShutdownModuleSystem();
164 void ModuleSystemTestEnvironment::RegisterModule(const std::string
& name
,
165 const std::string
& code
) {
166 source_map_
->RegisterModule(name
, code
);
169 void ModuleSystemTestEnvironment::RegisterModule(const std::string
& name
,
171 const std::string
& code
= ResourceBundle::GetSharedInstance()
172 .GetRawDataResource(resource_id
)
174 source_map_
->RegisterModule(name
, code
);
177 void ModuleSystemTestEnvironment::OverrideNativeHandler(
178 const std::string
& name
,
179 const std::string
& code
) {
180 RegisterModule(name
, code
);
181 context_
->module_system()->OverrideNativeHandlerForTest(name
);
184 void ModuleSystemTestEnvironment::RegisterTestFile(
185 const std::string
& module_name
,
186 const std::string
& file_name
) {
187 base::FilePath test_js_file_path
;
188 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &test_js_file_path
));
189 test_js_file_path
= test_js_file_path
.AppendASCII(file_name
);
191 ASSERT_TRUE(base::ReadFileToString(test_js_file_path
, &test_js
));
192 source_map_
->RegisterModule(module_name
, test_js
);
195 void ModuleSystemTestEnvironment::ShutdownGin() {
196 context_holder_
.reset();
199 void ModuleSystemTestEnvironment::ShutdownModuleSystem() {
200 CHECK(context_
->is_valid());
201 context_
->v8_context()->Exit();
202 context_
->Invalidate();
205 v8::Local
<v8::Object
> ModuleSystemTestEnvironment::CreateGlobal(
206 const std::string
& name
) {
207 v8::EscapableHandleScope
handle_scope(isolate_
);
208 v8::Local
<v8::Object
> object
= v8::Object::New(isolate_
);
209 isolate_
->GetCurrentContext()->Global()->Set(
210 v8::String::NewFromUtf8(isolate_
, name
.c_str()), object
);
211 return handle_scope
.Escape(object
);
214 ModuleSystemTest::ModuleSystemTest()
215 : isolate_(v8::Isolate::GetCurrent()),
216 should_assertions_be_made_(true) {
219 ModuleSystemTest::~ModuleSystemTest() {
222 void ModuleSystemTest::SetUp() {
223 env_
= CreateEnvironment();
226 void ModuleSystemTest::TearDown() {
227 // All tests must assert at least once unless otherwise specified.
228 EXPECT_EQ(should_assertions_be_made_
,
229 env_
->assert_natives()->assertion_made());
230 EXPECT_FALSE(env_
->assert_natives()->failed());
232 v8::HeapStatistics stats
;
233 isolate_
->GetHeapStatistics(&stats
);
234 size_t old_heap_size
= 0;
235 // Run the GC until the heap size reaches a steady state to ensure that
236 // all the garbage is collected.
237 while (stats
.used_heap_size() != old_heap_size
) {
238 old_heap_size
= stats
.used_heap_size();
239 isolate_
->RequestGarbageCollectionForTesting(
240 v8::Isolate::kFullGarbageCollection
);
241 isolate_
->GetHeapStatistics(&stats
);
245 scoped_ptr
<ModuleSystemTestEnvironment
> ModuleSystemTest::CreateEnvironment() {
246 return make_scoped_ptr(new ModuleSystemTestEnvironment(isolate_
));
249 void ModuleSystemTest::ExpectNoAssertionsMade() {
250 should_assertions_be_made_
= false;
253 void ModuleSystemTest::RunResolvedPromises() {
254 isolate_
->RunMicrotasks();
257 } // namespace extensions