tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_weakRefs_cross_compartment.html (2524B)


      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();
     10 
     11        let Cu = SpecialPowers.Cu;
     12        let isSameCompartment = Cu.getJSTestingFunctions().isSameCompartment;
     13 
     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.");
     18 
     19        let wr1, wr2, wr3;
     20        {
     21          let obj = {};
     22 
     23          // WeakRef and target are both from different compartment.
     24          wr1 = new win.WeakRef(new win.Object());
     25 
     26          // WeakRef is same compartment, but target isn't.
     27          wr2 = new WeakRef(new win.Object());
     28 
     29          // WeakRef is in different compartment, but target is.
     30          wr3 = new win.WeakRef(obj);
     31 
     32          obj = null;
     33        }
     34 
     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.");
     39 
     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        });
     50 
     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();
     55 
     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.");
     59 
     60          win.close();
     61          SimpleTest.finish();
     62        }, 0);
     63      }
     64 
     65    </script>
     66  </head>
     67  <body onload="go()"></body>
     68 </html>