summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/bugs/test_bug346659.html
blob: 78c1fc659f9e92223dbefc1251d833455ec5ba10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=346659
-->
<head>
  <title>Test for Bug 346659</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=346659">Mozilla Bug 346659</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 346659 **/
var numTests = 10;
SimpleTest.requestLongerTimeout(2); // test takes a long time on android and b2g emulators
SimpleTest.waitForExplicitFinish();

var wins = [];

function r(base, tail) {
  return base.replace(/\/[^\/]*$/, "/" + tail);
}

function handleCmd(evt) {
  var cmd;
  try {
    cmd = JSON.parse(evt.data);
  } catch (e) {
    // Not json
    return false;
  }  

  // Grab privileges so we can access cross-domain windows
  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

  if ("load" in cmd) {
    var testNum = cmd.load;
    var win = wins[testNum];
    win.childWin.x = testNum;
    if (win.childWin.opener == win) {
      if ("xsite" in cmd) {
        var loc = r(window.location.href, "bug346659-opener-echoer.html");
      } else {
        var loc = r(win.location.href, "bug346659-opener-echoer.html");
      }
    } else {
      if ("xsite" in cmd) {
        var loc = r(window.location.href, "bug346659-parent-echoer.html");
      } else {
        var loc = r(win.location.href, "bug346659-parent-echoer.html");
      }
    }
    win.childWin.location.href = loc;
    wins[testNum] = null;
  } else if ("write" in cmd) {
    var testNum = cmd.write;
    var win = wins[testNum];
    win.childWin.x = testNum;
    try {
      if (win.childWin.opener == win) {
        win.childWin.document.write('<script>window.opener.opener.postMessage(window.opener.testNum + " - " + window.x, "http://mochi.test:8888/"); window.opener.close(); window.close();<' + '/script>');
      } else {
        win.childWin.document.write('<script>window.parent.opener.postMessage(window.parent.testNum + " - " + window.x, "http://mochi.test:8888/"); window.parent.close();<' + '/script>');
      }
    } catch (e if (e.name == "SecurityError" && e.code == 18)) {
      // Security error on cross-site write() is fine
      if (win.childWin.opener == win) {
        win.childWin.close();
      }
      win.close()
      handleTestEnd();
    }
    wins[testNum] = null;
  }
  return true;
}

function messageReceiver(evt) {
  // First try to detect a load/write command
  if (handleCmd(evt)) {
    return;
  }

  var testNumber = parseInt(evt.data);
  var testResult = evt.data.substring(3 + Math.floor(Math.log(testNumber) * Math.LOG10E + 1));

  switch (testNumber) {
    case 1:
      is(testResult, "1", "Props on new window should be preserved when loading");
      break;
    case 2:
      is(testResult, "2", "Props on new window should be preserved when writing");
      break;
    case 3:
      is(testResult, "3", "Props on window opened from new window should be preserved when loading");
      break;
    case 4:
      is(testResult, "4", "Props on window opened from new window should be preserved when writing");
      break;
    case 5:
      is(testResult, "undefined", "Props on new window's child should go away when loading");
      break;
    case 6:
      is(testResult, "undefined", "Props on new window's child should go away when writing");
      break;
    case 7:
      is(testResult, "7", "Props on different-domain window opened from different-domain new window can stay");
      break;
    case 9:
      is(testResult, "undefined", "Props on different-domain new window's child should go away when loading");
      break;
    case 11:
      is(testResult, "undefined", "Props on same-domain window opened from different-domain new window should go away when loading");
      break;
    case 12:
      is(testResult, "undefined", "Props on different-domain new window's same-domain child should go away when loading");
      break;
    default:
      ok(0, "unexpected test number (" + testNumber + ") when data is " + evt.data);
  }

  handleTestEnd();
}

function handleTestEnd() {
  if (!--numTests) {
    SimpleTest.finish();
  }
}
window.addEventListener("message", messageReceiver, false);

var win = window.open("");
win.x = 1;
win.location.href = "bug346659-echoer.html";

win = window.open("");
win.x = 2;
win.document.write('<script> window.opener.postMessage("2 - " + window.x, window.location.href); window.close(); </' + 'script>');

wins[3] = window.open('bug346659-opener.html?{"load":3}');
wins[4] = window.open('bug346659-opener.html?{"write":4}');
wins[5] = window.open('bug346659-parent.html?{"load":5}');
wins[6] = window.open('bug346659-parent.html?{"write":6}');

is(location.host, "mochi.test:8888", "Unexpected host");

var baseurl = window.location.href.replace(/mochi\.test:8888/, "example.com");
wins[7] = window.open(r(baseurl, 'bug346659-opener.html?{"load":7}'));
wins[9] = window.open(r(baseurl, 'bug346659-parent.html?{"load":9}'));

wins[11] = window.open(r(baseurl, 'bug346659-opener.html?{"load":11,"xsite":true}'));
wins[12] = window.open(r(baseurl, 'bug346659-parent.html?{"load":12,"xsite":true}'));
</script>
</pre>
</body>
</html>