test_wrappers-2.xul 6.9 KB

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