summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/account/browser_actions.js
blob: dff3b3dc2da2e108cf82eca1cf57b5d9b3989610 (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
/* 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/. */

"use strict";

var { click_account_tree_row, get_account_tree_row, open_advanced_settings } =
  ChromeUtils.import(
    "resource://testing-common/mozmill/AccountManagerHelpers.jsm"
  );
var { close_popup, wait_for_popup_to_open } = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

var { content_tab_e } = ChromeUtils.import(
  "resource://testing-common/mozmill/ContentTabHelpers.jsm"
);

var { mc } = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var imapAccount, nntpAccount, originalAccountCount;

add_setup(function () {
  // There may be pre-existing accounts from other tests.
  originalAccountCount = MailServices.accounts.allServers.length;
  // There already should be a Local Folders account created.
  // It is needed for this test.
  Assert.ok(MailServices.accounts.localFoldersServer);

  // Create an IMAP server
  let imapServer = MailServices.accounts
    .createIncomingServer("nobody", "example.com", "imap")
    .QueryInterface(Ci.nsIImapIncomingServer);

  let identity = MailServices.accounts.createIdentity();
  identity.email = "tinderbox@example.com";

  imapAccount = MailServices.accounts.createAccount();
  imapAccount.incomingServer = imapServer;
  imapAccount.addIdentity(identity);

  // Create a NNTP server
  let nntpServer = MailServices.accounts
    .createIncomingServer(null, "example.nntp.invalid", "nntp")
    .QueryInterface(Ci.nsINntpIncomingServer);

  identity = MailServices.accounts.createIdentity();
  identity.email = "tinderbox2@example.com";

  nntpAccount = MailServices.accounts.createAccount();
  nntpAccount.incomingServer = nntpServer;
  nntpAccount.addIdentity(identity);
  // Now there should be 2 more accounts.
  Assert.equal(
    MailServices.accounts.allServers.length,
    originalAccountCount + 2
  );
});

registerCleanupFunction(function () {
  // Remove our test accounts to leave the profile clean.
  MailServices.accounts.removeAccount(nntpAccount);
  MailServices.accounts.removeAccount(imapAccount);
  // There should be only the original accounts left.
  Assert.equal(MailServices.accounts.allServers.length, originalAccountCount);
});

/**
 * Check that the account actions for the account are enabled or disabled appropriately.
 *
 * @param {object} tab - The account manager tab.
 * @param {number} accountKey - The key of the account to select.
 * @param {boolean} isSetAsDefaultEnabled - True if the menuitem should be enabled, false otherwise.
 * @param {boolean} isRemoveEnabled - True if the menuitem should be enabled, false otherwise.
 * @param {boolean} isAddAccountEnabled - True if the menuitems (Add Mail Account+Add Other Account)
 *                                         should be enabled, false otherwise.
 */
async function subtest_check_account_actions(
  tab,
  accountKey,
  isSetAsDefaultEnabled,
  isRemoveEnabled,
  isAddAccountEnabled
) {
  let accountRow = get_account_tree_row(accountKey, null, tab);
  click_account_tree_row(tab, accountRow);

  // click the Actions Button to bring up the popup with menuitems to test
  let button = content_tab_e(tab, "accountActionsButton");
  EventUtils.synthesizeMouseAtCenter(
    button,
    { clickCount: 1 },
    button.ownerGlobal
  );
  await wait_for_popup_to_open(content_tab_e(tab, "accountActionsDropdown"));

  let actionAddMailAccount = content_tab_e(tab, "accountActionsAddMailAccount");
  Assert.notEqual(actionAddMailAccount, undefined);
  Assert.equal(
    !actionAddMailAccount.getAttribute("disabled"),
    isAddAccountEnabled
  );

  let actionAddOtherAccount = content_tab_e(
    tab,
    "accountActionsAddOtherAccount"
  );
  Assert.notEqual(actionAddOtherAccount, undefined);
  Assert.equal(
    !actionAddOtherAccount.getAttribute("disabled"),
    isAddAccountEnabled
  );

  let actionSetDefault = content_tab_e(tab, "accountActionsDropdownSetDefault");
  Assert.notEqual(actionSetDefault, undefined);
  Assert.equal(
    !actionSetDefault.getAttribute("disabled"),
    isSetAsDefaultEnabled
  );

  let actionRemove = content_tab_e(tab, "accountActionsDropdownRemove");
  Assert.notEqual(actionRemove, undefined);
  Assert.equal(!actionRemove.getAttribute("disabled"), isRemoveEnabled);

  await close_popup(mc, content_tab_e(tab, "accountActionsDropdown"));
}

add_task(async function test_account_actions() {
  // IMAP account: can be default, can be removed.
  await open_advanced_settings(async function (tab) {
    await subtest_check_account_actions(tab, imapAccount.key, true, true, true);
  });

  // NNTP (News) account: can't be default, can be removed.
  await open_advanced_settings(async function (tab) {
    await subtest_check_account_actions(
      tab,
      nntpAccount.key,
      false,
      true,
      true
    );
  });

  // Local Folders account: can't be removed, can't be default.
  var localFoldersAccount = MailServices.accounts.FindAccountForServer(
    MailServices.accounts.localFoldersServer
  );
  await open_advanced_settings(async function (tab) {
    await subtest_check_account_actions(
      tab,
      localFoldersAccount.key,
      false,
      false,
      true
    );
  });
  // SMTP server row: can't be removed, can't be default.
  await open_advanced_settings(async function (tab) {
    await subtest_check_account_actions(tab, "smtp", false, false, true);
  });

  // on the IMAP account, disable Delete Account menu item
  let disableItemPref = "mail.disable_button.delete_account";

  // Set the pref on the default branch, otherwise .getBoolPref on it throws.
  Services.prefs.getDefaultBranch("").setBoolPref(disableItemPref, true);
  Services.prefs.lockPref(disableItemPref);

  await open_advanced_settings(async function (tab) {
    await subtest_check_account_actions(
      tab,
      imapAccount.key,
      true,
      false,
      true
    );
  });

  Services.prefs.unlockPref(disableItemPref);
  Services.prefs.getDefaultBranch("").deleteBranch(disableItemPref);

  // on the IMAP account, disable Set as Default menu item
  disableItemPref = "mail.disable_button.set_default_account";

  Services.prefs.getDefaultBranch("").setBoolPref(disableItemPref, true);
  Services.prefs.lockPref(disableItemPref);

  await open_advanced_settings(async function (tab) {
    await subtest_check_account_actions(
      tab,
      imapAccount.key,
      false,
      true,
      true
    );
  });

  Services.prefs.unlockPref(disableItemPref);
  Services.prefs.getDefaultBranch("").deleteBranch(disableItemPref);

  // on the IMAP account, disable Add new Account menu items
  disableItemPref = "mail.disable_new_account_addition";

  Services.prefs.getDefaultBranch("").setBoolPref(disableItemPref, true);
  Services.prefs.lockPref(disableItemPref);

  await open_advanced_settings(async function (tab) {
    await subtest_check_account_actions(
      tab,
      imapAccount.key,
      true,
      true,
      false
    );
  });

  Services.prefs.unlockPref(disableItemPref);
  Services.prefs.getDefaultBranch("").deleteBranch(disableItemPref);
});