summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/test/mochitest/test_tree_04.html
blob: 24948c003ffb6eb00a5b2f06b9afae92118c6482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!-- 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 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 src="head.js" type="application/javascript;version=1.8"></script>
<script type="application/javascript;version=1.8">
window.onload = Task.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/tree"));

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

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

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

    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");

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

    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)
    yield 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");

    yield 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>