js/xpconnect/tests/chrome/test_wrappers-2.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial