summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/unit/test_localToImapFilter.js
blob: b0a28aedda3fe5fcd8896959672f5ed16c8cefe6 (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
/* 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/. */

/*
 * This file tests copies of multiple messages using filters
 * from incoming POP3, with filter actions copying and moving
 * messages to IMAP folders. This test is adapted from
 * test_imapFolderCopy.js
 *
 * Original author: Kent James <kent@caspia.com>
 */

/**
 * NOTE:
 * There's a problem with this test in chaos mode (mach xpcshell-test --verify)
 * with the filter applying.
 * It's either a problem with the POP3Pump implementation (testing infrastructure failure)
 * or a problem with the copy filter.
 */

/* import-globals-from ../../../test/resources/POP3pump.js */
load("../../../resources/POP3pump.js");

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

var gEmptyLocal1, gEmptyLocal2;
var gFiles = ["../../../data/bugmail1", "../../../data/draft1"];

add_setup(async function () {
  setupIMAPPump();
  let emptyFolder1Listener = PromiseTestUtils.promiseFolderAdded("empty 1");
  gEmptyLocal1 = localAccountUtils.rootFolder.createLocalSubfolder("empty 1");
  await emptyFolder1Listener;
  let emptyFolder2Listener = PromiseTestUtils.promiseFolderAdded("empty 2");
  gEmptyLocal2 = localAccountUtils.rootFolder.createLocalSubfolder("empty 2");
  await emptyFolder2Listener;

  // These hacks are required because we've created the inbox before
  // running initial folder discovery, and adding the folder bails
  // out before we set it as verified online, so we bail out, and
  // then remove the INBOX folder since it's not verified.
  IMAPPump.inbox.hierarchyDelimiter = "/";
  IMAPPump.inbox.verifiedAsOnlineFolder = true;
});

add_task(async function copyFolder1() {
  let copyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyFolder(
    gEmptyLocal1,
    IMAPPump.inbox,
    false,
    copyListener,
    null
  );
  await copyListener.promise;
});

add_task(async function updateTrash() {
  let trashFolder = IMAPPump.incomingServer.rootFolder
    .getChildNamed("Trash")
    .QueryInterface(Ci.nsIMsgImapMailFolder);
  let listener = new PromiseTestUtils.PromiseUrlListener();
  // hack to force uid validity to get initialized for trash.
  trashFolder.updateFolderWithListener(null, listener);
  await listener.promise;
});

add_task(async function copyFolder2() {
  let copyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyFolder(
    gEmptyLocal2,
    IMAPPump.inbox,
    false,
    copyListener,
    null
  );
  await copyListener.promise;
});

add_task(async function getLocalMessages() {
  // setup copy then move mail filters on the inbox
  let filterList = gPOP3Pump.fakeServer.getFilterList(null);
  let filter = filterList.createFilter("copyThenMoveAll");
  let searchTerm = filter.createTerm();
  searchTerm.matchAll = true;
  filter.appendTerm(searchTerm);
  let copyAction = filter.createAction();
  copyAction.type = Ci.nsMsgFilterAction.CopyToFolder;
  copyAction.targetFolderUri = IMAPPump.inbox.getChildNamed("empty 1").URI;
  filter.appendAction(copyAction);
  let moveAction = filter.createAction();
  moveAction.type = Ci.nsMsgFilterAction.MoveToFolder;
  moveAction.targetFolderUri = IMAPPump.inbox.getChildNamed("empty 2").URI;
  filter.appendAction(moveAction);
  filter.enabled = true;
  filterList.insertFilterAt(0, filter);
  let resolveOnDone;
  let promiseOnDone = new Promise(resolve => {
    resolveOnDone = resolve;
  });
  gPOP3Pump.files = gFiles;
  gPOP3Pump.onDone = resolveOnDone;
  gPOP3Pump.run();

  await promiseOnDone;
});

add_task(async function test_update1_copyFilter() {
  let listener = new PromiseTestUtils.PromiseUrlListener();
  let folder1 = IMAPPump.inbox
    .getChildNamed("empty 1")
    .QueryInterface(Ci.nsIMsgImapMailFolder);
  folder1.updateFolderWithListener(null, listener);
  await listener.promise;
  Assert.ok(folder1 !== null);
  Assert.equal(
    folderCount(folder1),
    2,
    "the two filtered messages should be in empty 1"
  );
});

add_task(async function test_update2_moveFilter() {
  let listener = new PromiseTestUtils.PromiseUrlListener();
  let folder2 = IMAPPump.inbox
    .getChildNamed("empty 2")
    .QueryInterface(Ci.nsIMsgImapMailFolder);
  folder2.updateFolderWithListener(null, listener);
  await listener.promise;
  Assert.ok(folder2 !== null);
  Assert.equal(
    folderCount(folder2),
    2,
    "the two filtered messages should be in empty 2"
  );
});

add_task(async function verifyLocalFolder() {
  // the local inbox folder should now be empty, since the second
  // operation was a move
  Assert.equal(folderCount(localAccountUtils.inboxFolder), 0);
});

add_task(function endTest() {
  gEmptyLocal1 = null;
  gEmptyLocal2 = null;
  gPOP3Pump = null;
  teardownIMAPPump();
});

function folderCount(folder) {
  return [...folder.msgDatabase.enumerateMessages()].length;
}