summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_virtualFolders1.js
blob: 5fe32de2377348678a5c32cd7a4029fa66d7e434 (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
/* 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"
);

add_task(function () {
  MailServices.accounts.createLocalMailAccount();
  let account = MailServices.accounts.accounts[0];
  let rootFolder = account.incomingServer.rootFolder;

  // Create some real folders to test.
  rootFolder.createSubfolder("test A", null);
  let testFolderA = rootFolder.getChildNamed("test A");
  rootFolder.createSubfolder("test B", null);
  let testFolderB = rootFolder.getChildNamed("test B");
  rootFolder.createSubfolder("test C", null);
  let testFolderC = rootFolder.getChildNamed("test C");

  // Create a virtual folder with no search folders.
  let wrappedFolderZ = VirtualFolderHelper.createNewVirtualFolder(
    "virtual Z",
    rootFolder,
    [],
    "ANY",
    false
  );
  Assert.equal(
    wrappedFolderZ.virtualFolder,
    rootFolder.getChildNamed("virtual Z")
  );
  Assert.equal(wrappedFolderZ.searchFolderURIs, "");
  Assert.deepEqual(wrappedFolderZ.searchFolders, []);

  // Create a virtual folder with one search folder.
  let wrappedFolderY = VirtualFolderHelper.createNewVirtualFolder(
    "virtual Y",
    rootFolder,
    [testFolderA],
    "ANY",
    false
  );
  Assert.equal(
    wrappedFolderY.virtualFolder,
    rootFolder.getChildNamed("virtual Y")
  );
  Assert.equal(wrappedFolderY.searchFolderURIs, testFolderA.URI);
  Assert.deepEqual(wrappedFolderY.searchFolders, [testFolderA]);

  // Create a virtual folder with two search folders.
  let wrappedFolderX = VirtualFolderHelper.createNewVirtualFolder(
    "virtual X",
    rootFolder,
    [testFolderB, testFolderC],
    "ANY",
    false
  );
  Assert.equal(
    wrappedFolderX.virtualFolder,
    rootFolder.getChildNamed("virtual X")
  );
  Assert.equal(
    wrappedFolderX.searchFolderURIs,
    `${testFolderB.URI}|${testFolderC.URI}`
  );
  Assert.deepEqual(wrappedFolderX.searchFolders, [testFolderB, testFolderC]);

  // Add a subfolder to real folder B. Check it is added to virtual folder X.
  testFolderB.createSubfolder("test BB", null);
  let testFolderBB = testFolderB.getChildNamed("test BB");
  Assert.equal(
    wrappedFolderZ.searchFolderURIs,
    "",
    "virtual folder Z should not change"
  );
  Assert.deepEqual(
    wrappedFolderZ.searchFolders,
    [],
    "virtual folder Z should not change"
  );
  Assert.equal(
    wrappedFolderY.searchFolderURIs,
    testFolderA.URI,
    "virtual folder Y should not change"
  );
  Assert.deepEqual(
    wrappedFolderY.searchFolders,
    [testFolderA],
    "virtual folder Y should not change"
  );
  Assert.equal(
    wrappedFolderX.searchFolderURIs,
    `${testFolderB.URI}|${testFolderC.URI}|${testFolderBB.URI}`
  );
  Assert.deepEqual(wrappedFolderX.searchFolders, [
    testFolderB,
    testFolderBB,
    testFolderC,
  ]);

  // Add a subfolder to real folder BB. Check it is added to virtual folder X.
  testFolderBB.createSubfolder("test BBB", null);
  let testFolderBBB = testFolderBB.getChildNamed("test BBB");
  Assert.equal(
    wrappedFolderZ.searchFolderURIs,
    "",
    "virtual folder Z should not change"
  );
  Assert.deepEqual(
    wrappedFolderZ.searchFolders,
    [],
    "virtual folder Z should not change"
  );
  Assert.equal(
    wrappedFolderY.searchFolderURIs,
    testFolderA.URI,
    "virtual folder Y should not change"
  );
  Assert.deepEqual(
    wrappedFolderY.searchFolders,
    [testFolderA],
    "virtual folder Y should not change"
  );
  Assert.equal(
    wrappedFolderX.searchFolderURIs,
    `${testFolderB.URI}|${testFolderC.URI}|${testFolderBB.URI}|${testFolderBBB.URI}`
  );
  Assert.deepEqual(wrappedFolderX.searchFolders, [
    testFolderB,
    testFolderBB,
    testFolderBBB,
    testFolderC,
  ]);

  // Remove BB from virtual folder X.
  wrappedFolderX.searchFolders = [testFolderB, testFolderBBB, testFolderC];
  Assert.equal(
    wrappedFolderZ.searchFolderURIs,
    "",
    "virtual folder Z should not change"
  );
  Assert.deepEqual(
    wrappedFolderZ.searchFolders,
    [],
    "virtual folder Z should not change"
  );
  Assert.equal(
    wrappedFolderY.searchFolderURIs,
    testFolderA.URI,
    "virtual folder Y should not change"
  );
  Assert.deepEqual(
    wrappedFolderY.searchFolders,
    [testFolderA],
    "virtual folder Y should not change"
  );
  Assert.equal(
    wrappedFolderX.searchFolderURIs,
    `${testFolderB.URI}|${testFolderBBB.URI}|${testFolderC.URI}`
  );
  Assert.deepEqual(wrappedFolderX.searchFolders, [
    testFolderB,
    testFolderBBB,
    testFolderC,
  ]);

  // Add a second subfolder to the removed folder. Check it is not added to
  // virtual folder X, because the parent folder is not in X.
  testFolderBB.createSubfolder("test BBB two", null);
  Assert.equal(
    wrappedFolderZ.searchFolderURIs,
    "",
    "virtual folder Z should not change"
  );
  Assert.deepEqual(
    wrappedFolderZ.searchFolders,
    [],
    "virtual folder Z should not change"
  );
  Assert.equal(
    wrappedFolderY.searchFolderURIs,
    testFolderA.URI,
    "virtual folder Y should not change"
  );
  Assert.deepEqual(
    wrappedFolderY.searchFolders,
    [testFolderA],
    "virtual folder Y should not change"
  );
  Assert.equal(
    wrappedFolderX.searchFolderURIs,
    `${testFolderB.URI}|${testFolderBBB.URI}|${testFolderC.URI}`
  );
  Assert.deepEqual(wrappedFolderX.searchFolders, [
    testFolderB,
    testFolderBBB,
    testFolderC,
  ]);
});