dom/base/test/test_setting_opener.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/test/test_setting_opener.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=868996
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Test for Bug 868996</title>
    1.12 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.14 +  <script type="application/javascript">
    1.15 +
    1.16 +  /** Test for Bug 868996 **/
    1.17 +  SimpleTest.waitForExplicitFinish();
    1.18 +
    1.19 +  var sb1, sb2;
    1.20 +  var Cu = SpecialPowers.Cu;
    1.21 +  
    1.22 +  function testOpenerSet() {
    1.23 +    // Use setTimeout to make the relevant onerror run in this window
    1.24 +    var win = window.open("data:text/html,<script>opener.setTimeout(opener.basicOpenerTest, 0, this)</" + "script>");
    1.25 +    // A sandbox for the window
    1.26 +    sb1 = new Cu.Sandbox(win, {wantXrays: true })
    1.27 +    sb1.win = win
    1.28 +    // And a sandbox using the expanded principal.
    1.29 +    sb2 = new Cu.Sandbox([win], {wantXrays: true })
    1.30 +    sb2.win = win
    1.31 +  }
    1.32 +
    1.33 +  function evalsb(str, sb) {
    1.34 +    // Have to unwrap() to get objects we care about
    1.35 +    return SpecialPowers.unwrap(Cu.evalInSandbox(str, sb));
    1.36 +  }
    1.37 +
    1.38 +  function basicOpenerTest(win) {
    1.39 +    is(win.opener, window, "Opening a window should give it the right opener");
    1.40 +    is(evalsb("win.opener", sb1), window,
    1.41 +       "Reading opener in sandbox 1 should work");
    1.42 +    is(evalsb("win.opener", sb2), window,
    1.43 +       "Reading opener in sandbox 2 should work");
    1.44 +
    1.45 +    win.opener = $("x").contentWindow;
    1.46 +    evalsb("win.opener = win.opener.document.getElementById('y').contentWindow", sb1);
    1.47 +    evalsb("win.opener = win.opener.document.getElementById('z').contentWindow", sb2);
    1.48 +
    1.49 +    is(win.opener, $("x").contentWindow, "Should be able to set an opener to a different window");
    1.50 +    is(evalsb("win.opener", sb1), $("y").contentWindow,
    1.51 +       "Should be able to set the opener to a different window in a sandbox one");
    1.52 +    is(evalsb("win.opener", sb2), $("z").contentWindow,
    1.53 +       "Should be able to set the opener to a different window in a sandbox two");
    1.54 +
    1.55 +    win.location = "data:text/html,<script>opener.setTimeout(opener.continueOpenerTest, 0, this);</" + "script>";
    1.56 +  }
    1.57 +
    1.58 +  function continueOpenerTest(win) {
    1.59 +    is(win.opener, window, "Navigating a window should have reset the opener we stashed on it temporarily");
    1.60 +    is(evalsb("win.opener", sb1), window,
    1.61 +       "Navigating a window should have reset the opener in sb1");
    1.62 +    is(evalsb("win.opener", sb2), window,
    1.63 +       "Navigating a window should have reset the opener in sb2");
    1.64 +
    1.65 +    win.opener = 5;
    1.66 +    evalsb("win.opener = 5", sb1);
    1.67 +    evalsb("win.opener = 5", sb2);
    1.68 +    is(win.opener, 5, "Should be able to set an opener to a primitive");
    1.69 +    is(evalsb("win.opener", sb1), 5,
    1.70 +       "Should be able to set the opener to a primitive in a sandbox one");
    1.71 +    is(evalsb("win.opener", sb2), 5,
    1.72 +       "Should be able to set the opener to a primitive in a sandbox two");
    1.73 +    win.location = "data:text/html,<script>opener.setTimeout(opener.continueOpenerTest2, 0, this);</" + "script>";
    1.74 +  }
    1.75 +
    1.76 +  function continueOpenerTest2(win) {
    1.77 +    is(win.opener, window,
    1.78 +       "Navigating a window again should have reset the opener we stashed on it temporarily");
    1.79 +    is(evalsb("win.opener", sb1), window,
    1.80 +       "Navigating a window again should have reset the opener in sb1");
    1.81 +    is(evalsb("win.opener", sb2), window,
    1.82 +       "Navigating a window again should have reset the opener in sb2");
    1.83 +
    1.84 +    win.opener = null;
    1.85 +    is(win.opener, null, "Should be able to set the opener to null");
    1.86 +    is(evalsb("win.opener", sb1), null,
    1.87 +       "Setting the opener to null should be visible in sb1");
    1.88 +    is(evalsb("win.opener", sb2), null,
    1.89 +       "Setting the opener to null should be visible in sb2");
    1.90 +
    1.91 +    win.location = "data:text/html,Loaded";
    1.92 +    // Now poll for that load, since we have no way for the window to
    1.93 +    // communicate with us now
    1.94 +    setTimeout(checkForLoad, 0, win);
    1.95 +  }
    1.96 +
    1.97 +  function checkForLoad(win) {
    1.98 +    if (!win.document.documentElement ||
    1.99 +        win.document.documentElement.textContent != "Loaded") {
   1.100 +      setTimeout(checkForLoad, 0, win);
   1.101 +      return;
   1.102 +    }
   1.103 +
   1.104 +    is(win.opener, null, "Null opener should persist across navigations");
   1.105 +    is(evalsb("win.opener", sb1), null,
   1.106 +       "Null opener should persist across navigations in sb1");
   1.107 +    is(evalsb("win.opener", sb2), null,
   1.108 +       "Null opener should persist across navigations in sb2");
   1.109 +
   1.110 +    win.close();
   1.111 +    SimpleTest.finish();
   1.112 +  }
   1.113 +
   1.114 +  addLoadEvent(testOpenerSet);
   1.115 +  </script>
   1.116 +</head>
   1.117 +<body>
   1.118 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=868996">Mozilla Bug 868996</a>
   1.119 +<p id="display"></p>
   1.120 +<div id="content" style="display: none">
   1.121 +<iframe id="x"></iframe>
   1.122 +<iframe id="y"></iframe>
   1.123 +<iframe id="z"></iframe>
   1.124 +</div>
   1.125 +<pre id="test">
   1.126 +</pre>
   1.127 +</body>
   1.128 +</html>

mercurial