test_finalizationRegistry_incumbent.html (2177B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test FinalizationRegistry tracks its incumbent global</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script type="application/javascript"> 8 let resolvePromise, rejectPromise; 9 10 async function runTest(global, callback) { 11 let fr = new global.FinalizationRegistry(callback); 12 fr.register({}, undefined); 13 14 SpecialPowers.DOMWindowUtils.garbageCollect(); 15 16 let promise = new Promise((resolve, reject) => { 17 resolvePromise = resolve; 18 rejectPromise = reject; 19 }); 20 21 return promise; 22 } 23 24 function receiveMessage(event) { 25 resolvePromise(event.source.sourceName); 26 } 27 28 async function go() { 29 // This test uses FinalizationRegistry to trigger a callback and reports 30 // the incumbent global in the callback using postMessage. In all cases 31 // the author function that scheduled the callback is runTest(), so the 32 // incumbent global should be the main window. 33 34 SimpleTest.waitForExplicitFinish(); 35 36 window.sourceName = "main"; 37 window.addEventListener("message", receiveMessage, false); 38 39 let other = window.frames[0]; 40 other.sourceName = "other"; 41 other.addEventListener("message", receiveMessage, false); 42 43 is(await runTest(window, v => window.postMessage(v)), "main"); 44 is(await runTest(window, window.postMessage.bind(window)), "main"); 45 is(await runTest(other, v => other.postMessage(v)), "main"); 46 is(await runTest(other, other.postMessage.bind(other)), "main"); 47 is(await runTest(window, v => other.postMessage(v)), "main"); 48 is(await runTest(window, other.postMessage.bind(other)), "main"); 49 is(await runTest(other, v => window.postMessage(v)), "main"); 50 is(await runTest(other, window.postMessage.bind(window)), "main"); 51 52 SimpleTest.finish(); 53 } 54 </script> 55 </head> 56 <body onload="go()"> 57 <div style="display: none"> 58 <!-- A subframe so we have another global to work with --> 59 <iframe></iframe> 60 </div> 61 </body> 62 </html>