1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/tests/chrome/test_wrappers-2.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,215 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> 1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" 1.7 + type="text/css"?> 1.8 +<!-- 1.9 +https://bugzilla.mozilla.org/show_bug.cgi?id=500931 1.10 +--> 1.11 +<window title="Mozilla Bug 500931" 1.12 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.13 + <script type="application/javascript" 1.14 + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 1.15 + 1.16 + <!-- test results are displayed in the html:body --> 1.17 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.18 + <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=403005" 1.19 + target="_blank">Mozilla Bug 403005</a> 1.20 + <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=409298" 1.21 + target="_blank">Mozilla Bug 409298</a> 1.22 + </body> 1.23 + 1.24 + <!-- test code goes here --> 1.25 + <script type="application/javascript"><![CDATA[ 1.26 + 1.27 + /** Test for Bug 403005 and 409298 **/ 1.28 + 1.29 + SimpleTest.waitForExplicitFinish(); 1.30 + 1.31 + function go() 1.32 + { 1.33 + ok(window instanceof Object, "window is instanceof Object"); 1.34 + 1.35 + let wrap = $('ifr').contentWindow.wrappedJSObject; 1.36 + ok(!('obj' in XPCNativeWrapper(wrap)), "XPCNativeWrapper constructor works"); 1.37 + let iwin = $('ifr').contentWindow; 1.38 + is(iwin.document.ELEMENT_NODE, 1, 'constants work through XrayWrapper'); 1.39 + is(iwin.document.nodeName, "#document", 'attributes work through XrayWrappe 1.40 + 1.41 + location.foopy = 3; 1.42 + 1.43 + var xow_answer = []; 1.44 + for (let i in location) 1.45 + xow_answer.push(i); 1.46 + 1.47 + var xpcnw_answer = []; 1.48 + var xpcnw = new XPCNativeWrapper(location); 1.49 + xpcnw.barpy = 4; 1.50 + for (let i in xpcnw) 1.51 + xpcnw_answer.push(i); 1.52 + 1.53 + var expected = [ 1.54 + "hash", 1.55 + "host", 1.56 + "hostname", 1.57 + "href", 1.58 + "pathname", 1.59 + "port", 1.60 + "protocol", 1.61 + "search", 1.62 + "reload", 1.63 + "replace", 1.64 + "assign", 1.65 + "foopy" 1.66 + ]; 1.67 + 1.68 + var xpcnw_expected = expected.slice(0, expected.length - 1); 1.69 + xpcnw_expected.push("barpy"); 1.70 + 1.71 + is(xow_answer.sort().toString(), 1.72 + expected.sort().toString(), 1.73 + 'enumeration over XOWs walks the prototype chain'); 1.74 + 1.75 + is(xpcnw_answer.sort().toString(), 1.76 + xpcnw_expected.sort().toString(), 1.77 + 'enumeration over XPCNWs walks the prototype chain'); 1.78 + 1.79 + var for_each_expected = []; 1.80 + for each (let i in new XPCNativeWrapper(location)) 1.81 + for_each_expected.push(typeof i); 1.82 + 1.83 + var for_each_answer = []; 1.84 + for each (let i in Iterator(new XPCNativeWrapper(location))) 1.85 + for_each_answer.push(typeof i); 1.86 + 1.87 + is(for_each_answer.sort().toString(), 1.88 + for_each_expected.sort().toString(), 1.89 + 'wrapper iterators are properly iterators'); 1.90 + 1.91 + let sjow_answer = []; 1.92 + let (obj = { a: 3, next:1 }) { 1.93 + for (let i in wrap.to_iterate) 1.94 + sjow_answer.push(i); 1.95 + is(sjow_answer.sort().toString(), ['a', 'next'].sort().toString(), 1.96 + 'enumeration over SJOWs walks the prototype chain'); 1.97 + } 1.98 + 1.99 + sjow_answer = []; 1.100 + for (let i in wrap.location) 1.101 + sjow_answer.push(i); 1.102 + 1.103 + is(sjow_answer.sort().toString(), 1.104 + expected.sort().toString(), 1.105 + 'enumeration over SJOWs walks the prototype chain and works over XOWs'); 1.106 + 1.107 + for (let i in wrap.enumerate) { 1.108 + is(typeof i, "string", "enumeration on wrappers only returns strings"); 1.109 + } 1.110 + 1.111 + isnot(wrap, new XPCNativeWrapper(iwin), 1.112 + 'SJOWs equality hook returns false correctly against XPCNW'); 1.113 + 1.114 + is(typeof(wrap.func), 'function', 1.115 + 'SJOWs look like functions when they wrap functions'); 1.116 + is(typeof(wrap.o), 'object', 1.117 + 'SJOWs look like objects when they wrap objects'); 1.118 + ok(wrap.o === wrap.o, 1.119 + "Identity holds when accessing the same object twice"); 1.120 + 1.121 + var origProto = iwin.__proto__; 1.122 + try { 1.123 + iwin.__proto__ = iwin; 1.124 + ok(false, 'cyclic proto value allowed'); 1.125 + iwin.__proto__ = origProto; 1.126 + } catch (e) { 1.127 + is(e.toString(), 1.128 + 'TypeError: cyclic __proto__ value', 1.129 + 'throw the right exception for a cyclic proto'); 1.130 + is(iwin.__proto__, origProto, 'reset __proto__ after a cyclic proto'); 1.131 + } 1.132 + 1.133 + try { 1.134 + is('PASS', iwin.eval("'PASS'"), 'iwin.eval throws an exception'); 1.135 + } catch (e) { 1.136 + ok(false, 'iwin.eval does not throw an exception'); 1.137 + } 1.138 + 1.139 + try { 1.140 + new XPCNativeWrapper(""); 1.141 + ok(false, "How did we construct a wrapper around a primitive?"); 1.142 + } catch (e) { 1.143 + ok(true, "Unable to new XPCNativeWrapper(primitive)"); 1.144 + } 1.145 + 1.146 + try { 1.147 + is(XPCNativeWrapper(""), "", "XPCNativeWrapper as a function allows primitives"); 1.148 + } catch (e) { 1.149 + ok(false, "Unable to wrap a primitive, even without 'new'"); 1.150 + } 1.151 + 1.152 + // Some tests for SJOWs too. 1.153 + isnot(wrap.document.body != document.body, "body should not be the same"); 1.154 + 1.155 + XPCNativeWrapper.prototype = {}; 1.156 + let (w = new XPCNativeWrapper(iwin)) { 1.157 + ok(iwin.location, "able to set XPCNativeWrapper.prototype and do stuff with it"); 1.158 + } 1.159 + 1.160 + is(new XPCNativeWrapper(iwin, Window).closed, false, 1.161 + "able to wrap a window in a window XPCNativeWrapper"); 1.162 + try { 1.163 + new XPCNativeWrapper(document, Window); 1.164 + todo(false, "Able to wrap a document in a Window XPCNativeWrapper?") 1.165 + } catch (e) { 1.166 + ok(/ILLEGAL_VALUE/(e), "not able to wrap a document in a Window XPCNativeWrapper"); 1.167 + } 1.168 + 1.169 + let (w = new XPCNativeWrapper($('ifr').contentWindow)) { 1.170 + w.foopybar = 5; 1.171 + ok(!("foopybar" in iwin), "XPCNativeWrappers allow expandos through"); 1.172 + is(w.foopybar, 5, "can set expandos on XPCNativeWrappers, though"); 1.173 + 1.174 + ok(delete w.foopybar, "deleting properties returns true correctly"); 1.175 + ok(!("foopybar" in w), "Can delete properties from XPCNativeWrappers"); 1.176 + 1.177 + is(w.window, iwin, "w.window exists and is the window"); 1.178 + ok(delete w.iwin, "can delete builtin properties"); 1.179 + is(w.window, iwin, "w.window is automatically recreated"); 1.180 + 1.181 + iwin.foopy = 5; 1.182 + ok(delete w.foopy, "delete returns true"); 1.183 + is(iwin.foopy, 5, "delete doesn't delete underlying properties"); 1.184 + ok(delete iwin.foopy, "can delete window.foopy"); 1.185 + ok(!("foopy" in iwin), "foopy is no longer in window"); 1.186 + } 1.187 + 1.188 + try { 1.189 + is((function(x) { return x+1; }).apply(this, wrap.a), 2, 1.190 + "able to call apply with an XPCSafeJSObjectWrapped array"); 1.191 + } catch (e) { 1.192 + ok(false, 1.193 + "Unable to call apply() with a XPCSafeJSObjectWrapped array"); 1.194 + } 1.195 + 1.196 + try { 1.197 + iwin.__proto__ = null; 1.198 + is(iwin.__proto__, null, 1.199 + "allowed to update iwin.__proto__ to null"); 1.200 + } catch (e) { 1.201 + ok(false, "some crazy exception was thrown"); 1.202 + } 1.203 + 1.204 + try { 1.205 + new XPCSafeJSObjectWrapped(<x />).foo; 1.206 + ok(false, "Allowed to wrap E4X in SJOWs?"); 1.207 + } catch (e) { 1.208 + } 1.209 + 1.210 + SimpleTest.finish(); 1.211 + } 1.212 + ]]></script> 1.213 + <iframe type="content" 1.214 + src="http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_wrappers-2.html" 1.215 + onload="go()" 1.216 + id="ifr"> 1.217 + </iframe> 1.218 +</window>