OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "extensions/renderer/module_system.h" | 6 #include "extensions/renderer/module_system.h" |
7 #include "extensions/renderer/module_system_test.h" | 7 #include "extensions/renderer/module_system_test.h" |
8 #include "gin/modules/module_registry.h" | 8 #include "gin/modules/module_registry.h" |
9 | 9 |
10 namespace extensions { | 10 namespace extensions { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 env()->module_system()->Require("test"); | 59 env()->module_system()->Require("test"); |
60 ASSERT_TRUE(handler->handled_exception()); | 60 ASSERT_TRUE(handler->handled_exception()); |
61 | 61 |
62 ExpectNoAssertionsMade(); | 62 ExpectNoAssertionsMade(); |
63 } | 63 } |
64 | 64 |
65 TEST_F(ModuleSystemTest, TestRequire) { | 65 TEST_F(ModuleSystemTest, TestRequire) { |
66 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 66 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
67 env()->module_system()); | 67 env()->module_system()); |
68 env()->RegisterModule("add", | 68 env()->RegisterModule("add", |
69 "exports.Add = function(x, y) { return x + y; };"); | 69 "exports.$set('Add'," |
| 70 "function(x, y) { return x + y; });"); |
70 env()->RegisterModule("test", | 71 env()->RegisterModule("test", |
71 "var Add = require('add').Add;" | 72 "var Add = require('add').Add;" |
72 "requireNative('assert').AssertTrue(Add(3, 5) == 8);"); | 73 "requireNative('assert').AssertTrue(Add(3, 5) == 8);"); |
73 env()->module_system()->Require("test"); | 74 env()->module_system()->Require("test"); |
74 } | 75 } |
75 | 76 |
76 TEST_F(ModuleSystemTest, TestNestedRequire) { | 77 TEST_F(ModuleSystemTest, TestNestedRequire) { |
77 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 78 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
78 env()->module_system()); | 79 env()->module_system()); |
79 env()->RegisterModule("add", | 80 env()->RegisterModule("add", |
80 "exports.Add = function(x, y) { return x + y; };"); | 81 "exports.$set('Add'," |
| 82 "function(x, y) { return x + y; });"); |
81 env()->RegisterModule("double", | 83 env()->RegisterModule("double", |
82 "var Add = require('add').Add;" | 84 "var Add = require('add').Add;" |
83 "exports.Double = function(x) { return Add(x, x); };"); | 85 "exports.$set('Double'," |
| 86 "function(x) { return Add(x, x); });"); |
84 env()->RegisterModule("test", | 87 env()->RegisterModule("test", |
85 "var Double = require('double').Double;" | 88 "var Double = require('double').Double;" |
86 "requireNative('assert').AssertTrue(Double(3) == 6);"); | 89 "requireNative('assert').AssertTrue(Double(3) == 6);"); |
87 env()->module_system()->Require("test"); | 90 env()->module_system()->Require("test"); |
88 } | 91 } |
89 | 92 |
90 TEST_F(ModuleSystemTest, TestModuleInsulation) { | 93 TEST_F(ModuleSystemTest, TestModuleInsulation) { |
91 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 94 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
92 env()->module_system()); | 95 env()->module_system()); |
93 env()->RegisterModule("x", | 96 env()->RegisterModule("x", |
94 "var x = 10;" | 97 "var x = 10;" |
95 "exports.X = function() { return x; };"); | 98 "exports.$set('X', function() { return x; });"); |
96 env()->RegisterModule("y", | 99 env()->RegisterModule("y", |
97 "var x = 15;" | 100 "var x = 15;" |
98 "require('x');" | 101 "require('x');" |
99 "exports.Y = function() { return x; };"); | 102 "exports.$set('Y', function() { return x; });"); |
100 env()->RegisterModule("test", | 103 env()->RegisterModule("test", |
101 "var Y = require('y').Y;" | 104 "var Y = require('y').Y;" |
102 "var X = require('x').X;" | 105 "var X = require('x').X;" |
103 "var assert = requireNative('assert');" | 106 "var assert = requireNative('assert');" |
104 "assert.AssertTrue(!this.hasOwnProperty('x'));" | 107 "assert.AssertTrue(!this.hasOwnProperty('x'));" |
105 "assert.AssertTrue(Y() == 15);" | 108 "assert.AssertTrue(Y() == 15);" |
106 "assert.AssertTrue(X() == 10);"); | 109 "assert.AssertTrue(X() == 10);"); |
107 env()->module_system()->Require("test"); | 110 env()->module_system()->Require("test"); |
108 } | 111 } |
109 | 112 |
(...skipping 23 matching lines...) Expand all Loading... |
133 ModuleSystem::NativesEnabledScope natives_enabled_inner( | 136 ModuleSystem::NativesEnabledScope natives_enabled_inner( |
134 env()->module_system()); | 137 env()->module_system()); |
135 } | 138 } |
136 env()->module_system()->Require("test"); | 139 env()->module_system()->Require("test"); |
137 } | 140 } |
138 } | 141 } |
139 | 142 |
140 TEST_F(ModuleSystemTest, TestLazyField) { | 143 TEST_F(ModuleSystemTest, TestLazyField) { |
141 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 144 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
142 env()->module_system()); | 145 env()->module_system()); |
143 env()->RegisterModule("lazy", "exports.x = 5;"); | 146 env()->RegisterModule("lazy", "exports.$set('x', 5);"); |
144 | 147 |
145 v8::Local<v8::Object> object = env()->CreateGlobal("object"); | 148 v8::Local<v8::Object> object = env()->CreateGlobal("object"); |
146 | 149 |
147 env()->module_system()->SetLazyField(object, "blah", "lazy", "x"); | 150 env()->module_system()->SetLazyField(object, "blah", "lazy", "x"); |
148 | 151 |
149 env()->RegisterModule("test", | 152 env()->RegisterModule("test", |
150 "var assert = requireNative('assert');" | 153 "var assert = requireNative('assert');" |
151 "assert.AssertTrue(object.blah == 5);"); | 154 "assert.AssertTrue(object.blah == 5);"); |
152 env()->module_system()->Require("test"); | 155 env()->module_system()->Require("test"); |
153 } | 156 } |
154 | 157 |
155 TEST_F(ModuleSystemTest, TestLazyFieldYieldingObject) { | 158 TEST_F(ModuleSystemTest, TestLazyFieldYieldingObject) { |
156 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 159 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
157 env()->module_system()); | 160 env()->module_system()); |
158 env()->RegisterModule( | 161 env()->RegisterModule( |
159 "lazy", | 162 "lazy", |
160 "var object = {};" | 163 "var object = {};" |
161 "object.__defineGetter__('z', function() { return 1; });" | 164 "object.__defineGetter__('z', function() { return 1; });" |
162 "object.x = 5;" | 165 "object.x = 5;" |
163 "object.y = function() { return 10; };" | 166 "object.y = function() { return 10; };" |
164 "exports.object = object;"); | 167 "exports.$set('object', object);"); |
165 | 168 |
166 v8::Local<v8::Object> object = env()->CreateGlobal("object"); | 169 v8::Local<v8::Object> object = env()->CreateGlobal("object"); |
167 | 170 |
168 env()->module_system()->SetLazyField(object, "thing", "lazy", "object"); | 171 env()->module_system()->SetLazyField(object, "thing", "lazy", "object"); |
169 | 172 |
170 env()->RegisterModule("test", | 173 env()->RegisterModule("test", |
171 "var assert = requireNative('assert');" | 174 "var assert = requireNative('assert');" |
172 "assert.AssertTrue(object.thing.x == 5);" | 175 "assert.AssertTrue(object.thing.x == 5);" |
173 "assert.AssertTrue(object.thing.y() == 10);" | 176 "assert.AssertTrue(object.thing.y() == 10);" |
174 "assert.AssertTrue(object.thing.z == 1);"); | 177 "assert.AssertTrue(object.thing.z == 1);"); |
175 env()->module_system()->Require("test"); | 178 env()->module_system()->Require("test"); |
176 } | 179 } |
177 | 180 |
178 TEST_F(ModuleSystemTest, TestLazyFieldIsOnlyEvaledOnce) { | 181 TEST_F(ModuleSystemTest, TestLazyFieldIsOnlyEvaledOnce) { |
179 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 182 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
180 env()->module_system()); | 183 env()->module_system()); |
181 env()->module_system()->RegisterNativeHandler( | 184 env()->module_system()->RegisterNativeHandler( |
182 "counter", | 185 "counter", |
183 scoped_ptr<NativeHandler>(new CounterNatives(env()->context()))); | 186 scoped_ptr<NativeHandler>(new CounterNatives(env()->context()))); |
184 env()->RegisterModule("lazy", | 187 env()->RegisterModule("lazy", |
185 "requireNative('counter').Increment();" | 188 "requireNative('counter').Increment();" |
186 "exports.x = 5;"); | 189 "exports.$set('x', 5);"); |
187 | 190 |
188 v8::Local<v8::Object> object = env()->CreateGlobal("object"); | 191 v8::Local<v8::Object> object = env()->CreateGlobal("object"); |
189 | 192 |
190 env()->module_system()->SetLazyField(object, "x", "lazy", "x"); | 193 env()->module_system()->SetLazyField(object, "x", "lazy", "x"); |
191 | 194 |
192 env()->RegisterModule("test", | 195 env()->RegisterModule("test", |
193 "var assert = requireNative('assert');" | 196 "var assert = requireNative('assert');" |
194 "var counter = requireNative('counter');" | 197 "var counter = requireNative('counter');" |
195 "assert.AssertTrue(counter.Get() == 0);" | 198 "assert.AssertTrue(counter.Get() == 0);" |
196 "object.x;" | 199 "object.x;" |
197 "assert.AssertTrue(counter.Get() == 1);" | 200 "assert.AssertTrue(counter.Get() == 1);" |
198 "object.x;" | 201 "object.x;" |
199 "assert.AssertTrue(counter.Get() == 1);"); | 202 "assert.AssertTrue(counter.Get() == 1);"); |
200 env()->module_system()->Require("test"); | 203 env()->module_system()->Require("test"); |
201 } | 204 } |
202 | 205 |
203 TEST_F(ModuleSystemTest, TestRequireNativesAfterLazyEvaluation) { | 206 TEST_F(ModuleSystemTest, TestRequireNativesAfterLazyEvaluation) { |
204 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 207 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
205 env()->module_system()); | 208 env()->module_system()); |
206 env()->RegisterModule("lazy", "exports.x = 5;"); | 209 env()->RegisterModule("lazy", "exports.$set('x', 5);"); |
207 v8::Local<v8::Object> object = env()->CreateGlobal("object"); | 210 v8::Local<v8::Object> object = env()->CreateGlobal("object"); |
208 | 211 |
209 env()->module_system()->SetLazyField(object, "x", "lazy", "x"); | 212 env()->module_system()->SetLazyField(object, "x", "lazy", "x"); |
210 env()->RegisterModule("test", | 213 env()->RegisterModule("test", |
211 "object.x;" | 214 "object.x;" |
212 "requireNative('assert').AssertTrue(true);"); | 215 "requireNative('assert').AssertTrue(true);"); |
213 env()->module_system()->Require("test"); | 216 env()->module_system()->Require("test"); |
214 } | 217 } |
215 | 218 |
216 TEST_F(ModuleSystemTest, TestTransitiveRequire) { | 219 TEST_F(ModuleSystemTest, TestTransitiveRequire) { |
217 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 220 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
218 env()->module_system()); | 221 env()->module_system()); |
219 env()->RegisterModule("dependency", "exports.x = 5;"); | 222 env()->RegisterModule("dependency", "exports.$set('x', 5);"); |
220 env()->RegisterModule("lazy", "exports.output = require('dependency');"); | 223 env()->RegisterModule("lazy", |
| 224 "exports.$set('output', require('dependency'));"); |
221 | 225 |
222 v8::Local<v8::Object> object = env()->CreateGlobal("object"); | 226 v8::Local<v8::Object> object = env()->CreateGlobal("object"); |
223 | 227 |
224 env()->module_system()->SetLazyField(object, "thing", "lazy", "output"); | 228 env()->module_system()->SetLazyField(object, "thing", "lazy", "output"); |
225 | 229 |
226 env()->RegisterModule("test", | 230 env()->RegisterModule("test", |
227 "var assert = requireNative('assert');" | 231 "var assert = requireNative('assert');" |
228 "assert.AssertTrue(object.thing.x == 5);"); | 232 "assert.AssertTrue(object.thing.x == 5);"); |
229 env()->module_system()->Require("test"); | 233 env()->module_system()->Require("test"); |
230 } | 234 } |
(...skipping 15 matching lines...) Expand all Loading... |
246 "assert.AssertTrue(counter.Get() == 1);" | 250 "assert.AssertTrue(counter.Get() == 1);" |
247 "require('incrementsWhenEvaled');" | 251 "require('incrementsWhenEvaled');" |
248 "assert.AssertTrue(counter.Get() == 1);"); | 252 "assert.AssertTrue(counter.Get() == 1);"); |
249 | 253 |
250 env()->module_system()->Require("test"); | 254 env()->module_system()->Require("test"); |
251 } | 255 } |
252 | 256 |
253 TEST_F(ModuleSystemTest, TestOverrideNativeHandler) { | 257 TEST_F(ModuleSystemTest, TestOverrideNativeHandler) { |
254 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 258 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
255 env()->module_system()); | 259 env()->module_system()); |
256 env()->OverrideNativeHandler("assert", "exports.AssertTrue = function() {};"); | 260 env()->OverrideNativeHandler("assert", |
| 261 "exports.$set('AssertTrue', function() {});"); |
257 env()->RegisterModule("test", "requireNative('assert').AssertTrue(true);"); | 262 env()->RegisterModule("test", "requireNative('assert').AssertTrue(true);"); |
258 ExpectNoAssertionsMade(); | 263 ExpectNoAssertionsMade(); |
259 env()->module_system()->Require("test"); | 264 env()->module_system()->Require("test"); |
260 } | 265 } |
261 | 266 |
262 TEST_F(ModuleSystemTest, TestOverrideNonExistentNativeHandler) { | 267 TEST_F(ModuleSystemTest, TestOverrideNonExistentNativeHandler) { |
263 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 268 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
264 env()->module_system()); | 269 env()->module_system()); |
265 env()->OverrideNativeHandler("thing", "exports.x = 5;"); | 270 env()->OverrideNativeHandler("thing", "exports.$set('x', 5);"); |
266 env()->RegisterModule("test", | 271 env()->RegisterModule("test", |
267 "var assert = requireNative('assert');" | 272 "var assert = requireNative('assert');" |
268 "assert.AssertTrue(requireNative('thing').x == 5);"); | 273 "assert.AssertTrue(requireNative('thing').x == 5);"); |
269 env()->module_system()->Require("test"); | 274 env()->module_system()->Require("test"); |
270 } | 275 } |
271 | 276 |
272 TEST_F(ModuleSystemTest, TestRequireAsync) { | 277 TEST_F(ModuleSystemTest, TestRequireAsync) { |
273 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 278 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
274 env()->module_system()); | 279 env()->module_system()); |
275 env()->RegisterModule("add", | 280 env()->RegisterModule("add", |
(...skipping 20 matching lines...) Expand all Loading... |
296 "define('add', [], function() {" | 301 "define('add', [], function() {" |
297 " return { Add: function(x, y) { return x + y; } };" | 302 " return { Add: function(x, y) { return x + y; } };" |
298 "});"); | 303 "});"); |
299 env()->RegisterModule( | 304 env()->RegisterModule( |
300 "subtract", | 305 "subtract", |
301 "define('subtract', [], function() {" | 306 "define('subtract', [], function() {" |
302 " return { Subtract: function(x, y) { return x - y; } };" | 307 " return { Subtract: function(x, y) { return x - y; } };" |
303 "});"); | 308 "});"); |
304 env()->RegisterModule( | 309 env()->RegisterModule( |
305 "math", | 310 "math", |
306 "exports.AddAndSubtract = function(x, y, z) {" | 311 "exports.$set('AddAndSubtract', function(x, y, z) {" |
307 " return Promise.all([requireAsync('add')," | 312 " return Promise.all([requireAsync('add')," |
308 " requireAsync('subtract')" | 313 " requireAsync('subtract')" |
309 " ]).then(function(modules) {" | 314 " ]).then(function(modules) {" |
310 " return modules[1].Subtract(modules[0].Add(x, y), z);" | 315 " return modules[1].Subtract(modules[0].Add(x, y), z);" |
311 " });" | 316 " });" |
312 "};"); | 317 "});"); |
313 env()->RegisterModule("test", | 318 env()->RegisterModule("test", |
314 "var AddAndSubtract = require('math').AddAndSubtract;" | 319 "var AddAndSubtract = require('math').AddAndSubtract;" |
315 "AddAndSubtract(3, 5, 2).then(function(result) {" | 320 "AddAndSubtract(3, 5, 2).then(function(result) {" |
316 " requireNative('assert').AssertTrue(result == 6);" | 321 " requireNative('assert').AssertTrue(result == 6);" |
317 "});"); | 322 "});"); |
318 env()->module_system()->Require("test"); | 323 env()->module_system()->Require("test"); |
319 RunResolvedPromises(); | 324 RunResolvedPromises(); |
320 } | 325 } |
321 | 326 |
322 TEST_F(ModuleSystemTest, TestNestedRequireAsyncs) { | 327 TEST_F(ModuleSystemTest, TestNestedRequireAsyncs) { |
(...skipping 14 matching lines...) Expand all Loading... |
337 "}).then(function(module) {" | 342 "}).then(function(module) {" |
338 " requireNative('assert').AssertTrue(module.next === '');" | 343 " requireNative('assert').AssertTrue(module.next === '');" |
339 "});"); | 344 "});"); |
340 env()->module_system()->Require("test"); | 345 env()->module_system()->Require("test"); |
341 RunResolvedPromises(); | 346 RunResolvedPromises(); |
342 } | 347 } |
343 | 348 |
344 TEST_F(ModuleSystemTest, TestRequireFromAMDModule) { | 349 TEST_F(ModuleSystemTest, TestRequireFromAMDModule) { |
345 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 350 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
346 env()->module_system()); | 351 env()->module_system()); |
347 env()->RegisterModule("add", | 352 env()->RegisterModule( |
348 "exports.Add = function(x, y) { return x + y; };"); | 353 "add", "exports.$set('Add', function(x, y) { return x + y; });"); |
349 env()->RegisterModule("math", | 354 env()->RegisterModule("math", |
350 "define('math', [], function() {" | 355 "define('math', [], function() {" |
351 " var add = require('add');" | 356 " var add = require('add');" |
352 " return { Add: add.Add };" | 357 " return { Add: add.Add };" |
353 "});"); | 358 "});"); |
354 env()->RegisterModule( | 359 env()->RegisterModule( |
355 "test", | 360 "test", |
356 "requireAsync('math').then(function(math) {" | 361 "requireAsync('math').then(function(math) {" |
357 " requireNative('assert').AssertTrue(math.Add(3, 5) == 8);" | 362 " requireNative('assert').AssertTrue(math.Add(3, 5) == 8);" |
358 "});"); | 363 "});"); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 gin::ModuleRegistry::From(env()->context()->v8_context()) | 494 gin::ModuleRegistry::From(env()->context()->v8_context()) |
490 ->AddBuiltinModule( | 495 ->AddBuiltinModule( |
491 env()->isolate(), "natives", | 496 env()->isolate(), "natives", |
492 other_env->module_system()->NewInstance()); | 497 other_env->module_system()->NewInstance()); |
493 other_env->ShutdownModuleSystem(); | 498 other_env->ShutdownModuleSystem(); |
494 env()->module_system()->Require("test"); | 499 env()->module_system()->Require("test"); |
495 RunResolvedPromises(); | 500 RunResolvedPromises(); |
496 } | 501 } |
497 | 502 |
498 } // namespace extensions | 503 } // namespace extensions |
OLD | NEW |