Backed out changeset 713114c0331a (bug 1938707) by developer request CLOSED TREE
[gecko.git] / js / xpconnect / tests / mochitest / test_weakRefs_cross_compartment.html
blob87e509b535875a63d0d276eb0e3f1e6aa5bec6ee
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Test WeakRef works when target is in different compartment in the browser</title>
6 <script src="/tests/SimpleTest/SimpleTest.js"></script>
7 <script type="application/javascript">
8 function go() {
9 SimpleTest.waitForExplicitFinish();
11 let Cu = SpecialPowers.Cu;
12 let isSameCompartment = Cu.getJSTestingFunctions().isSameCompartment;
14 // Open a new window, which will be from different compartment.
15 let win = window.open();
16 is(isSameCompartment(win, window), false,
17 "Test for opeing a window from a different compartment.");
19 let wr1, wr2, wr3;
21 let obj = {};
23 // WeakRef and target are both from different compartment.
24 wr1 = new win.WeakRef(new win.Object());
26 // WeakRef is same compartment, but target isn't.
27 wr2 = new WeakRef(new win.Object());
29 // WeakRef is in different compartment, but target is.
30 wr3 = new win.WeakRef(obj);
32 obj = null;
35 // WeakRef should keep the target in the current task.
36 isnot(wr1.deref(), undefined, "wr1.deref() should return its target.");
37 isnot(wr2.deref(), undefined, "wr2.deref() should return its target.");
38 isnot(wr3.deref(), undefined, "we3.deref() should return its target.");
40 // Weakref should keep the target until the end of current Job, that
41 // includes microtask(Promise).
42 Promise.resolve().then(() => {
43 isnot(wr1.deref(), undefined,
44 "wr1.deref() should return its target in promise");
45 isnot(wr2.deref(), undefined,
46 "wr2.deref() should return its target in promise");
47 isnot(wr3.deref(), undefined,
48 "wr3.deref() should return its target in promise");
49 });
51 // setTimeout will launch a new job and call ClearKeptObjects().
52 setTimeout(() => {
53 // Call gc() forcibly to clear the target of wr.
54 SpecialPowers.DOMWindowUtils.garbageCollect();
56 is(wr1.deref(), undefined, "wr1.deref() should return undefined in the new job.");
57 is(wr2.deref(), undefined, "wr2.deref() should return undefined in the new job.");
58 is(wr3.deref(), undefined, "wr3.deref() should return undefined in the new job.");
60 win.close();
61 SimpleTest.finish();
62 }, 0);
65 </script>
66 </head>
67 <body onload="go()"></body>
68 </html>