dom/tests/mochitest/bugs/test_bug346659.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/bugs/test_bug346659.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,189 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=346659
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 346659</title>
    1.11 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=346659">Mozilla Bug 346659</a>
    1.16 +<p id="display"></p>
    1.17 +<div id="content" style="display: none">
    1.18 +  
    1.19 +</div>
    1.20 +<pre id="test">
    1.21 +<script type="application/javascript">
    1.22 +
    1.23 +/** Test for Bug 346659 **/
    1.24 +// do the tests in two batches, because otherwise the popup blocker kills off
    1.25 +// our test because it opens too many windows.
    1.26 +var numTestsSet1 = 6;
    1.27 +var numTestsSet2 = 4;
    1.28 +var numTestsSet3 = 2;
    1.29 +var complete = 0;
    1.30 +SimpleTest.waitForExplicitFinish();
    1.31 +
    1.32 +var wins = [];
    1.33 +
    1.34 +function r(base, tail) {
    1.35 +  return base.replace(/\/[^\/]*$/, "/" + tail);
    1.36 +}
    1.37 +
    1.38 +function handleCmd(evt) {
    1.39 +  var cmd;
    1.40 +  try {
    1.41 +    cmd = JSON.parse(evt.data);
    1.42 +  } catch (e) {
    1.43 +    // Not json
    1.44 +    return false;
    1.45 +  }  
    1.46 +
    1.47 +  // Grab privileges so we can access cross-domain windows
    1.48 +  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    1.49 +
    1.50 +  if ("load" in cmd) {
    1.51 +    var testNum = cmd.load;
    1.52 +    var win = wins[testNum];
    1.53 +    win.childWin.x = testNum;
    1.54 +    if (win.childWin.opener == win) {
    1.55 +      if ("xsite" in cmd) {
    1.56 +        var loc = r(window.location.href, "bug346659-opener-echoer.html");
    1.57 +      } else {
    1.58 +        var loc = r(win.location.href, "bug346659-opener-echoer.html");
    1.59 +      }
    1.60 +    } else {
    1.61 +      if ("xsite" in cmd) {
    1.62 +        var loc = r(window.location.href, "bug346659-parent-echoer.html");
    1.63 +      } else {
    1.64 +        var loc = r(win.location.href, "bug346659-parent-echoer.html");
    1.65 +      }
    1.66 +    }
    1.67 +    win.childWin.location.href = loc;
    1.68 +    wins[testNum] = null;
    1.69 +  } else if ("write" in cmd) {
    1.70 +    var testNum = cmd.write;
    1.71 +    var win = wins[testNum];
    1.72 +    win.childWin.x = testNum;
    1.73 +    try {
    1.74 +      if (win.childWin.opener == win) {
    1.75 +        win.childWin.document.write('<script>window.opener.opener.postMessage(window.opener.testNum + " - " + window.x, "http://mochi.test:8888/"); window.opener.close(); window.close();<' + '/script>');
    1.76 +      } else {
    1.77 +        win.childWin.document.write('<script>window.parent.opener.postMessage(window.parent.testNum + " - " + window.x, "http://mochi.test:8888/"); window.parent.close();<' + '/script>');
    1.78 +      }
    1.79 +    } catch (e if (e.name == "SecurityError" && e.code == 18)) {
    1.80 +      // Security error on cross-site write() is fine
    1.81 +      if (win.childWin.opener == win) {
    1.82 +        win.childWin.close();
    1.83 +      }
    1.84 +      win.close()
    1.85 +      handleTestEnd();
    1.86 +    }
    1.87 +    wins[testNum] = null;
    1.88 +  }
    1.89 +  return true;
    1.90 +}
    1.91 +
    1.92 +function messageReceiver(evt) {
    1.93 +  // First try to detect a load/write command
    1.94 +  if (handleCmd(evt)) {
    1.95 +    return;
    1.96 +  }
    1.97 +
    1.98 +  var testNumber = parseInt(evt.data);
    1.99 +  var testResult = evt.data.substring(3 + Math.floor(Math.log(testNumber) * Math.LOG10E + 1));
   1.100 +
   1.101 +  switch (testNumber) {
   1.102 +    case 1:
   1.103 +      is(testResult, 1, "Props on new window should be preserved when loading");
   1.104 +      break;
   1.105 +    case 2:
   1.106 +      is(testResult, 2, "Props on new window should be preserved when writing");
   1.107 +      break;
   1.108 +    case 3:
   1.109 +      is(testResult, 3, "Props on window opened from new window should be preserved when loading");
   1.110 +      break;
   1.111 +    case 4:
   1.112 +      is(testResult, 4, "Props on window opened from new window should be preserved when writing");
   1.113 +      break;
   1.114 +    case 5:
   1.115 +      is(testResult, "undefined", "Props on new window's child should go away when loading");
   1.116 +      break;
   1.117 +    case 6:
   1.118 +      is(testResult, "undefined", "Props on new window's child should go away when writing");
   1.119 +      break;
   1.120 +    case 7:
   1.121 +      is(testResult, 7, "Props on different-domain window opened from different-domain new window can stay");
   1.122 +      break;
   1.123 +    case 9:
   1.124 +      is(testResult, "undefined", "Props on different-domain new window's child should go away when loading");
   1.125 +      break;
   1.126 +    case 11:
   1.127 +      is(testResult, "undefined", "Props on same-domain window opened from different-domain new window should go away when loading");
   1.128 +      break;
   1.129 +    case 12:
   1.130 +      is(testResult, "undefined", "Props on different-domain new window's same-domain child should go away when loading");
   1.131 +      break;
   1.132 +    default:
   1.133 +      ok(0, "unexpected test number (" + testNumber + ") when data is " + evt.data);
   1.134 +  }
   1.135 +
   1.136 +  handleTestEnd();
   1.137 +}
   1.138 +
   1.139 +function handleTestEnd() {
   1.140 +  function gc() {
   1.141 +    SpecialPowers.DOMWindowUtils.garbageCollect();
   1.142 +  }
   1.143 +  if (numTestsSet1) {
   1.144 +    if (!--numTestsSet1) {
   1.145 +      // gc to get rid of all the old windows
   1.146 +      gc(); gc(); gc();
   1.147 +      setTimeout(startSecondBatch, 0);
   1.148 +    }
   1.149 +  } else if (numTestsSet2) {
   1.150 +    if (!--numTestsSet2) {
   1.151 +      // gc to get rid of all the old windows
   1.152 +      gc(); gc(); gc();
   1.153 +      setTimeout(startThirdBatch, 0);
   1.154 +    }
   1.155 +  } else if (!--numTestsSet3) {
   1.156 +    SimpleTest.finish();
   1.157 +  }
   1.158 +}
   1.159 +window.addEventListener("message", messageReceiver, false);
   1.160 +
   1.161 +var win = window.open("");
   1.162 +win.x = 1;
   1.163 +win.location.href = "bug346659-echoer.html";
   1.164 +
   1.165 +win = window.open("");
   1.166 +win.x = 2;
   1.167 +win.document.write('<script> window.opener.postMessage("2 - " + window.x, window.location.href); window.close(); </' + 'script>');
   1.168 +
   1.169 +wins[3] = window.open('bug346659-opener.html?{"load":3}');
   1.170 +wins[4] = window.open('bug346659-opener.html?{"write":4}');
   1.171 +wins[5] = window.open('bug346659-parent.html?{"load":5}');
   1.172 +wins[6] = window.open('bug346659-parent.html?{"write":6}');
   1.173 +
   1.174 +is(location.host, "mochi.test:8888", "Unexpected host");
   1.175 +
   1.176 +function startSecondBatch() {
   1.177 +  var baseurl = window.location.href.replace(/mochi\.test:8888/, "example.com");
   1.178 +  wins[7] = window.open(r(baseurl, 'bug346659-opener.html?{"load":7}'));
   1.179 +  wins[8] = window.open(r(baseurl, 'bug346659-opener.html?{"write":8}'));
   1.180 +  wins[9] = window.open(r(baseurl, 'bug346659-parent.html?{"load":9}'));
   1.181 +  wins[10] = window.open(r(baseurl, 'bug346659-parent.html?{"write":10}'));
   1.182 +}
   1.183 +
   1.184 +function startThirdBatch() {
   1.185 +  var baseurl = window.location.href.replace(/mochi\.test:8888/, "example.com");
   1.186 +  wins[11] = window.open(r(baseurl, 'bug346659-opener.html?{"load":11,"xsite":true}'));
   1.187 +  wins[12] = window.open(r(baseurl, 'bug346659-parent.html?{"load":12,"xsite":true}'));
   1.188 +}
   1.189 +</script>
   1.190 +</pre>
   1.191 +</body>
   1.192 +</html>

mercurial