summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_closedDB.js
blob: fd752bf81bb2b0b380a71b21d7ebf8a3fd720a85 (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
/* 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/. */

/**
 * Tests that an nsMsgDBFolder's database connection and listeners are
 * restored when called by certain functions.
 */

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

add_task(async function () {
  // Create a folder and some messages.

  let generator = new MessageGenerator();

  MailServices.accounts.createLocalMailAccount();
  let account = MailServices.accounts.accounts[0];
  account.addIdentity(MailServices.accounts.createIdentity());

  let rootFolder = account.incomingServer.rootFolder;
  rootFolder.QueryInterface(Ci.nsIMsgLocalMailFolder);

  let testFolder = rootFolder.createLocalSubfolder("testFolder");
  testFolder.QueryInterface(Ci.nsIMsgLocalMailFolder);
  testFolder.addMessageBatch(
    generator.makeMessages({ count: 5 }).map(message => message.toMboxString())
  );
  let testMessages = [...testFolder.messages];

  // Listen for notifications.

  let folderListener = {
    QueryInterface: ChromeUtils.generateQI(["nsIFolderListener"]),
    notifications: [],
    onFolderIntPropertyChanged(folder, property, oldValue, newValue) {
      if (property != "TotalUnreadMessages") {
        return;
      }

      this.notifications.push({ folder, property, oldValue, newValue });
    },
    consumeNotification(expectedFolder, expectedOldValue, expectedNewValue) {
      let { folder, oldValue, newValue } = this.notifications.shift();
      Assert.equal(folder, expectedFolder, "notification folder");
      Assert.equal(oldValue, expectedOldValue, "notification oldValue");
      Assert.equal(newValue, expectedNewValue, "notification newValue");
    },
  };
  MailServices.mailSession.AddFolderListener(
    folderListener,
    Ci.nsIFolderListener.intPropertyChanged
  );

  // Clear the database reference, then mark some messages as read. We should
  // see the unread count change and get two notifications about it. We could
  // check `testFolder.msgDatabase` is not null afterwards, but that would be
  // pointless, because the getter restores the database.

  testFolder.msgDatabase = null;
  testFolder.markMessagesRead([testMessages[0], testMessages[4]], true);
  Assert.equal(
    testFolder.getNumUnread(false),
    3,
    "unread message count should be updated"
  );
  Assert.equal(
    folderListener.notifications.length,
    2,
    "two folder notifications should have fired"
  );
  folderListener.consumeNotification(testFolder, 5, 4);
  folderListener.consumeNotification(testFolder, 4, 3);

  // Clear the database reference, then mark some messages as flagged. This
  // doesn't prove much except that nothing exploded.

  testFolder.msgDatabase = null;
  testFolder.markMessagesFlagged([testMessages[1], testMessages[3]], true);

  // Clear the database reference, then mark all messages as read. We should
  // see the unread count change to zero and get a notification about it.

  testFolder.msgDatabase = null;
  testFolder.markAllMessagesRead(null);
  Assert.equal(
    testFolder.getNumUnread(false),
    0,
    "unread message count should be updated"
  );
  Assert.equal(
    folderListener.notifications.length,
    1,
    "a folder notifications should have fired"
  );
  folderListener.consumeNotification(testFolder, 3, 0);

  // Clean up.

  MailServices.mailSession.RemoveFolderListener(folderListener);
  MailServices.accounts.removeAccount(account, false);
});