summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_setting_opener.html
blob: babdf85705b254969d19e0a897bf2ae54f41fd64 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=868996
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 868996</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 868996 **/
  SimpleTest.waitForExplicitFinish();

  var sb1, sb2;
  var Cu = SpecialPowers.Cu;
  
  function testOpenerSet() {
    // Use setTimeout to make the relevant onerror run in this window
    var win = window.open("data:text/html,<script>opener.setTimeout(opener.basicOpenerTest, 0, this)</" + "script>");
    // A sandbox for the window
    sb1 = new Cu.Sandbox(win, {wantXrays: true })
    sb1.win = win
    // And a sandbox using the expanded principal.
    sb2 = new Cu.Sandbox([win], {wantXrays: true })
    sb2.win = win
  }

  function evalsb(str, sb) {
    // Have to unwrap() to get objects we care about
    return SpecialPowers.unwrap(Cu.evalInSandbox(str, sb));
  }

  function basicOpenerTest(win) {
    is(win.opener, window, "Opening a window should give it the right opener");
    is(evalsb("win.opener", sb1), window,
       "Reading opener in sandbox 1 should work");
    is(evalsb("win.opener", sb2), window,
       "Reading opener in sandbox 2 should work");

    win.opener = $("x").contentWindow;
    evalsb("win.opener = win.opener.document.getElementById('y').contentWindow", sb1);
    evalsb("win.opener = win.opener.document.getElementById('z').contentWindow", sb2);

    is(win.opener, $("x").contentWindow, "Should be able to set an opener to a different window");
    is(evalsb("win.opener", sb1), $("y").contentWindow,
       "Should be able to set the opener to a different window in a sandbox one");
    is(evalsb("win.opener", sb2), $("z").contentWindow,
       "Should be able to set the opener to a different window in a sandbox two");

    win.location = "data:text/html,<script>opener.setTimeout(opener.continueOpenerTest, 0, this);</" + "script>";
  }

  function continueOpenerTest(win) {
    is(win.opener, window, "Navigating a window should have reset the opener we stashed on it temporarily");
    is(evalsb("win.opener", sb1), window,
       "Navigating a window should have reset the opener in sb1");
    is(evalsb("win.opener", sb2), window,
       "Navigating a window should have reset the opener in sb2");

    win.opener = 5;
    evalsb("win.opener = 5", sb1);
    evalsb("win.opener = 5", sb2);
    is(win.opener, 5, "Should be able to set an opener to a primitive");
    is(evalsb("win.opener", sb1), 5,
       "Should be able to set the opener to a primitive in a sandbox one");
    is(evalsb("win.opener", sb2), 5,
       "Should be able to set the opener to a primitive in a sandbox two");
    win.location = "data:text/html,<script>opener.setTimeout(opener.continueOpenerTest2, 0, this);</" + "script>";
  }

  function continueOpenerTest2(win) {
    is(win.opener, window,
       "Navigating a window again should have reset the opener we stashed on it temporarily");
    is(evalsb("win.opener", sb1), window,
       "Navigating a window again should have reset the opener in sb1");
    is(evalsb("win.opener", sb2), window,
       "Navigating a window again should have reset the opener in sb2");

    win.opener = null;
    is(win.opener, null, "Should be able to set the opener to null");
    is(evalsb("win.opener", sb1), null,
       "Setting the opener to null should be visible in sb1");
    is(evalsb("win.opener", sb2), null,
       "Setting the opener to null should be visible in sb2");

    win.location = "data:text/html,Loaded";
    // Now poll for that load, since we have no way for the window to
    // communicate with us now
    setTimeout(checkForLoad, 0, win);
  }

  function checkForLoad(win) {
    if (!win.document.documentElement ||
        win.document.documentElement.textContent != "Loaded") {
      setTimeout(checkForLoad, 0, win);
      return;
    }

    is(win.opener, null, "Null opener should persist across navigations");
    is(evalsb("win.opener", sb1), null,
       "Null opener should persist across navigations in sb1");
    is(evalsb("win.opener", sb2), null,
       "Null opener should persist across navigations in sb2");

    win.close();
    SimpleTest.finish();
  }

  addLoadEvent(testOpenerSet);
  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=868996">Mozilla Bug 868996</a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe id="x"></iframe>
<iframe id="y"></iframe>
<iframe id="z"></iframe>
</div>
<pre id="test">
</pre>
</body>
</html>