summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/events/test_focus_aria_activedescendant.html
blob: 661284619a182f45406c9602ddc3dee597612e26 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=429547
-->
<head>
  <title>aria-activedescendant focus tests</title>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <script type="application/javascript"
          src="../common.js"></script>
  <script type="application/javascript"
          src="../role.js"></script>
  <script type="application/javascript"
          src="../states.js"></script>
  <script type="application/javascript"
          src="../events.js"></script>

  <script type="application/javascript">
    let PromEvents = {};
    Services.scriptloader.loadSubScript(
      "chrome://mochitests/content/a11y/accessible/tests/mochitest/promisified-events.js",
      PromEvents);
    // gA11yEventDumpToConsole = true; // debugging

    function changeARIAActiveDescendant(aContainer, aItem, aPrevItemId) {
      let itemID = Node.isInstance(aItem) ? aItem.id : aItem;
      this.eventSeq = [new focusChecker(aItem)];

      if (aPrevItemId) {
        this.eventSeq.push(
          new stateChangeChecker(EXT_STATE_ACTIVE, true, false, aPrevItemId)
        );
      }

      this.eventSeq.push(
        new stateChangeChecker(EXT_STATE_ACTIVE, true, true, aItem)
      );

      this.invoke = function changeARIAActiveDescendant_invoke() {
        getNode(aContainer).setAttribute("aria-activedescendant", itemID);
      };

      this.getID = function changeARIAActiveDescendant_getID() {
        return "change aria-activedescendant on " + itemID;
      };
    }

    function clearARIAActiveDescendant(aID, aPrevItemId) {
      this.eventSeq = [
        new focusChecker(aID),
      ];

      if (aPrevItemId) {
        this.eventSeq.push(
          new stateChangeChecker(EXT_STATE_ACTIVE, true, false, aPrevItemId)
        );
      }

      this.invoke = function clearARIAActiveDescendant_invoke() {
        getNode(aID).removeAttribute("aria-activedescendant");
      };

      this.getID = function clearARIAActiveDescendant_getID() {
        return "clear aria-activedescendant on container " + aID;
      };
    }

    /**
     * Change aria-activedescendant to an invalid (non-existent) id.
     * Ensure that focus is fired on the element itself.
     */
    function changeARIAActiveDescendantInvalid(aID, aInvalidID, aPrevItemId) {
      if (!aInvalidID) {
        aInvalidID = "invalid";
      }

      this.eventSeq = [
        new focusChecker(aID),
      ];

      if (aPrevItemId) {
        this.eventSeq.push(
          new stateChangeChecker(EXT_STATE_ACTIVE, true, false, aPrevItemId)
        );
      }

      this.invoke = function changeARIAActiveDescendant_invoke() {
        getNode(aID).setAttribute("aria-activedescendant", aInvalidID);
      };

      this.getID = function changeARIAActiveDescendant_getID() {
        return "change aria-activedescendant to invalid id";
      };
    }
    
    function insertItemNFocus(aID, aNewItemID, aPrevItemId) {
      this.eventSeq = [
        new invokerChecker(EVENT_SHOW, aNewItemID),
        new focusChecker(aNewItemID),
      ];

      if (aPrevItemId) {
        this.eventSeq.push(
          new stateChangeChecker(EXT_STATE_ACTIVE, true, false, aPrevItemId)
        );
      }

      this.eventSeq.push(
        new stateChangeChecker(EXT_STATE_ACTIVE, true, true, aNewItemID)
      );

      this.invoke = function insertItemNFocus_invoke() {
        var container  = getNode(aID);

        var itemNode = document.createElement("div");
        itemNode.setAttribute("id", aNewItemID);
        itemNode.setAttribute("role", "listitem");
        itemNode.textContent = aNewItemID;
        container.appendChild(itemNode);

        container.setAttribute("aria-activedescendant", aNewItemID);
      };

      this.getID = function insertItemNFocus_getID() {
        return "insert new node and focus it with ID: " + aNewItemID;
      };
    }

    /**
     * Change the id of an element to another id which is the target of
     * aria-activedescendant.
     * If another element already has the desired id, remove it from that
     * element first.
     * Ensure that focus is fired on the target element which was given the
     * desired id.
     * @param aFromID The existing id of the target element.
     * @param aToID The desired id to be given to the target element.
    */
    function moveARIAActiveDescendantID(aFromID, aToID) {
      this.eventSeq = [
        new focusChecker(aToID),
        new stateChangeChecker(EXT_STATE_ACTIVE, true, true, aToID),
      ];

      this.invoke = function moveARIAActiveDescendantID_invoke() {
        let orig = document.getElementById(aToID);
        if (orig) {
          orig.id = "";
        }
        document.getElementById(aFromID).id = aToID;
      };

      this.getID = function moveARIAActiveDescendantID_getID() {
        return "move aria-activedescendant id " + aToID;
      };
    }

    var gQueue = null;
    async function doTest() {
      gQueue = new eventQueue();
      // Later tests use await.
      let queueFinished = new Promise(resolve => {
        gQueue.onFinish = function() {
          resolve();
          return DO_NOT_FINISH_TEST;
        };
      });

      gQueue.push(new synthFocus("listbox", new focusChecker("item1")));
      gQueue.push(new changeARIAActiveDescendant("listbox", "item2", "item1"));
      gQueue.push(new changeARIAActiveDescendant("listbox", "item3", "item2"));

      gQueue.push(new synthFocus("combobox_entry", new focusChecker("combobox_entry")));
      gQueue.push(new changeARIAActiveDescendant("combobox", "combobox_option2"));

      gQueue.push(new synthFocus("listbox", new focusChecker("item3")));
      gQueue.push(new insertItemNFocus("listbox", "item4", "item3"));

      gQueue.push(new clearARIAActiveDescendant("listbox", "item4"));
      gQueue.push(new changeARIAActiveDescendant("listbox", "item1"));
      gQueue.push(new changeARIAActiveDescendantInvalid("listbox", "invalid", "item1"));

      gQueue.push(new changeARIAActiveDescendant("listbox", "roaming"));
      gQueue.push(new moveARIAActiveDescendantID("roaming2", "roaming"));
      gQueue.push(new changeARIAActiveDescendantInvalid("listbox", "roaming3", "roaming"));
      gQueue.push(new moveARIAActiveDescendantID("roaming", "roaming3"));

      gQueue.push(new synthFocus("activedesc_nondesc_input",
        new focusChecker("activedesc_nondesc_option")));

      let shadowRoot = document.getElementById("shadow").shadowRoot;
      let shadowListbox = shadowRoot.getElementById("shadowListbox");
      let shadowItem1 = shadowRoot.getElementById("shadowItem1");
      let shadowItem2 = shadowRoot.getElementById("shadowItem2");
      gQueue.push(new synthFocus(shadowListbox, new focusChecker(shadowItem1)));
      gQueue.push(new changeARIAActiveDescendant(shadowListbox, shadowItem2));

      gQueue.invoke();
      await queueFinished;
      // Tests beyond this point use await rather than eventQueue.

      info("Testing simultaneous insertion, relocation and aria-activedescendant");
      let comboboxWithHiddenList = getNode("comboboxWithHiddenList");
      let evtProm = PromEvents.waitForEvent(EVENT_FOCUS, comboboxWithHiddenList);
      comboboxWithHiddenList.focus();
      await evtProm;
      testStates(comboboxWithHiddenList, STATE_FOCUSED);
      // hiddenList is owned, so unhiding causes insertion and relocation.
      getNode("hiddenList").hidden = false;
      evtProm = Promise.all([
        PromEvents.waitForEvent(EVENT_FOCUS, "hiddenListOption"),
        PromEvents.waitForStateChange("hiddenListOption", EXT_STATE_ACTIVE, true, true),
      ]);
      comboboxWithHiddenList.setAttribute("aria-activedescendant", "hiddenListOption");
      await evtProm;
      testStates("hiddenListOption", STATE_FOCUSED);

      info("Testing active state changes when not focused");
      testStates("listbox", 0, 0, STATE_FOCUSED);
      evtProm = Promise.all([
        PromEvents.waitForStateChange("roaming3", EXT_STATE_ACTIVE, false, true),
        PromEvents.waitForStateChange("item1", EXT_STATE_ACTIVE, true, true),
      ]);
      getNode("listbox").setAttribute("aria-activedescendant", "item1");
      await evtProm;

      info("Testing that focus is always fired first");
      const listbox = getNode("listbox");
      evtProm = PromEvents.waitForEvent(EVENT_FOCUS, "item1");
      listbox.focus();
      await evtProm;
      const item1 = getNode("item1");
      evtProm = PromEvents.waitForOrderedEvents([
        [EVENT_FOCUS, "item2"],
        [EVENT_NAME_CHANGE, item1],
      ], "Focus then name change");
      item1.setAttribute("aria-label", "changed");
      listbox.setAttribute("aria-activedescendant", "item2");
      await evtProm;

      info("Setting aria-activedescendant to invalid id on non-focused node");
      const combobox_entry = getNode("combobox_entry");
      evtProm = PromEvents.waitForEvents({
        expected: [[EVENT_FOCUS, combobox_entry]],
        unexpected: [[EVENT_FOCUS, listbox]],
      });
      combobox_entry.focus();
      listbox.setAttribute("aria-activedescendant", "invalid");
      await evtProm;

      SimpleTest.finish();
    }

    SimpleTest.waitForExplicitFinish();
    addA11yLoadEvent(doTest);
  </script>
