devtools/server/tests/mochitest/test_Debugger.Source.prototype.introductionType.html
author Kris Maglione <maglione.k@gmail.com>
Wed, 24 Jan 2018 15:48:47 -0800
changeset 455769 d4a7c018420e408fbe0a13ffddd2861623fda5a7
parent 421622 83e350c46e945d6172c86da8c4cf6b8a85182e59
child 455772 cc87ad81ff86109c7ea0187424fa9a8ed3b4af6c
permissions -rw-r--r--
Bug 1431533: Part 5a - Auto-rewrite code to use ChromeUtils import methods. r=florian MozReview-Commit-ID: 8V1ZT53ReiP

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

Debugger.Source.prototype.introductionType should return 'eventHandler' for
JavaScrip appearing in an inline event handler attribute.
-->
<head>
  <meta charset="utf-8">
  <title>Debugger.Source.prototype.introductionType should identify event handlers</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="inspector-helpers.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);

let dbg;
let iframeDO, doc;
let Tootles, TootlesDO;

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

addTest(function setup() {
  // Create an iframe to debug.
  let iframe = document.createElement("iframe");
  iframe.srcdoc = "<div id='Tootles' onclick='debugger;'>I'm a DIV!</div>" +
                  "<script id='Auddie'>function auddie() { debugger; }<\/script>";
  iframe.onload = onLoadHandler;
  document.body.appendChild(iframe);

  function onLoadHandler() {
    // Now that the iframe's window has been created, we can add
    // it as a debuggee.
    dbg = new Debugger();
    iframeDO = dbg.addDebuggee(iframe.contentWindow);
    doc = iframe.contentWindow.document;
    Tootles = doc.getElementById("Tootles");
    TootlesDO = iframeDO.makeDebuggeeValue(Tootles);

    runNextTest();
  }
});

// Check the introduction type of in-markup event handler code.
// Send a click event to Tootles, whose handler has a 'debugger' statement,
// and check that script's introduction type.
addTest(function ClickOnTootles() {
  dbg.onDebuggerStatement = TootlesClickDebugger;
  Tootles.dispatchEvent(new Event("click"));

  function TootlesClickDebugger(frame) {
    // some sanity checks
    ok(frame.script.source.element === TootlesDO,
       "top frame source belongs to element 'Tootles'");
    is(frame.script.source.elementAttributeName, "onclick",
       "top frame source belongs to 'onclick' attribute");

    // And, the actual point of this test:
    is(frame.script.source.introductionType, "eventHandler",
       "top frame source's introductionType is 'eventHandler'");

    runNextTest();
  }
});

// Check the introduction type of dynamically added event handler code.
// Add a drag event handler to Tootles as a string, and then send
// Tootles a drag event.
addTest(function DragTootles() {
  dbg.onDebuggerStatement = TootlesDragDebugger;
  Tootles.setAttribute("ondrag", "debugger;");
  Tootles.dispatchEvent(new Event("drag"));

  function TootlesDragDebugger(frame) {
    // sanity checks
    ok(frame.script.source.element === TootlesDO,
       "top frame source belongs to element 'Tootles'");
    is(frame.script.source.elementAttributeName, "ondrag",
       "top frame source belongs to 'ondrag' attribute");

    // And, the actual point of this test:
    is(frame.script.source.introductionType, "eventHandler",
       "top frame source's introductionType is 'eventHandler'");

    runNextTest();
  }
});

// Check the introduction type of an in-markup script element.
addTest(function checkAuddie() {
  let fnDO = iframeDO.getOwnPropertyDescriptor("auddie").value;
  let AuddieDO = iframeDO.makeDebuggeeValue(doc.getElementById("Auddie"));

  is(fnDO.class, "Function",
     "Script element 'Auddie' defined function 'auddie'.");
  ok(fnDO.script.source.element === AuddieDO,
     "Function auddie's script belongs to script element 'Auddie'");
  is(fnDO.script.source.elementAttributeName, undefined,
     "Function auddie's script doesn't belong to any attribute of 'Auddie'");
  is(fnDO.script.source.introductionType, "scriptElement",
     "Function auddie's script's source was introduced by a script element");

  runNextTest();
});

// Check the introduction type of a dynamically inserted script element.
addTest(function InsertRover() {
  dbg.onDebuggerStatement = RoverDebugger;
  let rover = doc.createElement("script");
  let roverDO = iframeDO.makeDebuggeeValue(rover);
  rover.text = "debugger;";
  doc.body.appendChild(rover);

  function RoverDebugger(frame) {
    // sanity checks
    ok(frame.script.source.element === roverDO,
       "Rover script belongs to Rover");
    ok(frame.script.source.elementAttributeName === undefined,
       "Rover script doesn't belong to an attribute of Rover");

    // Check the introduction type.
    ok(frame.script.source.introductionType === "scriptElement",
       "Rover script's introduction type is 'scriptElement'");

    runNextTest();
  }
});

// Create a XUL document with a script element, and check its introduction type.
addTest(function XULDocumentScript() {
  let xulFrame = document.createElement("iframe");
  xulFrame.src = "doc_Debugger.Source.prototype.introductionType.xul";
  xulFrame.onload = xulLoaded;
  info("Appending iframe containing XUL document");
  document.body.appendChild(xulFrame);

  function xulLoaded() {
    info("Loaded XUL document");
    let xulFrameDO = dbg.addDebuggee(xulFrame.contentWindow);
    let xulFnDO = xulFrameDO.getOwnPropertyDescriptor("xulScriptFunc").value;
    is(typeof xulFnDO, "object", "XUL script element defined 'xulScriptFunc'");
    is(xulFnDO.class, "Function",
       "XUL global 'xulScriptFunc' is indeed a function");

    // A XUL document's script elements' code gets shared amongst all
    // instantiations of the document, so there's no specific DOM element
    // we can attribute the code to.
    is(xulFnDO.script.source.element, undefined,
       "XUL script code should not be attributed to any individual element");

    is(xulFnDO.script.source.introductionType, "scriptElement",
       "xulScriptFunc's introduction type is 'scriptElement'");
    runNextTest();
  }
});
</script>
</pre>
</body>
</html>