summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/tabmail/browser_tabSwitch.js
blob: 38a158a66d00316f2c1c46405e33cb51d7d1e6e3 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* 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/. */

add_task(async function () {
  // Helper functions.

  function assertSelected(element, name) {
    Assert.ok(
      element.hasAttribute("selected"),
      `${name} has selected attribute`
    );
  }
  function assertNotSelected(element, name) {
    Assert.ok(
      !element.hasAttribute("selected"),
      `${name} does NOT have selected attribute`
    );
  }
  function getTabElements() {
    return [...tabmail.tabContainer.querySelectorAll("tab")];
  }
  function checkTabElements(expectedCount, expectedSelection) {
    let tabElements = getTabElements();
    Assert.equal(
      tabElements.length,
      expectedCount,
      `${expectedCount} tab elements exist`
    );

    for (let i = 0; i < expectedCount; i++) {
      if (i == expectedSelection) {
        assertSelected(tabElements[i], `tab element ${i}`);
      } else {
        assertNotSelected(tabElements[i], `tab element ${i}`);
      }
    }
  }
  async function switchTab(index) {
    let tabElement = getTabElements()[index];
    eventPromise = BrowserTestUtils.waitForEvent(
      tabmail.tabContainer,
      "TabSelect"
    );
    EventUtils.synthesizeMouseAtCenter(tabElement, {});
    event = await eventPromise;
    Assert.equal(
      event.target,
      tabElement,
      `TabSelect event fired from tab ${index}`
    );
  }
  async function closeTab(index) {
    let tabElement = getTabElements()[index];
    eventPromise = BrowserTestUtils.waitForEvent(
      tabmail.tabContainer,
      "TabClose"
    );
    EventUtils.synthesizeMouseAtCenter(
      tabElement.querySelector(".tab-close-button"),
      {}
    );
    event = await eventPromise;
    Assert.equal(
      event.target,
      tabElement,
      `TabClose event fired from tab ${index}`
    );
  }

  // Collect some elements.

  let tabmail = document.getElementById("tabmail");
  let calendarTabButton = document.getElementById("calendarButton");

  let mailTabPanel = document.getElementById("mail3PaneTab1");
  let mailTabBrowser = document.getElementById("mail3PaneTabBrowser1");
  let folderTree = mailTabBrowser.contentDocument.getElementById("folderTree");
  let calendarTabPanel = document.getElementById("calendarTabPanel");
  let contentTab;
  let contentTabPanel;

  let calendarList = document.getElementById("calendar-list");

  let eventPromise;
  let event;

  // Check we're in a good state to start with.

  Assert.equal(tabmail.tabInfo.length, 1, "only one tab is open");
  checkTabElements(1, 0);
  assertSelected(mailTabPanel, "mail tab's panel");

  // Set the focus on the mail tab.

  folderTree.focus();
  Assert.equal(
    document.activeElement,
    mailTabBrowser,
    "mail tab's browser has focus"
  );
  Assert.equal(
    mailTabBrowser.contentDocument.activeElement,
    folderTree,
    "folder tree has focus"
  );

  // Switch to the calendar tab.

  eventPromise = BrowserTestUtils.waitForEvent(tabmail.tabContainer, "TabOpen");
  EventUtils.synthesizeMouseAtCenter(calendarTabButton, {});
  event = await eventPromise;
  Assert.equal(
    event.target,
    getTabElements()[1],
    "TabOpen event fired from tab 1"
  );

  checkTabElements(2, 1);
  assertNotSelected(mailTabPanel, "mail tab's panel");
  assertSelected(calendarTabPanel, "calendar tab's panel");
  Assert.equal(
    document.activeElement,
    document.body,
    "mail tab's browser does NOT have focus"
  );
  Assert.equal(
    tabmail.tabInfo[0].lastActiveElement,
    folderTree,
    "mail tab's last active element should be stored"
  );
  Assert.ok(
    !tabmail.tabInfo[1].lastActiveElement,
    "calendar tab's last active element should not be stored yet"
  );

  // Set the focus on the calendar list.

  EventUtils.synthesizeMouseAtCenter(calendarList, {});
  Assert.equal(document.activeElement, calendarList, "calendar list has focus");

  // Switch to the mail tab.

  await switchTab(0);

  checkTabElements(2, 0);
  assertSelected(mailTabPanel, "mail tab's panel");
  assertNotSelected(calendarTabPanel, "calendar tab's panel");
  Assert.equal(
    document.activeElement,
    mailTabBrowser,
    "mail tab's browser has focus"
  );
  Assert.equal(
    mailTabBrowser.contentDocument.activeElement,
    folderTree,
    "folder tree has focus"
  );
  Assert.ok(
    !tabmail.tabInfo[0].lastActiveElement,
    "mail tab's last active element should have been cleaned up"
  );
  Assert.equal(
    tabmail.tabInfo[1].lastActiveElement,
    calendarList,
    "calendar tab's last active element should be stored"
  );

  // Switch to the calendar tab.

  await switchTab(1);

  checkTabElements(2, 1);
  assertNotSelected(mailTabPanel, "mail tab's panel");
  assertSelected(calendarTabPanel, "calendar tab's panel");
  Assert.equal(document.activeElement, calendarList, "calendar list has focus");
  Assert.equal(
    tabmail.tabInfo[0].lastActiveElement,
    folderTree,
    "mail tab's last active element should be stored"
  );
  Assert.ok(
    !tabmail.tabInfo[1].lastActiveElement,
    "calendar tab's last active element should have been cleaned up"
  );

  // Open a content tab.

  eventPromise = BrowserTestUtils.waitForEvent(tabmail.tabContainer, "TabOpen");
  contentTab = window.openContentTab("https://example.org/");
  contentTabPanel = contentTab.browser.closest(
    ".contentTabInstance"
  ).parentNode;
  event = await eventPromise;
  Assert.equal(
    event.target,
    getTabElements()[2],
    "TabOpen event fired from tab 2"
  );

  checkTabElements(3, 2);
  assertNotSelected(mailTabPanel, "mail tab's panel");
  assertNotSelected(calendarTabPanel, "calendar tab's panel");
  assertSelected(contentTabPanel, "content tab's panel");
  Assert.equal(
    document.activeElement,
    document.body,
    "folder tree and calendar list do NOT have focus"
  );
  Assert.equal(
    tabmail.tabInfo[0].lastActiveElement,
    folderTree,
    "mail tab's last active element should be stored"
  );
  Assert.equal(
    tabmail.tabInfo[1].lastActiveElement,
    calendarList,
    "calendar tab's last active element should be stored"
  );
  Assert.ok(
    !tabmail.tabInfo[2].lastActiveElement,
    "content tab should have no last active element"
  );

  // Switch to the mail tab.

  await switchTab(0);

  checkTabElements(3, 0);
  assertSelected(mailTabPanel, "mail tab's panel");
  assertNotSelected(calendarTabPanel, "calendar tab's panel");
  Assert.equal(
    document.activeElement,
    mailTabBrowser,
    "mail tab's browser has focus"
  );
  Assert.ok(
    !tabmail.tabInfo[0].lastActiveElement,
    "mail tab's last active element should be cleaned up"
  );
  Assert.equal(
    tabmail.tabInfo[1].lastActiveElement,
    calendarList,
    "calendar tab's last active element should be stored"
  );
  Assert.ok(
    !tabmail.tabInfo[2].lastActiveElement,
    "content tab should have no last active element"
  );

  // Switch to the calendar tab.

  await switchTab(1);

  checkTabElements(3, 1);
  assertNotSelected(mailTabPanel, "mail tab's panel");
  assertSelected(calendarTabPanel, "calendar tab's panel");
  Assert.equal(document.activeElement, calendarList, "calendar list has focus");
  Assert.equal(
    tabmail.tabInfo[0].lastActiveElement,
    folderTree,
    "mail tab's last active element should be stored"
  );
  Assert.ok(
    !tabmail.tabInfo[1].lastActiveElement,
    "calendar tab's last active element should be cleaned up"
  );
  Assert.ok(
    !tabmail.tabInfo[2].lastActiveElement,
    "content tab should have no last active element"
  );

  // Switch to the content tab.

  await switchTab(2);

  checkTabElements(3, 2);
  assertNotSelected(mailTabPanel, "mail tab's panel");
  assertNotSelected(calendarTabPanel, "calendar tab's panel");
  assertSelected(contentTabPanel, "content tab's panel");
  Assert.equal(
    document.activeElement,
    document.body,
    "folder tree and calendar list do NOT have focus"
  );
  Assert.equal(
    tabmail.tabInfo[0].lastActiveElement,
    folderTree,
    "mail tab's last active element should be stored"
  );
  Assert.equal(
    tabmail.tabInfo[1].lastActiveElement,
    calendarList,
    "calendar tab's last active element should be stored"
  );
  Assert.ok(
    !tabmail.tabInfo[2].lastActiveElement,
    "content tab should have no last active element"
  );

  // Close the content tab.

  await closeTab(2);

  checkTabElements(2, 1);
  assertNotSelected(mailTabPanel, "mail tab's panel");
  assertSelected(calendarTabPanel, "calendar tab's panel");
  // At this point contentTabPanel is still part of the DOM, it is removed
  // after the TabClose event.
  assertNotSelected(contentTabPanel, "content tab's panel");
  Assert.equal(document.activeElement, calendarList, "calendar list has focus");
  Assert.equal(
    tabmail.tabInfo[0].lastActiveElement,
    folderTree,
    "mail tab's last active element should be stored"
  );
  Assert.ok(
    !tabmail.tabInfo[1].lastActiveElement,
    "calendar tab's last active element should have been cleaned up"
  );

  await new Promise(resolve => setTimeout(resolve));
  Assert.ok(
    !contentTabPanel.parentNode,
    "content tab's panel is removed from the DOM"
  );

  // Close the calendar tab.

  await closeTab(1);

  checkTabElements(1, 0);
  assertSelected(mailTabPanel, "mail tab's panel");
  assertNotSelected(calendarTabPanel, "calendar tab's panel");
  Assert.equal(
    document.activeElement,
    mailTabBrowser,
    "mail tab's browser has focus"
  );
  Assert.ok(
    !tabmail.tabInfo[0].lastActiveElement,
    "mail tab's last active element should have been cleaned up"
  );
});