summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/composition/browser_drafts.js
blob: a5d6bed0cc43aaeffc1e0c0a4ca229be6338739f (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* 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 draft related functionality:
 * - that we don't allow opening multiple copies of a draft.
 */

"use strict";

var {
  close_compose_window,
  get_compose_body,
  get_msg_source,
  open_compose_new_mail,
  save_compose_message,
  setup_msg_contents,
  wait_for_compose_window,
} = ChromeUtils.import("resource://testing-common/mozmill/ComposeHelpers.jsm");
var {
  be_in_folder,
  get_special_folder,
  get_about_message,
  make_message_sets_in_folders,
  mc,
  press_delete,
  select_click_row,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
var { wait_for_notification_to_show, get_notification } = ChromeUtils.import(
  "resource://testing-common/mozmill/NotificationBoxHelpers.jsm"
);

var {
  click_menus_in_sequence,
  close_popup_sequence,
  plan_for_new_window,
  wait_for_window_focused,
} = ChromeUtils.import("resource://testing-common/mozmill/WindowHelpers.jsm");

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

let aboutMessage = get_about_message();

var kBoxId = "mail-notification-top";
var draftsFolder;

add_setup(async function () {
  requestLongerTimeout(2);
  draftsFolder = await get_special_folder(Ci.nsMsgFolderFlags.Drafts, true);
});

/**
 * Bug 349547.
 * Tests that we only open one compose window for one instance of a draft.
 */
add_task(async function test_open_draft_again() {
  await make_message_sets_in_folders([draftsFolder], [{ count: 1 }]);
  await be_in_folder(draftsFolder);
  select_click_row(0);

  // Wait for the notification with the Edit button.
  wait_for_notification_to_show(aboutMessage, kBoxId, "draftMsgContent");
  let box = get_notification(aboutMessage, kBoxId, "draftMsgContent");

  plan_for_new_window("msgcompose");
  // Click on the "Edit" button in the draft notification.
  EventUtils.synthesizeMouseAtCenter(
    box.buttonContainer.firstElementChild,
    {},
    aboutMessage
  );
  let cwc = wait_for_compose_window();

  let cwins = [...Services.wm.getEnumerator("msgcompose")].length;

  // click edit in main win again
  EventUtils.synthesizeMouseAtCenter(
    box.buttonContainer.firstElementChild,
    {},
    aboutMessage
  );

  // Wait a sec to see if it caused a new window.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 1000));

  Assert.ok(
    Services.ww.activeWindow == cwc.window,
    "the original draft composition window should have got focus (again)"
  );

  let cwins2 = [...Services.wm.getEnumerator("msgcompose")].length;

  Assert.ok(cwins2 > 0, "No compose window open!");
  Assert.equal(cwins, cwins2, "The number of compose windows changed!");

  // Type something and save, then check that we only have one draft.
  cwc.window.document.getElementById("messageEditor").focus();
  EventUtils.sendString("Hello!", cwc.window);
  await save_compose_message(cwc.window);
  close_compose_window(cwc);
  Assert.equal(draftsFolder.getTotalMessages(false), 1);

  select_click_row(0);
  press_delete(mc); // clean up after ourselves
});

/**
 * Bug 1202165
 * Test that the user set delivery format is preserved in a draft message.
 */
async function internal_check_delivery_format(editDraft) {
  let cwc = open_compose_new_mail();

  setup_msg_contents(
    cwc,
    "test@example.invalid",
    "Testing storing of the composition properties in the draft!",
    "Hello!"
  );

  // Select our wanted format.
  EventUtils.synthesizeMouseAtCenter(
    cwc.window.document.getElementById("optionsMenu"),
    {},
    cwc.window.document.getElementById("optionsMenu").ownerGlobal
  );
  await click_menus_in_sequence(
    cwc.window.document.getElementById("optionsMenuPopup"),
    [{ id: "outputFormatMenu" }, { id: "format_both" }]
  );

  /**
   * Check if the right format is selected in the menu.
   *
   * @param aMenuItemId  The id of the menuitem expected to be selected.
   * @param aValue       A value of nsIMsgCompSendFormat constants of the expected selected format.
   */
  async function assert_format_value(aMenuItemId, aValue) {
    EventUtils.synthesizeMouseAtCenter(
      cwc.window.document.getElementById("optionsMenu"),
      {},
      cwc.window.document.getElementById("optionsMenu").ownerGlobal
    );
    let formatMenu = await click_menus_in_sequence(
      cwc.window.document.getElementById("optionsMenuPopup"),
      [{ id: "outputFormatMenu" }],
      true
    );
    let formatItem = cwc.window.document
      .getElementById("outputFormatMenuPopup")
      .querySelector("[name=output_format][checked=true]");
    Assert.equal(formatItem.id, aMenuItemId);
    close_popup_sequence(formatMenu);
  }

  await save_compose_message(cwc.window);
  close_compose_window(cwc);

  // Open a new composition see if the menu is again at default value, not the one
  // chosen above.
  cwc = open_compose_new_mail();

  await assert_format_value("format_auto", Ci.nsIMsgCompSendFormat.Auto);

  close_compose_window(cwc);

  await be_in_folder(draftsFolder);
  select_click_row(0);

  // Wait for the notification with the Edit button.
  wait_for_notification_to_show(aboutMessage, kBoxId, "draftMsgContent");
  let box = get_notification(aboutMessage, kBoxId, "draftMsgContent");

  plan_for_new_window("msgcompose");
  if (editDraft) {
    // Trigger "edit draft".
    EventUtils.synthesizeMouseAtCenter(
      box.buttonContainer.firstElementChild,
      {},
      aboutMessage
    );
  } else {
    // Trigger "edit as new" resulting in template processing.
    EventUtils.synthesizeKey(
      "e",
      { shiftKey: false, accelKey: true },
      mc.window
    );
  }
  cwc = wait_for_compose_window();

  // Check if format value was restored.
  await assert_format_value("format_both", Ci.nsIMsgCompSendFormat.Both);

  close_compose_window(cwc);

  press_delete(mc); // clean up the created draft
}

add_task(async function test_save_delivery_format_with_edit_draft() {
  await internal_check_delivery_format(true);
}).__skipMe = AppConstants.platform == "macosx"; // Can't click menu bar on Mac.

add_task(async function test_save_delivery_format_with_edit_template() {
  await internal_check_delivery_format(false);
}).__skipMe = AppConstants.platform == "macosx"; // Can't click menu bar on Mac.

/**
 * Tests that 'Edit as New' leaves the original message in drafts folder.
 */
add_task(async function test_edit_as_new_in_draft() {
  await make_message_sets_in_folders([draftsFolder], [{ count: 1 }]);
  await be_in_folder(draftsFolder);

  Assert.equal(draftsFolder.getTotalMessages(false), 1);

  select_click_row(0);

  // Wait for the notification with the Edit button.
  wait_for_notification_to_show(aboutMessage, kBoxId, "draftMsgContent");

  plan_for_new_window("msgcompose");
  EventUtils.synthesizeKey("e", { shiftKey: false, accelKey: true });
  let cwc = wait_for_compose_window();

  cwc.window.document.getElementById("messageEditor").focus();
  EventUtils.sendString("Hello!", cwc.window);
  await save_compose_message(cwc.window);
  close_compose_window(cwc);

  await TestUtils.waitForCondition(
    () => draftsFolder.getTotalMessages(false) == 2,
    "message saved to drafts folder"
  );

  // Clean up the created drafts and count again.
  press_delete(mc);
  select_click_row(0);
  press_delete(mc);
  Assert.equal(draftsFolder.getTotalMessages(false), 0);
});

/**
 * Tests that editing a draft works as it should also when the identity
 * name has properties that require mime encoding when sent out.
 */
add_task(async function test_edit_draft_mime_from() {
  const identity = MailServices.accounts.createIdentity();
  identity.email = "skinner@example.com";
  identity.fullName = "SKINNER, Seymore";
  const accounts = MailServices.accounts.accounts.at(-1); // Local Folders
  accounts.addIdentity(identity);
  registerCleanupFunction(() => {
    accounts.removeIdentity(identity);
  });

  draftsFolder
    .QueryInterface(Ci.nsIMsgLocalMailFolder)
    .addMessage(
      "From - Sun Oct 01 01:02:03 2023\n" +
        "X-Mozilla-Status: 0000\n" +
        "X-Mozilla-Status2: 00000000\n" +
        "X-Mozilla-Keys:\n" +
        `X-Account-Key: ${accounts.key}\n` +
        `From: "SKINNER, Seymore <skinner@example.com>\n` +
        "To: nobody@example.invalid\n" +
        "Subject: test_edit_draft_mime_from!\n" +
        `Message-ID: <${Date.now()}@example.invalid>\n` +
        "Date: Sun, 1 Oct 2017 01:02:03 +0100\n" +
        "X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0; DSN=0; uuencode=0;\n" +
        " attachmentreminder=0; deliveryformat=4\n" +
        "MIME-Version: 1.0\n" +
        "Content-Type: text/plain; charset=utf-8\n" +
        "Content-Transfer-Encoding: 8bit\n" +
        "\n" +
        "Identitiy names should not show quotes!.\n"
    );
  await be_in_folder(draftsFolder);

  Assert.equal(
    draftsFolder.getTotalMessages(false),
    1,
    "should have one draft"
  );

  select_click_row(0);

  // Wait for the notification with the Edit button.
  wait_for_notification_to_show(aboutMessage, kBoxId, "draftMsgContent");

  plan_for_new_window("msgcompose");
  EventUtils.synthesizeKey("e", { shiftKey: false, accelKey: true });
  let cwc = wait_for_compose_window();

  const msgIdentity = cwc.window.document.getElementById("msgIdentity");
  // Should show no quotes in the address.
  Assert.equal(
    msgIdentity.value,
    "SKINNER, Seymore <skinner@example.com>",
    "should show human readable version of identity"
  );
  // Should not be editable - which it would be if no identity matched.
  Assert.equal(
    msgIdentity.getAttribute("editable"),
    "",
    "msgIdentity should not be editable since a draft identity email matches"
  );

  close_compose_window(cwc);
  // Clean up the created draft and count again.
  press_delete(mc);
  Assert.equal(
    draftsFolder.getTotalMessages(false),
    0,
    "should have no drafts after deleting"
  );
});

/**
 * Tests Content-Language header.
 */
add_task(async function test_content_language_header() {
  let cwc = open_compose_new_mail();

  setup_msg_contents(
    cwc,
    "test@example.invalid",
    "Testing Content-Language header",
    "Hello, we speak en-US"
  );

  await save_compose_message(cwc.window);
  close_compose_window(cwc);

  await TestUtils.waitForCondition(
    () => draftsFolder.getTotalMessages(false) == 1,
    "message saved to drafts folder"
  );

  await be_in_folder(draftsFolder);
  let draftMsg = select_click_row(0);
  let draftMsgContent = await get_msg_source(draftMsg);

  // Check for a single line that contains our header.
  if (
    !draftMsgContent
      .split("\n")
      .some(line => line.trim() == "Content-Language: en-US")
  ) {
    Assert.ok(false, "Failed to find Content-Language: en-US");
  }

  // Clean up the created draft.
  press_delete(mc);
});

/**
 * Tests Content-Language header suppression.
 */
add_task(async function test_content_language_header_suppression() {
  let statusQuo = Services.prefs.getBoolPref("mail.suppress_content_language");
  Services.prefs.setBoolPref("mail.suppress_content_language", true);

  let cwc = open_compose_new_mail();

  setup_msg_contents(
    cwc,
    "test@example.invalid",
    "Testing Content-Language header suppression",
    "Hello, we speak blank"
  );

  await save_compose_message(cwc.window);
  close_compose_window(cwc);

  await TestUtils.waitForCondition(
    () => draftsFolder.getTotalMessages(false) == 1,
    "message saved to drafts folder"
  );

  await be_in_folder(draftsFolder);
  let draftMsg = select_click_row(0);
  let draftMsgContent = await get_msg_source(draftMsg);

  // Check no line contains our Content-Language.
  Assert.ok(
    !draftMsgContent.split("\n").some(line => /^Content-Language:/.test(line)),
    "Didn't find Content-Language header in draft content"
  );

  // Clean up the created draft.
  press_delete(mc);

  Services.prefs.setBoolPref("mail.suppress_content_language", statusQuo);
});

/**
 * Tests space stuffing of plaintext message.
 */
add_task(async function test_remove_space_stuffing_format_flowed() {
  // Prepare for plaintext email.
  let oldHtmlPref = Services.prefs.getBoolPref(
    "mail.identity.default.compose_html"
  );
  Services.prefs.setBoolPref("mail.identity.default.compose_html", false);

  let cwc = open_compose_new_mail();

  setup_msg_contents(
    cwc,
    "test@example.invalid",
    "Testing space stuffing in plain text email",
    "NoSpace\n OneSpace\n  TwoSpaces"
  );

  await save_compose_message(cwc.window);
  close_compose_window(cwc);

  await TestUtils.waitForCondition(
    () => draftsFolder.getTotalMessages(false) == 1,
    "message saved to drafts folder"
  );

  await be_in_folder(draftsFolder);

  select_click_row(0);

  // Wait for the notification with the Edit button.
  wait_for_notification_to_show(aboutMessage, kBoxId, "draftMsgContent");
  let box = get_notification(aboutMessage, kBoxId, "draftMsgContent");

  plan_for_new_window("msgcompose");
  // Click on the "Edit" button in the draft notification.
  EventUtils.synthesizeMouseAtCenter(
    box.buttonContainer.firstElementChild,
    {},
    aboutMessage
  );
  cwc = wait_for_compose_window();

  let bodyText = get_compose_body(cwc).innerHTML;
  if (!bodyText.includes("NoSpace<br> OneSpace<br>  TwoSpaces")) {
    Assert.ok(false, "Something went wrong with space stuffing");
  }
  close_compose_window(cwc);

  // Clean up the created draft.
  press_delete(mc);

  Services.prefs.setBoolPref("mail.identity.default.compose_html", oldHtmlPref);
});