Bug 1648453 - Add tests to check that FinalizationRegistry sets the incumbent global correctly when calling the callback r=smaug
authorJon Coppeard <jcoppeard@mozilla.com>
Fri, 17 Jul 2020 17:35:01 +0000
changeset 540950 623bebcf0cd6a89bd9b1ca0280ec79ce52531e17
parent 540949 13ee873ffdff46a34eea8b50844f8aba99a1517f
child 540951 0ebd8b6fce88c207493c81571caa11c360ed65cd
push id37612
push userabutkovits@mozilla.com
push dateFri, 17 Jul 2020 21:28:41 +0000
treeherdermozilla-central@bd7c35b7e234 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1648453
milestone80.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1648453 - Add tests to check that FinalizationRegistry sets the incumbent global correctly when calling the callback r=smaug Differential Revision: https://phabricator.services.mozilla.com/D83615
js/xpconnect/tests/mochitest/mochitest.ini
js/xpconnect/tests/mochitest/test_finalizationRegistry_incumbent.html
--- a/js/xpconnect/tests/mochitest/mochitest.ini
+++ b/js/xpconnect/tests/mochitest/mochitest.ini
@@ -111,11 +111,12 @@ skip-if = (debug == false)
 [test_sameOriginPolicy.html]
 [test_sandbox_fetch.html]
   support-files =
     ../../../../dom/tests/mochitest/fetch/test_fetch_basic.js
 [test_weakmaps.html]
 [test_finalizationRegistry.html]
 [test_finalizationRegistryInWorker.html]
 [test_finalizationRegistry_cleanupSome.html]
+[test_finalizationRegistry_incumbent.html]
 [test_weakRefs.html]
 [test_weakRefs_cross_compartment.html]
 [test_weakRefs_collected_wrapper.html]
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/mochitest/test_finalizationRegistry_incumbent.html
@@ -0,0 +1,62 @@
+<!DOCTYPE HTML>
+<html>
+  <head>
+    <meta charset="utf-8">
+    <title>Test FinalizationRegistry tracks its incumbent global</title>
+    <script src="/tests/SimpleTest/SimpleTest.js"></script>
+    <script type="application/javascript">
+      let resolvePromise, rejectPromise;
+
+      async function runTest(global, callback) {
+        let fr = new global.FinalizationRegistry(callback);
+        fr.register({}, undefined);
+
+        SpecialPowers.DOMWindowUtils.garbageCollect();
+
+        let promise = new Promise((resolve, reject) => {
+          resolvePromise = resolve;
+          rejectPromise = reject;
+        });
+
+        return promise;
+      }
+
+      function receiveMessage(event) {
+        resolvePromise(event.source.sourceName);
+      }
+
+      async function go() {
+        // This test uses FinalizationRegistry to trigger a callback and reports
+        // the incumbent global in the callback using postMessage. In all cases
+        // the author function that scheduled the callback is runTest(), so the
+        // incumbent global should be the main window.
+
+        SimpleTest.waitForExplicitFinish();
+
+        window.sourceName = "main";
+        window.addEventListener("message", receiveMessage, false);
+
+        let other = window.frames[0];
+        other.sourceName = "other";
+        other.addEventListener("message", receiveMessage, false);
+
+        is(await runTest(window, v => window.postMessage(v)), "main");
+        is(await runTest(window, window.postMessage.bind(window)), "main");
+        is(await runTest(other, v => other.postMessage(v)), "main");
+        is(await runTest(other, other.postMessage.bind(other)), "main");
+        is(await runTest(window, v => other.postMessage(v)), "main");
+        is(await runTest(window, other.postMessage.bind(other)), "main");
+        is(await runTest(other, v => window.postMessage(v)), "main");
+        is(await runTest(other, window.postMessage.bind(window)), "main");
+
+        SimpleTest.finish();
+      }
+    </script>
+  </head>
+  <body onload="go()">
+    <div style="display: none">
+      <!-- A subframe so we have another global to work with -->
+      <iframe></iframe>
+    </div>
+  </body>
+</html>