<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<!DOCTYPE HTML>
<html>
<!--
Test that we only render visible tree items.
-->
<head>
  <meta charset="utf-8">
  <title>Tree component test</title>
  <script 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 src="head.js" type="application/javascript"></script>
<script type="application/javascript">

'use strict'

window.onload = async function () {
  try {
    function getSpacerHeights() {
      return {
        top: document.querySelector(".tree > div:first-of-type").clientHeight,
        bottom: document.querySelector(".tree > div:last-of-type").clientHeight,
      };
    }

    const ITEM_HEIGHT = 3;

    const ReactDOM = browserRequire("devtools/client/shared/vendor/react-dom");
    const React = browserRequire("devtools/client/shared/vendor/react");
    const Tree = React.createFactory(browserRequire("devtools/client/shared/components/VirtualizedTree"));

    const tree = ReactDOM.render(
      Tree(Object.assign({}, TEST_TREE_INTERFACE, { itemHeight: ITEM_HEIGHT })),
      window.document.body);

    TEST_TREE.expanded = new Set("ABCDEFGHIJKLMNO".split(""));

    await setState(tree, {
      height: 3 * ITEM_HEIGHT,
      scroll: 1 * ITEM_HEIGHT
    });

    isAccessibleTree(tree);
    isRenderedTree(document.body.textContent, [
      "A:false",
      "-B:false",
      "--E:false",
      "---K:false",
      "---L:false",
    ], "Tree should show the 2nd, 3rd, and 4th items + buffer of 1 item at each end");

    let spacers = getSpacerHeights();
    is(spacers.top, 0, "Top spacer has the correct height");
    is(spacers.bottom, 10 * ITEM_HEIGHT, "Bottom spacer has the correct height");

    await setState(tree, {
      height: 2 * ITEM_HEIGHT,
      scroll: 3 * ITEM_HEIGHT
    });

    isAccessibleTree(tree);
    isRenderedTree(document.body.textContent, [
      "--E:false",
      "---K:false",
      "---L:false",
      "--F:false",
    ], "Tree should show the 4th and 5th item + buffer of 1 item at each end");

    spacers = getSpacerHeights();
    is(spacers.top, 2 * ITEM_HEIGHT, "Top spacer has the correct height");
    is(spacers.bottom, 9 * ITEM_HEIGHT, "Bottom spacer has the correct height");

    // Set height to 2 items + 1 pixel at each end, scroll so that 4 items are visible
    // (2 fully, 2 partially with 1 visible pixel)
    await setState(tree, {
      height: 2 * ITEM_HEIGHT + 2,
      scroll: 3 * ITEM_HEIGHT - 1
    });

    isRenderedTree(document.body.textContent, [
      "-B:false",
      "--E:false",
      "---K:false",
      "---L:false",
      "--F:false",
      "--G:false",
    ], "Tree should show the 4 visible items + buffer of 1 item at each end");

    spacers = getSpacerHeights();
    is(spacers.top, 1 * ITEM_HEIGHT, "Top spacer has the correct height");
    is(spacers.bottom, 8 * ITEM_HEIGHT, "Bottom spacer has the correct height");

    await setState(tree, {
      height: 20 * ITEM_HEIGHT,
      scroll: 0
    });

    isRenderedTree(document.body.textContent, [
      "A:false",
      "-B:false",
      "--E:false",
      "---K:false",
      "---L:false",
      "--F:false",
      "--G:false",
      "-C:false",
      "--H:false",
      "--I:false",
      "-D:false",
      "--J:false",
      "M:false",
      "-N:false",
      "--O:false",
    ], "Tree should show all rows");

    spacers = getSpacerHeights();
    is(spacers.top, 0, "Top spacer has zero height");
    is(spacers.bottom, 0, "Bottom spacer has zero height");
  } catch(e) {
    ok(false, "Got an error: " + DevToolsUtils.safeErrorString(e));
  } finally {
    SimpleTest.finish();
  }
};
</script>
</pre>
</body>
</html>
