summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/local/test/unit/test_pop3MultiCopy2.js
blob: 007a0fd99badb5ac9c05bf75a215ed77dabb2d84 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This tests that moved multiple messages from maildir->mbox and
   mbox->maildir are correct.
 */

/* import-globals-from ../../../test/resources/POP3pump.js */
load("../../../resources/POP3pump.js");
const { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

Services.prefs.setCharPref(
  "mail.serverDefaultStoreContractID",
  "@mozilla.org/msgstore/maildirstore;1"
);

var gInboxFolder, gTestFolder;

gPOP3Pump.files = ["../../../data/bugmail1", "../../../data/draft1"];
var gTestSubjects = [
  "[Bug 397009] A filter will let me tag, but not untag",
  "Hello, did you receive my bugmail?",
];

add_setup(async function () {
  let storeID = "@mozilla.org/msgstore/maildirstore;1";
  resetPluggableStoreLocal(storeID);

  // We want to test cross-server copy, so don't defer.
  gPOP3Pump.fakeServer.deferredToAccount = "";

  // Create a test folder on the Local Folders account.
  gTestFolder = localAccountUtils.rootFolder
    .QueryInterface(Ci.nsIMsgLocalMailFolder)
    .createLocalSubfolder("test");
  dump("testFolder is at " + gTestFolder.filePath.path + "\n");
  await gPOP3Pump.run();
});

add_task(async function maildirToMbox() {
  // Test for multiple message copy for maildir->mbox.

  // get message headers for the inbox folder
  gInboxFolder = gPOP3Pump.fakeServer.rootMsgFolder.getFolderWithFlags(
    Ci.nsMsgFolderFlags.Inbox
  );
  dump("inbox is at " + gInboxFolder.filePath.path + "\n");

  // Accumulate messages to copy.
  let messages = [];
  for (let hdr of gInboxFolder.msgDatabase.enumerateMessages()) {
    messages.push(hdr);
  }
  Assert.equal(messages.length, 2);

  // Move messages to mbox test folder.
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyMessages(
    gInboxFolder,
    messages,
    gTestFolder,
    true, // isMove
    promiseCopyListener,
    null, // window
    false // allowUndo
  );
  await promiseCopyListener.promise;

  // Check the destination headers.
  messages = [];
  let subjects = [];
  for (let hdr of gTestFolder.msgDatabase.enumerateMessages()) {
    messages.push(hdr);
    dump("Subject: " + hdr.subject + "\n");
    subjects.push(hdr.subject);
  }
  Assert.equal(messages.length, 2);

  // messages should be missing from source
  Assert.equal(gInboxFolder.getTotalMessages(false), 0);

  // Check for subjects. maildir order for messages may not match
  // order for creation, hence the array.includes.
  for (let subject of gTestSubjects) {
    Assert.ok(subjects.includes(subject));
  }

  // Make sure the body matches the message.
  for (let hdr of gTestFolder.msgDatabase.enumerateMessages()) {
    let body = mailTestUtils.loadMessageToString(gTestFolder, hdr);
    Assert.ok(body.includes(hdr.subject));
  }
});

add_task(async function mboxToMaildir() {
  // Test for multiple message copy for mbox->maildir.

  // Accumulate messages to copy.
  let messages = [];
  for (let hdr of gTestFolder.msgDatabase.enumerateMessages()) {
    messages.push(hdr);
  }
  Assert.equal(messages.length, 2);

  // Move messages to inbox folder.
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyMessages(
    gTestFolder,
    messages,
    gInboxFolder,
    true,
    promiseCopyListener,
    null,
    false
  );
  await promiseCopyListener.promise;

  // Check the destination headers.
  messages = [];
  let subjects = [];
  for (let hdr of gInboxFolder.msgDatabase.enumerateMessages()) {
    messages.push(hdr);
    dump("Subject: " + hdr.subject + "\n");
    subjects.push(hdr.subject);
  }
  Assert.equal(messages.length, 2);

  // messages should be missing from source
  Assert.equal(gTestFolder.getTotalMessages(false), 0);

  // Check for subjects. maildir order for messages may not match
  // order for creation, hence the array.includes.
  for (let subject of gTestSubjects) {
    Assert.ok(subjects.includes(subject));
  }

  // Make sure the body matches the message.
  for (let hdr of gInboxFolder.msgDatabase.enumerateMessages()) {
    let body = mailTestUtils.loadMessageToString(gInboxFolder, hdr);
    Assert.ok(body.includes(hdr.subject));
  }
});

add_task(function testCleanup() {
  gPOP3Pump = null;
});

// Clone of POP3pump resetPluggableStore that does not reset local folders.
function resetPluggableStoreLocal(aStoreContractID) {
  Services.prefs.setCharPref(
    "mail.serverDefaultStoreContractID",
    aStoreContractID
  );

  // Cleanup existing files, server and account instances, if any.
  if (gPOP3Pump._server) {
    gPOP3Pump._server.stop();
  }

  if (gPOP3Pump.fakeServer && gPOP3Pump.fakeServer.valid) {
    gPOP3Pump.fakeServer.closeCachedConnections();
    MailServices.accounts.removeIncomingServer(gPOP3Pump.fakeServer, false);
  }

  gPOP3Pump.fakeServer = localAccountUtils.create_incoming_server(
    "pop3",
    gPOP3Pump.kPOP3_PORT,
    "fred",
    "wilma"
  );

  // localAccountUtils.clearAll();

  gPOP3Pump._incomingServer = gPOP3Pump.fakeServer;
  gPOP3Pump._mailboxStoreContractID = aStoreContractID;
}