devtools/server/tests/mochitest/test_setupInParentChild.html
author Sebastian Hengst <archaeopteryx@coole-files.de>
Fri, 14 Apr 2017 23:39:22 +0200
changeset 401420 d523c49136775a7187486dbc96858a2cb0383bfb
parent 401401 322fde2d53bf49266dbc293321e6b36a4c789c12
child 401430 56bf52d4ddeb1dc7019c4f9e23961670d0a31414
permissions -rw-r--r--
Backed out changeset 322fde2d53bf (bug 1356569) so bug 1355161 can be backed out. r=backout

<!DOCTYPE HTML>
<html>
<!--
Bug 1181100 - Test DebuggerServerConnection.setupInParent and DebuggerServer.setupInChild
-->
<head>
  <meta charset="utf-8">
  <title>Mozilla Bug</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>
<pre id="test">
<script type="application/javascript">
"use strict";

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

let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
let {DebuggerClient} = require("devtools/shared/client/main");
let {DebuggerServer} = require("devtools/server/main");
let Services = require("Services");

window.onload = function () {
  SimpleTest.waitForExplicitFinish();

  SpecialPowers.pushPrefEnv({
    "set": [
      // Always log packets when running tests.
      ["devtools.debugger.log", true],
      ["dom.mozBrowserFramesEnabled", true]
    ]
  }, runTests);
};

function runTests() {
  // Create a minimal iframe with a message manager
  let iframe = document.createElement("iframe");
  iframe.mozbrowser = true;
  document.body.appendChild(iframe);

  // Instantiate a minimal server
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
  }
  if (!DebuggerServer.createRootActor) {
    DebuggerServer.addBrowserActors();
  }

  // Fake a connection to an iframe
  let transport = DebuggerServer.connectPipe();
  let conn = transport._serverConnection;
  let client = new DebuggerClient(transport);

  // Wait for a response from setupInChild
  const ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
                 .getService(Ci.nsIMessageListenerManager);
  let onChild = msg => {
    ppmm.removeMessageListener("test:setupChild", onChild);
    let args = msg.json;

    is(args[0], 1, "Got first numeric argument");
    is(args[1], "two", "Got second string argument");
    is(args[2].three, true, "Got last JSON argument");

    // Ask the child to call setupInParent
    DebuggerServer.setupInChild({
      module: "chrome://mochitests/content/chrome/devtools/server/tests/mochitest/setup-in-child.js",
      setupChild: "callParent"
    });
  };
  ppmm.addMessageListener("test:setupChild", onChild);

  // Wait also for a reponse from setupInParent called from setup-in-child.js
  let onParent = (_, topic, args) => {
    Services.obs.removeObserver(onParent, "test:setupParent");
    args = JSON.parse(args);

    is(args[0], true, "Got `mm` argument, a message manager");
    ok(args[1].match(/server\d+.conn\d+.child\d+/), "Got `prefix` argument");

    cleanup();
  };
  Services.obs.addObserver(onParent, "test:setupParent", false);

  // Instanciate e10s machinery and call setupInChild
  DebuggerServer.connectToChild(conn, iframe).then(actor => {
    DebuggerServer.setupInChild({
      module: "chrome://mochitests/content/chrome/devtools/server/tests/mochitest/setup-in-child.js",
      setupChild: "setupChild",
      args: [1, "two", {three: true}]
    });
  });

  function cleanup() {
    client.close().then(function () {
      DebuggerServer.destroy();
      iframe.remove();
      SimpleTest.finish();
    });
  }
}
</script>
</pre>
</body>
</html>