summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_richlistbox_keyboard.js
blob: a1287335b454fb7540230752f728531cd74e471e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function test_richlistbox_keyboard() {
  await SpecialPowers.pushPrefEnv({ set: [["accessibility.tabfocus", 7]] });
  await BrowserTestUtils.withNewTab("about:about", browser => {
    let document = browser.contentDocument;
    let box = document.createXULElement("richlistbox");

    function checkTabIndices(selectedLine) {
      for (let button of box.querySelectorAll(`.line${selectedLine} button`)) {
        is(
          button.tabIndex,
          0,
          `Should have ensured buttons inside selected line ${selectedLine} are focusable`
        );
      }
      for (let otherButton of box.querySelectorAll(
        `richlistitem:not(.line${selectedLine}) button`
      )) {
        is(
          otherButton.tabIndex,
          -1,
          `Should have ensured buttons outside selected line ${selectedLine} are not focusable`
        );
      }
    }

    let poem = `I wandered lonely as a cloud
         That floats on high o'er vales and hills
         When all at once I saw a crowd
         A host, of golden daffodils;
         Beside the lake, beneath the trees,
         Fluttering and dancing in the breeze.`;
    let items = poem.split("\n").map((line, index) => {
      let item = document.createXULElement("richlistitem");
      item.className = `line${index + 1}`;
      let button1 = document.createXULElement("button");
      button1.textContent = "Like";
      let button2 = document.createXULElement("button");
      button2.textContent = "Subscribe";
      item.append(line.trim(), button1, button2);
      return item;
    });
    box.append(...items);
    document.body.prepend(box);
    box.focus();
    box.getBoundingClientRect(); // force a flush
    box.selectedItem = box.firstChild;
    checkTabIndices(1);
    EventUtils.synthesizeKey("VK_DOWN", {}, document.defaultView);
    is(
      box.selectedItem.className,
      "line2",
      "Should have moved selection to the next line."
    );
    checkTabIndices(2);
    EventUtils.synthesizeKey("VK_TAB", {}, document.defaultView);
    is(
      document.activeElement,
      box.selectedItem.querySelector("button"),
      "Initial button gets focus in the selected list item."
    );
    EventUtils.synthesizeKey("VK_UP", {}, document.defaultView);
    checkTabIndices(1);
    is(
      document.activeElement,
      box.selectedItem.querySelector("button"),
      "Initial button gets focus in the selected list item when moving up with arrow key."
    );
    EventUtils.synthesizeKey("VK_DOWN", {}, document.defaultView);
    checkTabIndices(2);
    is(
      document.activeElement,
      box.selectedItem.querySelector("button"),
      "Initial button gets focus in the selected list item when moving down with arrow key."
    );
  });
});