js/xpconnect/tests/chrome/test_evalInWindow.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/chrome/test_evalInWindow.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     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=877673
    1.10 +-->
    1.11 +<window title="Mozilla Bug 877673"
    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 +  </body>
    1.19 +
    1.20 +  <!-- test code goes here -->
    1.21 +  <script type="application/javascript"><![CDATA[
    1.22 +      SimpleTest.waitForExplicitFinish();
    1.23 +      const Cu = Components.utils;
    1.24 +      var sb = new Cu.Sandbox("http://example.org", {wantExportHelpers: true});
    1.25 +      sb.ok = ok;
    1.26 +
    1.27 +      function executeIn(frame, script, exceptionCb) {
    1.28 +        sb.frame = frame;
    1.29 +        sb.exceptionCb = exceptionCb;
    1.30 +        if (exceptionCb) {
    1.31 +          return Cu.evalInSandbox("try {evalInWindow('" + script + "',frame); ok(false, 'Exception should have been thrown.')} catch(e) {exceptionCb(e)}", sb);
    1.32 +        }
    1.33 +
    1.34 +        return Cu.evalInSandbox("evalInWindow('" + script + "',frame)", sb);
    1.35 +      }
    1.36 +
    1.37 +      function testSameOrigin(frame) {
    1.38 +        frame.contentWindow.document.wrappedJSObject.str = "foobar";
    1.39 +        is(executeIn(frame.contentWindow, "document.str"), "foobar",
    1.40 +           "Same origin string property access.");
    1.41 +
    1.42 +        executeIn(frame.contentWindow, 'document.obj = {prop: "foobar"}');
    1.43 +        is((executeIn(frame.contentWindow, "document.obj")).prop, "foobar",
    1.44 +           "Same origin object property access (cloning).");
    1.45 +        isnot(executeIn(frame.contentWindow, "document.obj"), frame.contentWindow.document.wrappedJSObject.obj,
    1.46 +              "Ensure cloning for js objects.");
    1.47 +        is(executeIn(frame.contentWindow, "document"), frame.contentWindow.document,
    1.48 +           "Xrayables should just pass without cloning.");
    1.49 +        is( executeIn(frame.contentWindow, "({a:{doc: document}})").a.doc, frame.contentWindow.document,
    1.50 +           "Deep cloning works.");
    1.51 +
    1.52 +        executeIn(frame.contentWindow, "throw 42", function(e){is(e, 42,
    1.53 +                                                                  "Exception was thrown from script.")});
    1.54 +
    1.55 +        executeIn(frame.contentDocument, "var a = 42;", function(e){ok(e.toString().indexOf("Second argument must be a window") > -1,
    1.56 +                                                                       "Passing non-window to evalInWindow should throw.");});
    1.57 +        // evalInWindow is also available from Cu:
    1.58 +        is(Cu.evalInWindow("document.str", frame.contentWindow), "foobar");
    1.59 +        testDone();
    1.60 +      }
    1.61 +
    1.62 +      function testCrossOrigin(frame) {
    1.63 +        executeIn(frame.contentWindow, "var a = 42;", function(e){ok(e.toString().indexOf("Permission denied") > -1,
    1.64 +                                                                     "Executing script in a window from cross origin should throw.");});
    1.65 +        testDone();
    1.66 +      }
    1.67 +
    1.68 +      var testsRun = 0;
    1.69 +      function testDone() {
    1.70 +        if (++testsRun == 2)
    1.71 +          SimpleTest.finish();
    1.72 +      }
    1.73 +  ]]></script>
    1.74 +  <iframe src="http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html"
    1.75 +          onload="testSameOrigin(this)">
    1.76 +  </iframe>
    1.77 +  <iframe src="http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_empty.html"
    1.78 +          onload="testCrossOrigin(this)">
    1.79 +  </iframe>
    1.80 +</window>
    1.81 \ No newline at end of file

mercurial