<!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;version=1.8" src="inspector-helpers.js"></script>
  <script type="application/javascript;version=1.8">
const inspector = require("devtools/server/actors/inspector");

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

const testSummaryLength = 10;
inspector.setValueSummaryLength(testSummaryLength);
SimpleTest.registerCleanupFunction(function() {
  inspector.setValueSummaryLength(inspector.DEFAULT_VALUE_SUMMARY_LENGTH);
});

var gInspectee = null;
var gWalker = null;
var gClient = null;
var valueNode;
var valueFront;
var longStringFront;
var longString = "stringstringstringstringstringstringstringstringstringstringstring";
var truncatedLongString = longString.substring(0, testSummaryLength);
var shortString = "str";
var shortString2 = "str2";

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.");
      gClient = client;
      gWalker = walker;
    }).then(runNextTest));
  });
});

addTest(setupValueTest);
addTest(testKeepLongValue);
addTest(testSetShortValue);
addTest(testKeepShortValue);
addTest(testSetLongValue);
addTest(setupFrameValueTest);
addTest(testKeepLongValue);
addTest(testSetShortValue);
addTest(testKeepShortValue);
addTest(testSetLongValue);

function setupValueTest() {
  valueNode = gInspectee.querySelector("#longstring").firstChild;
  promiseDone(gWalker.querySelector(gWalker.rootNode, "#longstring").then(node => {
    longStringFront = node;
    return gWalker.children(node);
  }).then(children => {
    valueFront = children.nodes[0];
  }).then(runNextTest));
}

function setupFrameValueTest() {
  let frame = gInspectee.querySelector('#childFrame');
  valueNode = frame.contentDocument.querySelector("#longstring").firstChild;

  promiseDone(gWalker.querySelector(gWalker.rootNode, "#childFrame").then(childFrame => {
    return gWalker.children(childFrame);
  }).then(children => {
    let nodes = children.nodes;
    is(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], "#longstring");
  }).then(node => {
    longStringFront = node;
    return gWalker.children(node);
  }).then(children => {
    valueFront = children.nodes[0];
  }).then(runNextTest));
}

function checkNodeFrontValue(front, expectedValue) {
  return front.getNodeValue().then(longstring => {
    return longstring.string();
  }).then(str => {
    is(str, expectedValue, "Node value is as expected");
  })
}

function testKeepLongValue() {
  // After first setup we should have a long string in the node
  ok(!longStringFront.inlineTextChild, "Text node is too long to be inlined.");

  valueNode.nodeValue = longString;
  gWalker.once("mutations", (changes) => {
    ok(!longStringFront.inlineTextChild, "Text node is too long to be inlined.");
    ok(!changes.some(change => change.type === "inlineTextChild"),
      "No inline text child mutation was fired.");
    checkNodeFrontValue(valueFront, longString).then(runNextTest);
  });
}

function testSetShortValue() {
  ok(!longStringFront.inlineTextChild, "Text node is too long to be inlined.");

  valueNode.nodeValue = shortString;
  gWalker.once("mutations", (changes) => {
    ok(!!longStringFront.inlineTextChild, "Text node is short enough to be inlined.");
    ok(changes.some(change => change.type === "inlineTextChild"),
      "An inlineTextChild mutation was fired.");
    checkNodeFrontValue(valueFront, shortString).then(runNextTest);
  });
}

function testKeepShortValue() {
  ok(!!longStringFront.inlineTextChild, "Text node is short enough to be inlined.");

  valueNode.nodeValue = shortString2;
  gWalker.once("mutations", (changes) => {
    ok(!!longStringFront.inlineTextChild, "Text node is short enough to be inlined.");
    ok(!changes.some(change => change.type === "inlineTextChild"),
      "No inline text child mutation was fired.");
    checkNodeFrontValue(valueFront, shortString2).then(runNextTest);
  });
}

function testSetLongValue() {
  ok(!!longStringFront.inlineTextChild, "Text node is short enough to be inlined.");

  valueNode.nodeValue = longString;
  gWalker.once("mutations", (changes) => {
    ok(!longStringFront.inlineTextChild, "Text node is too long to be inlined.");
    ok(changes.some(change => change.type === "inlineTextChild"),
      "An inlineTextChild mutation was fired.");
    checkNodeFrontValue(valueFront, longString).then(runNextTest);
  });
}

addTest(function cleanup() {
  delete gInspectee;
  delete gWalker;
  delete gClient;
  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>