summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_accessibility_tabbing_order_highlighter.js
blob: fb995343186c6ab5b004fc5de86506b45695e87f (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
/* 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/. */

"use strict";

// Checks for the TabbingOrderHighlighter.

add_task(async function () {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: MAIN_DOMAIN + "doc_accessibility_infobar.html",
    },
    async function (browser) {
      await SpecialPowers.spawn(browser, [], async function () {
        const { require } = ChromeUtils.importESModule(
          "resource://devtools/shared/loader/Loader.sys.mjs"
        );
        const {
          HighlighterEnvironment,
        } = require("resource://devtools/server/actors/highlighters.js");
        const {
          TabbingOrderHighlighter,
        } = require("resource://devtools/server/actors/highlighters/tabbing-order.js");

        // Start testing. First, create highlighter environment and initialize.
        const env = new HighlighterEnvironment();
        env.initFromWindow(content.window);

        // Wait for loading highlighter environment content to complete before
        // creating the highlighter.
        await new Promise(resolve => {
          const doc = env.document;

          function onContentLoaded() {
            if (
              doc.readyState === "interactive" ||
              doc.readyState === "complete"
            ) {
              resolve();
            } else {
              doc.addEventListener("DOMContentLoaded", onContentLoaded, {
                once: true,
              });
            }
          }

          onContentLoaded();
        });

        // Now, we can test the Infobar's index content.
        const node = content.document.createElement("div");
        content.document.body.append(node);
        const highlighter = new TabbingOrderHighlighter(env);
        await highlighter.isReady;

        info("Showing tabbing order highlighter for all tabbable nodes");
        const { contentDOMReference, index } = await highlighter.show(
          content.document,
          {
            index: 0,
          }
        );

        is(
          contentDOMReference,
          null,
          "No current element when at the end of the tab order"
        );
        is(index, 2, "Current index is correct");
        is(
          highlighter._highlighters.size,
          2,
          "Number of node tabbing order highlighters is correct"
        );
        for (let i = 0; i < highlighter._highlighters.size; i++) {
          const nodeHighlighter = [...highlighter._highlighters.values()][i];
          const infoBarText = nodeHighlighter.getElement("infobar-text");

          is(
            parseInt(infoBarText.getTextContent(), 10),
            i + 1,
            "infobar text content is correct"
          );
        }

        info("Showing focus highlighting");
        const input = content.document.getElementById("input");
        highlighter.updateFocus({ node: input, focused: true });
        const nodeHighlighter = highlighter._highlighters.get(input);
        const { classList } = nodeHighlighter.getElement("root");
        ok(classList.contains("focused"), "Focus styling is applied");
        highlighter.updateFocus({ node: input, focused: false });
        ok(!classList.contains("focused"), "Focus styling is removed");

        highlighter.hide();
      });
    }
  );
});