summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/test/browser/browser_threads.js
blob: 4bfcb5fc11f832af8a8b67b2b620aabb9f45ebaa (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* 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/. */

const { MessageGenerator } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageGenerator.jsm"
);

let tabmail = document.getElementById("tabmail");
let about3Pane = tabmail.currentAbout3Pane;
let { threadPane, threadTree } = about3Pane;
let { notificationBox } = threadPane;
let rootFolder, testFolder, testMessages;

add_setup(async function () {
  Services.prefs.setStringPref(
    "mail.ignore_thread.learn_more_url",
    "http://mochi.test:8888/"
  );
  document.getElementById("toolbar-menubar").removeAttribute("autohide");

  let generator = new MessageGenerator();

  MailServices.accounts.createLocalMailAccount();
  let account = MailServices.accounts.accounts[0];
  account.addIdentity(MailServices.accounts.createIdentity());
  rootFolder = account.incomingServer.rootFolder;

  rootFolder.createSubfolder("threads", null);
  testFolder = rootFolder
    .getChildNamed("threads")
    .QueryInterface(Ci.nsIMsgLocalMailFolder);

  testFolder.addMessageBatch(
    generator
      .makeMessages({ count: 25, msgsPerThread: 5 })
      .map(message => message.toMboxString())
  );
  testMessages = [...testFolder.messages];

  about3Pane.displayFolder(testFolder.URI);
  about3Pane.paneLayout.messagePaneVisible = false;
  goDoCommand("cmd_expandAllThreads");

  await ensure_table_view();

  // Check the initial state of a sample of messages.

  checkRowThreadState(0, true);
  checkRowThreadState(1, false);
  checkRowThreadState(2, false);
  checkRowThreadState(3, false);
  checkRowThreadState(4, false);
  checkRowThreadState(5, true);
  checkRowThreadState(10, true);
  checkRowThreadState(15, true);
  checkRowThreadState(20, true);

  registerCleanupFunction(async () => {
    await ensure_cards_view();
    MailServices.accounts.removeAccount(account, false);
    about3Pane.paneLayout.messagePaneVisible = true;
    Services.prefs.clearUserPref("mail.ignore_thread.learn_more_url");
  });
});

/**
 * Test that a double click on a button doesn't trigger the opening of the
 * message.
 */
add_task(async function checkDoubleClickOnThreadButton() {
  let row = threadTree.getRowAtIndex(20);
  Assert.ok(
    !row.classList.contains("collapsed"),
    "The thread row should be expanded"
  );

  Assert.equal(tabmail.tabInfo.length, 1, "Only 1 tab currently visible");

  let button = row.querySelector(".thread-container .twisty");
  // Simulate a double click on the twisty icon.
  EventUtils.synthesizeMouseAtCenter(button, { clickCount: 2 }, about3Pane);

  Assert.equal(
    tabmail.tabInfo.length,
    1,
    "The message wasn't opened in another tab"
  );

  // Normally a double click on the twisty would close and open the thread, but
  // this simulated click is too fast and the second click happens before the
  // row is collapsed. Let's click on it again once to return to the original
  // state.
  EventUtils.synthesizeMouseAtCenter(button, {}, about3Pane);

  Assert.ok(
    !row.classList.contains("collapsed"),
    "The double click was registered as 2 separate clicks and the thread row is still expanded"
  );
});

