summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_virtualFolders3.js
blob: e3d1b9c73c294dd8cce9f5c69dfe413fd925fae2 (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
/* 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 subfolders added to searched folders are also searched.
 */

const { VirtualFolderHelper } = ChromeUtils.import(
  "resource:///modules/VirtualFolderWrapper.jsm"
);

let rootFolder;

add_setup(function () {
  MailServices.accounts.createLocalMailAccount();
  let account = MailServices.accounts.accounts[0];
  rootFolder = account.incomingServer.rootFolder;
  rootFolder.QueryInterface(Ci.nsIMsgLocalMailFolder);

  // Listen to folder events for debugging purposes.

  MailServices.mailSession.AddFolderListener(
    {
      QueryInterface: ChromeUtils.generateQI(["nsIFolderListener"]),
      onFolderAdded(parentFolder, childFolder) {
        console.log(
          `onFolderAdded: ${
            childFolder?.URI
          } (flags: ${childFolder.flags.toString(16)}) added to ${
            parentFolder?.URI
          }`
        );
      },
      onMessageAdded(parentFolder, msg) {},
      onFolderRemoved(parentFolder, childFolder) {
        console.log(
          `onFolderRemoved: ${
            childFolder?.URI
          } (flags: ${childFolder.flags.toString(16)}) removed from ${
            parentFolder?.URI
          }`
        );
      },
      onMessageRemoved(parentFolder, msg) {},
      onFolderPropertyChanged(folder, property, oldValue, newValue) {},
      onFolderIntPropertyChanged(folder, property, oldValue, newValue) {
        if (property == "FolderFlag") {
          console.log(
            `onFolderIntPropertyChanged: ${
              folder.URI
            } changed flags from ${oldValue.toString(
              16
            )} to ${newValue.toString(16)}`
          );
        }
      },
      onFolderBoolPropertyChanged(folder, property, oldValue, newValue) {},
      onFolderUnicharPropertyChanged(folder, property, oldValue, newValue) {},
      onFolderPropertyFlagChanged(folder, property, oldFlag, newFlag) {},
      onFolderEvent(folder, event) {},
    },
    Ci.nsIFolderListener.all
  );
});

// Test each of the folder types.
add_task(function testInbox() {
  subtest("Inbox");
});
add_task(function testDrafts() {
  subtest("Drafts");
});
add_task(function testTemplates() {
  subtest("Templates");
});
add_task(function testSentMail() {
  subtest("SentMail");
});
add_task(function testArchive() {
  subtest("Archive");
});
add_task(function testJunk() {
  subtest("Junk");
});
add_task(function testTrash() {
  subtest("Trash");
});

function subtest(flag) {
  // Create a virtual folder. This is very similar to the code in about3Pane.js.

  let virtualFolder = rootFolder.createLocalSubfolder(`virtual${flag}`);
  virtualFolder.flags |=
    Ci.nsMsgFolderFlags.Virtual | Ci.nsMsgFolderFlags[flag];

  let msgDatabase = virtualFolder.msgDatabase;
  let folderInfo = msgDatabase.dBFolderInfo;

  folderInfo.setCharProperty("searchStr", "ALL");
  folderInfo.setUint32Property("searchFolderFlag", Ci.nsMsgFolderFlags[flag]);
  folderInfo.setBooleanProperty("searchOnline", true);
  msgDatabase.summaryValid = true;
  msgDatabase.close(true);

  function checkVirtualFolder(searchFolders, message) {
    let wrappedVirtualFolder =
      VirtualFolderHelper.wrapVirtualFolder(virtualFolder);
    Assert.deepEqual(
      wrappedVirtualFolder.searchFolderURIs.split("|").filter(Boolean).sort(),
      searchFolders.map(f => f.URI).sort(),
      message
    );
    Assert.deepEqual(
      wrappedVirtualFolder.searchFolders,
      searchFolders,
      message
    );
  }

  rootFolder.notifyFolderAdded(virtualFolder);
  checkVirtualFolder([], "new virtual folder should have no search folders");

  // Create a disconnected folder with some descendants, add the flag and then
  // add it to the parent. The folder and descendants should all be added to
  // the virtual folder.

  let parent = MailServices.folderLookup.getOrCreateFolderForURL(
    `${rootFolder.URI}/parent${flag}`
  );
  parent.setFlag(Ci.nsMsgFolderFlags[flag]);
  let child = MailServices.folderLookup.getOrCreateFolderForURL(
    `${rootFolder.URI}/parent${flag}/child`
  );
  parent.addSubfolder(child.name);
  let grandchild = MailServices.folderLookup.getOrCreateFolderForURL(
    `${rootFolder.URI}/parent${flag}/child/grandchild`
  );
  child.addSubfolder(grandchild.name);

  rootFolder.addSubfolder(parent.name);
  rootFolder.notifyFolderAdded(parent);
  parent.notifyFolderAdded(child);
  child.notifyFolderAdded(grandchild);

  checkVirtualFolder(
    [parent, child, grandchild],
    "added folder and descendants should be added to the virtual folder"
  );

  // Create a subfolder of a real folder with some descendants, then set the
  // flag. The folder and descendants should all be added to the virtual folder.

  let more = rootFolder.createLocalSubfolder(`more${flag}`);
  more.QueryInterface(Ci.nsIMsgLocalMailFolder);
  let evenMore = more.createLocalSubfolder("even more");
  evenMore.QueryInterface(Ci.nsIMsgLocalMailFolder);
  let yetMore = evenMore.createLocalSubfolder("yet more");
  more.setFlag(Ci.nsMsgFolderFlags[flag]);

  checkVirtualFolder(
    [more, evenMore, yetMore, parent, child, grandchild],
    "folder with changed flag and descendants should be added to the virtual folder"
  );

  // Test what happens if a folder of one type is not added to a parent in the
  // virtual folder of another type. This should really only matter for inboxes
  // containing other types of folders, which happens in some configurations.
  // Other combinations shouldn't really exist, but let's test them anyway.

  if (!["SentMail", "Archive"].includes(flag)) {
    for (let otherFlag of [
      "Inbox",
      "Drafts",
      "Templates",
      "SentMail",
      "Archive",
      "Junk",
      "Trash",
    ]) {
      if (otherFlag == flag) {
        continue;
      }
      let otherFlagChild = MailServices.folderLookup.getOrCreateFolderForURL(
        `${rootFolder.URI}/parent${flag}/other${otherFlag}Child`
      );
      otherFlagChild.setFlag(Ci.nsMsgFolderFlags[otherFlag]);
      parent.addSubfolder(otherFlagChild.name);
      parent.notifyFolderAdded(otherFlagChild);

      if (flag == "Trash") {
        checkVirtualFolder(
          [more, evenMore, yetMore, parent, child, grandchild, otherFlagChild],
          `folder with ${otherFlag} flag should be added to the virtual ${flag} folder`
        );
      } else {
        checkVirtualFolder(
          [more, evenMore, yetMore, parent, child, grandchild],
          `folder with ${otherFlag} flag should not be added to the virtual ${flag} folder`
        );
      }

      parent.propagateDelete(otherFlagChild, false);
    }
  }

  // Now reverse the additions.

  rootFolder.propagateDelete(parent, false);
  checkVirtualFolder(
    [more, evenMore, yetMore],
    "deleted folder and descendants should be removed from the virtual folder"
  );

  if (flag != "Inbox") {
    // Clearing the inbox flag from a folder that has it throws an assertion.
    more.clearFlag(Ci.nsMsgFolderFlags[flag]);
    checkVirtualFolder(
      [],
      "folder with changed flag and descendants should be removed from the virtual folder"
    );

    Assert.equal(
      virtualFolder.parent,
      rootFolder,
      "smart virtual folder should NOT be removed with last search folder"
    );
  }
}