summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_event_handler_cc.html
blob: b2ad5aa0886a14a73968ac8e1bc7eed31482bc45 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for cycle collection of event handlers</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.waitForExplicitFinish();

    function listener() {}

    function make_weak_ref(obj) {
      let m = new WeakMap;
      m.set(obj, {});
      return m;
    }

    function weak_ref_dead(r) {
      return SpecialPowers.nondeterministicGetWeakMapKeys(r).length == 0;
    }

    function setupTarget1() {
      let ifr = document.getElementById("target1");
      let doc = ifr.contentDocument;
      let b = doc.createElement("button");
      doc.body.appendChild(b);
      b.onclick = listener;
      ifr.remove();
      return make_weak_ref(b);
    }

    function setupTarget2() {
      let ifr = document.getElementById("target2");
      let doc = ifr.contentDocument;
      let b = doc.createElement("button");
      doc.body.appendChild(b);
      let setFunc = new ifr.contentWindow.Function(`
           var b = document.querySelector("button");
           var proto = parent.HTMLElement.prototype;
           var setter = Object.getOwnPropertyDescriptor(proto, "onclick").set;
           // Here the current global (and hence CallbackObject global) will be
           // the parent, the callback will be a known-live thing from the
           // parent, but the incumbent global will be the child.
           setter.call(b, parent.listener);
         `);
      setFunc();
      ifr.remove();
      return make_weak_ref(b);
    }

    addLoadEvent(function() {
      let ref1 = setupTarget1();
      let ref2 = setupTarget2();
      SpecialPowers.exactGC(function () {
        SpecialPowers.forceCC();
        SpecialPowers.forceGC();
        SpecialPowers.forceGC();

        ok(weak_ref_dead(ref1),
           "Should collect cycle through callback global");
        ok(weak_ref_dead(ref2),
           "Should collect cycle through incumbent global");

        SimpleTest.finish();
      });
    });
  </script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<iframe id="target1"></iframe>
<iframe id="target2"></iframe>
<pre id="test"></pre>
</body>
</html>