add_task(async function testIgnoreThread() {
  // Check the menu items for the root message in a thread.

  threadTree.selectedIndex = 0;
  await checkMessageMenu({ killThread: false });
  await checkContextMenu(0, { "mailContext-ignoreThread": false });

  // Check and use the menu items for a message inside a thread. Ignoring a
  // thread should work from any message in the thread.
  threadTree.selectedIndex = 2;
  await checkMessageMenu({ killThread: false });
  await checkContextMenu(
    2,
    { "mailContext-ignoreThread": false },
    "mailContext-ignoreThread"
  );

  // Check the thread is ignored and collapsed.

  checkRowThreadState(0, "ignore");
  Assert.ok(
    threadTree.getRowAtIndex(0).classList.contains("collapsed"),
    "ignored row should have the 'collapsed' class"
  );

  // Restore the thread using the context menu item.

  threadTree.selectedIndex = 0;
  await checkMessageMenu({ killThread: true });
  await checkContextMenu(
    0,
    { "mailContext-ignoreThread": true },
    "mailContext-ignoreThread"
  );

  checkRowThreadState(0, true);

  // Ignore the next thread. The first thread was collapsed by ignoring it,
  // so the next thread is at index 1.

  threadTree.selectedIndex = 1;
  await checkMessageMenu({ killThread: false });
  await checkContextMenu(
    1,
    { "mailContext-ignoreThread": false },
    "mailContext-ignoreThread"
  );

  checkRowThreadState(1, "ignore");
  Assert.ok(
    threadTree.getRowAtIndex(1).classList.contains("collapsed"),
    "ignored row should have the 'collapsed' class"
  );

  // Check the notification about the ignored thread.

  let notification =
    notificationBox.getNotificationWithValue("ignoreThreadInfo");
  let label = notification.shadowRoot.querySelector(
    "label.notification-message"
  );
  Assert.stringContains(label.textContent, testMessages[5].subject);
  let buttons = notification.shadowRoot.querySelectorAll(
    "button.notification-button"
  );
  Assert.equal(buttons.length, 2);

  // Click the Learn More button, and check it opens the support page in a new tab.
  let tabOpenPromise = BrowserTestUtils.waitForEvent(
    tabmail.tabContainer,
    "TabOpen"
  );
  EventUtils.synthesizeMouseAtCenter(buttons[0], {}, about3Pane);
  let event = await tabOpenPromise;
  await BrowserTestUtils.browserLoaded(event.detail.tabInfo.browser);
  Assert.equal(
    event.detail.tabInfo.browser.currentURI.spec,
    "http://mochi.test:8888/"
  );
  tabmail.closeTab(event.detail.tabInfo);
  Assert.ok(notification.parentNode, "notification should not be closed");

  // Click the Undo button, and check it stops ignoring the thread.
  EventUtils.synthesizeMouseAtCenter(buttons[1], {}, about3Pane);
  await TestUtils.waitForCondition(() => !notification.parentNode);
  checkRowThreadState(1, true);

  goDoCommand("cmd_expandAllThreads");
});

add_task(async function testIgnoreSubthread() {
  // Check and use the menu items for a message inside a thread.

  threadTree.selectedIndex = 12;
  await checkMessageMenu({ killSubthread: false });
  await checkContextMenu(
    12,
    { "mailContext-ignoreSubthread": false },
    "mailContext-ignoreSubthread"
  );

  // Check all messages in that subthread are marked as ignored.

  checkRowThreadState(12, "ignoreSubthread");
  checkRowThreadState(13, "ignoreSubthread");
  checkRowThreadState(14, "ignoreSubthread");

  // Restore the subthread using the context menu item.

  threadTree.selectedIndex = 12;
  await checkMessageMenu({ killSubthread: true });
  await checkContextMenu(
    12,
    { "mailContext-ignoreSubthread": true },
    "mailContext-ignoreSubthread"
  );

  checkRowThreadState(12, false);
  checkRowThreadState(13, false);
  checkRowThreadState(14, false);

  // Ignore a different subthread.

  threadTree.selectedIndex = 17;
  await checkMessageMenu({ killSubthread: false });
  await checkContextMenu(
    17,
    { "mailContext-ignoreSubthread": false },
    "mailContext-ignoreSubthread"
  );

  checkRowThreadState(17, "ignoreSubthread");
  checkRowThreadState(18, "ignoreSubthread");
  checkRowThreadState(19, "ignoreSubthread");

  // Check the notification about the ignored subthread.

  let notification =
    notificationBox.getNotificationWithValue("ignoreThreadInfo");
  let label = notification.shadowRoot.querySelector(
    "label.notification-message"
  );
  Assert.stringContains(label.textContent, testMessages[17].subject);
  let buttons = notification.shadowRoot.querySelectorAll(
    "button.notification-button"
  );
  Assert.equal(buttons.length, 2);

  // Click the Undo button, and check it stops ignoring the subthread.
  EventUtils.synthesizeMouseAtCenter(buttons[1], {}, about3Pane);
  await TestUtils.waitForCondition(() => !notification.parentNode);
  checkRowThreadState(17, false);
  checkRowThreadState(18, false);
  checkRowThreadState(19, false);
});

