summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/test/browser/browser_unifiedToolbarTab.js
blob: 336199ee5127a8c97c088970ad6afce64c6c66ad (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
/* 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/. */

let tabmail = document.getElementById("tabmail");
registerCleanupFunction(() => {
  tabmail.closeOtherTabs(tabmail.tabInfo[0]);
});
let browser;
let testDocument;

const waitForRender = () => {
  return new Promise(resolve => {
    window.requestAnimationFrame(resolve);
  });
};
const getTabButton = tab => tab.shadowRoot.querySelector("button");
/**
 * Get the relevant elements for the tab at the given index.
 *
 * @param {number} tabIndex
 * @returns {{tab: UnifiedToolbarTab, button: HTMLButtonElement, pane: HTMLElement}}
 */
const getTabElements = tabIndex => {
  const tab = testDocument.querySelector(
    `unified-toolbar-tab:nth-child(${tabIndex})`
  );
  const button = getTabButton(tab);
  const pane = tab.pane;
  return { tab, button, pane };
};

add_setup(async function () {
  let tab = tabmail.openTab("contentTab", {
    url: "chrome://mochitests/content/browser/comm/mail/components/unifiedtoolbar/test/browser/files/unifiedToolbarTab.xhtml",
  });

  await BrowserTestUtils.browserLoaded(tab.browser);
  tab.browser.focus();
  browser = tab.browser;
  testDocument = tab.browser.contentWindow.document;
});

add_task(function test_tabElementInitialization() {
  const activeTab = testDocument.querySelector("unified-toolbar-tab[selected]");
  is(
    activeTab.getAttribute("role"),
    "presentation",
    "The custom element is just for show"
  );
  ok(
    !activeTab.hasAttribute("aria-controls"),
    "aria-controls removed from custom element"
  );
  ok(activeTab.hasAttribute("selected"), "Active tab kept itself selected");
  const tabButton = getTabButton(activeTab);
  is(tabButton.getAttribute("role"), "tab", "Active tab is marked as tab");
  is(tabButton.tabIndex, 0, "Active tab is in the focus ring");
  is(
    tabButton.getAttribute("aria-selected"),
    "true",
    "Tab is marked as selected"
  );
  ok(
    tabButton.hasAttribute("aria-controls"),
    "aria-controls got given to button"
  );

  const otherTab = testDocument.querySelector(
    "unified-toolbar-tab:not([selected])"
  );
  is(
    otherTab.getAttribute("role"),
    "presentation",
    "The custom element is just for show on the other tab"
  );
  ok(
    !otherTab.hasAttribute("aria-controls"),
    "aria-controls removed from the other tab"
  );
  ok(!otherTab.hasAttribute("selected"), "Other tab didn't select itself");
  const otherButton = getTabButton(otherTab);
  is(otherButton.getAttribute("role"), "tab", "Other tab is marked as tab");
  is(otherButton.tabIndex, -1, "Other tab is not in the focus ring");
  ok(
    !otherButton.hasAttribute("aria-selected"),
    "Other tab isn't marked as selected"
  );
  ok(
    otherButton.hasAttribute("aria-controls"),
    "aria-controls got given to other button"
  );
});

add_task(async function test_paneGetter() {
  const tab1 = getTabElements(1);
  const tabPane = testDocument.getElementById("tabPane");
  const tab2 = getTabElements(2);
  const otherTabPane = testDocument.getElementById("otherTabPane");

  is(
    tab1.button.getAttribute("aria-controls"),
    tabPane.id,
    "Tab 1 controls tab 1 pane"
  );
  is(
    tab2.button.getAttribute("aria-controls"),
    otherTabPane.id,
    "Tab 2 controls tab 2 pane"
  );

  Assert.strictEqual(
    tab1.tab.pane,
    tabPane,
    "Tab 1 pane getter returns #tabPane"
  );
  Assert.strictEqual(
    tab2.tab.pane,
    otherTabPane,
    "Tab 2 pane getter returns #otherTabPane"
  );
});

add_task(async function test_unselect() {
  const tab = getTabElements(1);

  tab.tab.unselect();

  ok(!tab.button.hasAttribute("aria-selected"), "Tab not marked as selected");
  is(tab.button.tabIndex, -1, "Tab not in focus ring");
  ok(!tab.tab.hasAttribute("selected"), "Tab not marked selected");
  ok(tab.pane.hidden, "Tab pane hidden");
});

add_task(async function test_select() {
  const tab1 = getTabElements(1);
  const tab2 = getTabElements(2);

  let tabswitchPromise = BrowserTestUtils.waitForEvent(
    testDocument.body,
    "tabswitch"
  );
  tab1.tab.select();

  await tabswitchPromise;
  ok(tab1.tab.hasAttribute("selected"), "Tab 1 selected");
  is(
    tab1.button.getAttribute("aria-selected"),
    "true",
    "Tab 1 marked as selected"
  );
  is(tab1.button.tabIndex, 0, "Tab 1 keyboard selectable");
  ok(!tab1.pane.hidden, "Tab pane for tab 1 visible");

  tabswitchPromise = BrowserTestUtils.waitForEvent(tab2.tab, "tabswitch");
  tab2.tab.select();

  await tabswitchPromise;
  ok(tab2.tab.hasAttribute("selected"), "Tab 2 selected");
  is(
    tab2.button.getAttribute("aria-selected"),
    "true",
    "Tab 2 has a11y selection"
  );
  is(tab2.button.tabIndex, 0, "Tab 2 keyboard selectable");
  ok(!tab2.pane.hidden, "Tab pane for tab 2 visible");

  ok(!tab1.tab.hasAttribute("selected"), "Tab 1 unselected");
  ok(!tab1.button.hasAttribute("aria-selected"), "Tab 1 marked as unselected");
  is(tab1.button.tabIndex, -1, "Tab 1 not in focus ring");
  ok(tab1.pane.hidden, "Tab pane for tab 1 hidden");
});

add_task(async function test_switchingTabWithMouse() {
  const tab1 = getTabElements(1);
  const tab2 = getTabElements(2);

  tab2.button.click();
  ok(tab2.tab.hasAttribute("selected"), "Other tab is selected");
  is(tab2.button.tabIndex, 0, "Other tab is in focus ring");
  ok(!tab1.tab.hasAttribute("selected"), "First tab is not selected");
  is(tab1.button.tabIndex, -1, "First tab is not in focus ring");
  ok(
    BrowserTestUtils.is_visible(tab2.pane),
    "Tab pane for selected tab is visible"
  );
  ok(BrowserTestUtils.is_hidden(tab1.pane), "Tab pane for first tab is hidden");

  tab1.button.click();
  ok(tab1.tab.hasAttribute("selected"), "First tab is selected");
  is(tab1.button.tabIndex, 0, "First tab is in focus ring");
  ok(!tab2.tab.hasAttribute("selected"), "Other tab is not selected");
  is(tab2.button.tabIndex, -1, "Other tab is not in focus ring");
  ok(
    BrowserTestUtils.is_visible(tab1.pane),
    "Tab pane for first tab is visible"
  );
  ok(BrowserTestUtils.is_hidden(tab2.pane), "Tab pane for other tab is hidden");
});

add_task(async function test_switchingTabWithKeyboard() {
  const tab1 = getTabElements(1);
  const tab2 = getTabElements(2);

  tab1.tab.focus();
  is(testDocument.activeElement, tab1.tab, "Initially first tab is active");

  await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, browser);
  is(testDocument.activeElement, tab2.tab, "Second tab is focused");
  is(
    tab2.tab.shadowRoot.activeElement,
    tab2.button,
    "Button within tab is focused"
  );
  await BrowserTestUtils.synthesizeKey(" ", {}, browser);
  ok(tab2.tab.hasAttribute("selected"), "Other tab is selected");
  is(tab2.button.tabIndex, 0, "Other tab is in focus ring");
  ok(!tab1.tab.hasAttribute("selected"), "First tab is not selected");
  is(tab1.button.tabIndex, -1, "First tab is not in focus ring");
  ok(
    BrowserTestUtils.is_visible(tab2.pane),
    "Tab pane for selected tab is visible"
  );
  ok(BrowserTestUtils.is_hidden(tab1.pane), "Tab pane for first tab is hidden");

  await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", {}, browser);
  is(testDocument.activeElement, tab1.tab, "Previous tab is selected");
  await BrowserTestUtils.synthesizeKey("KEY_End", {}, browser);
  is(testDocument.activeElement, tab2.tab, "Last tab is selected");
  await BrowserTestUtils.synthesizeKey("KEY_Home", {}, browser);
  is(testDocument.activeElement, tab1.tab, "First tab is selected");
  await BrowserTestUtils.synthesizeKey("KEY_Enter", {}, browser);
  ok(tab1.tab.hasAttribute("selected"), "First tab is selected");
  is(tab1.button.tabIndex, 0, "First tab is in focus ring");
  ok(!tab2.tab.hasAttribute("selected"), "Other tab is not selected");
  is(tab2.button.tabIndex, -1, "Other tab is not in focus ring");
  ok(
    BrowserTestUtils.is_visible(tab1.pane),
    "Tab pane for first tab is visible"
  );
  ok(BrowserTestUtils.is_hidden(tab2.pane), "Tab pane for other tab is hidden");
});

