content/base/test/test_messagemanager_principal.html
author Margaret Leibovic <margaret.leibovic@gmail.com>
Mon, 25 Aug 2014 15:28:10 -0700
changeset 216776 8d7b3bfaf3ab9bb3c32afb51d9714246d7c854f4
parent 168402 fecda5f4a0dfbe47db97c7a1663a8c2583e61d25
permissions -rw-r--r--
Bug 996753 - Telemetry probes for settings pages. r=liuche, a=sledru

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Principal in MessageManager</title>
  <script type="application/javascript" 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;version=1.7">
    "use strict";

    const Ci = Components.interfaces;
    const Cc = Components.classes;
    const Cu = Components.utils;

    var permManager = Cc["@mozilla.org/permissionmanager;1"]
                        .getService(Ci.nsIPermissionManager);

    SimpleTest.waitForExplicitFinish();

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

    function childFrameScript() {
      "use strict";

      addMessageListener("test:ipcMessage", function(message) {
        sendAsyncMessage(message.name, "principal: " + (message.principal ? "OK" : "KO"));

        sendAsyncMessage(message.name, "principal.appId: " +
                         ("appId" in message.principal ? "OK" : "KO"));

        sendAsyncMessage(message.name, "principal.origin: " +
                         ("origin" in message.principal ? "OK" : "KO"));

        sendAsyncMessage(message.name, "principal.isInBrowserElement: " +
                         ("isInBrowserElement" in message.principal ? "OK" : "KO"));

        sendAsyncMessage(message.name, "DONE");
      });
    }

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

      let iframe = document.createElement("iframe");
      SpecialPowers.wrap(iframe).mozbrowser = true;
      iframe.id = "iframe";
      iframe.src = childFrameURL;

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

        let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
        mm.addMessageListener("test:ipcMessage", function(message) {
          // We need to wrap to access message.json, and unwrap to do the
          // identity check.
          var msg = SpecialPowers.unwrap(SpecialPowers.wrap(message).json);
          if (/OK$/.exec(msg)) {
            ok(true, msg);
          } else if(/KO$/.exec(msg)) {
            ok(true, false);
          } else if (/DONE/.exec(msg)) {
            permManager.removeFromPrincipal(window.document.nodePrincipal, "browser",
                                            Ci.nsIPermissionManager.ALLOW_ACTION);
            SimpleTest.finish();
          }
        });
        mm.loadFrameScript("data:,(" + childFrameScript.toString() + ")();",
                           false);

        mm.sendAsyncMessage("test:ipcMessage", 42, null, window.document.nodePrincipal);
      });

      document.body.appendChild(iframe);
    }

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

      permManager.addFromPrincipal(window.document.nodePrincipal, "browser",
                                   Ci.nsIPermissionManager.ALLOW_ACTION);

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