add_task(async function testWatchThread() {
  threadTree.selectedIndex = 20;
  await checkMessageMenu({ watchThread: false });
  await checkContextMenu(
    20,
    { "mailContext-watchThread": false },
    "mailContext-watchThread"
  );

  checkRowThreadState(20, "watched");
  checkRowThreadState(21, false);

  await checkMessageMenu({ watchThread: true });
  await checkContextMenu(
    20,
    { "mailContext-watchThread": true },
    "mailContext-watchThread"
  );

  checkRowThreadState(20, true);
  checkRowThreadState(21, false);
});

async function checkContextMenu(index, expectedStates, itemToActivate) {
  let contextMenu = about3Pane.document.getElementById("mailContext");
  let row = threadTree.getRowAtIndex(index);

  let shownPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(
    row.querySelector(".subject-line"),
    { type: "contextmenu" },
    about3Pane
  );
  await shownPromise;

  for (let [id, checkedState] of Object.entries(expectedStates)) {
    assertCheckedState(about3Pane.document.getElementById(id), checkedState);
  }

  let hiddenPromise = BrowserTestUtils.waitForEvent(contextMenu, "popuphidden");
  if (itemToActivate) {
    contextMenu.activateItem(
      about3Pane.document.getElementById(itemToActivate)
    );
  } else {
    contextMenu.hidePopup();
  }
  await hiddenPromise;
}

async function checkMessageMenu(expectedStates) {
  if (AppConstants.platform == "macosx") {
    // Can't check the menu.
    return;
  }

  let messageMenu = document.getElementById("messageMenu");

  let shownPromise = BrowserTestUtils.waitForEvent(
    messageMenu.menupopup,
    "popupshown"
  );
  EventUtils.synthesizeMouseAtCenter(messageMenu, {}, window);
  await shownPromise;

  for (let [id, checkedState] of Object.entries(expectedStates)) {
    assertCheckedState(document.getElementById(id), checkedState);
  }

  messageMenu.menupopup.hidePopup();
}

function assertCheckedState(menuItem, checkedState) {
  if (checkedState) {
    Assert.equal(menuItem.getAttribute("checked"), "true");
  } else {
    Assert.ok(
      !menuItem.hasAttribute("checked") ||
        menuItem.getAttribute("checked") == "false"
    );
  }
}

function checkRowThreadState(index, expected) {
  let row = threadTree.getRowAtIndex(index);
  let icon = row.querySelector(".threadcol-column img");

  if (!expected) {
    Assert.ok(
      !row.classList.contains("children"),
      "row should not have the 'children' class"
    );
    Assert.ok(BrowserTestUtils.is_hidden(icon), "icon should be hidden");
    return;
  }

  Assert.ok(BrowserTestUtils.is_visible(icon), "icon should be visible");

  let shouldHaveChildrenClass = true;
  let iconContent = getComputedStyle(icon).content;

  switch (expected) {
    case true:
      Assert.stringContains(iconContent, "/thread-sm.svg");
      break;
    case "ignore":
      Assert.stringContains(row.dataset.properties, "ignore");
      Assert.stringContains(iconContent, "/thread-ignored.svg");
      break;
    case "ignoreSubthread":
      Assert.stringContains(row.dataset.properties, "ignoreSubthread");
      Assert.stringContains(iconContent, "/subthread-ignored.svg");
      shouldHaveChildrenClass = false;
      break;
    case "watched":
      Assert.stringContains(row.dataset.properties, "watch");
      Assert.stringContains(iconContent, "/eye.svg");
      break;
  }

  Assert.equal(
    row.classList.contains("children"),
    shouldHaveChildrenClass,
    `row should${
      shouldHaveChildrenClass ? "" : " not"
    } have the 'children' class`
  );
}