summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/test_finalizationRegistry_incumbent.html
blob: 8b6c71c9cfe396af3574794988590d069a4d2ce1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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>