dom/devicestorage/ipc/test_ipc.html
author Ahmed Kachkach <ahmed.kachkach@gmail.com>
Thu, 10 Jul 2014 13:33:00 +0200
changeset 215477 cf85693280c77de186b5d1ce194615d91e6f430e
parent 155660 0f21776e01b99b87877b1c24c30690cbc9b7548b
child 215498 059b364302aff7371bf82aa4f534ad32e6b915e8
permissions -rw-r--r--
Bug 886570 - Initial integration of structured logs in mochitest. r=ahal,Ms2ger

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for OOP DeviceStorage</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
  <body>

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

    SimpleTest.waitForExplicitFinish();

    function iframeScriptFirst() {
      content.wrappedJSObject.RunSet.reloadAndRunAll({
          preventDefault: function() { },
          __exposedProps__: { preventDefault: 'r' }
      });
    }

    function iframeScriptSecond() {
      let TestRunner = content.wrappedJSObject.TestRunner;

      let oldComplete = TestRunner.onComplete;

      TestRunner.onComplete = function() {
        TestRunner.onComplete = oldComplete;

        sendAsyncMessage("test:DeviceStorage:ipcTestComplete", {
          result: JSON.stringify(TestRunner._failedTests)
        });

        if (oldComplete) {
          oldComplete();
        }
      };

      TestRunner.structuredLogger._dumpMessage = function(msg) {
        sendAsyncMessage("test:DeviceStorage:ipcTestMessage", { msg: msg });
      }
    }

    let VALID_ACTIONS = ['suite_start', 'suite_end', 'test_start', 'test_end', 'test_status', 'process_output', 'log'];
    function validStructuredMessage(message) {
      return message.action !== undefined && VALID_ACTIONS.indexOf(message.action) >= 0;
    }
    function onTestMessage(data) {
      let message = SpecialPowers.wrap(data).data.msg;

      if (validStructuredMessage(message)) {
        if (message.test === undefined || message.message === undefined) {
          return;
        }

        switch (message.action) {
          case "test_status":
          case "test_end":
            ok(message.expected === undefined, message.test, message.message);
            break;
          default:
            let details = message.test + " | " + message.message;
            info(details);
        }
      }
    }

    function onTestComplete() {
      let comp = SpecialPowers.wrap(SpecialPowers.Components);
      let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
      let spObserver = comp.classes["@mozilla.org/special-powers-observer;1"]
                            .getService(comp.interfaces.nsIMessageListener);

      mm.removeMessageListener("SPPrefService", spObserver);
      mm.removeMessageListener("SPProcessCrashService", spObserver);
      mm.removeMessageListener("SPPingService", spObserver);
      mm.removeMessageListener("SpecialPowers.Quit", spObserver);
      mm.removeMessageListener("SPPermissionManager", spObserver);

      mm.removeMessageListener("test:DeviceStorage:ipcTestMessage", onTestMessage);
      mm.removeMessageListener("test:DeviceStorage:ipcTestComplete", onTestComplete);

      SimpleTest.executeSoon(function () { SimpleTest.finish(); });
    }

    function runTests() {
      let iframe = document.createElement("iframe");
      SpecialPowers.wrap(iframe).mozbrowser = true;
      iframe.id = "iframe";
      iframe.style.width = "100%";
      iframe.style.height = "1000px";

      function iframeLoadSecond() {
        ok(true, "Got second iframe load event.");
        iframe.removeEventListener("mozbrowserloadend", iframeLoadSecond);
        let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
        mm.loadFrameScript("data:,(" + iframeScriptSecond.toString() + ")();",
                           false);
      }

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

        let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);

        let comp = SpecialPowers.wrap(SpecialPowers.Components);

        let spObserver =
          comp.classes["@mozilla.org/special-powers-observer;1"]
              .getService(comp.interfaces.nsIMessageListener);

        mm.addMessageListener("SPPrefService", spObserver);
        mm.addMessageListener("SPProcessCrashService", spObserver);
        mm.addMessageListener("SPPingService", spObserver);
        mm.addMessageListener("SpecialPowers.Quit", spObserver);
        mm.addMessageListener("SPPermissionManager", spObserver);

        mm.addMessageListener("test:DeviceStorage:ipcTestMessage", onTestMessage);
        mm.addMessageListener("test:DeviceStorage:ipcTestComplete", onTestComplete);

        let specialPowersBase = "chrome://specialpowers/content/";
        mm.loadFrameScript(specialPowersBase + "MozillaLogger.js", false);
        mm.loadFrameScript(specialPowersBase + "specialpowersAPI.js", false);
        mm.loadFrameScript(specialPowersBase + "specialpowers.js", false);

        mm.loadFrameScript("data:,(" + iframeScriptFirst.toString() + ")();", false);
      }

      iframe.addEventListener("mozbrowserloadend", iframeLoadFirst);

      // Strip this filename and one directory level and then add "/test".
      let href =  window.location.href;
      href = href.substring(0, href.lastIndexOf('/'));
      href = href.substring(0, href.lastIndexOf('/'));
      iframe.src = href + "/test?consoleLevel=INFO";

      document.body.appendChild(iframe);
    }

    addEventListener("load", function() {

      SpecialPowers.addPermission("browser", true, document);
      SpecialPowers.pushPrefEnv({
        "set": [

          ["device.storage.enabled", true],
          ["device.storage.testing", true],
          ["device.storage.prompt.testing", true],

          // TODO: remove this as part of bug 820712
          ["network.disable.ipc.security", true],

          ["dom.ipc.browser_frames.oop_by_default", true],
          ["dom.mozBrowserFramesEnabled", true],
          ["browser.pagethumbnails.capturing_disabled", true]
        ]
      }, runTests);
    });

  </script>
</body>
</html>