<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Principal in MessageManager</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<body>

  <script type="application/javascript">
    "use strict";

    SimpleTest.waitForExplicitFinish();

    const childFrameURL =
      "data:text/html,<!DOCTYPE HTML><html><body></body></html>";

    function childFrameScript() {
      "use strict";


      addMessageListener("test:content", function(message) {
        sendAsyncMessage("test:result", "is nsIPrincipal: " +
                         (message.data instanceof Ci.nsIPrincipal ? "OK" : "KO"));

        sendAsyncMessage("test:result", "principal.origin: " +
                         ("origin" in message.data ? "OK" : "KO"));
      });

      addMessageListener("test:system", function(message) {
        sendAsyncMessage("test:result", "isSystemPrincipal: " +
                         (message.data.isSystemPrincipal ? "OK" : "KO"));
      });

      addMessageListener("test:ep", function(message) {
        sendAsyncMessage("test:result", "expanded principal: " +
                         (message.data.isExpandedPrincipal ? "OK" : "KO"));
        sendAsyncMessage("test:result", "correct origin: " +
                         (message.data.origin == "[Expanded Principal [http://bar.example.com, http://foo.example.com]]" ? "OK" : "KO"));
      });

      addMessageListener("test:null", function(message) {
        sendAsyncMessage("test:result", "is nsIPrincipal: " +
                         (message.data instanceof Ci.nsIPrincipal ? "OK" : "KO"));

        sendAsyncMessage("test:result", "isNullPrincipal: " +
                         (message.data.isNullPrincipal ? "OK" : "KO"));
        sendAsyncMessage("test:result", "DONE");
      });
    }

    function runTests() {
      ok("Browser prefs set.");

      let iframe = document.createXULElement("browser");
      iframe.setAttribute("type", "content");
      iframe.setAttribute("forcemessagemanager", "true");
      iframe.id = "iframe";
      iframe.src = childFrameURL;

      let sb = new Cu.Sandbox(['http://foo.example.com', 'http://bar.example.com']);
      let ep = Cu.getObjectPrincipal(sb);

      iframe.addEventListener("load", function() {
        ok(true, "Got iframe load event.");

        let mm = iframe.messageManager;
        mm.addMessageListener("test:result", function(message) {
          // We need to wrap to access message.json, and unwrap to do the
          // identity check.
          var msg = SpecialPowers.unwrap(SpecialPowers.wrap(message).data);
          if (/OK$/.exec(msg)) {
            ok(true, msg);
          } else if(/KO$/.exec(msg)) {
            ok(true, false);
          } else if (/DONE/.exec(msg)) {
            SimpleTest.finish();
          }
        });
        mm.loadFrameScript("data:,(" + childFrameScript.toString() + ")();",
                           false);

        mm.sendAsyncMessage("test:content", window.document.nodePrincipal);

        let system = Services.scriptSecurityManager.getSystemPrincipal();
        mm.sendAsyncMessage("test:system", system);

        mm.sendAsyncMessage("test:ep", ep);

        let nullP = Services.scriptSecurityManager.createNullPrincipal({});
        mm.sendAsyncMessage("test:null", nullP);
      });

      document.body.appendChild(iframe);
    }

    addEventListener("load", function() {
      info("Got load event.");

      SpecialPowers.pushPrefEnv({
        "set": [
          ["browser.pagethumbnails.capturing_disabled", true]
        ]
      }, runTests);
    });
  </script>
</body>
</html>
