summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_accessibility_walker.js
blob: 607807efb47f7b729784ad26a9fdc6d7c45e6deb (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
/* 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 AccessibleWalkerActor

add_task(async function () {
  const { target, walker, a11yWalker, parentAccessibility } =
    await initAccessibilityFrontsForUrl(MAIN_DOMAIN + "doc_accessibility.html");

  ok(a11yWalker, "The AccessibleWalkerFront was returned");
  const rootNode = await walker.getRootNode();
  const a11yDoc = await a11yWalker.getAccessibleFor(rootNode);
  ok(a11yDoc, "The AccessibleFront for root doc is created");

  const children = await a11yWalker.children();
  is(
    children.length,
    1,
    "AccessibleWalker only has 1 child - root doc accessible"
  );
  is(
    a11yDoc,
    children[0],
    "Root accessible must be AccessibleWalker's only child"
  );

  const buttonNode = await walker.querySelector(walker.rootNode, "#button");
  const accessibleFront = await a11yWalker.getAccessibleFor(buttonNode);

  checkA11yFront(accessibleFront, {
    name: "Accessible Button",
    role: "button",
  });

  const ancestry = await a11yWalker.getAncestry(accessibleFront);
  is(ancestry.length, 1, "Button is a direct child of a root document.");
  is(
    ancestry[0].accessible,
    a11yDoc,
    "Button's only ancestor is a root document"
  );
  is(
    ancestry[0].children.length,
    7,
    "Root doc should have correct number of children"
  );
  ok(
    ancestry[0].children.includes(accessibleFront),
    "Button accessible front is in root doc's children"
  );

  const browser = gBrowser.selectedBrowser;

  // Ensure name-change event is emitted by walker when cached accessible's name
  // gets updated (via DOM manipularion).
  await emitA11yEvent(
    a11yWalker,
    "name-change",
    (front, parent) => {
      checkA11yFront(front, { name: "Renamed" }, accessibleFront);
      checkA11yFront(parent, {}, a11yDoc);
    },
    () =>
      SpecialPowers.spawn(browser, [], () =>
        content.document
          .getElementById("button")
          .setAttribute("aria-label", "Renamed")
      )
  );

  // Ensure reorder event is emitted by walker when DOM tree changes.
  let docChildren = await a11yDoc.children();
  is(docChildren.length, 7, "Root doc should have correct number of children");

  await emitA11yEvent(
    a11yWalker,
    "reorder",
    front => checkA11yFront(front, {}, a11yDoc),
    () =>
      SpecialPowers.spawn(browser, [], () => {
        const input = content.document.createElement("input");
        input.type = "text";
        input.title = "This is a tooltip";
        input.value = "New input";
        content.document.body.appendChild(input);
      })
  );

  docChildren = await a11yDoc.children();
  is(docChildren.length, 8, "Root doc should have correct number of children");

  let shown = await a11yWalker.highlightAccessible(docChildren[0]);
  ok(shown, "AccessibleHighlighter highlighted the node");

  shown = await a11yWalker.highlightAccessible(a11yDoc);
  ok(shown, "AccessibleHighlighter highlights the document correctly.");
  await a11yWalker.unhighlight();

  info("Checking AccessibleWalker picker functionality");
  ok(a11yWalker.pick, "AccessibleWalker pick method exists");
  ok(a11yWalker.pickAndFocus, "AccessibleWalker pickAndFocus method exists");
  ok(a11yWalker.cancelPick, "AccessibleWalker cancelPick method exists");

  let onPickerEvent = a11yWalker.once("picker-accessible-hovered");
  await a11yWalker.pick();
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#h1",
    { type: "mousemove" },
    browser
  );
  let acc = await onPickerEvent;
  checkA11yFront(acc, { name: "Accessibility Test" }, docChildren[0]);

  onPickerEvent = a11yWalker.once("picker-accessible-previewed");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#h1",
    { shiftKey: true },
    browser
  );
  acc = await onPickerEvent;
  checkA11yFront(acc, { name: "Accessibility Test" }, docChildren[0]);

  onPickerEvent = a11yWalker.once("picker-accessible-canceled");
  await BrowserTestUtils.synthesizeKey(
    "VK_ESCAPE",
    { type: "keydown" },
    browser
  );
  await onPickerEvent;

  onPickerEvent = a11yWalker.once("picker-accessible-hovered");
  await a11yWalker.pick();
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#h1",
    { type: "mousemove" },
    browser
  );
  await onPickerEvent;

  onPickerEvent = a11yWalker.once("picker-accessible-picked");
  await BrowserTestUtils.synthesizeMouseAtCenter("#h1", {}, browser);
  acc = await onPickerEvent;
  checkA11yFront(acc, { name: "Accessibility Test" }, docChildren[0]);

  await a11yWalker.cancelPick();

  info("Checking tabbing order highlighter");
  let { elm, index } = await a11yWalker.showTabbingOrder(rootNode, 0);
  isnot(!!elm, "No current element when at the end of the tab order");
  is(index, 3, "Current index is correct");
  await a11yWalker.hideTabbingOrder();

  ({ elm, index } = await a11yWalker.showTabbingOrder(buttonNode, 0));
  isnot(!!elm, "No current element when at the end of the tab order");
  is(index, 2, "Current index is correct");
  await a11yWalker.hideTabbingOrder();

  info(
    "When targets follow the WindowGlobal lifecycle and handle only one document, " +
      "only check that the panel refreshes correctly and emit its 'reloaded' event"
  );
  await reloadBrowser();

  await waitForA11yShutdown(parentAccessibility);
  await target.destroy();
  gBrowser.removeCurrentTab();
});