summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_window_indexing.html
blob: 32713039b07502cfa6346cea36621b13bdd9207a (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=823228
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 823228</title>
  <script 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=823228">Mozilla Bug 823228</a>
<p id="display"></p>
<div id="content" style="display: none">
  <iframe name="x" id="x"></iframe>
  <iframe name="y" id="y"></iframe>
</div>
<pre id="test">
</pre>
  <script type="application/javascript">

  /** Test for Bug 823228 **/
  is(window, window.frames, "These better be equal");
  ok("0" in window, "We have a subframe");
  ok("1" in window, "We have two subframes");
  ok(!("2" in window), "But we don't have three subframes");
  window[2] = "myString";
  ok(!("2" in window), "Should not be able to set indexed expando");
  Object.getPrototypeOf(window)[3] = "Hey there";
  ok("3" in window, "Should be walking up the proto chain");

  is(window[0].name, "x", "First frame is x");
  is(window[1].name, "y", "Second frame is y");
  is(window[2], undefined, "We should still not have our expando");
  is(window[3], "Hey there", "We should still have our prop on the proto chain");

  var x = $("x");
  var y = $("y");

  is(x.contentWindow, window[0], "First frame should have correct window");
  is(y.contentWindow, window[1], "Second frame should have correct window");

  // set() hook test
  throws(TypeError, function () {
    "use strict";
    window[1] = "FAIL strict";
  });

  // set() hook test
  window[1] = "FAIL";
  is(window[1].name, "y", "Second frame is still y");
  y.remove();
  ok(!("1" in window), "We no longer have two subframes");
  is(window[1], undefined, "We should not have a value here");

  // defineProperty() hook test
  function throws(errorCtor, f) {
    try {
      f();
    } catch (exc) {
      if (!(exc instanceof errorCtor))
        throw exc;
      return;
    }
    throw new Error("expected " + errCtor.name + ", no exception thrown: " + f);
  }

  x.parentNode.appendChild(y);
  throws(TypeError, function () {
    Object.defineProperty(window, "1", { value: "FAIL2", configurable: true,
                                         writable: true });
  });
  y.remove();
  ok(!("1" in window), "We no longer have two subframes, again");
  is(window[1], undefined, "We should not have a value here either");

  // More set() hook test
  throws(TypeError, function () {
    "use strict";
    window[1] = "FAIL3 strict";
  });
  window[1] = "FAIL3";
  ok(!("1" in window), "We shouldn't allow indexed expandos");
  is(window[1], undefined, "We should not have a value for an indexed expando");
  var desc = Object.getOwnPropertyDescriptor(window, "1");
  is(desc, undefined, "We really really shouldn't have indexed expandos");

  x.parentNode.appendChild(y);
  is(window[1], y.contentWindow, "Second frame should now be visible");
  desc = Object.getOwnPropertyDescriptor(window, "1");
  ok(desc.configurable, "Subframe should be configurable");
  ok(desc.enumerable, "Subframe should be configurable");
  ok(!desc.writable, "Subframe should not be writable");
  is(desc.value, y.contentWindow, "Subframe should have correct value");

  y.remove();
  is(window[1], undefined, "And now we should be back to no [1] property");

  // And more defineProperty()
  throws(TypeError, function () {
    Object.defineProperty(window, "1", { value: "FAIL2", configurable: true,
                                         writable: true });
  });
  ok(!("1" in window), "Defining indexed properties really just shouldn't work");
  is(window[1], undefined, "Defining past end of list should not work");

  // Enumeration tests
  x.parentNode.appendChild(y);

  var names = Object.getOwnPropertyNames(window);
  is(names[0], "0", "Must start with 0");
  is(names[1], "1", "Must continue with 1");
  is(names.indexOf("2"), -1, "And 2, an attempted expando, should not be in there");
  is(names.indexOf("3"), -1, "But no 3; that's on the proto");

  names = [];
  for (var name in window) {
    names.push(name);
  }
  is(names[0], "0", "Enumeration must start with 0");
  is(names[1], "1", "Enumeration must continue with 1");
  is(names.indexOf("2"), -1, "Enumeration: but no expando 2");
  isnot(names.indexOf("3"), -1, "Enumeration: and then 3, defined on the proto");
  is(names.indexOf("4"), -1, "But no 4 around");

  // Delete tests
  is(delete window[1], false, "Deleting supported index should return false");
  is(window[1], y.contentWindow, "Shouldn't be able to delete a supported index");
  y.remove();
  is(window[1], undefined,
     "And now we should have no property here");
  is(delete window[1], true, "Deleting unsupported index should return true");
  is(window[1], undefined,
     "And we shouldn't have things magically appear due to delete");
  </script>
</body>
</html>