summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/folder-tree-modes/browser_smartFolders.js
blob: 47fdca00585bdcd5001d6819706093ecf04fba7f (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
/* 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 that the smart folder mode works properly.
 */

"use strict";

var {
  archive_selected_messages,
  expand_folder,
  FAKE_SERVER_HOSTNAME,
  get_about_3pane,
  get_smart_folder_named,
  get_special_folder,
  inboxFolder,
  make_message_sets_in_folders,
  select_click_row,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var about3Pane;
var rootFolder;
var inboxSubfolder;
var trashFolder;
var trashSubfolder;

var smartInboxFolder;

var inboxSet;

add_setup(async function () {
  about3Pane = get_about_3pane();
  rootFolder = inboxFolder.server.rootFolder;
  // Create a folder as a subfolder of the inbox
  inboxFolder.createSubfolder("SmartFoldersA", null);
  inboxSubfolder = inboxFolder.getChildNamed("SmartFoldersA");

  trashFolder = inboxFolder.server.rootFolder.getFolderWithFlags(
    Ci.nsMsgFolderFlags.Trash
  );
  trashFolder.createSubfolder("SmartFoldersB", null);
  trashSubfolder = trashFolder.getChildNamed("SmartFoldersB");

  // The message itself doesn't really matter, as long as there's at least one
  // in the folder.
  [inboxSet] = await make_message_sets_in_folders(
    [inboxFolder],
    [{ count: 1 }]
  );
  await make_message_sets_in_folders([inboxSubfolder], [{ count: 1 }]);

  // Switch to the smart folder mode.
  about3Pane.folderPane.activeModes = ["smart"];

  // The smart inbox may not have been created at setup time, so get it now.
  smartInboxFolder = get_smart_folder_named("Inbox");
});

/**
 * Test that smart folders are updated when the folders they should be
 * searching over are added/removed or have the relevant flag set/cleared.
 */
add_task(async function test_folder_flag_changes() {
  expand_folder(smartInboxFolder);
  // Now attempt to select the folder.
  about3Pane.displayFolder(inboxSubfolder);
  // Need to archive two messages in two different accounts in order to
  // create a smart Archives folder.
  select_click_row(0);
  archive_selected_messages();
  let pop3Server = MailServices.accounts.findServer(
    "tinderbox",
    FAKE_SERVER_HOSTNAME,
    "pop3"
  );
  let pop3Inbox = await get_special_folder(
    Ci.nsMsgFolderFlags.Inbox,
    false,
    pop3Server
  );
  await make_message_sets_in_folders([pop3Inbox], [{ count: 1 }]);
  about3Pane.displayFolder(pop3Inbox);
  select_click_row(0);
  archive_selected_messages();

  let smartArchiveFolder = get_smart_folder_named("Archives");
  let archiveScope =
    "|" +
    smartArchiveFolder.msgDatabase.dBFolderInfo.getCharProperty(
      "searchFolderUri"
    ) +
    "|";
  // We should have both this account, and a folder corresponding
  // to this year in the scope.
  rootFolder = inboxFolder.server.rootFolder;
  let archiveFolder = rootFolder.getChildNamed("Archives");
  assert_folder_and_children_in_scope(archiveFolder, archiveScope);
  archiveFolder = pop3Server.rootFolder.getChildNamed("Archives");
  assert_folder_and_children_in_scope(archiveFolder, archiveScope);

  // Remove the archive flag, and make sure the archive folder and
  // its children are no longer in the search scope.
  archiveFolder.clearFlag(Ci.nsMsgFolderFlags.Archive);

  // Refresh the archive scope because clearing the flag should have
  // changed it.
  archiveScope =
    "|" +
    smartArchiveFolder.msgDatabase.dBFolderInfo.getCharProperty(
      "searchFolderUri"
    ) +
    "|";

  // figure out what we expect the archiveScope to now be.
  rootFolder = inboxFolder.server.rootFolder;
  let localArchiveFolder = rootFolder.getChildNamed("Archives");
  let desiredScope = "|" + localArchiveFolder.URI + "|";
  for (let folder of localArchiveFolder.descendants) {
    desiredScope += folder.URI + "|";
  }

  Assert.equal(
    archiveScope,
    desiredScope,
    "archive scope after removing folder"
  );
  assert_folder_and_children_not_in_scope(archiveFolder, archiveScope);
});

function assert_folder_and_children_in_scope(folder, searchScope) {
  let folderURI = "|" + folder.URI + "|";
  assert_uri_found(folderURI, searchScope);
  for (let f of folder.descendants) {
    assert_uri_found(f.URI, searchScope);
  }
}

function assert_folder_and_children_not_in_scope(folder, searchScope) {
  let folderURI = "|" + folder.URI + "|";
  assert_uri_not_found(folderURI, searchScope);
  for (let f of folder.descendants) {
    assert_uri_not_found(f.URI, searchScope);
  }
}

function assert_uri_found(folderURI, scopeList) {
  if (!scopeList.includes(folderURI)) {
    throw new Error("scope " + scopeList + "doesn't contain " + folderURI);
  }
}

function assert_uri_not_found(folderURI, scopeList) {
  if (scopeList.includes(folderURI)) {
    throw new Error(
      "scope " + scopeList + "contains " + folderURI + " but shouldn't"
    );
  }
}

registerCleanupFunction(async function () {
  about3Pane.folderPane.activeModes = ["all"];
  inboxFolder.propagateDelete(inboxSubfolder, true);
  inboxFolder.deleteMessages(
    [...inboxFolder.messages],
    top.msgWindow,
    false,
    false,
    null,
    false
  );
  trashFolder.propagateDelete(trashSubfolder, true);
});