test_evalInWindow.xul 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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=877673
  7. -->
  8. <window title="Mozilla Bug 877673"
  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. </body>
  15. <!-- test code goes here -->
  16. <script type="application/javascript"><![CDATA[
  17. SimpleTest.waitForExplicitFinish();
  18. const Cu = Components.utils;
  19. var sb = new Cu.Sandbox("http://example.org", {wantExportHelpers: true});
  20. sb.ok = ok;
  21. function executeIn(frame, script, exceptionCb) {
  22. sb.frame = frame;
  23. sb.exceptionCb = exceptionCb;
  24. if (exceptionCb) {
  25. return Cu.evalInSandbox("try {frame.eval('" + script + "'); ok(false, 'Exception should have been thrown.')} catch(e) {exceptionCb(e)}", sb);
  26. }
  27. return Cu.evalInSandbox("frame.eval('" + script + "')", sb);
  28. }
  29. function testSameOrigin(frame) {
  30. frame.contentWindow.document.wrappedJSObject.str = "foobar";
  31. is(executeIn(frame.contentWindow, "document.str"), "foobar",
  32. "Same origin string property access.");
  33. executeIn(frame.contentWindow, 'document.obj = {prop: "foobar"}');
  34. is((executeIn(frame.contentWindow, "document.obj")).prop, "foobar",
  35. "Same origin object property access (cloning).");
  36. isnot(executeIn(frame.contentWindow, "document.obj"), frame.contentWindow.document.wrappedJSObject.obj,
  37. "Ensure cloning for js objects.");
  38. is(executeIn(frame.contentWindow, "document"), frame.contentWindow.document,
  39. "Xrayables should just pass without cloning.");
  40. is( executeIn(frame.contentWindow, "({a:{doc: document}})").a.doc, frame.contentWindow.document,
  41. "Deep cloning works.");
  42. executeIn(frame.contentWindow, "throw 42", function(e){is(e, 42,
  43. "Exception was thrown from script.")});
  44. testDone();
  45. }
  46. function testCrossOrigin(frame) {
  47. executeIn(frame.contentWindow, "var a = 42;", function(e){ok(e.toString().indexOf("Permission denied") > -1,
  48. "Executing script in a window from cross origin should throw.");});
  49. testDone();
  50. }
  51. var testsRun = 0;
  52. function testDone() {
  53. if (++testsRun == 2)
  54. SimpleTest.finish();
  55. }
  56. ]]></script>
  57. <iframe src="http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html"
  58. onload="testSameOrigin(this)">
  59. </iframe>
  60. <iframe src="http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_empty.html"
  61. onload="testCrossOrigin(this)">
  62. </iframe>
  63. </window>