tor-browser

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

test_setting_opener.html (4478B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=868996
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Test for Bug 868996</title>
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     11  <script type="application/javascript">
     12 
     13  /** Test for Bug 868996 */
     14  SimpleTest.waitForExplicitFinish();
     15 
     16  var sb1, sb2;
     17  var Cu = SpecialPowers.Cu;
     18  
     19  function testOpenerSet() {
     20    // Use setTimeout to make the relevant onerror run in this window
     21    var win = window.open("file1_setting_opener.html");
     22    // A sandbox for the window
     23    sb1 = new Cu.Sandbox(win, {wantXrays: true })
     24    sb1.win = win
     25    // And a sandbox using the expanded principal.
     26    sb2 = new Cu.Sandbox([win], {wantXrays: true })
     27    sb2.win = win
     28  }
     29 
     30  function evalsb(str, sb) {
     31    // Have to unwrap() to get objects we care about
     32    return SpecialPowers.unwrap(Cu.evalInSandbox(str, sb));
     33  }
     34 
     35  function basicOpenerTest(win) {
     36    is(win.opener, window, "Opening a window should give it the right opener");
     37    is(evalsb("win.opener", sb1), window,
     38       "Reading opener in sandbox 1 should work");
     39    is(evalsb("win.opener", sb2), window,
     40       "Reading opener in sandbox 2 should work");
     41 
     42    win.opener = $("x").contentWindow;
     43    evalsb("win.opener = win.opener.document.getElementById('y').contentWindow", sb1);
     44    evalsb("win.opener = win.opener.document.getElementById('z').contentWindow", sb2);
     45 
     46    is(win.opener, $("x").contentWindow, "Should be able to set an opener to a different window");
     47    is(evalsb("win.opener", sb1), $("y").contentWindow,
     48       "Should be able to set the opener to a different window in a sandbox one");
     49    is(evalsb("win.opener", sb2), $("z").contentWindow,
     50       "Should be able to set the opener to a different window in a sandbox two");
     51 
     52    win.location = "file2_setting_opener.html";
     53  }
     54 
     55  function continueOpenerTest(win) {
     56    is(win.opener, window, "Navigating a window should have reset the opener we stashed on it temporarily");
     57    is(evalsb("win.opener", sb1), window,
     58       "Navigating a window should have reset the opener in sb1");
     59    is(evalsb("win.opener", sb2), window,
     60       "Navigating a window should have reset the opener in sb2");
     61 
     62    win.opener = 5;
     63    evalsb("win.opener = 5", sb1);
     64    evalsb("win.opener = 5", sb2);
     65    is(win.opener, 5, "Should be able to set an opener to a primitive");
     66    is(evalsb("win.opener", sb1), 5,
     67       "Should be able to set the opener to a primitive in a sandbox one");
     68    is(evalsb("win.opener", sb2), 5,
     69       "Should be able to set the opener to a primitive in a sandbox two");
     70    win.location = "file3_setting_opener.html";
     71  }
     72 
     73  function continueOpenerTest2(win) {
     74    is(win.opener, window,
     75       "Navigating a window again should have reset the opener we stashed on it temporarily");
     76    is(evalsb("win.opener", sb1), window,
     77       "Navigating a window again should have reset the opener in sb1");
     78    is(evalsb("win.opener", sb2), window,
     79       "Navigating a window again should have reset the opener in sb2");
     80 
     81    win.opener = null;
     82    is(win.opener, null, "Should be able to set the opener to null");
     83    is(evalsb("win.opener", sb1), null,
     84       "Setting the opener to null should be visible in sb1");
     85    is(evalsb("win.opener", sb2), null,
     86       "Setting the opener to null should be visible in sb2");
     87 
     88    win.location = "file4_setting_opener.html";
     89    // Now poll for that load, since we have no way for the window to
     90    // communicate with us now
     91    setTimeout(checkForLoad, 0, win);
     92  }
     93 
     94  function checkForLoad(win) {
     95    if (!win.document.documentElement ||
     96        win.document.documentElement.innerText != "Loaded") {
     97      setTimeout(checkForLoad, 0, win);
     98      return;
     99    }
    100 
    101    is(win.opener, null, "Null opener should persist across navigations");
    102    is(evalsb("win.opener", sb1), null,
    103       "Null opener should persist across navigations in sb1");
    104    is(evalsb("win.opener", sb2), null,
    105       "Null opener should persist across navigations in sb2");
    106 
    107    win.close();
    108    SimpleTest.finish();
    109  }
    110 
    111  addLoadEvent(testOpenerSet);
    112  </script>
    113 </head>
    114 <body>
    115 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=868996">Mozilla Bug 868996</a>
    116 <p id="display"></p>
    117 <div id="content" style="display: none">
    118 <iframe id="x"></iframe>
    119 <iframe id="y"></iframe>
    120 <iframe id="z"></iframe>
    121 </div>
    122 <pre id="test">
    123 </pre>
    124 </body>
    125 </html>