Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(634)

Side by Side Diff: chrome/test/base/module_system_test.cc

Issue 359413004: Add support for using AMD modules from extensions modules. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: extensions_renderer should depend on gin Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/test/base/module_system_test.h ('k') | extensions/extensions.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/test/base/module_system_test.h" 5 #include "chrome/test/base/module_system_test.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 std::vector<const char*> names_; 55 std::vector<const char*> names_;
56 scoped_ptr<v8::ExtensionConfiguration> configuration_; 56 scoped_ptr<v8::ExtensionConfiguration> configuration_;
57 }; 57 };
58 58
59 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = 59 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator =
60 LAZY_INSTANCE_INITIALIZER; 60 LAZY_INSTANCE_INITIALIZER;
61 61
62 } // namespace 62 } // namespace
63 63
64 // Native JS functions for doing asserts. 64 // Native JS functions for doing asserts.
65 class ModuleSystemTest::AssertNatives : public ObjectBackedNativeHandler { 65 class ModuleSystemTestEnvironment::AssertNatives
66 : public ObjectBackedNativeHandler {
66 public: 67 public:
67 explicit AssertNatives(extensions::ChromeV8Context* context) 68 explicit AssertNatives(extensions::ChromeV8Context* context)
68 : ObjectBackedNativeHandler(context), 69 : ObjectBackedNativeHandler(context),
69 assertion_made_(false), 70 assertion_made_(false),
70 failed_(false) { 71 failed_(false) {
71 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, 72 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue,
72 base::Unretained(this))); 73 base::Unretained(this)));
73 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, 74 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse,
74 base::Unretained(this))); 75 base::Unretained(this)));
75 } 76 }
(...skipping 12 matching lines...) Expand all
88 assertion_made_ = true; 89 assertion_made_ = true;
89 failed_ = failed_ || args[0]->ToBoolean()->Value(); 90 failed_ = failed_ || args[0]->ToBoolean()->Value();
90 } 91 }
91 92
92 private: 93 private:
93 bool assertion_made_; 94 bool assertion_made_;
94 bool failed_; 95 bool failed_;
95 }; 96 };
96 97
97 // Source map that operates on std::strings. 98 // Source map that operates on std::strings.
98 class ModuleSystemTest::StringSourceMap : public ModuleSystem::SourceMap { 99 class ModuleSystemTestEnvironment::StringSourceMap
100 : public extensions::ModuleSystem::SourceMap {
99 public: 101 public:
100 StringSourceMap() {} 102 StringSourceMap() {}
101 virtual ~StringSourceMap() {} 103 virtual ~StringSourceMap() {}
102 104
103 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, 105 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate,
104 const std::string& name) OVERRIDE { 106 const std::string& name) OVERRIDE {
105 if (source_map_.count(name) == 0) 107 if (source_map_.count(name) == 0)
106 return v8::Undefined(isolate); 108 return v8::Undefined(isolate);
107 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str()); 109 return v8::String::NewFromUtf8(isolate, source_map_[name].c_str());
108 } 110 }
109 111
110 virtual bool Contains(const std::string& name) OVERRIDE { 112 virtual bool Contains(const std::string& name) OVERRIDE {
111 return source_map_.count(name); 113 return source_map_.count(name);
112 } 114 }
113 115
114 void RegisterModule(const std::string& name, const std::string& source) { 116 void RegisterModule(const std::string& name, const std::string& source) {
115 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found"; 117 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
116 source_map_[name] = source; 118 source_map_[name] = source;
117 } 119 }
118 120
119 private: 121 private:
120 std::map<std::string, std::string> source_map_; 122 std::map<std::string, std::string> source_map_;
121 }; 123 };
122 124
123 ModuleSystemTest::ModuleSystemTest() 125 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(
124 : isolate_(v8::Isolate::GetCurrent()), 126 gin::IsolateHolder* isolate_holder)
125 handle_scope_(isolate_), 127 : isolate_holder_(isolate_holder),
126 context_( 128 context_holder_(new gin::ContextHolder(isolate_holder_->isolate())),
127 new extensions::ChromeV8Context( 129 handle_scope_(isolate_holder_->isolate()),
128 v8::Context::New( 130 source_map_(new StringSourceMap()) {
129 isolate_, 131 context_holder_->SetContext(
130 g_v8_extension_configurator.Get().GetConfiguration()), 132 v8::Context::New(isolate_holder->isolate(),
131 NULL, // WebFrame 133 g_v8_extension_configurator.Get().GetConfiguration()));
132 NULL, // Extension 134 context_.reset(new extensions::ChromeV8Context(
133 extensions::Feature::UNSPECIFIED_CONTEXT)), 135 context_holder_->context(),
134 source_map_(new StringSourceMap()), 136 NULL, // WebFrame
135 should_assertions_be_made_(true) { 137 NULL, // Extension
138 extensions::Feature::UNSPECIFIED_CONTEXT));
136 context_->v8_context()->Enter(); 139 context_->v8_context()->Enter();
137 assert_natives_ = new AssertNatives(context_.get()); 140 assert_natives_ = new AssertNatives(context_.get());
138 141
139 { 142 {
140 scoped_ptr<ModuleSystem> module_system( 143 scoped_ptr<ModuleSystem> module_system(
141 new ModuleSystem(context_.get(), source_map_.get())); 144 new ModuleSystem(context_.get(), source_map_.get()));
142 context_->set_module_system(module_system.Pass()); 145 context_->set_module_system(module_system.Pass());
143 } 146 }
144 ModuleSystem* module_system = context_->module_system(); 147 ModuleSystem* module_system = context_->module_system();
145 module_system->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( 148 module_system->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
146 assert_natives_)); 149 assert_natives_));
147 module_system->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>( 150 module_system->RegisterNativeHandler("logging", scoped_ptr<NativeHandler>(
148 new extensions::LoggingNativeHandler(context_.get()))); 151 new extensions::LoggingNativeHandler(context_.get())));
149 module_system->RegisterNativeHandler("utils", scoped_ptr<NativeHandler>( 152 module_system->RegisterNativeHandler("utils", scoped_ptr<NativeHandler>(
150 new extensions::UtilsNativeHandler(context_.get()))); 153 new extensions::UtilsNativeHandler(context_.get())));
151 module_system->SetExceptionHandlerForTest( 154 module_system->SetExceptionHandlerForTest(
152 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); 155 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
153 } 156 }
154 157
155 ModuleSystemTest::~ModuleSystemTest() { 158 ModuleSystemTestEnvironment::~ModuleSystemTestEnvironment() {
156 context_->v8_context()->Exit(); 159 if (context_)
160 context_->v8_context()->Exit();
157 } 161 }
158 162
159 void ModuleSystemTest::RegisterModule(const std::string& name, 163 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
160 const std::string& code) { 164 const std::string& code) {
161 source_map_->RegisterModule(name, code); 165 source_map_->RegisterModule(name, code);
162 } 166 }
163 167
164 void ModuleSystemTest::RegisterModule(const std::string& name, 168 void ModuleSystemTestEnvironment::RegisterModule(const std::string& name,
165 int resource_id) { 169 int resource_id) {
166 const std::string& code = ResourceBundle::GetSharedInstance(). 170 const std::string& code = ResourceBundle::GetSharedInstance().
167 GetRawDataResource(resource_id).as_string(); 171 GetRawDataResource(resource_id).as_string();
168 source_map_->RegisterModule(name, code); 172 source_map_->RegisterModule(name, code);
169 } 173 }
170 174
171 void ModuleSystemTest::OverrideNativeHandler(const std::string& name, 175 void ModuleSystemTestEnvironment::OverrideNativeHandler(
172 const std::string& code) { 176 const std::string& name,
177 const std::string& code) {
173 RegisterModule(name, code); 178 RegisterModule(name, code);
174 context_->module_system()->OverrideNativeHandlerForTest(name); 179 context_->module_system()->OverrideNativeHandlerForTest(name);
175 } 180 }
176 181
177 void ModuleSystemTest::RegisterTestFile(const std::string& module_name, 182 void ModuleSystemTestEnvironment::RegisterTestFile(
178 const std::string& file_name) { 183 const std::string& module_name,
184 const std::string& file_name) {
179 base::FilePath test_js_file_path; 185 base::FilePath test_js_file_path;
180 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path)); 186 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_js_file_path));
181 test_js_file_path = test_js_file_path.AppendASCII("extensions") 187 test_js_file_path = test_js_file_path.AppendASCII("extensions")
182 .AppendASCII(file_name); 188 .AppendASCII(file_name);
183 std::string test_js; 189 std::string test_js;
184 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js)); 190 ASSERT_TRUE(base::ReadFileToString(test_js_file_path, &test_js));
185 source_map_->RegisterModule(module_name, test_js); 191 source_map_->RegisterModule(module_name, test_js);
186 } 192 }
187 193
194 void ModuleSystemTestEnvironment::ShutdownGin() {
195 context_holder_.reset();
196 }
197
198 void ModuleSystemTestEnvironment::ShutdownModuleSystem() {
199 context_->v8_context()->Exit();
200 context_.reset();
201 }
202
203 v8::Handle<v8::Object> ModuleSystemTestEnvironment::CreateGlobal(
204 const std::string& name) {
205 v8::Isolate* isolate = isolate_holder_->isolate();
206 v8::EscapableHandleScope handle_scope(isolate);
207 v8::Local<v8::Object> object = v8::Object::New(isolate);
208 isolate->GetCurrentContext()->Global()->Set(
209 v8::String::NewFromUtf8(isolate, name.c_str()), object);
210 return handle_scope.Escape(object);
211 }
212
213 ModuleSystemTest::ModuleSystemTest()
214 : isolate_holder_(v8::Isolate::GetCurrent(), NULL),
215 env_(CreateEnvironment()),
216 should_assertions_be_made_(true) {
217 }
218
219 ModuleSystemTest::~ModuleSystemTest() {
220 }
221
188 void ModuleSystemTest::TearDown() { 222 void ModuleSystemTest::TearDown() {
189 // All tests must assert at least once unless otherwise specified. 223 // All tests must assert at least once unless otherwise specified.
190 EXPECT_EQ(should_assertions_be_made_, 224 EXPECT_EQ(should_assertions_be_made_,
191 assert_natives_->assertion_made()); 225 env_->assert_natives()->assertion_made());
192 EXPECT_FALSE(assert_natives_->failed()); 226 EXPECT_FALSE(env_->assert_natives()->failed());
227 }
228
229 scoped_ptr<ModuleSystemTestEnvironment> ModuleSystemTest::CreateEnvironment() {
230 return make_scoped_ptr(new ModuleSystemTestEnvironment(&isolate_holder_));
193 } 231 }
194 232
195 void ModuleSystemTest::ExpectNoAssertionsMade() { 233 void ModuleSystemTest::ExpectNoAssertionsMade() {
196 should_assertions_be_made_ = false; 234 should_assertions_be_made_ = false;
197 } 235 }
198 236
199 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { 237 void ModuleSystemTest::RunResolvedPromises() {
200 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 238 isolate_holder_.isolate()->RunMicrotasks();
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);
206 } 239 }
OLDNEW
« no previous file with comments | « chrome/test/base/module_system_test.h ('k') | extensions/extensions.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698