devtools/server/tests/mochitest/test_inspector-mutations-attr.html
author Fabien Casters <fabien@vaga.io>
Mon, 13 Mar 2017 21:15:44 +0100
changeset 348382 1f17eeabf363349bd01b0c6c4edfaf58ade60c6e
parent 344949 a072d7934ff7c6fd2ad974d7abc0b08d11041aca
child 407713 49cce2c9e2ab21606a9abcdc0ab0af991806957f
permissions -rw-r--r--
Bug 1325988 - Fix ESLint issues in devtools/server/tests/mochitest/ r=tromey MozReview-Commit-ID: BFaOv5i5Tun

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=
-->
<head>
  <meta charset="utf-8">
  <title>Test for 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">
  <script type="application/javascript" src="inspector-helpers.js"></script>
  <script type="application/javascript">
"use strict";

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

let gInspectee = null;
let gWalker = null;
let attrNode;
let attrFront;

addTest(function setup() {
  let url = document.getElementById("inspectorContent").href;
  attachURL(url, function (err, client, tab, doc) {
    gInspectee = doc;
    let {InspectorFront} = require("devtools/shared/fronts/inspector");
    let inspector = InspectorFront(client, tab);
    promiseDone(inspector.getWalker().then(walker => {
      ok(walker, "getWalker() should return an actor.");
      gWalker = walker;
    }).then(runNextTest));
  });
});

addTest(setupAttrTest);
addTest(testAddAttribute);
addTest(testChangeAttribute);
addTest(testRemoveAttribute);
addTest(testQueuedMutations);
addTest(setupFrameAttrTest);
addTest(testAddAttribute);
addTest(testChangeAttribute);
addTest(testRemoveAttribute);
addTest(testQueuedMutations);

function setupAttrTest() {
  attrNode = gInspectee.querySelector("#a");
  promiseDone(gWalker.querySelector(gWalker.rootNode, "#a").then(node => {
    attrFront = node;
  }).then(runNextTest));
}

function setupFrameAttrTest() {
  let frame = gInspectee.querySelector("#childFrame");
  attrNode = frame.contentDocument.querySelector("#a");

  promiseDone(gWalker.querySelector(gWalker.rootNode, "#childFrame").then(childFrame => {
    return gWalker.children(childFrame);
  }).then(children => {
    let nodes = children.nodes;
    ok(nodes.length, 1, "There should be only one child of the iframe");
    is(nodes[0].nodeType, Node.DOCUMENT_NODE, "iframe child should be a document node");
    return gWalker.querySelector(nodes[0], "#a");
  }).then(node => {
    attrFront = node;
  }).then(runNextTest));
}

function testAddAttribute() {
  attrNode.setAttribute("data-newattr", "newvalue");
  attrNode.setAttribute("data-newattr2", "newvalue");
  gWalker.once("mutations", () => {
    is(attrFront.attributes.length, 3, "Should have id and two new attributes.");
    is(attrFront.getAttribute("data-newattr"), "newvalue",
       "Node front should have the first new attribute");
    is(attrFront.getAttribute("data-newattr2"), "newvalue",
       "Node front should have the second new attribute.");
    runNextTest();
  });
}

function testChangeAttribute() {
  attrNode.setAttribute("data-newattr", "changedvalue1");
  attrNode.setAttribute("data-newattr", "changedvalue2");
  attrNode.setAttribute("data-newattr", "changedvalue3");
  gWalker.once("mutations", mutations => {
    is(mutations.length, 1,
       "Only one mutation is sent for multiple queued attribute changes");
    is(attrFront.attributes.length, 3, "Should have id and two new attributes.");
    is(attrFront.getAttribute("data-newattr"), "changedvalue3",
       "Node front should have the changed first value");
    is(attrFront.getAttribute("data-newattr2"), "newvalue",
       "Second value should remain unchanged.");
    runNextTest();
  });
}

function testRemoveAttribute() {
  attrNode.removeAttribute("data-newattr2");
  gWalker.once("mutations", () => {
    is(attrFront.attributes.length, 2, "Should have id and one remaining attribute.");
    is(attrFront.getAttribute("data-newattr"), "changedvalue3",
       "Node front should still have the first value");
    ok(!attrFront.hasAttribute("data-newattr2"), "Second value should be removed.");
    runNextTest();
  });
}

function testQueuedMutations() {
  // All modifications to each attribute should be queued in one mutation event.

  attrNode.removeAttribute("data-newattr");
  attrNode.setAttribute("data-newattr", "1");
  attrNode.removeAttribute("data-newattr");
  attrNode.setAttribute("data-newattr", "2");
  attrNode.removeAttribute("data-newattr");

  for (let i = 0; i <= 1000; i++) {
    attrNode.setAttribute("data-newattr2", i);
  }

  attrNode.removeAttribute("data-newattr3");
  attrNode.setAttribute("data-newattr3", "1");
  attrNode.removeAttribute("data-newattr3");
  attrNode.setAttribute("data-newattr3", "2");
  attrNode.removeAttribute("data-newattr3");
  attrNode.setAttribute("data-newattr3", "3");

  // This shouldn't be added in the attribute set, since it's a new
  // attribute that's been added and removed.
  attrNode.setAttribute("data-newattr4", "4");
  attrNode.removeAttribute("data-newattr4");

  gWalker.once("mutations", mutations => {
    is(mutations.length, 4,
       "Only one mutation each is sent for multiple queued attribute changes");
    is(attrFront.attributes.length, 3,
       "Should have id, data-newattr2, and data-newattr3.");

    is(attrFront.getAttribute("data-newattr2"), "1000",
       "Node front should still have the correct value");
    is(attrFront.getAttribute("data-newattr3"), "3",
       "Node front should still have the correct value");
    ok(!attrFront.hasAttribute("data-newattr"), "Attribute value should be removed.");
    ok(!attrFront.hasAttribute("data-newattr4"), "Attribute value should be removed.");

    runNextTest();
  });
}

addTest(function cleanup() {
  gInspectee = null;
  gWalker = null;
  runNextTest();
});
  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug </a>
<a id="inspectorContent" target="_blank" href="inspector-traversal-data.html">Test Document</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>