summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/account/browser_values.js
blob: 8d44a65f5abdf02d86f7343abe3bee5a240751df (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* 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/. */

/**
 * This test checks proper operation of the account settings panes
 * when certain special or invalid values are entered into the fields.
 */

"use strict";

var { click_account_tree_row, get_account_tree_row, open_advanced_settings } =
  ChromeUtils.import(
    "resource://testing-common/mozmill/AccountManagerHelpers.jsm"
  );
var { input_value } = ChromeUtils.import(
  "resource://testing-common/mozmill/KeyboardHelpers.jsm"
);
var { gMockPromptService } = ChromeUtils.import(
  "resource://testing-common/mozmill/PromptHelpers.jsm"
);
var { plan_for_modal_dialog, wait_for_modal_dialog } = ChromeUtils.import(
  "resource://testing-common/mozmill/WindowHelpers.jsm"
);

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

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

var gPopAccount, gOriginalAccountCount;

add_setup(function () {
  // There may be pre-existing accounts from other tests.
  gOriginalAccountCount = MailServices.accounts.allServers.length;

  // Create a POP server
  let popServer = MailServices.accounts
    .createIncomingServer("nobody", "example.invalid", "pop3")
    .QueryInterface(Ci.nsIPop3IncomingServer);

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

  gPopAccount = MailServices.accounts.createAccount();
  gPopAccount.incomingServer = popServer;
  gPopAccount.addIdentity(identity);

  // Now there should be one more account.
  Assert.equal(
    MailServices.accounts.allServers.length,
    gOriginalAccountCount + 1
  );
});

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

/**
 * Bug 208628.
 * Check that if the CC field is empty, enabling CC will automatically
 * prefill the currently default email address.
 */
add_task(async function test_default_CC_address() {
  await open_advanced_settings(function (tab) {
    subtest_check_default_CC_address(tab);
  });
});

/**
 * Check that if the CC field is empty, enabling CC will automatically
 * prefill the currently default email address.
 *
 * @param {object} tab - The account manager tab.
 */
function subtest_check_default_CC_address(tab) {
  let accountRow = get_account_tree_row(
    gPopAccount.key,
    "am-copies.xhtml",
    tab
  );
  click_account_tree_row(tab, accountRow);

  let iframe =
    tab.browser.contentWindow.document.getElementById("contentFrame");

  let defaultAddress =
    iframe.contentDocument.getElementById("identity.email").value;
  let ccCheck = iframe.contentDocument.getElementById("identity.doCc");
  let ccAddress = iframe.contentDocument.getElementById("identity.doCcList");
  // The CC checkbox is not enabled and the address value is empty.
  Assert.ok(!ccCheck.checked);
  Assert.equal(ccAddress.value, "");
  // After ticking the CC checkbox the default address should be prefilled.
  EventUtils.synthesizeMouseAtCenter(ccCheck, {}, ccCheck.ownerGlobal);
  Assert.equal(ccAddress.value, defaultAddress);

  let bccCheck = iframe.contentDocument.getElementById("identity.doBcc");
  let bccAddress = iframe.contentDocument.getElementById("identity.doBccList");
  // The BCC checkbox is not enabled but we set the address value to something.
  Assert.ok(!bccCheck.checked);
  Assert.equal(bccAddress.value, "");
  let bccUserAddress = "somebody@else.invalid";
  bccAddress.value = bccUserAddress;
  // After ticking the BCC checkbox the current value of the address should not change.
  EventUtils.synthesizeMouseAtCenter(bccCheck, {}, bccCheck.ownerGlobal);
  Assert.equal(bccAddress.value, bccUserAddress);
}

/**
 * Bug 720199.
 * Check if the account name automatically changes when the user changes
 * the username or hostname.
 */
add_task(async function test_account_name() {
  // We already have a POP account ready.
  // Create also a NNTP server.
  let nntpServer = MailServices.accounts
    .createIncomingServer(null, "example.nntp.invalid", "nntp")
    .QueryInterface(Ci.nsINntpIncomingServer);

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

  let nntpAccount = MailServices.accounts.createAccount();
  nntpAccount.incomingServer = nntpServer;
  nntpAccount.addIdentity(identity);

  Assert.equal(
    gPopAccount.incomingServer.prettyName,
    "nobody on example.invalid"
  );
  Assert.equal(nntpAccount.incomingServer.prettyName, "example.nntp.invalid");

  // The automatic account name update works only if the name is
  // in the form of user@host.
  gPopAccount.incomingServer.prettyName = "nobody@example.invalid";

  let newHost = "some.host.invalid";
  let newUser = "somebody";

  // On NNTP there is no user name so just set new hostname.
  await open_advanced_settings(function (tab) {
    subtest_check_account_name(nntpAccount, newHost, null, tab);
  });

  // And see if the account name is updated to it.
  Assert.equal(nntpAccount.incomingServer.prettyName, newHost);

  // On POP3 there is both user name and host name.
  // Set new host name first.
  await open_advanced_settings(function (tab) {
    subtest_check_account_name(gPopAccount, newHost, null, tab);
  });

  // And see if in the account name the host part is updated to it.
  Assert.equal(gPopAccount.incomingServer.prettyName, "nobody@" + newHost);

  // Set new host name first.
  await open_advanced_settings(function (tab) {
    subtest_check_account_name(gPopAccount, null, newUser, tab);
  });

  // And see if in the account name the user part is updated.
  Assert.equal(gPopAccount.incomingServer.prettyName, newUser + "@" + newHost);

  newHost = "another.host.invalid";
  newUser = "anotherbody";

  // Set user name and host name at once.
  await open_advanced_settings(function (tab) {
    subtest_check_account_name(gPopAccount, newHost, newUser, tab);
  });

  // And see if in the account name the host part is updated to it.
  Assert.equal(gPopAccount.incomingServer.prettyName, newUser + "@" + newHost);

  // Now have an account name where the name does not match the hostname.
  gPopAccount.incomingServer.prettyName = newUser + "@example.invalid";

  newHost = "third.host.invalid";
  // Set the host name again.
  await open_advanced_settings(function (tab) {
    subtest_check_account_name(gPopAccount, newHost, null, tab);
  });

  // And the account name should not be touched.
  Assert.equal(
    gPopAccount.incomingServer.prettyName,
    newUser + "@example.invalid"
  );

  MailServices.accounts.removeAccount(nntpAccount);
}).skip(); // Restart is required to apply change to server name or username.

/**
 * Changes the user name and hostname to the supplied values.
 *
 * @param {object} account - The account to change
 * @param {string} newHostname - The hostname value to set
 * @param {string} newUsername - The username value to set
 * @param {object} tab - The account manager tab.
 */
function subtest_check_account_name(account, newHostname, newUsername, tab) {
  let accountRow = get_account_tree_row(account.key, "am-server.xhtml", tab);
  click_account_tree_row(tab, accountRow);

  let iframe =
    tab.browser.contentWindow.document.getElementById("contentFrame");

  if (newHostname) {
    let hostname = iframe.contentDocument.getElementById("server.hostName");
    Assert.equal(hostname.value, account.incomingServer.hostName);

    // Now change the server host name.
    hostname.value = newHostname;
  }

  if (newUsername) {
    let username = iframe.contentDocument.getElementById("server.username");
    Assert.equal(username.value, account.incomingServer.username);

    // Now change the server user name.
    username.value = newUsername;
  }

  if (newUsername) {
    gMockPromptService.register();
  }

  tab.browser.contentWindow.onAccept(true);
  if (newUsername) {
    Assert.equal("alert", gMockPromptService.promptState.method);
    gMockPromptService.unregister();
  }
}

/**
 * Bug 536768.
 * Check if invalid junk target settings (folders) are fixed to sane values.
 */
add_task(async function test_invalid_junk_target() {
  // Set the junk target prefs to invalid values.
  let branch = Services.prefs.getBranch(
    "mail.server." + gPopAccount.incomingServer.key + "."
  );
  branch.setCharPref("spamActionTargetAccount", "some random non-existent URI");
  branch.setStringPref(
    "spamActionTargetFolder",
    "some random non-existent URI"
  );
  let moveOnSpam = true;
  branch.setBoolPref("moveOnSpam", moveOnSpam);
  await open_advanced_settings(function (tab) {
    subtest_check_invalid_junk_target(tab);
  });

  // The pref has no default so its non-existence means it was cleared.
  moveOnSpam = branch.getBoolPref("moveOnSpam", false);
  Assert.ok(!moveOnSpam);
  // The targets should point to the same pop account now.
  let targetAccount = branch.getCharPref("spamActionTargetAccount");
  Assert.equal(targetAccount, gPopAccount.incomingServer.serverURI);
  let targetFolder = branch.getStringPref("spamActionTargetFolder");
  Assert.equal(targetFolder, gPopAccount.incomingServer.serverURI + "/Junk");
});

/**
 * Just show the Junk settings pane and let it fix the values.
 *
 * @param {object} tab - The account manager tab.
 */
function subtest_check_invalid_junk_target(tab) {
  let accountRow = get_account_tree_row(gPopAccount.key, "am-junk.xhtml", tab);
  click_account_tree_row(tab, accountRow);
  tab.browser.contentWindow.onAccept(true);
}

/**
 * Bug 327812.
 * Checks if invalid server hostnames are not accepted.
 */
add_task(async function test_invalid_hostname() {
  let branch = Services.prefs.getBranch(
    "mail.server." + gPopAccount.incomingServer.key + "."
  );
  let origHostname = branch.getCharPref("hostname");

  await open_advanced_settings(function (tab) {
    subtest_check_invalid_hostname(tab, false, origHostname);
  });
  await open_advanced_settings(function (tab) {
    subtest_check_invalid_hostname(tab, true, origHostname);
  });

  // The new bad hostname should not have been saved.
  let newHostname = branch.getCharPref("hostname");
  Assert.equal(origHostname, newHostname);
});

/**
 * Set the hostname to an invalid value and check if it gets fixed.
 *
 * @param {object} tab - The account manager tab.
 * @param {boolean} exitSettings - Attempt to close the Account settings dialog.
 * @param {string} originalHostname - Original hostname of this server.
 */
function subtest_check_invalid_hostname(tab, exitSettings, originalHostname) {
  let accountRow = get_account_tree_row(
    gPopAccount.key,
    "am-server.xhtml",
    tab
  );
  click_account_tree_row(tab, accountRow);

  let iframe =
    tab.browser.contentWindow.document.getElementById("contentFrame");
  let hostname = iframe.contentDocument.getElementById("server.hostName");
  Assert.equal(hostname.value, originalHostname);

  hostname.value = "some_invalid+host&domain*in>invalid";

  if (!exitSettings) {
    accountRow = get_account_tree_row(gPopAccount.key, "am-junk.xhtml", tab);
    click_account_tree_row(tab, accountRow);

    // The invalid hostname should be set back to previous value at this point...
    accountRow = get_account_tree_row(gPopAccount.key, "am-server.xhtml", tab);
    click_account_tree_row(tab, accountRow);

    // ...let's check that:
    iframe = tab.browser.contentWindow.document.getElementById("contentFrame");
    hostname = iframe.contentDocument.getElementById("server.hostName");
    Assert.equal(hostname.value, originalHostname);
  } else {
    // If the hostname is bad, we should get a warning dialog.
    plan_for_modal_dialog("commonDialogWindow", function (cdc) {
      // Just dismiss it.
      cdc.window.document.documentElement
        .querySelector("dialog")
        .acceptDialog();
    });
    tab.browser.contentWindow.onAccept(true);
    wait_for_modal_dialog("commonDialogWindow");
  }
}

/**
 * Bug 1426328.
 * Check that the AM will trim user added spaces around text values.
 */
const badName = "trailing  space ";
const badEmail = " leading_space@example.com";

add_task(async function test_trailing_spaces() {
  await open_advanced_settings(function (tab) {
    subtest_check_trailing_spaces(tab);
  });
  Assert.equal(gPopAccount.incomingServer.prettyName, badName.trim());
  Assert.equal(gPopAccount.defaultIdentity.email, badEmail.trim());
});

/**
 * Check that the AM will trim user added spaces around text values
 * when storing them into the account.
 *
 * @param {object} tab - The account manager tab.
 */
function subtest_check_trailing_spaces(tab) {
  let accountRow = get_account_tree_row(gPopAccount.key, null, tab);
  click_account_tree_row(tab, accountRow);

  let iframe =
    tab.browser.contentWindow.document.getElementById("contentFrame");

  let accountName = iframe.contentDocument.getElementById("server.prettyName");
  let defaultAddress = iframe.contentDocument.getElementById("identity.email");
  accountName.value = "";
  defaultAddress.value = "";
  input_value(mc, badName, accountName);
  input_value(mc, badEmail, defaultAddress);

  Assert.equal(
    accountName.value,
    badName,
    "accountName should have the correct value typed in"
  );
  // type="email" inputs are now automatically trimmed
  Assert.equal(
    defaultAddress.value,
    badEmail.trim(),
    "defaultAddress should have the correct value typed in"
  );
}