add_task(async function test_switchingTabWithKeyboardRTL() {
  testDocument.dir = "rtl";
  await waitForRender();
  const tab1 = getTabElements(1);
  const tab2 = getTabElements(2);

  tab1.tab.focus();
  is(testDocument.activeElement, tab1.tab, "Initially first tab is active");

  await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", {}, browser);
  is(testDocument.activeElement, tab2.tab, "Second tab is selected");
  is(
    tab2.tab.shadowRoot.activeElement,
    tab2.button,
    "Button within tab is focused"
  );
  await BrowserTestUtils.synthesizeKey(" ", {}, browser);
  ok(tab2.tab.hasAttribute("selected"), "Other tab is selected");
  is(tab2.button.tabIndex, 0, "Other tab is in focus ring");
  ok(!tab1.tab.hasAttribute("selected"), "First tab is not selected");
  is(tab1.button.tabIndex, -1, "First tab is not in focus ring");
  ok(
    BrowserTestUtils.is_visible(tab2.pane),
    "Tab pane for selected tab is visible"
  );
  ok(BrowserTestUtils.is_hidden(tab1.pane), "Tab pane for first tab is hidden");

  await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, browser);
  is(testDocument.activeElement, tab1.tab, "Previous tab is selected");
  await BrowserTestUtils.synthesizeKey("KEY_Enter", {}, browser);
  ok(tab1.tab.hasAttribute("selected"), "First tab is selected");
  is(tab1.button.tabIndex, 0, "First tab is in focus ring");
  ok(!tab2.tab.hasAttribute("selected"), "Other tab is not selected");
  is(tab2.button.tabIndex, -1, "Other tab is not in focus ring");
  ok(
    BrowserTestUtils.is_visible(tab1.pane),
    "Tab pane for first tab is visible"
  );
  ok(BrowserTestUtils.is_hidden(tab2.pane), "Tab pane for other tab is hidden");

  testDocument.dir = "ltr";
});