summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/shared-modules/JunkHelpers.jsm
blob: 6d53271ed5d518bd8d91804cfdb69c5d07a677a3 (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
/* 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/. */

"use strict";

const EXPORTED_SYMBOLS = [
  "mark_selected_messages_as_junk",
  "delete_mail_marked_as_junk",
];

var EventUtils = ChromeUtils.import(
  "resource://testing-common/mozmill/EventUtils.jsm"
);
var { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);

var {
  mc,
  get_about_3pane,
  plan_to_wait_for_folder_events,
  wait_for_message_display_completion,
  wait_for_folder_events,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

/**
 * Mark the selected messages as junk. This is done by pressing the J key.
 *
 * @param aController The controller in whose context to do this, defaults to
 *     |mc| if omitted.
 */
function mark_selected_messages_as_junk(aController) {
  if (aController === undefined) {
    aController = mc;
  }
  let win = get_about_3pane(aController.window);
  if (aController == mc) {
    win.document.getElementById("threadTree").focus();
  }
  EventUtils.synthesizeKey("j", {}, win);
}

/**
 * Delete all mail marked as junk in the selected folder. This is done by
 * activating the menu option from the Tools menu.
 *
 * @param aNumDeletesExpected The number of deletes expected.
 * @param aController The controller in whose context to do this, defaults to
 *     |mc| if omitted.
 */
async function delete_mail_marked_as_junk(aNumDeletesExpected, aController) {
  if (aController === undefined) {
    aController = mc;
  }
  let win = get_about_3pane(aController.window);

  // Monkey patch and wrap around the deleteJunkInFolder function, mainly for
  // the case where deletes aren't expected.
  let realDeleteJunkInFolder = win.deleteJunkInFolder;
  let numMessagesDeleted = null;
  let fakeDeleteJunkInFolder = function () {
    numMessagesDeleted = realDeleteJunkInFolder();
    return numMessagesDeleted;
  };
  try {
    win.deleteJunkInFolder = fakeDeleteJunkInFolder;

    // If something is loading, make sure it finishes loading...
    wait_for_message_display_completion(aController);
    if (aNumDeletesExpected != 0) {
      plan_to_wait_for_folder_events(
        "DeleteOrMoveMsgCompleted",
        "DeleteOrMoveMsgFailed"
      );
    }

    win.goDoCommand("cmd_deleteJunk");

    if (aNumDeletesExpected != 0) {
      wait_for_folder_events();
    }

    // If timeout waiting for numMessagesDeleted to turn non-null,
    // this either means that deleteJunkInFolder didn't get called or that it
    // didn't return a value."

    await TestUtils.waitForCondition(
      () => numMessagesDeleted === aNumDeletesExpected,
      `Should have got ${aNumDeletesExpected} deletes, not ${numMessagesDeleted}`
    );
  } finally {
    win.deleteJunkInFolder = realDeleteJunkInFolder;
  }
}