summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/attachment/browser_attachmentMenus.js
blob: cc24e6ccc9f5710e70a2a405d40d385e0e4d3d2b (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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* 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 folder;
var messenger;
var epsilon;

var {
  create_body_part,
  create_deleted_attachment,
  create_detached_attachment,
  create_enclosure_attachment,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/AttachmentHelpers.jsm"
);
var {
  add_message_to_folder,
  be_in_folder,
  close_popup,
  create_folder,
  create_message,
  get_about_message,
  mc,
  select_click_row,
  wait_for_popup_to_open,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

var aboutMessage = get_about_message();

var textAttachment =
  "Can't make the frug contest, Helen; stomach's upset. I'll fix you, " +
  "Ubik! Ubik drops you back in the thick of things fast. Taken as " +
  "directed, Ubik speeds relief to head and stomach. Remember: Ubik is " +
  "only seconds away. Avoid prolonged use.";

var detachedName = "./attachment.txt";
var missingName = "./nonexistent.txt";
var deletedName = "deleted.txt";

// create some messages that have various types of attachments
var messages = [
  {
    name: "regular_attachment",
    attachments: [{ body: textAttachment, filename: "ubik.txt", format: "" }],
    menuStates: [{ open: true, save: true, detach: true, delete_: true }],
    allMenuStates: { open: true, save: true, detach: true, delete_: true },
  },
  {
    name: "detached_attachment",
    bodyPart: null,
    menuStates: [{ open: true, save: true, detach: false, delete_: false }],
    allMenuStates: { open: true, save: true, detach: false, delete_: false },
  },
  {
    name: "detached_attachment_with_missing_file",
    bodyPart: null,
    menuStates: [{ open: false, save: false, detach: false, delete_: false }],
    allMenuStates: { open: false, save: false, detach: false, delete_: false },
  },
  {
    name: "deleted_attachment",
    bodyPart: null,
    menuStates: [{ open: false, save: false, detach: false, delete_: false }],
    allMenuStates: { open: false, save: false, detach: false, delete_: false },
  },
  {
    name: "multiple_attachments",
    attachments: [
      { body: textAttachment, filename: "ubik.txt", format: "" },
      { body: textAttachment, filename: "ubik2.txt", format: "" },
    ],
    menuStates: [
      { open: true, save: true, detach: true, delete_: true },
      { open: true, save: true, detach: true, delete_: true },
    ],
    allMenuStates: { open: true, save: true, detach: true, delete_: true },
  },
  {
    name: "multiple_attachments_one_detached",
    bodyPart: null,
    attachments: [{ body: textAttachment, filename: "ubik.txt", format: "" }],
    menuStates: [
      { open: true, save: true, detach: false, delete_: false },
      { open: true, save: true, detach: true, delete_: true },
    ],
    allMenuStates: { open: true, save: true, detach: true, delete_: true },
  },
  {
    name: "multiple_attachments_one_detached_with_missing_file",
    bodyPart: null,
    attachments: [{ body: textAttachment, filename: "ubik.txt", format: "" }],
    menuStates: [
      { open: false, save: false, detach: false, delete_: false },
      { open: true, save: true, detach: true, delete_: true },
    ],
    allMenuStates: { open: true, save: true, detach: true, delete_: true },
  },
  {
    name: "multiple_attachments_one_deleted",
    bodyPart: null,
    attachments: [{ body: textAttachment, filename: "ubik.txt", format: "" }],
    menuStates: [
      { open: false, save: false, detach: false, delete_: false },
      { open: true, save: true, detach: true, delete_: true },
    ],
    allMenuStates: { open: true, save: true, detach: true, delete_: true },
  },
  {
    name: "multiple_attachments_all_detached",
    bodyPart: null,
    menuStates: [
      { open: true, save: true, detach: false, delete_: false },
      { open: true, save: true, detach: false, delete_: false },
    ],
    allMenuStates: { open: true, save: true, detach: false, delete_: false },
  },
  {
    name: "multiple_attachments_all_detached_with_missing_files",
    bodyPart: null,
    menuStates: [
      { open: false, save: false, detach: false, delete_: false },
      { open: false, save: false, detach: false, delete_: false },
    ],
    allMenuStates: { open: false, save: false, detach: false, delete_: false },
  },
  {
    name: "multiple_attachments_all_deleted",
    bodyPart: null,
    menuStates: [
      { open: false, save: false, detach: false, delete_: false },
      { open: false, save: false, detach: false, delete_: false },
    ],
    allMenuStates: { open: false, save: false, detach: false, delete_: false },
  },
  {
    name: "link_enclosure_valid",
    bodyPart: null,
    menuStates: [{ open: true, save: true, detach: false, delete_: false }],
    allMenuStates: { open: true, save: true, detach: false, delete_: false },
  },
  {
    name: "link_enclosure_invalid",
    bodyPart: null,
    menuStates: [{ open: false, save: false, detach: false, delete_: false }],
    allMenuStates: { open: false, save: false, detach: false, delete_: false },
  },
  {
    name: "link_multiple_enclosures",
    bodyPart: null,
    menuStates: [
      { open: true, save: true, detach: false, delete_: false },
      { open: true, save: true, detach: false, delete_: false },
    ],
    allMenuStates: { open: true, save: true, detach: false, delete_: false },
  },
  {
    name: "link_multiple_enclosures_one_invalid",
    bodyPart: null,
    menuStates: [
      { open: true, save: true, detach: false, delete_: false },
      { open: false, save: false, detach: false, delete_: false },
    ],
    allMenuStates: { open: true, save: true, detach: false, delete_: false },
  },
  {
    name: "link_multiple_enclosures_all_invalid",
    bodyPart: null,
    menuStates: [
      { open: false, save: false, detach: false, delete_: false },
      { open: false, save: false, detach: false, delete_: false },
    ],
    allMenuStates: { open: false, save: false, detach: false, delete_: false },
  },
];

add_setup(async function () {
  messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);

  /* Today's gory details (thanks to Jonathan Protzenko): libmime somehow
   * counts the trailing newline for an attachment MIME part. Most of the time,
   * assuming attachment has N bytes (no matter what's inside, newlines or
   * not), libmime will return N + 1 bytes. On Linux and Mac, this always
   * holds. However, on Windows, if the attachment is not encoded (that is, is
   * inline text), libmime will return N + 2 bytes.
   */
  epsilon = "@mozilla.org/windows-registry-key;1" in Cc ? 2 : 1;

  // set up our detached/deleted attachments
  var detachedFile = new FileUtils.File(
    getTestFilePath(`data/${detachedName}`)
  );
  var detached = create_body_part("Here is a file", [
    create_detached_attachment(detachedFile, "text/plain"),
  ]);
  var multiple_detached = create_body_part("Here are some files", [
    create_detached_attachment(detachedFile, "text/plain"),
    create_detached_attachment(detachedFile, "text/plain"),
  ]);

  var missingFile = new FileUtils.File(getTestFilePath(`data/${missingName}`));
  var missing = create_body_part(
    "Here is a file (but you deleted the external file, you silly oaf!)",
    [create_detached_attachment(missingFile, "text/plain")]
  );
  var multiple_missing = create_body_part(
    "Here are some files (but you deleted the external files, you silly oaf!)",
    [
      create_detached_attachment(missingFile, "text/plain"),
      create_detached_attachment(missingFile, "text/plain"),
    ]
  );

  var deleted = create_body_part("Here is a file that you deleted", [
    create_deleted_attachment(deletedName, "text/plain"),
  ]);
  var multiple_deleted = create_body_part(
    "Here are some files that you deleted",
    [
      create_deleted_attachment(deletedName, "text/plain"),
      create_deleted_attachment(deletedName, "text/plain"),
    ]
  );

  var enclosure_valid_url = create_body_part("My blog has the best enclosure", [
    create_enclosure_attachment(
      "purr.mp3",
      "audio/mpeg",
      "https://example.com",
      12345678
    ),
  ]);
  var enclosure_invalid_url = create_body_part(
    "My blog has the best enclosure with a dead link",
    [
      create_enclosure_attachment(
        "meow.mp3",
        "audio/mpeg",
        "https://example.com/invalid"
      ),
    ]
  );
  var multiple_enclosures = create_body_part(
    "My blog has the best 2 cat sound enclosures",
    [
      create_enclosure_attachment(
        "purr.mp3",
        "audio/mpeg",
        "https://example.com",
        1234567
      ),
      create_enclosure_attachment(
        "meow.mp3",
        "audio/mpeg",
        "https://example.com",
        987654321
      ),
    ]
  );
  var multiple_enclosures_one_link_invalid = create_body_part(
    "My blog has the best 2 cat sound enclosures but one is invalid",
    [
      create_enclosure_attachment(
        "purr.mp3",
        "audio/mpeg",
        "https://example.com",
        1234567
      ),
      create_enclosure_attachment(
        "meow.mp3",
        "audio/mpeg",
        "https://example.com/invalid"
      ),
    ]
  );
  var multiple_enclosures_all_links_invalid = create_body_part(
    "My blog has 2 enclosures with 2 bad links",
    [
      create_enclosure_attachment(
        "purr.mp3",
        "audio/mpeg",
        "https://example.com/invalid"
      ),
      create_enclosure_attachment(
        "meow.mp3",
        "audio/mpeg",
        "https://example.com/invalid"
      ),
    ]
  );

  folder = await create_folder("AttachmentMenusA");
  for (let i = 0; i < messages.length; i++) {
    // First, add any missing info to the message object.
    switch (messages[i].name) {
      case "detached_attachment":
      case "multiple_attachments_one_detached":
        messages[i].bodyPart = detached;
        break;
      case "multiple_attachments_all_detached":
        messages[i].bodyPart = multiple_detached;
        break;
      case "detached_attachment_with_missing_file":
      case "multiple_attachments_one_detached_with_missing_file":
        messages[i].bodyPart = missing;
        break;
      case "multiple_attachments_all_detached_with_missing_files":
        messages[i].bodyPart = multiple_missing;
        break;
      case "deleted_attachment":
      case "multiple_attachments_one_deleted":
        messages[i].bodyPart = deleted;
        break;
      case "multiple_attachments_all_deleted":
        messages[i].bodyPart = multiple_deleted;
        break;
      case "link_enclosure_valid":
        messages[i].bodyPart = enclosure_valid_url;
        break;
      case "link_enclosure_invalid":
        messages[i].bodyPart = enclosure_invalid_url;
        break;
      case "link_multiple_enclosures":
        messages[i].bodyPart = multiple_enclosures;
        break;
      case "link_multiple_enclosures_one_invalid":
        messages[i].bodyPart = multiple_enclosures_one_link_invalid;
        break;
      case "link_multiple_enclosures_all_invalid":
        messages[i].bodyPart = multiple_enclosures_all_links_invalid;
        break;
    }

    await add_message_to_folder([folder], create_message(messages[i]));
  }
});

/**
 * Ensure that the specified element is visible/hidden
 *
 * @param id the id of the element to check
 * @param visible true if the element should be visible, false otherwise
 */
function assert_shown(id, visible) {
  Assert.notEqual(
    aboutMessage.document.getElementById(id).hidden,
    visible,
    `"${id}" should be ${visible ? "visible" : "hidden"}`
  );
}

/**
 * Ensure that the specified element is enabled/disabled
 *
 * @param id the id of the element to check
 * @param enabled true if the element should be enabled, false otherwise
 */
function assert_enabled(id, enabled) {
  Assert.notEqual(
    aboutMessage.document.getElementById(id).disabled,
    enabled,
    `"${id}" should be ${enabled ? "enabled" : "disabled"}`
  );
}

/**
 * Check that the menu states in the "save" toolbar button are correct.
 *
 * @param expected a dictionary containing the expected states
 */
async function check_toolbar_menu_states_single(expected) {
  assert_shown("attachmentSaveAllSingle", true);
  assert_shown("attachmentSaveAllMultiple", false);

  if (expected.save === false) {
    assert_enabled("attachmentSaveAllSingle", false);
  } else {
    assert_enabled("attachmentSaveAllSingle", true);
    let dm = aboutMessage.document.querySelector(
      "#attachmentSaveAllSingle .toolbarbutton-menubutton-dropmarker"
    );
    EventUtils.synthesizeMouseAtCenter(dm, { clickCount: 1 }, aboutMessage);
    await wait_for_popup_to_open(
      aboutMessage.document.getElementById("attachmentSaveAllSingleMenu")
    );

    try {
      assert_enabled("button-openAttachment", expected.open);
      assert_enabled("button-saveAttachment", expected.save);
      assert_enabled("button-detachAttachment", expected.detach);
      assert_enabled("button-deleteAttachment", expected.delete_);
    } finally {
      await close_popup(
        aboutMessage,
        aboutMessage.document.getElementById("attachmentSaveAllSingleMenu")
      );
    }
  }
}

/**
 * Check that the menu states in the "save all" toolbar button are correct.
 *
 * @param expected a dictionary containing the expected states
 */
async function check_toolbar_menu_states_multiple(expected) {
  assert_shown("attachmentSaveAllSingle", false);
  assert_shown("attachmentSaveAllMultiple", true);

  if (expected.save === false) {
    assert_enabled("attachmentSaveAllMultiple", false);
  } else {
    assert_enabled("attachmentSaveAllMultiple", true);
    let dm = aboutMessage.document.querySelector(
      "#attachmentSaveAllMultiple .toolbarbutton-menubutton-dropmarker"
    );
    EventUtils.synthesizeMouseAtCenter(dm, { clickCount: 1 }, aboutMessage);
    await wait_for_popup_to_open(
      aboutMessage.document.getElementById("attachmentSaveAllMultipleMenu")
    );

    try {
      assert_enabled("button-openAllAttachments", expected.open);
      assert_enabled("button-saveAllAttachments", expected.save);
      assert_enabled("button-detachAllAttachments", expected.detach);
      assert_enabled("button-deleteAllAttachments", expected.delete_);
    } finally {
      await close_popup(
        mc,
        aboutMessage.document.getElementById("attachmentSaveAllMultipleMenu")
      );
    }
  }
}

/**
 * Check that the menu states in the single item context menu are correct
 *
 * @param expected a dictionary containing the expected states
 */
async function check_menu_states_single(index, expected) {
  let attachmentList = aboutMessage.document.getElementById("attachmentList");
  let node = attachmentList.getItemAtIndex(index);

  let contextMenu = aboutMessage.document.getElementById(
    "attachmentItemContext"
  );
  let shownPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  attachmentList.selectItem(node);
  EventUtils.synthesizeMouseAtCenter(
    node,
    { type: "contextmenu" },
    aboutMessage
  );
  await shownPromise;

  try {
    assert_shown("context-openAttachment", true);
    assert_shown("context-saveAttachment", true);
    assert_shown("context-menu-separator", true);
    assert_shown("context-detachAttachment", true);
    assert_shown("context-deleteAttachment", true);

    assert_enabled("context-openAttachment", expected.open);
    assert_enabled("context-saveAttachment", expected.save);
    assert_enabled("context-detachAttachment", expected.detach);
    assert_enabled("context-deleteAttachment", expected.delete_);
  } finally {
    let hiddenPromise = BrowserTestUtils.waitForEvent(
      contextMenu,
      "popuphidden"
    );
    contextMenu.hidePopup();
    await hiddenPromise;
    await new Promise(resolve => requestAnimationFrame(resolve));
  }
}

/**
 * Check that the menu states in the all items context menu are correct
 *
 * @param expected a dictionary containing the expected states
 */
async function check_menu_states_all(expected) {
  // Using a rightClick here is unsafe, because we need to hit the empty area
  // beside the attachment items and that seems to be different per platform.
  // Using DOM methods to open the popup works fine.
  let contextMenu = aboutMessage.document.getElementById(
    "attachmentListContext"
  );
  let shownPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  aboutMessage.document
    .getElementById("attachmentListContext")
    .openPopup(aboutMessage.document.getElementById("attachmentList"));
  await shownPromise;

  try {
    assert_shown("context-openAllAttachments", true);
    assert_shown("context-saveAllAttachments", true);
    assert_shown("context-menu-separator-all", true);
    assert_shown("context-detachAllAttachments", true);
    assert_shown("context-deleteAllAttachments", true);

    assert_enabled("context-openAllAttachments", expected.open);
    assert_enabled("context-saveAllAttachments", expected.save);
    assert_enabled("context-detachAllAttachments", expected.detach);
    assert_enabled("context-deleteAllAttachments", expected.delete_);
  } finally {
    await close_popup(
      aboutMessage,
      aboutMessage.document.getElementById("attachmentListContext")
    );
  }
}

async function help_test_attachment_menus(index) {
  await be_in_folder(folder);
  select_click_row(index);
  let expectedStates = messages[index].menuStates;

  let aboutMessage = get_about_message();
  aboutMessage.toggleAttachmentList(true);

  for (let attachment of aboutMessage.currentAttachments) {
    // Ensure all attachments are resolved; other than external they already
    // should be.
    await attachment.isEmpty();
  }

  if (expectedStates.length == 1) {
    await check_toolbar_menu_states_single(messages[index].allMenuStates);
  } else {
    await check_toolbar_menu_states_multiple(messages[index].allMenuStates);
  }

  await check_menu_states_all(messages[index].allMenuStates);
  for (let i = 0; i < expectedStates.length; i++) {
    await check_menu_states_single(i, expectedStates[i]);
  }
}

// Generate a test for each message in |messages|.
for (let i = 0; i < messages.length; i++) {
  add_task(function () {
    return help_test_attachment_menus(i);
  });
}

add_task(() => {
  Assert.report(
    false,
    undefined,
    undefined,
    "Test ran to completion successfully"
  );
});

registerCleanupFunction(() => {
  // Remove created folders.
  folder.deleteSelf(null);
});