devtools/server/tests/mochitest/test_makeGlobalObjectReference.html
author Kris Maglione <maglione.k@gmail.com>
Mon, 29 Jan 2018 15:20:18 -0800
changeset 454011 e6a7b5e11ba856ee3535f76c6bcca17ea29e3d5f
parent 453889 c6e0fe339cb1e7c0c029e68be5332e52f53ab6b7
child 461246 49cce2c9e2ab21606a9abcdc0ab0af991806957f
permissions -rw-r--r--
Bug 1431533: Part 5a - Auto-rewrite code to use ChromeUtils import methods. r=florian This was done using the following script: https://bitbucket.org/kmaglione/m-c-rewrites/src/37e3803c7a23385bab5ea077ba8692bcfe1608fc/processors/chromeutils-import.jsm MozReview-Commit-ID: 1Nc3XDu0wGl

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=914405

Debugger.prototype.makeGlobalObjectReference should dereference WindowProxy
(outer window) objects.
-->
<head>
  <meta charset="utf-8">
  <title>Mozilla Bug 914405</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>
"use strict";

ChromeUtils.import("resource://gre/modules/jsdebugger.jsm");
addDebuggerToGlobal(this);

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

  let iframe = document.createElement("iframe");
  iframe.src = "iframe1_makeGlobalObjectReference.html";
  iframe.onload = iframeOnLoad;
  document.body.appendChild(iframe);

  function iframeOnLoad() {
    let dbg = new Debugger();

    // 'o' for 'outer window'
    let g1o = iframe.contentWindow;
    ok(!dbg.hasDebuggee(g1o), "iframe is not initially a debuggee");

    // Like addDebuggee, makeGlobalObjectReference innerizes.
    // 'i' stands for 'inner window'.
    // 'DO' stands for 'Debugger.Object'.
    let g1iDO = dbg.makeGlobalObjectReference(g1o);
    ok(!dbg.hasDebuggee(g1o),
       "makeGlobalObjectReference does not add g1 as debuggee, designated via outer");
    ok(!dbg.hasDebuggee(g1iDO),
       "makeGlobalObjectReference does not add g1 as debuggee, designated via D.O ");

    // Wrapping an object automatically outerizes it, so dereferencing an
    // inner object D.O gets you an outer object.
    // ('===' does distinguish inner and outer objects.)
    // (That's a capital '=', if you must know.)
    ok(g1iDO.unsafeDereference() === g1o, "g1iDO has the right referent");

    // However, Debugger.Objects do distinguish inner and outer windows.
    let g1oDO = g1iDO.makeDebuggeeValue(g1o);
    ok(g1iDO !== g1oDO, "makeDebuggeeValue doesn't innerize");
    ok(g1iDO.unsafeDereference() === g1oDO.unsafeDereference(),
       "unsafeDereference() outerizes," +
       " so inner and outer window D.Os both dereference to outer");

    ok(dbg.addDebuggee(g1o) === g1iDO, "addDebuggee returns the inner window's D.O");
    ok(dbg.hasDebuggee(g1o), "addDebuggee adds the correct global");
    ok(dbg.hasDebuggee(g1iDO),
       "hasDebuggee can take a D.O referring to the inner window");
    ok(dbg.hasDebuggee(g1oDO),
       "hasDebuggee can take a D.O referring to the outer window");

    let iframe2 = document.createElement("iframe");
    iframe2.src = "iframe2_makeGlobalObjectReference.html";
    iframe2.onload = iframe2OnLoad;
    document.body.appendChild(iframe2);

    function iframe2OnLoad() {
      // makeGlobalObjectReference dereferences CCWs.
      let g2o = iframe2.contentWindow;
      g2o.g1o = g1o;

      let g2iDO = dbg.addDebuggee(g2o);
      let g2g1oDO = g2iDO.getOwnPropertyDescriptor("g1o").value;
      ok(g2g1oDO !== g1oDO, "g2's cross-compartment wrapper for g1o gets its own D.O");
      ok(g2g1oDO.unwrap() === g1oDO,
         "unwrapping g2's cross-compartment wrapper for g1o gets the right D.O");
      ok(dbg.makeGlobalObjectReference(g2g1oDO) === g1iDO,
         "makeGlobalObjectReference unwraps cross-compartment wrappers, and innerizes");

      SimpleTest.finish();
    }
  }
};

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