|
1 <?xml version="1.0"?> |
|
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" |
|
4 type="text/css"?> |
|
5 <!-- |
|
6 https://bugzilla.mozilla.org/show_bug.cgi?id=500931 |
|
7 --> |
|
8 <window title="Mozilla Bug 500931" |
|
9 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> |
|
10 <script type="application/javascript" |
|
11 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
12 |
|
13 <!-- test results are displayed in the html:body --> |
|
14 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
15 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=403005" |
|
16 target="_blank">Mozilla Bug 403005</a> |
|
17 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=409298" |
|
18 target="_blank">Mozilla Bug 409298</a> |
|
19 </body> |
|
20 |
|
21 <!-- test code goes here --> |
|
22 <script type="application/javascript"><![CDATA[ |
|
23 |
|
24 /** Test for Bug 403005 and 409298 **/ |
|
25 |
|
26 SimpleTest.waitForExplicitFinish(); |
|
27 |
|
28 function go() |
|
29 { |
|
30 ok(window instanceof Object, "window is instanceof Object"); |
|
31 |
|
32 let wrap = $('ifr').contentWindow.wrappedJSObject; |
|
33 ok(!('obj' in XPCNativeWrapper(wrap)), "XPCNativeWrapper constructor works"); |
|
34 let iwin = $('ifr').contentWindow; |
|
35 is(iwin.document.ELEMENT_NODE, 1, 'constants work through XrayWrapper'); |
|
36 is(iwin.document.nodeName, "#document", 'attributes work through XrayWrappe |
|
37 |
|
38 location.foopy = 3; |
|
39 |
|
40 var xow_answer = []; |
|
41 for (let i in location) |
|
42 xow_answer.push(i); |
|
43 |
|
44 var xpcnw_answer = []; |
|
45 var xpcnw = new XPCNativeWrapper(location); |
|
46 xpcnw.barpy = 4; |
|
47 for (let i in xpcnw) |
|
48 xpcnw_answer.push(i); |
|
49 |
|
50 var expected = [ |
|
51 "hash", |
|
52 "host", |
|
53 "hostname", |
|
54 "href", |
|
55 "pathname", |
|
56 "port", |
|
57 "protocol", |
|
58 "search", |
|
59 "reload", |
|
60 "replace", |
|
61 "assign", |
|
62 "foopy" |
|
63 ]; |
|
64 |
|
65 var xpcnw_expected = expected.slice(0, expected.length - 1); |
|
66 xpcnw_expected.push("barpy"); |
|
67 |
|
68 is(xow_answer.sort().toString(), |
|
69 expected.sort().toString(), |
|
70 'enumeration over XOWs walks the prototype chain'); |
|
71 |
|
72 is(xpcnw_answer.sort().toString(), |
|
73 xpcnw_expected.sort().toString(), |
|
74 'enumeration over XPCNWs walks the prototype chain'); |
|
75 |
|
76 var for_each_expected = []; |
|
77 for each (let i in new XPCNativeWrapper(location)) |
|
78 for_each_expected.push(typeof i); |
|
79 |
|
80 var for_each_answer = []; |
|
81 for each (let i in Iterator(new XPCNativeWrapper(location))) |
|
82 for_each_answer.push(typeof i); |
|
83 |
|
84 is(for_each_answer.sort().toString(), |
|
85 for_each_expected.sort().toString(), |
|
86 'wrapper iterators are properly iterators'); |
|
87 |
|
88 let sjow_answer = []; |
|
89 let (obj = { a: 3, next:1 }) { |
|
90 for (let i in wrap.to_iterate) |
|
91 sjow_answer.push(i); |
|
92 is(sjow_answer.sort().toString(), ['a', 'next'].sort().toString(), |
|
93 'enumeration over SJOWs walks the prototype chain'); |
|
94 } |
|
95 |
|
96 sjow_answer = []; |
|
97 for (let i in wrap.location) |
|
98 sjow_answer.push(i); |
|
99 |
|
100 is(sjow_answer.sort().toString(), |
|
101 expected.sort().toString(), |
|
102 'enumeration over SJOWs walks the prototype chain and works over XOWs'); |
|
103 |
|
104 for (let i in wrap.enumerate) { |
|
105 is(typeof i, "string", "enumeration on wrappers only returns strings"); |
|
106 } |
|
107 |
|
108 isnot(wrap, new XPCNativeWrapper(iwin), |
|
109 'SJOWs equality hook returns false correctly against XPCNW'); |
|
110 |
|
111 is(typeof(wrap.func), 'function', |
|
112 'SJOWs look like functions when they wrap functions'); |
|
113 is(typeof(wrap.o), 'object', |
|
114 'SJOWs look like objects when they wrap objects'); |
|
115 ok(wrap.o === wrap.o, |
|
116 "Identity holds when accessing the same object twice"); |
|
117 |
|
118 var origProto = iwin.__proto__; |
|
119 try { |
|
120 iwin.__proto__ = iwin; |
|
121 ok(false, 'cyclic proto value allowed'); |
|
122 iwin.__proto__ = origProto; |
|
123 } catch (e) { |
|
124 is(e.toString(), |
|
125 'TypeError: cyclic __proto__ value', |
|
126 'throw the right exception for a cyclic proto'); |
|
127 is(iwin.__proto__, origProto, 'reset __proto__ after a cyclic proto'); |
|
128 } |
|
129 |
|
130 try { |
|
131 is('PASS', iwin.eval("'PASS'"), 'iwin.eval throws an exception'); |
|
132 } catch (e) { |
|
133 ok(false, 'iwin.eval does not throw an exception'); |
|
134 } |
|
135 |
|
136 try { |
|
137 new XPCNativeWrapper(""); |
|
138 ok(false, "How did we construct a wrapper around a primitive?"); |
|
139 } catch (e) { |
|
140 ok(true, "Unable to new XPCNativeWrapper(primitive)"); |
|
141 } |
|
142 |
|
143 try { |
|
144 is(XPCNativeWrapper(""), "", "XPCNativeWrapper as a function allows primitives"); |
|
145 } catch (e) { |
|
146 ok(false, "Unable to wrap a primitive, even without 'new'"); |
|
147 } |
|
148 |
|
149 // Some tests for SJOWs too. |
|
150 isnot(wrap.document.body != document.body, "body should not be the same"); |
|
151 |
|
152 XPCNativeWrapper.prototype = {}; |
|
153 let (w = new XPCNativeWrapper(iwin)) { |
|
154 ok(iwin.location, "able to set XPCNativeWrapper.prototype and do stuff with it"); |
|
155 } |
|
156 |
|
157 is(new XPCNativeWrapper(iwin, Window).closed, false, |
|
158 "able to wrap a window in a window XPCNativeWrapper"); |
|
159 try { |
|
160 new XPCNativeWrapper(document, Window); |
|
161 todo(false, "Able to wrap a document in a Window XPCNativeWrapper?") |
|
162 } catch (e) { |
|
163 ok(/ILLEGAL_VALUE/(e), "not able to wrap a document in a Window XPCNativeWrapper"); |
|
164 } |
|
165 |
|
166 let (w = new XPCNativeWrapper($('ifr').contentWindow)) { |
|
167 w.foopybar = 5; |
|
168 ok(!("foopybar" in iwin), "XPCNativeWrappers allow expandos through"); |
|
169 is(w.foopybar, 5, "can set expandos on XPCNativeWrappers, though"); |
|
170 |
|
171 ok(delete w.foopybar, "deleting properties returns true correctly"); |
|
172 ok(!("foopybar" in w), "Can delete properties from XPCNativeWrappers"); |
|
173 |
|
174 is(w.window, iwin, "w.window exists and is the window"); |
|
175 ok(delete w.iwin, "can delete builtin properties"); |
|
176 is(w.window, iwin, "w.window is automatically recreated"); |
|
177 |
|
178 iwin.foopy = 5; |
|
179 ok(delete w.foopy, "delete returns true"); |
|
180 is(iwin.foopy, 5, "delete doesn't delete underlying properties"); |
|
181 ok(delete iwin.foopy, "can delete window.foopy"); |
|
182 ok(!("foopy" in iwin), "foopy is no longer in window"); |
|
183 } |
|
184 |
|
185 try { |
|
186 is((function(x) { return x+1; }).apply(this, wrap.a), 2, |
|
187 "able to call apply with an XPCSafeJSObjectWrapped array"); |
|
188 } catch (e) { |
|
189 ok(false, |
|
190 "Unable to call apply() with a XPCSafeJSObjectWrapped array"); |
|
191 } |
|
192 |
|
193 try { |
|
194 iwin.__proto__ = null; |
|
195 is(iwin.__proto__, null, |
|
196 "allowed to update iwin.__proto__ to null"); |
|
197 } catch (e) { |
|
198 ok(false, "some crazy exception was thrown"); |
|
199 } |
|
200 |
|
201 try { |
|
202 new XPCSafeJSObjectWrapped(<x />).foo; |
|
203 ok(false, "Allowed to wrap E4X in SJOWs?"); |
|
204 } catch (e) { |
|
205 } |
|
206 |
|
207 SimpleTest.finish(); |
|
208 } |
|
209 ]]></script> |
|
210 <iframe type="content" |
|
211 src="http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_wrappers-2.html" |
|
212 onload="go()" |
|
213 id="ifr"> |
|
214 </iframe> |
|
215 </window> |