test_loader.js (2561B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { 7 useDistinctSystemPrincipalLoader, 8 releaseDistinctSystemPrincipalLoader, 9 } = ChromeUtils.importESModule( 10 "resource://devtools/shared/loader/DistinctSystemPrincipalLoader.sys.mjs", 11 { global: "shared" } 12 ); 13 14 function run_test() { 15 const requester = {}, 16 requester2 = {}; 17 18 const loader = useDistinctSystemPrincipalLoader(requester); 19 20 // The DevTools dedicated global forces invisibleToDebugger on its realm at 21 // https://searchfox.org/mozilla-central/rev/12a18f7e112a4dcf88d8441d439b84144bfbe9a3/js/xpconnect/loader/mozJSModuleLoader.cpp#591-593 22 // but this is not observable directly. 23 const DevToolsSpecialGlobal = Cu.getGlobalForObject( 24 ChromeUtils.importESModule( 25 "resource://devtools/shared/DevToolsInfaillibleUtils.sys.mjs", 26 { global: "devtools" } 27 ) 28 ); 29 30 const regularLoader = new DevToolsLoader(); 31 Assert.notStrictEqual( 32 DevToolsSpecialGlobal, 33 regularLoader.loader.sharedGlobal, 34 "The regular loader is not using the special DevTools global" 35 ); 36 37 info("Assert the key difference with the other regular loaders:"); 38 Assert.strictEqual( 39 DevToolsSpecialGlobal, 40 loader.loader.sharedGlobal, 41 "The system principal loader is using the special DevTools global" 42 ); 43 44 ok(loader.loader, "Loader is not destroyed before calling release"); 45 46 info("Now assert the precise behavior of release"); 47 releaseDistinctSystemPrincipalLoader({}); 48 ok( 49 loader.loader, 50 "Loader is still not destroyed after calling release with another requester" 51 ); 52 53 releaseDistinctSystemPrincipalLoader(requester); 54 ok( 55 !loader.loader, 56 "Loader is destroyed after calling release with the right requester" 57 ); 58 59 info("Now test the behavior with two concurrent usages"); 60 const loader2 = useDistinctSystemPrincipalLoader(requester); 61 Assert.notEqual(loader, loader2, "We get a new loader instance"); 62 Assert.strictEqual( 63 DevToolsSpecialGlobal, 64 loader2.loader.sharedGlobal, 65 "The new system principal loader is also using the special DevTools global" 66 ); 67 68 const loader3 = useDistinctSystemPrincipalLoader(requester2); 69 Assert.equal(loader2, loader3, "The two loader last loaders are shared"); 70 71 releaseDistinctSystemPrincipalLoader(requester); 72 ok(loader2.loader, "Loader isn't destroy on the first call to destroy"); 73 74 releaseDistinctSystemPrincipalLoader(requester2); 75 ok(!loader2.loader, "Loader is destroyed on the second call to destroy"); 76 }