summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/unit/test_customCommandReturnsFetchResponse.js
blob: abf45ab3e396ca42070e08a0e1d5c477007eb9de (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
/* 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 to ensure that imap customCommandResult function works properly
 * Bug 778246
 */

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

// IMAP pump

// Globals

// Messages to load must have CRLF line endings, that is Windows style
var gMessageFileName = "bugmail10"; // message file used as the test message
var gMessage, gExpectedLength;

var gCustomList = ["Custom1", "Custom2", "Custom3"];

var gMsgWindow = Cc["@mozilla.org/messenger/msgwindow;1"].createInstance(
  Ci.nsIMsgWindow
);

setupIMAPPump("CUSTOM1");

add_setup(async function () {
  Services.prefs.setBoolPref(
    "mail.server.server1.autosync_offline_stores",
    false
  );
  // Load and update a message in the imap fake server.

  gMessage = new ImapMessage(
    specForFileName(gMessageFileName),
    IMAPPump.mailbox.uidnext++,
    []
  );
  gMessage.xCustomList = [];
  IMAPPump.mailbox.addMessage(gMessage);
  let listener = new PromiseTestUtils.PromiseUrlListener();
  IMAPPump.inbox.updateFolderWithListener(null, listener);
  await listener.promise;
});

add_task(async function testStoreCustomList() {
  let msgHdr = mailTestUtils.firstMsgHdr(IMAPPump.inbox);
  gExpectedLength = gCustomList.length;
  let uri = IMAPPump.inbox.issueCommandOnMsgs(
    "STORE",
    msgHdr.messageKey + " X-CUSTOM-LIST (" + gCustomList.join(" ") + ")",
    gMsgWindow
  );
  uri.QueryInterface(Ci.nsIMsgMailNewsUrl);
  // Listens for response from customCommandResult request for X-CUSTOM-LIST.
  let storeCustomListSetListener = new PromiseTestUtils.PromiseUrlListener({
    OnStopRunningUrl(aUrl, aExitCode) {
      aUrl.QueryInterface(Ci.nsIImapUrl);
      Assert.equal(
        aUrl.customCommandResult,
        "(" + gMessage.xCustomList.join(" ") + ")"
      );
      Assert.equal(gMessage.xCustomList.length, gExpectedLength);
    },
  });
  uri.RegisterListener(storeCustomListSetListener);
  await storeCustomListSetListener.promise;
});

add_task(async function testStoreMinusCustomList() {
  let msgHdr = mailTestUtils.firstMsgHdr(IMAPPump.inbox);
  gExpectedLength--;
  let uri = IMAPPump.inbox.issueCommandOnMsgs(
    "STORE",
    msgHdr.messageKey + " -X-CUSTOM-LIST (" + gCustomList[0] + ")",
    gMsgWindow
  );
  uri.QueryInterface(Ci.nsIMsgMailNewsUrl);
  // Listens for response from customCommandResult request for X-CUSTOM-LIST.
  let storeCustomListRemovedListener = new PromiseTestUtils.PromiseUrlListener({
    OnStopRunningUrl(aUrl, aExitCode) {
      aUrl.QueryInterface(Ci.nsIImapUrl);
      Assert.equal(
        aUrl.customCommandResult,
        "(" + gMessage.xCustomList.join(" ") + ")"
      );
      Assert.equal(gMessage.xCustomList.length, gExpectedLength);
    },
  });
  uri.RegisterListener(storeCustomListRemovedListener);
  await storeCustomListRemovedListener.promise;
});

add_task(async function testStorePlusCustomList() {
  let msgHdr = mailTestUtils.firstMsgHdr(IMAPPump.inbox);
  gExpectedLength++;
  let uri = IMAPPump.inbox.issueCommandOnMsgs(
    "STORE",
    msgHdr.messageKey + ' +X-CUSTOM-LIST ("Custom4")',
    gMsgWindow
  );
  uri.QueryInterface(Ci.nsIMsgMailNewsUrl);
  let storeCustomListAddedListener = new PromiseTestUtils.PromiseUrlListener({
    OnStopRunningUrl(aUrl, aExitCode) {
      aUrl.QueryInterface(Ci.nsIImapUrl);
      Assert.equal(
        aUrl.customCommandResult,
        "(" + gMessage.xCustomList.join(" ") + ")"
      );
      Assert.equal(gMessage.xCustomList.length, gExpectedLength);
    },
  });
  uri.RegisterListener(storeCustomListAddedListener);
  await storeCustomListAddedListener.promise;
});

// Cleanup at end
add_task(function endTest() {
  teardownIMAPPump();
});

/*
 * helper functions
 */

// given a test file, return the file uri spec
function specForFileName(aFileName) {
  let file = do_get_file("../../../data/" + aFileName);
  let msgfileuri = Services.io.newFileURI(file).QueryInterface(Ci.nsIFileURL);
  return msgfileuri.spec;
}