summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/unit/test_imapAutoSync.js
blob: 1f49a408cc40b95f2ace54aac7942d90db447d5a (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* 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 tests various features of imap autosync
// N.B. We need to beware of MessageInjection, since it turns off
// imap autosync.

// Our general approach is to attach an nsIAutoSyncMgrListener to the
// autoSyncManager, and listen for the expected events. We simulate idle
// by directly poking the nsIAutoSyncManager QI'd to nsIObserver with app
// idle events. If we really go idle, duplicate idle events are ignored.

// We test that checking non-inbox folders for new messages isn't
// interfering with autoSync's detection of new messages.

// We also test that folders that have messages added to them via move/copy
// get put in the front of the queue.

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

var msgFlagOffline = Ci.nsMsgMessageFlags.Offline;
var nsIAutoSyncMgrListener = Ci.nsIAutoSyncMgrListener;

var gAutoSyncManager = Cc["@mozilla.org/imap/autosyncmgr;1"].getService(
  Ci.nsIAutoSyncManager
);
var gTargetFolder;

add_setup(function () {
  setupIMAPPump();
  addMessageToFolder(IMAPPump.inbox);
});

add_task(async function test_createTargetFolder() {
  gAutoSyncManager.addListener(gAutoSyncListener);

  IMAPPump.incomingServer.rootFolder.createSubfolder("targetFolder", null);
  await PromiseTestUtils.promiseFolderAdded("targetFolder");
  gTargetFolder =
    IMAPPump.incomingServer.rootFolder.getChildNamed("targetFolder");
  Assert.ok(gTargetFolder instanceof Ci.nsIMsgImapMailFolder);
  // set folder to be checked for new messages when inbox is checked.
  gTargetFolder.setFlag(Ci.nsMsgFolderFlags.CheckNew);
});

add_task(function test_checkForNewMessages() {
  addMessageToFolder(gTargetFolder);
  // This will update the INBOX and STATUS targetFolder. We only care about
  // the latter.
  IMAPPump.inbox.getNewMessages(null, null);
  IMAPPump.server.performTest("STATUS");
  // Now we'd like to make autosync update folders it knows about, to
  // get the initial autosync out of the way.
});

add_task(function test_triggerAutoSyncIdle() {
  // wait for both folders to get updated.
  gAutoSyncListener._waitingForDiscoveryList.push(IMAPPump.inbox);
  gAutoSyncListener._waitingForDiscoveryList.push(gTargetFolder);
  gAutoSyncListener._waitingForDiscovery = true;
  let observer = gAutoSyncManager.QueryInterface(Ci.nsIObserver);
  observer.observe(null, "mail-startup-done", "");
  observer.observe(null, "mail:appIdle", "idle");
});

// move the message to a diffent folder
add_task(async function test_moveMessageToTargetFolder() {
  let observer = gAutoSyncManager.QueryInterface(Ci.nsIObserver);
  observer.observe(null, "mail:appIdle", "back");
  let msgHdr = mailTestUtils.firstMsgHdr(IMAPPump.inbox);
  Assert.ok(msgHdr !== null);

  let listener = new PromiseTestUtils.PromiseCopyListener();
  // Now move this message to the target folder.
  MailServices.copy.copyMessages(
    IMAPPump.inbox,
    [msgHdr],
    gTargetFolder,
    true,
    listener,
    null,
    false
  );
  await listener.promise;
});

add_task(async function test_waitForTargetUpdate() {
  // After the copy, now we expect to get notified of the gTargetFolder
  // getting updated, after we simulate going idle.
  gAutoSyncListener._waitingForUpdate = true;
  gAutoSyncListener._waitingForUpdateList.push(gTargetFolder);
  gAutoSyncManager
    .QueryInterface(Ci.nsIObserver)
    .observe(null, "mail:appIdle", "idle");
  await gAutoSyncListener.promiseOnDownloadCompleted;
  await gAutoSyncListener.promiseOnDiscoveryQProcessed;
});

// Cleanup
add_task(function endTest() {
  let numMsgs = 0;
  for (let header of gTargetFolder.messages) {
    numMsgs++;
    Assert.notEqual(header.flags & Ci.nsMsgMessageFlags.Offline, 0);
  }
  Assert.equal(2, numMsgs);
  Assert.equal(gAutoSyncListener._waitingForUpdateList.length, 0);
  Assert.ok(!gAutoSyncListener._waitingForDiscovery);
  Assert.ok(!gAutoSyncListener._waitingForUpdate);
  teardownIMAPPump();
});

function autoSyncListenerPromise() {
  this._inQFolderList = [];
  this._runnning = false;
  this._lastMessage = {};
  this._waitingForUpdateList = [];
  this._waitingForUpdate = false;
  this._waitingForDiscoveryList = [];
  this._waitingForDiscovery = false;

  this._promiseOnDownloadCompleted = new Promise(resolve => {
    this._resolveOnDownloadCompleted = resolve;
  });
  this._promiseOnDiscoveryQProcessed = new Promise(resolve => {
    this._resolveOnDiscoveryQProcessed = resolve;
  });
}
autoSyncListenerPromise.prototype = {
  onStateChanged(running) {
    this._runnning = running;
  },

  onFolderAddedIntoQ(queue, folder) {
    dump("Folder added into Q " + this.qName(queue) + " " + folder.URI + "\n");
  },
  onFolderRemovedFromQ(queue, folder) {
    dump(
      "Folder removed from Q " + this.qName(queue) + " " + folder.URI + "\n"
    );
  },
  onDownloadStarted(folder, numOfMessages, totalPending) {
    dump("Folder download started" + folder.URI + "\n");
  },

  onDownloadCompleted(folder) {
    dump("Folder download completed" + folder.URI + "\n");
    if (folder instanceof Ci.nsIMsgFolder) {
      let index = mailTestUtils.non_strict_index_of(
        this._waitingForUpdateList,
        folder
      );
      if (index != -1) {
        this._waitingForUpdateList.splice(index, 1);
      }
      if (this._waitingForUpdate && this._waitingForUpdateList.length == 0) {
        dump("Got last folder update looking for.\n");
        this._waitingForUpdate = false;
        this._resolveOnDownloadCompleted();
      }
    }
  },

  onDownloadError(folder) {
    if (folder instanceof Ci.nsIMsgFolder) {
      dump("OnDownloadError: " + folder.prettyName + "\n");
    }
  },

  onDiscoveryQProcessed(folder, numOfHdrsProcessed, leftToProcess) {
    dump("onDiscoveryQProcessed: " + folder.prettyName + "\n");
    let index = mailTestUtils.non_strict_index_of(
      this._waitingForDiscoveryList,
      folder
    );
    if (index != -1) {
      this._waitingForDiscoveryList.splice(index, 1);
    }
    if (
      this._waitingForDiscovery &&
      this._waitingForDiscoveryList.length == 0
    ) {
      dump("Got last folder discovery looking for\n");
      this._waitingForDiscovery = false;
      this._resolveOnDiscoveryQProcessed();
    }
  },

  onAutoSyncInitiated(folder) {},
  qName(queueType) {
    if (queueType == Ci.nsIAutoSyncMgrListener.PriorityQueue) {
      return "priorityQ";
    }
    if (queueType == Ci.nsIAutoSyncMgrListener.UpdateQueue) {
      return "updateQ";
    }
    if (queueType == Ci.nsIAutoSyncMgrListener.DiscoveryQueue) {
      return "discoveryQ";
    }
    return "";
  },
  get promiseOnDownloadCompleted() {
    return this._promiseOnDownloadCompleted;
  },
  get promiseOnDiscoveryQProcessed() {
    return this._promiseOnDiscoveryQProcessed;
  },
};
var gAutoSyncListener = new autoSyncListenerPromise();

/*
 * helper functions
 */

// load and update a message in the imap fake server
function addMessageToFolder(folder) {
  let messages = [];
  let gMessageGenerator = new MessageGenerator();
  messages = messages.concat(gMessageGenerator.makeMessage());

  let msgURI = Services.io.newURI(
    "data:text/plain;base64," + btoa(messages[0].toMessageString())
  );
  let ImapMailbox = IMAPPump.daemon.getMailbox(folder.name);
  // We add messages with \Seen flag set so that we won't accidentally
  // trigger the code that updates imap folders that have unread messages moved
  // into them.
  let message = new ImapMessage(msgURI.spec, ImapMailbox.uidnext++, ["\\Seen"]);
  ImapMailbox.addMessage(message);
}