summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_fccReply.js
blob: b7e44bce172a98d4eafdd3f74da358d8d1250182 (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
/* 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 when nsIMsgIdentity.fccReplyFollowsParent is true, the reply mail
 * is copied to the same folder as the original mail.
 */

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

var gServer;

/**
 * Send a reply to originalMsgURI.
 */
async function sendReply(identity, fields, originalMsgURI, compType) {
  let params = Cc[
    "@mozilla.org/messengercompose/composeparams;1"
  ].createInstance(Ci.nsIMsgComposeParams);
  params.composeFields = fields;
  params.originalMsgURI = originalMsgURI;
  let msgCompose = MailServices.compose.initCompose(params);
  msgCompose.type = compType;
  let progress = Cc["@mozilla.org/messenger/progress;1"].createInstance(
    Ci.nsIMsgProgress
  );
  let promise = new Promise((resolve, reject) => {
    progressListener.resolve = resolve;
    progressListener.reject = reject;
  });
  progress.registerListener(progressListener);
  msgCompose.sendMsg(
    Ci.nsIMsgSend.nsMsgDeliverNow,
    identity,
    "",
    null,
    progress
  );
  return promise;
}

/**
 * Load local mail account and start fake SMTP server.
 */
add_setup(function () {
  localAccountUtils.loadLocalMailAccount();
  gServer = setupServerDaemon();
  gServer.start();
  registerCleanupFunction(() => {
    gServer.stop();
  });
});

/**
 * With fccReplyFollowsParent enabled, send a few replies then check the replies
 * exists in the Inbox folder.
 */
add_task(async function testFccReply() {
  // Turn on fccReplyFollowsParent.
  let identity = getSmtpIdentity(
    "from@tinderbox.invalid",
    getBasicSmtpServer(gServer.port)
  );
  identity.fccReplyFollowsParent = true;

  // Copy a test mail into the Inbox.
  let file = do_get_file("data/message1.eml"); // mail to reply to
  let promiseCopyListener = new PromiseTestUtils.PromiseCopyListener();
  MailServices.copy.copyFileMessage(
    file,
    localAccountUtils.inboxFolder,
    null,
    false,
    0,
    "",
    promiseCopyListener,
    null
  );
  await promiseCopyListener.promise;

  let CompFields = CC(
    "@mozilla.org/messengercompose/composefields;1",
    Ci.nsIMsgCompFields
  );
  let msgHdr = mailTestUtils.firstMsgHdr(localAccountUtils.inboxFolder);
  let originalMsgURI = msgHdr.folder.getUriForMsg(msgHdr);

  // Test nsIMsgCompFields.Reply.
  let fields = new CompFields();
  fields.to = "Nobody <nobody@tinderbox.invalid>";
  fields.subject = "Test fcc reply";
  await sendReply(identity, fields, originalMsgURI, Ci.nsIMsgCompType.Reply);
  await TestUtils.waitForCondition(() => gServer._daemon.post);
  let msgData = mailTestUtils.loadMessageToString(
    localAccountUtils.inboxFolder,
    mailTestUtils.getMsgHdrN(localAccountUtils.inboxFolder, 1)
  );
  Assert.ok(msgData.includes("Subject: Test fcc reply"));

  // Test nsIMsgCompFields.ReplyToGroup.
  gServer.resetTest();
  fields.subject = "Test fccReplyToGroup";
  await sendReply(
    identity,
    fields,
    originalMsgURI,
    Ci.nsIMsgCompType.ReplyToGroup
  );
  await TestUtils.waitForCondition(() => gServer._daemon.post);
  msgData = mailTestUtils.loadMessageToString(
    localAccountUtils.inboxFolder,
    mailTestUtils.getMsgHdrN(localAccountUtils.inboxFolder, 2)
  );
  Assert.ok(msgData.includes("Subject: Test fccReplyToGroup"));

  // Test nsIMsgCompFields.ReplyToList.
  gServer.resetTest();
  fields.subject = "Test fccReplyToList";
  await sendReply(
    identity,
    fields,
    originalMsgURI,
    Ci.nsIMsgCompType.ReplyToList
  );
  await TestUtils.waitForCondition(() => gServer._daemon.post);
  msgData = mailTestUtils.loadMessageToString(
    localAccountUtils.inboxFolder,
    mailTestUtils.getMsgHdrN(localAccountUtils.inboxFolder, 3)
  );
  Assert.ok(msgData.includes("Subject: Test fccReplyToList"));
});