summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_junkingWhenDisabled.js
blob: b6a103b069758b0cfbe3d3909e9133a1e1d463ba (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
/* 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/. */

/*
 * Test that junk actions work even when the bayes filtering of incoming
 *  messages is disabled, as fixed in bug 487610. Test developed by Kent
 *  James using test_nsMsgDBView.js as a base.
 */

const { TreeSelection } = ChromeUtils.importESModule(
  "chrome://messenger/content/tree-selection.mjs"
);
var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { MessageGenerator, SyntheticMessageSet } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageGenerator.jsm"
);
var { MessageInjection } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageInjection.jsm"
);
var { PromiseUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PromiseUtils.sys.mjs"
);

var nsIMFNService = Ci.nsIMsgFolderNotificationService;

// fake objects needed to get nsMsgDBView to operate on selected messages.
// Warning: these are partial implementations. If someone adds additional
// calls to these objects in nsMsgDBView and friends, it will also
// be necessary to add fake versions of those calls here.

var gFakeSelection = new TreeSelection(null);

// Items used to add messages to the folder

var gMessageGenerator = new MessageGenerator();

var messageInjection = new MessageInjection(
  { mode: "local" },
  gMessageGenerator
);

var gLocalInboxFolder = messageInjection.getInboxFolder();
var gListener;
var gCommandUpdater;

var gDBView;
var gTreeView;

var CommandUpdaterWithPromise = function () {
  this.deferred = PromiseUtils.defer();
};
CommandUpdaterWithPromise.prototype = {
  async promiseSelectionSummarized() {
    await this.deferred.promise;
    this.deferred = PromiseUtils.defer();
    return this.deferred.promise;
  },

  updateCommandStatus() {
    // the back end is smart and is only telling us to update command status
    // when the # of items in the selection has actually changed.
  },

  displayMessageChanged(aFolder, aSubject, aKeywords) {},

  updateNextMessageAfterDelete() {},
  summarizeSelection() {
    this.deferred.resolve();
  },
};

// Our listener, which captures events and does the real tests.
function gMFListener() {
  this._promiseMsgsMoveCopyCompleted = new Promise(resolve => {
    this._resolveMsgsMoveCopyCompleted = resolve;
  });
  this._promiseFolderAdded = new Promise(resolve => {
    this._resolveFolderAdded = resolve;
  });
}
gMFListener.prototype = {
  msgsMoveCopyCompleted(aMove, aSrcMsgs, aDestFolder, aDestMsgs) {
    Assert.ok(aDestFolder.getFlag(Ci.nsMsgFolderFlags.Junk));
    // I tried to test this by counting messages in the folder, didn't work.
    //  Maybe all updates are not completed yet. Anyway I do it by just
    //  making sure there is something in the destination array.
    Assert.ok(aDestMsgs.length > 0);
    this._resolveMsgsMoveCopyCompleted();
  },

  folderAdded(aFolder) {
    // this should be a junk folder
    Assert.ok(aFolder.getFlag(Ci.nsMsgFolderFlags.Junk));
    this._resolveFolderAdded();
  },
  get promiseMsgsMoveCopyCompleted() {
    return this._promiseMsgsMoveCopyCompleted;
  },
  get promiseFolderAdded() {
    return this._promiseFolderAdded;
  },
};

add_setup(async function () {
  // Set option so that when messages are marked as junk, they move to the junk folder
  Services.prefs.setBoolPref("mail.spam.manualMark", true);

  // 0 == "move to junk folder", 1 == "delete"
  Services.prefs.setIntPref("mail.spam.manualMarkMode", 0);

  // Disable bayes filtering on the local account. That's the whole point of this test,
  //  to make sure that the junk move happens anyway.
  gLocalInboxFolder.server.spamSettings.level = 0;

  // Add folder listeners that will capture async events.
  let flags = nsIMFNService.msgsMoveCopyCompleted | nsIMFNService.folderAdded;
  gListener = new gMFListener();
  MailServices.mfn.addListener(gListener, flags);

  // Build up a message.
  await messageInjection.makeNewSetsInFolders([gLocalInboxFolder], [{}]);
  let view_type = "threaded";
  let view_flag = Ci.nsMsgViewFlagsType.kThreadedDisplay;
  let dbviewContractId = "@mozilla.org/messenger/msgdbview;1?type=" + view_type;

  // Always start out fully expanded.
  view_flag |= Ci.nsMsgViewFlagsType.kExpandAll;

  gCommandUpdater = new CommandUpdaterWithPromise();

  gDBView = Cc[dbviewContractId].createInstance(Ci.nsIMsgDBView);
  gDBView.init(null, null, null);
  var outCount = {};
  gDBView.open(
    gLocalInboxFolder,
    Ci.nsMsgViewSortType.byDate,
    Ci.nsMsgViewSortOrder.ascending,
    view_flag,
    outCount
  );

  gTreeView = gDBView.QueryInterface(Ci.nsITreeView);
  gTreeView.selection = gFakeSelection;
  gFakeSelection.view = gTreeView;
});

add_task(async function test_first_junking_create_folder() {
  // In the proposed fix for bug 487610, the first call to junk messages
  //  only creates the junk folder, it does not actually successfully move
  //  messages. So we junk messages twice so we can really see a move. But
  //  if that gets fixed and the messages actually move on the first call,
  //  I want this test to succeed as well. So I don't actually count how
  //  many messages get moved, just that some do on the second move.

  // Select and junk all messages.
  gDBView.doCommand(Ci.nsMsgViewCommandType.selectAll);
  gDBView.doCommand(Ci.nsMsgViewCommandType.junk);
  await gCommandUpdater.promiseSelectionSummarized;
  await gListener.promiseFolderAdded;
});

add_task(async function test_add_further_message() {
  // Add another message in case the first one moved.
  await messageInjection.makeNewSetsInFolders([gLocalInboxFolder], [{}]);
});

add_task(async function test_second_junking_move_msgs() {
  // Select and junk all messages.
  gDBView.doCommand(Ci.nsMsgViewCommandType.selectAll);
  gDBView.doCommand(Ci.nsMsgViewCommandType.junk);
  await gCommandUpdater.promiseSelectionSummarized;
  await gListener.promiseMsgsMoveCopyCompleted;
});