test_import_from_sandbox.js (1726B)
1 "use strict"; 2 3 function makeSandbox() { 4 return Cu.Sandbox( 5 Services.scriptSecurityManager.getSystemPrincipal(), 6 { 7 wantXrays: false, 8 wantGlobalProperties: ["ChromeUtils"], 9 sandboxName: `Sandbox type used for ext-*.js ExtensionAPI subscripts`, 10 } 11 ); 12 } 13 14 // This tests the ESMification transition for extension API scripts. 15 add_task(function test_import_from_sandbox_transition() { 16 let sandbox = makeSandbox(); 17 18 Object.assign(sandbox, { 19 injected3: ChromeUtils.importESModule("resource://test/esmified-3.sys.mjs"), 20 }); 21 22 Services.scriptloader.loadSubScript("resource://test/api_script.js", sandbox); 23 let tr = sandbox.testResults; 24 25 Assert.equal(tr.injected3, 16, "Injected esmified-3.mjs has correct value."); 26 Assert.equal(tr.module3, 16, "Iimported esmified-3.mjs has correct value."); 27 Assert.ok(tr.sameInstance3, "Injected and imported are the same instance."); 28 Assert.equal(tr.module4, 14, "Iimported esmified-4.mjs has correct value."); 29 }); 30 31 // Same as above, just using a PrecompiledScript. 32 add_task(async function test_import_from_sandbox_transition() { 33 let sandbox = makeSandbox(); 34 35 Object.assign(sandbox, { 36 injected3: ChromeUtils.importESModule("resource://test/esmified-3.sys.mjs"), 37 }); 38 39 let script = await ChromeUtils.compileScript("resource://test/api_script.js"); 40 script.executeInGlobal(sandbox); 41 let tr = sandbox.testResults; 42 43 Assert.equal(tr.injected3, 22, "Injected esmified-3.mjs has correct value."); 44 Assert.equal(tr.module3, 22, "Iimported esmified-3.mjs has correct value."); 45 Assert.ok(tr.sameInstance3, "Injected and imported are the same instance."); 46 Assert.equal(tr.module4, 18, "Iimported esmified-4.mjs has correct value."); 47 });