</head>
<body>

  <a target="_blank"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=429547"
     title="Support aria-activedescendant usage in nsIAccesible::TakeFocus()">
    Mozilla Bug 429547
  </a>
  <a target="_blank"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=761102"
     title="Focus may be missed when ARIA active-descendant is changed on active composite widget">
    Mozilla Bug 761102
  </a>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test">
  </pre>

  <div role="listbox" aria-activedescendant="item1" id="listbox" tabindex="1"
       aria-owns="item3">
    <div role="listitem" id="item1">item1</div>
    <div role="listitem" id="item2">item2</div>
    <div role="listitem" id="roaming">roaming</div>
    <div role="listitem" id="roaming2">roaming2</div>
  </div>
  <div role="listitem" id="item3">item3</div>

  <div role="combobox" id="combobox">
    <input id="combobox_entry">
    <ul>
      <li role="option" id="combobox_option1">option1</li>
      <li role="option" id="combobox_option2">option2</li>
    </ul>
  </div>

  <!-- aria-activedescendant targeting a non-descendant -->
  <input id="activedesc_nondesc_input" aria-activedescendant="activedesc_nondesc_option">
  <div role="listbox">
    <div role="option" id="activedesc_nondesc_option">option</div>
  </div>

  <div id="shadow"></div>
  <script>
    let host = document.getElementById("shadow");
    let shadow = host.attachShadow({mode: "open"});
    let listbox = document.createElement("div");
    listbox.id = "shadowListbox";
    listbox.setAttribute("role", "listbox");
    listbox.setAttribute("tabindex", "0");
    shadow.appendChild(listbox);
    let item = document.createElement("div");
    item.id = "shadowItem1";
    item.setAttribute("role", "option");
    listbox.appendChild(item);
    listbox.setAttribute("aria-activedescendant", "shadowItem1");
    item = document.createElement("div");
    item.id = "shadowItem2";
    item.setAttribute("role", "option");
    listbox.appendChild(item);
  </script>

  <div id="comboboxWithHiddenList" tabindex="0" role="combobox" aria-owns="hiddenList">
  </div>
  <div id="hiddenList" hidden role="listbox">
    <div id="hiddenListOption" role="option"></div>
  </div>
</body>
</html>