summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/test/chrome/test_tree_14.html
blob: d68d87d6c525863f28937887d30b447dfab461e1 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!-- 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 Tree component has working keyboard interactions.
-->
<head>
  <meta charset="utf-8">
  <title>Tree component keyboard test</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.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 {
    const { a, button, div } =
      require("devtools/client/shared/vendor/react-dom-factories");
    const { createFactory } = browserRequire("devtools/client/shared/vendor/react");
    const {
      Simulate,
      findRenderedDOMComponentWithClass,
      findRenderedDOMComponentWithTag,
    } = browserRequire("devtools/client/shared/vendor/react-dom-test-utils");
    const Tree = createFactory(
      browserRequire("devtools/client/shared/components/VirtualizedTree"));

    let gTree, gFocused, gActive;
    function renderTree(props = {}) {
      let toggle = true;
      const treeProps = {
        ...TEST_TREE_INTERFACE,
        onFocus: x => {
          gFocused = x;
          renderTree({ focused: gFocused, active: gActive });
        },
        onActivate: x => {
          gActive = x;
          renderTree({ focused: gFocused, active: gActive });
        },
        renderItem: (x, depth, focused) => {
          toggle = !toggle;
          return toggle ?
            (div(
              {},
                `${"-".repeat(depth)}${x}:${focused}`,
                a({ href: "#" }, "Focusable 1"),
                button({ }, "Focusable 2"),
                "\n",
              )
            ) : `${"-".repeat(depth)}${x}:${focused}`;
        },
        ...props
      };

      gTree = ReactDOM.render(Tree(treeProps), document.body);
    }

    renderTree();
    const els = {
      get tree() {
        // React will replace the tree via renderTree.
        return findRenderedDOMComponentWithClass(gTree, "tree");
      },
      get anchor() {
        // When tree node becomes active/inactive, it is replaced with a newly rendered
        // one.
        return findRenderedDOMComponentWithTag(gTree, "a");
      },
      get button() {
        // When tree node becomes active/inactive, it is replaced with a newly rendered
        // one.
        return findRenderedDOMComponentWithTag(gTree, "button");
      },
    };

    const tests = [{
      name: "Test default Tree props. Keyboard focus is set to document body by default.",
      props: { focused: undefined, active: undefined },
      activeElement: document.body,
    }, {
      name: "Focused props must be set to the first node on initial focus. " +
            "Keyboard focus should be set on the tree.",
      action: () => els.tree.focus(),
      activeElement: "tree",
      props: { focused: "A" },
    }, {
      name: "Focused node should remain set even when the tree is blured. " +
            "Keyboard focus should be set back to document body.",
      action: () => els.tree.blur(),
      props: { focused: "A" },
      activeElement: document.body,
    }, {
      name: "Unset tree's focused prop.",
      action: () => renderTree({ focused: null }),
      props: { focused: null },
    }, {
      name: "Focused node must be re-set again to the first tree node on initial " +
            "focus. Keyboard focus should be set on tree's conatiner.",
      action: () => els.tree.focus(),
      activeElement: "tree",
      props: { focused: "A" },
    }, {
      name: "Focused node should be set as active on Enter.",
      event: { type: "keyDown", el: "tree", options: { key: "Enter" }},
      props: { focused: "A", active: "A" },
      activeElement: "tree",
    }, {
      name: "Active node should be unset on Escape.",
      event: { type: "keyDown", el: "tree", options: { key: "Escape" }},
      props: { focused: "A", active: null },
    }, {
      name: "Focused node should be set as active on Space.",
      event: { type: "keyDown", el: "tree", options: { key: " " }},
      props: { focused: "A", active: "A" },
      activeElement: "tree",
    }, {
      name: "Active node should unset when focus leaves the tree.",
      action: () => els.tree.blur(),
      props: { focused: "A", active: null },
      activeElement: document.body,
    }, {
      name: "Keyboard focus should be set on tree's conatiner on focus.",
      action: () => els.tree.focus(),
      activeElement: "tree",
    }, {
      name: "Focused node should be updated to next on ArrowDown.",
      event: { type: "keyDown", el: "tree", options: { key: "ArrowDown" }},
      props: { focused: "M", active: null },
    }, {
      name: "Focused item should be set as active on Enter. Keyboard focus should be " +
            "set on the first focusable element inside the tree node, if available.",
      event: { type: "keyDown", el: "tree", options: { key: "Enter" }},
      props: { focused: "M", active: "M" },
      activeElement: "anchor",
    }, {
      name: "Keyboard focus should be set to next tabbable element inside the active " +
            "node on Tab.",
      action() {
        synthesizeKey("KEY_Tab");
      },
      props: { focused: "M", active: "M" },
      activeElement: "button",
    }, {
      name: "Keyboard focus should wrap inside the tree node when focused on last " +
            "tabbable element.",
      action() {
        synthesizeKey("KEY_Tab");
      },
      props: { focused: "M", active: "M" },
      activeElement: "anchor",
    }, {
      name: "Keyboard focus should wrap inside the tree node when focused on first " +
            "tabbable element.",
      action() {
        synthesizeKey("KEY_Tab", { shiftKey: true });
      },
      props: { focused: "M", active: "M" },
      activeElement: "button",
    }, {
      name: "Active tree node should be unset on Escape. Focus should move back to the " +
            "tree container.",
      event: { type: "keyDown", el: "tree", options: { key: "Escape" }},
      props: { focused: "M", active: null },
      activeElement: "tree",
    }, {
      name: "Focused node should be set as active on Space. Keyboard focus should be " +
            "set on the first focusable element inside the tree node, if available.",
      event: { type: "keyDown", el: "tree", options: { key: " " }},
      props: { focused: "M", active: "M" },
      activeElement: "anchor",
    }, {
      name: "Focused tree node should remain set even when the tree is blured. " +
            "Keyboard focus should be set back to document body.",
      action: () => document.activeElement.blur(),
      props: { focused: "M", active: null, },
      activeElement: document.body,
    }, {
      name: "Keyboard focus should be set on tree's conatiner on focus.",
      action: () => els.tree.focus(),
      props: { focused: "M", active: null },
      activeElement: "tree",
    }, {
      name: "Focused tree node should be updated to previous on ArrowUp.",
      event: { type: "keyDown", el: "tree", options: { key: "ArrowUp" }},
      props: { focused: "A", active: null },
    }, {
      name: "Focused item should be set as active on Enter.",
      event: { type: "keyDown", el: "tree", options: { key: "Enter" }},
      props: { focused: "A", active: "A" },
      activeElement: "tree",
    }, {
      name: "Keyboard focus should move to another focusable element outside of the " +
            "tree when there's nothing to focus on inside the tree node.",
      action() {
        synthesizeKey("KEY_Tab", { shiftKey: true });
      },
      props: { focused: "A", active: null },
      activeElement: document.documentElement,
    }];

    for (const test of tests) {
      const { action, event, props, name } = test;

      info(name);
      if (event) {
        const { type, options, el } = event;
        const target = typeof el === "string" ? els[el] : el;
        Simulate[type](target, options);
      } else if (action) {
        action();
      }

      await forceRender(gTree);

      if (test.activeElement) {
        const expected = typeof test.activeElement === "string" ?
          els[test.activeElement] : test.activeElement;
        // eslint-disable-next-line no-debugger
        if (document.activeElement!==expected) {debugger;}
        is(document.activeElement, expected, "Focus is set correctly.");
      }

      for (const key in props) {
        is(gTree.props[key], props[key], `${key} prop is correct.`);
      }
    }
  } catch (e) {
    ok(false, "Got an error: " + DevToolsUtils.safeErrorString(e));
  } finally {
    SimpleTest.finish();
  }
};
</script>
</pre>
</body>
</html>