summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/tests/chrome/test_card_container.html
blob: c76c6ec222ef1e6c469fa67fbd7452e67f040b87 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>CardContainer Tests</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <link rel="stylesheet" href="chrome://global/skin/in-content/common.css">
  <link rel="localization" href="browser/firefoxView.ftl"/>
  <script type="module" src="chrome://browser/content/firefoxview/card-container.mjs"></script>
</head>
<body>
  <style>
  </style>
<p id="display"></p>
<div id="content">
  <card-container viewAllPage="history">
    <h2 slot="header" data-l10n-id="history-header"></h2>
    <ul slot="main">
        <li>History Row 1</li>
        <li>History Row 2</li>
        <li>History Row 3</li>
        <li>History Row 4</li>
        <li>History Row 5</li>
    </ul>
  </card-container>
</div>
<pre id="test">
<script class="testbody" type="application/javascript">
  const cardContainer = document.querySelector("card-container");

  /**
  * Tests that the card-container can expand and collapse when the summary element is clicked
  */
  add_task(async function test_open_close_card() {
    is(
      cardContainer.isExpanded,
      true,
      "The card-container is expanded initially"
    );

    // Click the summary to collapse the details disclosure
    cardContainer.summaryEl.click();
    is(
      cardContainer.detailsEl.hasAttribute("open"),
      false,
      "The card-container is collapsed"
    );

    // Click on the summary again to expand the details disclosure
    cardContainer.summaryEl.click();
    is(
      cardContainer.detailsEl.hasAttribute("open"),
      true,
      "The card-container is expanded"
    );
  });

   /**
   * Tests keyboard navigation of the card-container component
   */
  add_task(async function test_keyboard_navigation() {
    const tab = async shiftKey => {
      info(`Tab${shiftKey ? ' + Shift' : ''}`);
      synthesizeKey("KEY_Tab", { shiftKey });
    };
    const enter = async () => {
      info("Enter");
      synthesizeKey("KEY_Enter", {});
    };

     // Setting this pref allows the test to run as expected with a keyboard on MacOS
    await SpecialPowers.pushPrefEnv({
      set: [["accessibility.tabfocus", 7]],
    });

    cardContainer.summaryEl.focus();
    is(
      cardContainer.shadowRoot.activeElement,
      cardContainer.summaryEl,
      "Focus should be on the summary element within card-container"
    );

    // Tab to the 'View all' link
    await tab();
    is(
      cardContainer.shadowRoot.activeElement,
      cardContainer.viewAllLink,
      "Focus should be on the 'View all' link within card-container"
    );

    // Shift + Tab back to the summary element
    await tab(true);
    is(
      cardContainer.shadowRoot.activeElement,
      cardContainer.summaryEl,
      "Focus should be back on the summary element within card-container"
    );

    // Select the summary to collapse the details disclosure
    await enter();
    is(
      cardContainer.detailsEl.hasAttribute("open"),
      false,
      "The card-container is collapsed"
    );

    // Select the summary again to expand the details disclosure
    await enter();
    is(
      cardContainer.detailsEl.hasAttribute("open"),
      true,
      "The card-container is expanded"
    );

    await SpecialPowers.popPrefEnv();
  });
</script>
</pre>
</body>
</html>