summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/cloudfile/browser_attachmentUrls.js
blob: 38604afc6d68fede8001259e11ef78a59e801dc6 (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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
/* 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 Filelink URL insertion behaviours in compose windows.
 */

"use strict";

var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");
var { gMockFilePicker, gMockFilePickReg, select_attachments } =
  ChromeUtils.import("resource://testing-common/mozmill/AttachmentHelpers.jsm");
var { gMockCloudfileManager, MockCloudfileAccount } = ChromeUtils.import(
  "resource://testing-common/mozmill/CloudfileHelpers.jsm"
);
var {
  add_cloud_attachments,
  convert_selected_to_cloud_attachment,
  rename_selected_cloud_attachment,
  assert_previous_text,
  close_compose_window,
  get_compose_body,
  open_compose_new_mail,
  open_compose_with_forward,
  open_compose_with_reply,
  type_in_composer,
} = ChromeUtils.import("resource://testing-common/mozmill/ComposeHelpers.jsm");
var { assert_next_nodes, assert_previous_nodes, wait_for_element } =
  ChromeUtils.import("resource://testing-common/mozmill/DOMHelpers.jsm");
var {
  add_message_to_folder,
  assert_selected_and_displayed,
  be_in_folder,
  create_message,
  FAKE_SERVER_HOSTNAME,
  get_special_folder,
  mc,
  select_click_row,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);

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

var kHtmlPrefKey = "mail.identity.default.compose_html";
var kReplyOnTopKey = "mail.identity.default.reply_on_top";
var kReplyOnTop = 1;
var kReplyOnBottom = 0;
var kTextNodeType = 3;
var kSigPrefKey = "mail.identity.id1.htmlSigText";
var kSigOnReplyKey = "mail.identity.default.sig_on_reply";
var kSigOnForwardKey = "mail.identity.default.sig_on_fwd";
var kDefaultSigKey = "mail.identity.id1.htmlSigText";
var kDefaultSig = "This is my signature.\n\nCheck out my website sometime!";
var kFiles = ["./data/testFile1", "./data/testFile2"];
var kLines = ["This is a line of text", "and here's another!"];

const DATA_URLS = {
  "chrome://messenger/content/extension.svg":
    "data:image/svg+xml;filename=extension.svg;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBUaGlzIFNvdXJjZSBDb2RlIEZvcm0gaXMgc3ViamVjdCB0byB0aGUgdGVybXMgb2YgdGhlIE1vemlsbGEgUHVibGljCiAgIC0gTGljZW5zZSwgdi4gMi4wLiBJZiBhIGNvcHkgb2YgdGhlIE1QTCB3YXMgbm90IGRpc3RyaWJ1dGVkIHdpdGggdGhpcwogICAtIGZpbGUsIFlvdSBjYW4gb2J0YWluIG9uZSBhdCBodHRwOi8vbW96aWxsYS5vcmcvTVBMLzIuMC8uIC0tPgo8c3ZnI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jAzIDIuMzc2ek0xNCA4YTUuOTYzIDUuOTYzIDAgMCAxLS4zMzUgMS45NThoLTEuODIxQTEyLjMyNyAxMi4zMjcgMCAwIDAgMTIgOGExMi4zMjcgMTIuMzI3IDAgMCAwLS4xNTYtMS45NThoMS44MjFBNS45NjMgNS45NjMgMCAwIDEgMTQgOHptLTYgNmMtMS4wNzUgMC0yLjAzNy0xLjItMi41NjctMi45NThoNS4xMzVDMTAuMDM3IDEyLjggOS4wNzUgMTQgOCAxNHpNNS4xNzQgOS45NThhMTEuMDg0IDExLjA4NCAwIDAgMSAwLTMuOTE2aDUuNjUxQTExLjExNCAxMS4xMTQgMCAwIDEgMTEgOGExMS4xMTQgMTEuMTE0IDAgMCAxLS4xNzQgMS45NTh6TTIgOGE1Ljk2MyA1Ljk2MyAwIDAgMSAuMzM1LTEuOTU4aDEuODIxYTEyLjM2MSAxMi4zNjEgMCAwIDAgMCAzLjkxNkgyLjMzNUE1Ljk2MyA1Ljk2MyAwIDAgMSAyIDh6bTYtNmMxLjA3NSAwIDIuMDM3IDEuMiAyLjU2NyAyLjk1OEg1LjQzM0M1Ljk2MyAzLjIgNi45MjUgMiA4IDJ6bS0yLjU2LjU4MmE3LjcgNy43IDAgMCAwLTEuMDUxIDIuMzc2SDIuODM3QTYuMDMgNi4wMyAwIDAgMSA1LjQ0IDIuNTgyem0tMi42IDguNDZoMS41NDlhNy43IDcuNyAwIDAgMCAxLjA1MSAyLjM3NiA2LjAzIDYuMDMgMCAwIDEtMi42MDMtMi4zNzZ6bTcuNzIzIDIuMzc2YTcuNyA3LjcgMCAwIDAgMS4wNTEtMi4zNzZoMS41NTJhNi4wMyA2LjAzIDAgMCAxLTIuNjA2IDIuMzc2eiI+PC9wYXRoPgo8L3N2Zz4K",
};

var gInbox;

function test_expected_included(actual, expected, description) {
  Assert.equal(
    actual.length,
    expected.length,
    `${description}: correct length`
  );

  for (let i = 0; i < expected.length; i++) {
    for (let item of Object.keys(expected[i])) {
      Assert.deepEqual(
        actual[i][item],
        expected[i][item],
        `${description}: ${item} should exist and be correct`
      );
    }
  }
}

add_setup(async function () {
  requestLongerTimeout(4);

  // These prefs can't be set in the manifest as they contain white-space.
  Services.prefs.setStringPref(
    "mail.identity.id1.htmlSigText",
    "Tinderbox is soo 90ies"
  );
  Services.prefs.setStringPref(
    "mail.identity.id2.htmlSigText",
    "Tinderboxpushlog is the new <b>hotness!</b>"
  );

  // For replies and forwards, we'll work off a message in the Inbox folder
  // of the fake "tinderbox" account.
  let server = MailServices.accounts.findServer(
    "tinderbox",
    FAKE_SERVER_HOSTNAME,
    "pop3"
  );
  gInbox = await get_special_folder(Ci.nsMsgFolderFlags.Inbox, false, server);
  await add_message_to_folder([gInbox], create_message());

  gMockFilePickReg.register();
  gMockCloudfileManager.register();

  Services.prefs.setBoolPref(kHtmlPrefKey, true);

  // Don't create paragraphs in the test.
  // The test fails if it encounters paragraphs <p> instead of breaks <br>.
  Services.prefs.setBoolPref("mail.compose.default_to_paragraph", false);
});

registerCleanupFunction(function () {
  gMockCloudfileManager.unregister();
  gMockFilePickReg.unregister();
  Services.prefs.clearUserPref(kDefaultSigKey);
  Services.prefs.clearUserPref(kHtmlPrefKey);
  Services.prefs.clearUserPref("mail.compose.default_to_paragraph");
});

function setupTest() {
  // If our signature got accidentally wiped out, let's just put it back.
  Services.prefs.setCharPref(kDefaultSigKey, kDefaultSig);
}

/**
 * Given some compose window controller, wait for some Filelink URLs to be
 * inserted.
 *
 * Note: This function also validates, if the correct items have been added to
 *       the template (serviceUrl, downloadLimit, downloadExpiryDate,
 *       downloadPasswordProtected). There is no dedicated test for the different
 *       conditions, but the tests in this file are using different setups.
 *       See the values in the used provider.init() calls.
 *
 * @param aController the controller for a compose window.
 * @param aNumUrls the number of Filelink URLs that are expected.
 * @param aUploads an array containing the objects returned by
 *                 cloudFileAccounts.uploadFile() for all uploads
 * @returns an array containing the root containment node, the list node, and
 *          an array of the link URL nodes.
 */
function wait_for_attachment_urls(aController, aNumUrls, aUploads = []) {
  let mailBody = get_compose_body(aController);

  // Wait until we can find the root attachment URL node...
  let root = wait_for_element(
    mailBody.parentNode,
    "body > #cloudAttachmentListRoot"
  );

  let list = wait_for_element(
    mailBody,
    "#cloudAttachmentListRoot > #cloudAttachmentList"
  );

  let header = wait_for_element(
    mailBody,
    "#cloudAttachmentListRoot > #cloudAttachmentListHeader"
  );

  let footer = wait_for_element(
    mailBody,
    "#cloudAttachmentListRoot > #cloudAttachmentListFooter"
  );

  let urls = null;
  utils.waitFor(function () {
    urls = mailBody.querySelectorAll(
      "#cloudAttachmentList > .cloudAttachmentItem"
    );
    return urls != null && urls.length == aNumUrls;
  });

  Assert.equal(
    aUploads.length,
    aNumUrls,
    "Number of links should match number of linked files."
  );

  Assert.equal(
    header.textContent,
    aNumUrls == 1
      ? `I’ve linked 1 file to this email:`
      : `I’ve linked ${aNumUrls} files to this email:`,
    "Number of links mentioned in header should matches number of linked files."
  );

  let footerExpected = false;
  for (let entry of aUploads) {
    if (!entry.serviceUrl) {
      continue;
    }

    footerExpected = true;
    Assert.ok(
      footer.innerHTML.includes(entry.serviceUrl),
      `Footer "${footer.innerHTML}" should include serviceUrl "${entry.serviceUrl}".`
    );
    Assert.ok(
      footer.innerHTML.includes(entry.serviceName),
      `Footer "${footer.innerHTML}" should include serviceName "${entry.serviceName}".`
    );
  }
  if (footerExpected) {
    Assert.ok(
      footer.innerHTML.startsWith("Learn more about"),
      `Footer "${footer.innerHTML}" should start with "Learn more about "`
    );
  } else {
    Assert.ok(
      footer.innerHTML == "",
      `Footer should be empty if no serviceUrl is specified.`
    );
  }

  let bucket = aController.window.document.getElementById("attachmentBucket");

  // Check the actual content of the generated cloudAttachmentItems.
  for (let i = 0; i < urls.length; i++) {
    if (aController.window.gMsgCompose.composeHTML) {
      // Test HTML message.

      let paperClipIcon = urls[i].querySelector(".paperClipIcon");
      Assert.equal(
        aUploads[i].downloadPasswordProtected
          ? "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAA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/NldwkJuFsD6AMjYCYfASfKBbUd4BkoKvxJmiDWKA1/AXrAtrynmIUIshJ9DXgEmt/km8oVwHEIANu8u0LTleYhBMBUzZMVmkSaSQgAe9DW1d3L/wzAqW6jJpQ3+5cA3vbW1Vz3Np6BCBABIkAE+DWAmX7TUixdynm15Wf6jf5fa3Cq60K5qrraNuHrK1kbmJcGWJ8rB9DC4yvaq5odlmK7wBB4lw8Vs9ZRzdgHwLmaXa5RM1DNmQ+AA2ABfACZgz4DctXs+QAAuMLc0dsPEJk0BXDhazjAFnCnxjlmiTuYg5kAR4rl0twCmz4BLMQAs7RVH6kLzJ17H162fczhGmO+mqa6PqXGnn8CxMN0PcC9DrQAAAAASUVORK5CYII=",
        paperClipIcon.src,
        "The paperClipIcon should be correct."
      );

      Assert.equal(
        urls[i].querySelector(".cloudfile-name").href,
        aUploads[i].url,
        "The link attached to the cloudfile name should be correct."
      );

      let providerIcon = urls[i].querySelector(".cloudfile-service-icon");
      if (providerIcon) {
        Assert.equal(
          DATA_URLS[aUploads[i].serviceIcon] || aUploads[i].serviceIcon,
          providerIcon.src,
          "The cloufile service icon should be correct."
        );
      }

      let expected = {
        url: aUploads[i].downloadPasswordProtected
          ? ".cloudfile-password-protected-link"
          : ".cloudfile-link",
        name: ".cloudfile-name",
        serviceName: ".cloudfile-service-name",
        downloadLimit: ".cloudfile-download-limit",
        downloadExpiryDateString: ".cloudfile-expiry-date",
      };

      for (let [fieldName, id] of Object.entries(expected)) {
        let element = urls[i].querySelector(id);
        Assert.ok(
          !!element == !!aUploads[i][fieldName],
          `The ${fieldName} should have been correctly added.`
        );
        if (aUploads[i][fieldName]) {
          Assert.equal(
            element.textContent,
            `${aUploads[i][fieldName]}`,
            `The cloudfile ${fieldName} should be correct.`
          );
        } else {
          Assert.equal(
            element,
            null,
            `The cloudfile ${fieldName} should not be present.`
          );
        }
      }
    } else {
      // Test plain text message.

      let lines = urls[i].textContent.split("\n");
      let expected = {
        url: aUploads[i].downloadPasswordProtected
          ? `    Password Protected Link: `
          : `    Link: `,
        name: `  * `,
        downloadLimit: `    Download Limit: `,
        downloadExpiryDateString: `    Expiry Date: `,
      };

      if (urls[i].serviceUrl) {
        expected.serviceName = `    CloudFile Service: `;
      }

      for (let [fieldName, prefix] of Object.entries(expected)) {
        if (aUploads[i][fieldName]) {
          let line = `${prefix}${aUploads[i][fieldName]}`;
          Assert.ok(
            lines.includes(line),
            `Line "${line}" should be part of "${lines}".`
          );
        } else {
          !lines.find(
            line => line.startsWith(prefix),
            `There should be no line starting with "${prefix}" part of "${lines}".`
          );
        }
      }
    }

    // Find the bucket entry for this upload.
    let items = Array.from(
      bucket.querySelectorAll(".attachmentItem"),
      item => item
    ).filter(item => item.attachment.name == aUploads[i].name);
    Assert.equal(
      items.length,
      1,
      `Should find one matching bucket entry for ${aUploads[i].serviceName} / ${aUploads[i].name}.`
    );
    Assert.equal(
      items[0].querySelector("img.attachmentcell-icon").src,
      aUploads[i].serviceIcon,
      `CloudFile icon should be correct for ${aUploads[i].serviceName} / ${aUploads[i].name}`
    );
  }

  return [root, list, urls];
}

/**
 * Helper function that sets up the mock file picker for a series of files,
 * spawns a reply window for the first message in the gInbox, optionally
 * types some strings into the compose window, and then attaches some
 * Filelinks.
 *
 * @param aText an array of strings to type into the compose window. Each
 *              string is followed by pressing the RETURN key, except for
 *              the final string.  Pass an empty array if you don't want
 *              anything typed.
 * @param aFiles an array of filename strings for files located beneath
 *               the test directory.
 */
async function prepare_some_attachments_and_reply(aText, aFiles) {
  gMockFilePicker.returnFiles = collectFiles(aFiles);

  let provider = new MockCloudfileAccount();
  provider.init("providerF", {
    serviceName: "MochiTest F",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
    serviceUrl: "https://www.provider-F.org",
    downloadLimit: 2,
  });

  await be_in_folder(gInbox);
  let msg = select_click_row(0);
  assert_selected_and_displayed(mc, msg);

  let cw = open_compose_with_reply();

  // If we have any typing to do, let's do it.
  type_in_composer(cw, aText);
  let uploads = add_cloud_attachments(cw, provider);

  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerF/testFile1",
        name: "testFile1",
        serviceName: "MochiTest F",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-F.org",
        downloadLimit: 2,
      },
      {
        url: "https://www.example.com/providerF/testFile2",
        name: "testFile2",
        serviceName: "MochiTest F",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-F.org",
        downloadLimit: 2,
      },
    ],
    `Expected values in uploads array #11`
  );
  let [root] = wait_for_attachment_urls(cw, aFiles.length, uploads);

  return [cw, root];
}

/**
 * Helper function that sets up the mock file picker for a series of files,
 * spawns an inline forward compose window for the first message in the gInbox,
 * optionally types some strings into the compose window, and then attaches
 * some Filelinks.
 *
 * @param aText an array of strings to type into the compose window. Each
 *              string is followed by pressing the RETURN key, except for
 *              the final string.  Pass an empty array if you don't want
 *              anything typed.
 * @param aFiles an array of filename strings for files located beneath
 *               the test directory.
 */
async function prepare_some_attachments_and_forward(aText, aFiles) {
  gMockFilePicker.returnFiles = collectFiles(aFiles);

  let provider = new MockCloudfileAccount();
  provider.init("providerG", {
    serviceName: "MochiTest G",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
    serviceUrl: "https://www.provider-G.org",
    downloadExpiryDate: { timestamp: 1639827408073 },
  });

  await be_in_folder(gInbox);
  let msg = select_click_row(0);
  assert_selected_and_displayed(mc, msg);

  let cw = open_compose_with_forward();

  // Put the selection at the beginning of the document...
  let editor = cw.window.GetCurrentEditor();
  editor.beginningOfDocument();

  // Do any necessary typing...
  type_in_composer(cw, aText);
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerG/testFile1",
        name: "testFile1",
        serviceName: "MochiTest G",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-G.org",
        downloadExpiryDate: { timestamp: 1639827408073 },
      },
      {
        url: "https://www.example.com/providerG/testFile2",
        name: "testFile2",
        serviceName: "MochiTest G",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-G.org",
        downloadExpiryDate: { timestamp: 1639827408073 },
      },
    ],
    `Expected values in uploads array #12`
  );

  // Add the expected time string.
  let timeString = new Date(1639827408073).toLocaleString(undefined, {
    day: "2-digit",
    month: "2-digit",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit",
    timeZoneName: "short",
  });
  uploads[0].downloadExpiryDateString = timeString;
  uploads[1].downloadExpiryDateString = timeString;
  let [root] = wait_for_attachment_urls(cw, aFiles.length, uploads);

  return [cw, root];
}

/**
 * Helper function that runs a test function with signature-in-reply and
 * signature-in-forward enabled, and then runs the test again with those
 * prefs disabled.
 *
 * @param aSpecialTest a test that takes two arguments - the first argument
 *                     is the aText array of any text that should be typed,
 *                     and the second is a boolean for whether or not the
 *                     special test should expect a signature or not.
 * @param aText any text to be typed into the compose window, passed to
 *              aSpecialTest.
 */
async function try_with_and_without_signature_in_reply_or_fwd(
  aSpecialTest,
  aText
) {
  // By default, we have a signature included in replies, so we'll start
  // with that.
  Services.prefs.setBoolPref(kSigOnReplyKey, true);
  Services.prefs.setBoolPref(kSigOnForwardKey, true);
  await aSpecialTest(aText, true);

  Services.prefs.setBoolPref(kSigOnReplyKey, false);
  Services.prefs.setBoolPref(kSigOnForwardKey, false);
  await aSpecialTest(aText, false);
}

/**
 * Helper function that runs a test function without a signature, once
 * in HTML mode, and again in plaintext mode.
 *
 * @param aTest a test that takes no arguments.
 */
async function try_without_signature(aTest) {
  let oldSig = Services.prefs.getCharPref(kSigPrefKey);
  Services.prefs.setCharPref(kSigPrefKey, "");

  await try_with_plaintext_and_html_mail(aTest);
  Services.prefs.setCharPref(kSigPrefKey, oldSig);
}

/**
 * Helper function that runs a test function for HTML mail composition, and
 * then again in plaintext mail composition.
 *
 * @param aTest a test that takes no arguments.
 */
async function try_with_plaintext_and_html_mail(aTest) {
  await aTest();
  Services.prefs.setBoolPref(kHtmlPrefKey, false);
  await aTest();
  Services.prefs.setBoolPref(kHtmlPrefKey, true);
}

/**
 * Test that if we open up a composer and immediately attach a Filelink,
 * a linebreak is inserted before the containment node in order to allow
 * the user to write before the attachment URLs.  This assumes the user
 * does not have a signature already inserted into the message body.
 */
add_task(async function test_inserts_linebreak_on_empty_compose() {
  await try_without_signature(subtest_inserts_linebreak_on_empty_compose);
});

/**
 * Subtest for test_inserts_linebreak_on_empty_compose - can be executed
 * on both plaintext and HTML compose windows.
 */
function subtest_inserts_linebreak_on_empty_compose() {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  provider.init("someKey", {
    downloadPasswordProtected: false,
  });
  let cw = open_compose_new_mail();
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/someKey/testFile1",
        name: "testFile1",
        serviceName: "default",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "",
        downloadPasswordProtected: false,
      },
      {
        url: "https://www.example.com/someKey/testFile2",
        name: "testFile2",
        serviceName: "default",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "",
        downloadPasswordProtected: false,
      },
    ],
    `Expected values in uploads array #1`
  );
  let [root] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  let br = root.previousSibling;
  Assert.equal(
    br.localName,
    "br",
    "The attachment URL containment node should be preceded by a linebreak"
  );

  let mailBody = get_compose_body(cw);

  Assert.equal(
    mailBody.firstChild,
    br,
    "The linebreak should be the first child of the compose body"
  );

  close_compose_window(cw);
}

/**
 * Test that if we open up a composer and immediately attach a Filelink,
 * a linebreak is inserted before the containment node. This test also
 * ensures that, with a signature already in the compose window, we don't
 * accidentally insert the attachment URL containment within the signature
 * node.
 */
add_task(function test_inserts_linebreak_on_empty_compose_with_signature() {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  provider.init("someKey", {
    downloadPasswordProtected: true,
  });

  let cw = open_compose_new_mail();
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/someKey/testFile1",
        name: "testFile1",
        serviceName: "default",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "",
        downloadPasswordProtected: true,
      },
      {
        url: "https://www.example.com/someKey/testFile2",
        name: "testFile2",
        serviceName: "default",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "",
        downloadPasswordProtected: true,
      },
    ],
    `Expected values in uploads array #2`
  );
  // wait_for_attachment_urls ensures that the attachment URL containment
  // node is an immediate child of the body of the message, so if this
  // succeeds, then we were not in the signature node.
  let [root] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  let br = assert_previous_nodes("br", root, 1);

  let mailBody = get_compose_body(cw);
  Assert.equal(
    mailBody.firstChild,
    br,
    "The linebreak should be the first child of the compose body"
  );

  // Now ensure that the node after the attachments is a br, and following
  // that is the signature.
  br = assert_next_nodes("br", root, 1);

  let pre = br.nextSibling;
  Assert.equal(
    pre.localName,
    "pre",
    "The linebreak should be followed by the signature pre"
  );
  Assert.ok(
    pre.classList.contains("moz-signature"),
    "The pre should have the moz-signature class"
  );

  close_compose_window(cw);

  Services.prefs.setBoolPref(kHtmlPrefKey, false);

  // Now let's try with plaintext mail.
  cw = open_compose_new_mail();
  uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/someKey/testFile1",
        name: "testFile1",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceName: "default",
        serviceUrl: "",
        downloadPasswordProtected: true,
      },
      {
        url: "https://www.example.com/someKey/testFile2",
        name: "testFile2",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceName: "default",
        serviceUrl: "",
        downloadPasswordProtected: true,
      },
    ],
    `Expected values in uploads array #3`
  );
  [root] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  br = assert_previous_nodes("br", root, 1);

  mailBody = get_compose_body(cw);
  Assert.equal(
    mailBody.firstChild,
    br,
    "The linebreak should be the first child of the compose body"
  );

  // Now ensure that the node after the attachments is a br, and following
  // that is the signature.
  br = assert_next_nodes("br", root, 1);

  let div = br.nextSibling;
  Assert.equal(
    div.localName,
    "div",
    "The linebreak should be followed by the signature div"
  );
  Assert.ok(
    div.classList.contains("moz-signature"),
    "The div should have the moz-signature class"
  );

  close_compose_window(cw);

  Services.prefs.setBoolPref(kHtmlPrefKey, true);
});

/**
 * Tests that removing all Filelinks causes the root node to be removed.
 */
add_task(async function test_removing_filelinks_removes_root_node() {
  await try_with_plaintext_and_html_mail(
    subtest_removing_filelinks_removes_root_node
  );
});

/**
 * Test for test_removing_filelinks_removes_root_node - can be executed
 * on both plaintext and HTML compose windows.
 */
async function subtest_removing_filelinks_removes_root_node() {
  let [cw, root] = await prepare_some_attachments_and_reply([], kFiles);

  // Now select the attachments in the attachment bucket, and remove them.
  select_attachments(cw, 0, 1);
  cw.window.goDoCommand("cmd_delete");

  // Wait for the root to be removed.
  let mailBody = get_compose_body(cw);
  utils.waitFor(function () {
    let result = mailBody.querySelector(root.id);
    return result == null;
  }, "Timed out waiting for attachment container to be removed");

  close_compose_window(cw);
}

/**
 * Test that if we write some text in an empty message (no signature),
 * and the selection is at the end of a line of text, attaching some Filelinks
 * causes the attachment URL container to be separated from the text by
 * two br tags.
 */
add_task(async function test_adding_filelinks_to_written_message() {
  await try_without_signature(subtest_adding_filelinks_to_written_message);
});

/**
 * Subtest for test_adding_filelinks_to_written_message - generalized for both
 * HTML and plaintext mail.
 */
function subtest_adding_filelinks_to_written_message() {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  provider.init("someKey");
  let cw = open_compose_new_mail();

  type_in_composer(cw, kLines);
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/someKey/testFile1",
        name: "testFile1",
        serviceName: "default",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "",
      },
      {
        url: "https://www.example.com/someKey/testFile2",
        name: "testFile2",
        serviceName: "default",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "",
      },
    ],
    `Expected values in uploads array #4`
  );
  let [root] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  let br = root.previousSibling;
  Assert.equal(
    br.localName,
    "br",
    "The attachment URL containment node should be preceded by a linebreak"
  );
  br = br.previousSibling;
  Assert.equal(
    br.localName,
    "br",
    "The attachment URL containment node should be preceded by " +
      "two linebreaks"
  );
  close_compose_window(cw);
}

/**
 * Tests for inserting Filelinks into a reply, when we're configured to
 * reply above the quote.
 */
add_task(async function test_adding_filelinks_to_empty_reply_above() {
  let oldReplyOnTop = Services.prefs.getIntPref(kReplyOnTopKey);
  Services.prefs.setIntPref(kReplyOnTopKey, kReplyOnTop);

  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_reply_above,
    []
  );
  // Now with HTML mail...
  Services.prefs.setBoolPref(kHtmlPrefKey, false);
  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_reply_above_plaintext,
    []
  );

  Services.prefs.setBoolPref(kHtmlPrefKey, true);
  Services.prefs.setIntPref(kReplyOnTopKey, oldReplyOnTop);
});

/**
 * Tests for inserting Filelinks into a reply, when we're configured to
 * reply above the quote, after entering some text.
 */
add_task(async function test_adding_filelinks_to_nonempty_reply_above() {
  let oldReplyOnTop = Services.prefs.getIntPref(kReplyOnTopKey);
  Services.prefs.setIntPref(kReplyOnTopKey, kReplyOnTop);

  await subtest_adding_filelinks_to_reply_above(kLines);

  Services.prefs.setBoolPref(kHtmlPrefKey, false);
  await subtest_adding_filelinks_to_reply_above_plaintext(kLines);
  Services.prefs.setBoolPref(kHtmlPrefKey, true);

  Services.prefs.setIntPref(kReplyOnTopKey, oldReplyOnTop);
});

/**
 * Subtest for test_adding_filelinks_to_reply_above for the plaintext composer.
 * Does some special casing for the weird br insertions that happens in
 * various cases.
 */
async function subtest_adding_filelinks_to_reply_above_plaintext(
  aText,
  aWithSig
) {
  let [cw, root] = await prepare_some_attachments_and_reply(aText, kFiles);

  let br;
  if (aText.length) {
    br = assert_next_nodes("br", root, 2);
  } else {
    br = assert_next_nodes("br", root, 1);
  }

  let div = br.nextSibling;
  Assert.equal(
    div.localName,
    "div",
    "The linebreak should be followed by a div"
  );

  Assert.ok(div.classList.contains("moz-cite-prefix"));

  if (aText.length) {
    br = assert_previous_nodes("br", root, 2);
  } else {
    br = assert_previous_nodes("br", root, 1);
  }

  if (aText.length == 0) {
    // If we didn't type anything, that br should be the first element of the
    // message body.
    let msgBody = get_compose_body(cw);
    Assert.equal(
      msgBody.firstChild,
      br,
      "The linebreak should have been the first element in the " +
        "message body"
    );
  } else {
    let targetText = aText[aText.length - 1];
    let textNode = br.previousSibling;
    Assert.equal(textNode.nodeType, kTextNodeType);
    Assert.equal(textNode.nodeValue, targetText);
  }

  close_compose_window(cw);
}

/**
 * Subtest for test_adding_filelinks_to_reply_above for the HTML composer.
 */
async function subtest_adding_filelinks_to_reply_above(aText) {
  let [cw, root] = await prepare_some_attachments_and_reply(aText, kFiles);

  // If there's any text written, then there's only a single break between the
  // end of the text and the reply. Otherwise, there are two breaks.
  let br =
    aText.length > 1
      ? assert_next_nodes("br", root, 2)
      : assert_next_nodes("br", root, 1);

  // ... which is followed by a div with a class of "moz-cite-prefix".
  let div = br.nextSibling;
  Assert.equal(
    div.localName,
    "div",
    "The linebreak should be followed by a div"
  );

  Assert.ok(div.classList.contains("moz-cite-prefix"));

  close_compose_window(cw);
}

/**
 * Tests for inserting Filelinks into a reply, when we're configured to
 * reply below the quote.
 */
add_task(async function test_adding_filelinks_to_empty_reply_below() {
  let oldReplyOnTop = Services.prefs.getIntPref(kReplyOnTopKey);
  Services.prefs.setIntPref(kReplyOnTopKey, kReplyOnBottom);

  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_reply_below,
    []
  );
  Services.prefs.setBoolPref(kHtmlPrefKey, false);
  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_plaintext_reply_below,
    []
  );
  Services.prefs.setBoolPref(kHtmlPrefKey, true);

  Services.prefs.setIntPref(kReplyOnTopKey, oldReplyOnTop);
});

/**
 * Tests for inserting Filelinks into a reply, when we're configured to
 * reply below the quote, after entering some text.
 */
add_task(async function test_adding_filelinks_to_nonempty_reply_below() {
  let oldReplyOnTop = Services.prefs.getIntPref(kReplyOnTopKey);
  Services.prefs.setIntPref(kReplyOnTopKey, kReplyOnBottom);

  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_reply_below,
    kLines
  );

  Services.prefs.setBoolPref(kHtmlPrefKey, false);
  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_plaintext_reply_below,
    kLines
  );
  Services.prefs.setBoolPref(kHtmlPrefKey, true);

  Services.prefs.setIntPref(kReplyOnTopKey, oldReplyOnTop);
});

/**
 * Subtest for test_adding_filelinks_to_reply_below for the HTML composer.
 */
async function subtest_adding_filelinks_to_reply_below(aText, aWithSig) {
  let [cw, root] = await prepare_some_attachments_and_reply(aText, kFiles);

  // So, we should have the root, followed by a br
  let br = root.nextSibling;
  Assert.equal(
    br.localName,
    "br",
    "The attachment URL containment node should be followed by a br"
  );

  let blockquote;
  if (aText.length) {
    // If there was any text inserted, check for 2 previous br nodes, and then
    // the inserted text, and then the blockquote.
    br = assert_previous_nodes("br", root, 2);
    let textNode = assert_previous_text(br.previousSibling, aText);
    blockquote = textNode.previousSibling;
  } else {
    // If no text was inserted, check for 1 previous br node, and then the
    // blockquote.
    br = assert_previous_nodes("br", root, 1);
    blockquote = br.previousSibling;
  }

  Assert.equal(
    blockquote.localName,
    "blockquote",
    "The linebreak should be preceded by a blockquote."
  );

  let prefix = blockquote.previousSibling;
  Assert.equal(
    prefix.localName,
    "div",
    "The blockquote should be preceded by the prefix div"
  );
  Assert.ok(
    prefix.classList.contains("moz-cite-prefix"),
    "The prefix should have the moz-cite-prefix class"
  );

  close_compose_window(cw);
}

/**
 * Subtest for test_adding_filelinks_to_reply_below for the plaintext composer.
 */
async function subtest_adding_filelinks_to_plaintext_reply_below(
  aText,
  aWithSig
) {
  let [cw, root] = await prepare_some_attachments_and_reply(aText, kFiles);
  let br, span;

  assert_next_nodes("br", root, 1);

  if (aText.length) {
    br = assert_previous_nodes("br", root, 2);
    // If text was entered, make sure it matches what we expect...
    let textNode = assert_previous_text(br.previousSibling, aText);
    // And then grab the span, which should be before the final text node.
    span = textNode.previousSibling;
  } else {
    br = assert_previous_nodes("br", root, 1);
    // If no text was entered, just grab the last br's previous sibling - that
    // will be the span.
    span = br.previousSibling;
    // Sometimes we need to skip one more linebreak.
    if (span.localName != "span") {
      span = span.previousSibling;
    }
  }

  Assert.equal(
    span.localName,
    "span",
    "The linebreak should be preceded by a span."
  );

  let prefix = span.previousSibling;
  Assert.equal(
    prefix.localName,
    "div",
    "The blockquote should be preceded by the prefix div"
  );
  Assert.ok(
    prefix.classList.contains("moz-cite-prefix"),
    "The prefix should have the moz-cite-prefix class"
  );

  close_compose_window(cw);
}

/**
 * Tests Filelink insertion on an inline-forward compose window with nothing
 * typed into it.
 */
add_task(async function test_adding_filelinks_to_empty_forward() {
  Services.prefs.setIntPref(kReplyOnTopKey, kReplyOnTop);
  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_forward,
    []
  );
  Services.prefs.setBoolPref(kHtmlPrefKey, false);
  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_forward,
    []
  );
  Services.prefs.setBoolPref(kHtmlPrefKey, true);
});

/**
 * Tests Filelink insertion on an inline-forward compose window with some
 * text typed into it.
 */
add_task(async function test_adding_filelinks_to_forward() {
  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_forward,
    kLines
  );
  Services.prefs.setBoolPref(kHtmlPrefKey, false);
  await try_with_and_without_signature_in_reply_or_fwd(
    subtest_adding_filelinks_to_forward,
    kLines
  );
  Services.prefs.setBoolPref(kHtmlPrefKey, true);
});

/**
 * Subtest for both test_adding_filelinks_to_empty_forward and
 * test_adding_filelinks_to_forward - ensures that the inserted Filelinks
 * are positioned correctly.
 */
async function subtest_adding_filelinks_to_forward(aText, aWithSig) {
  let [cw, root] = await prepare_some_attachments_and_forward(aText, kFiles);

  let br = assert_next_nodes("br", root, 1);
  let forwardDiv = br.nextSibling;
  Assert.equal(forwardDiv.localName, "div");
  Assert.ok(forwardDiv.classList.contains("moz-forward-container"));

  if (aText.length) {
    // If there was text typed in, it should be separated from the root by two
    // br's
    let br = assert_previous_nodes("br", root, 2);
    assert_previous_text(br.previousSibling, aText);
  } else {
    // Otherwise, there's only 1 br, and that br should be the first element
    // of the message body.
    let br = assert_previous_nodes("br", root, 1);
    let mailBody = get_compose_body(cw);
    Assert.equal(br, mailBody.firstChild);
  }

  close_compose_window(cw);
}

/**
 * Test that if we convert a Filelink from one provider to another, that the
 * old Filelink is removed, and a new Filelink is added for the new provider.
 * We test this on both HTML and plaintext mail.
 */
add_task(async function test_converting_filelink_updates_urls() {
  await try_with_plaintext_and_html_mail(
    subtest_converting_filelink_updates_urls
  );
});

/**
 * Subtest for test_converting_filelink_updates_urls that creates two
 * storage provider accounts, uploads files to one, converts them to the
 * other, and ensures that the attachment links in the message body get
 * get updated.
 */
function subtest_converting_filelink_updates_urls() {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let providerA = new MockCloudfileAccount();
  let providerB = new MockCloudfileAccount();
  providerA.init("providerA", {
    serviceName: "MochiTest A",
    serviceUrl: "https://www.provider-A.org",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
  });
  providerB.init("providerB", {
    serviceName: "MochiTest B",
    serviceUrl: "https://www.provider-B.org",
  });

  let cw = open_compose_new_mail();
  let uploads = add_cloud_attachments(cw, providerA);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerA/testFile1",
        name: "testFile1",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceName: "MochiTest A",
        serviceUrl: "https://www.provider-A.org",
      },
      {
        url: "https://www.example.com/providerA/testFile2",
        name: "testFile2",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceName: "MochiTest A",
        serviceUrl: "https://www.provider-A.org",
      },
    ],
    `Expected values in uploads array #5`
  );
  let [, , UrlsA] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  // Convert each Filelink to providerB, ensuring that the URLs are replaced.
  uploads = [];
  for (let i = 0; i < kFiles.length; ++i) {
    select_attachments(cw, i);
    uploads.push(...convert_selected_to_cloud_attachment(cw, providerB));
  }
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerB/testFile1",
        name: "testFile1",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceName: "MochiTest B",
        serviceUrl: "https://www.provider-B.org",
      },
      {
        url: "https://www.example.com/providerB/testFile2",
        name: "testFile2",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceName: "MochiTest B",
        serviceUrl: "https://www.provider-B.org",
      },
    ],
    `Expected values in uploads array #6`
  );
  let [, , UrlsB] = wait_for_attachment_urls(cw, kFiles.length, uploads);
  Assert.notEqual(UrlsA, UrlsB, "The original URL should have been replaced");

  close_compose_window(cw);
}

/**
 * Test that if we rename a Filelink, that the old Filelink is removed, and a
 * new Filelink is added. We test this on both HTML and plaintext mail.
 */
add_task(async function test_renaming_filelink_updates_urls() {
  await try_with_plaintext_and_html_mail(
    subtest_renaming_filelink_updates_urls
  );
});

/**
 * Subtest for test_renaming_filelink_updates_urls that uploads a file to a
 * storage provider account, renames the upload, and ensures that the attachment
 * links in the message body get get updated.
 */
function subtest_renaming_filelink_updates_urls() {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  provider.init("providerA", {
    serviceName: "MochiTest A",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
    serviceUrl: "https://www.provider-A.org",
    downloadExpiryDate: {
      timestamp: 1639827408073,
      format: { dateStyle: "short" },
    },
  });

  let cw = open_compose_new_mail();
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerA/testFile1",
        name: "testFile1",
        serviceName: "MochiTest A",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-A.org",
        downloadExpiryDate: {
          timestamp: 1639827408073,
          format: { dateStyle: "short" },
        },
      },
      {
        url: "https://www.example.com/providerA/testFile2",
        name: "testFile2",
        serviceName: "MochiTest A",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-A.org",
        downloadExpiryDate: {
          timestamp: 1639827408073,
          format: { dateStyle: "short" },
        },
      },
    ],
    `Expected values in uploads array before renaming the files`
  );

  // Add the expected time string.
  let timeString = new Date(1639827408073).toLocaleString(undefined, {
    dateStyle: "short",
  });
  uploads[0].downloadExpiryDateString = timeString;
  uploads[1].downloadExpiryDateString = timeString;
  let [, , Urls1] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  // Rename each Filelink, ensuring that the URLs are replaced.
  let newNames = ["testFile1Renamed", "testFile2Renamed"];
  uploads = [];
  for (let i = 0; i < kFiles.length; ++i) {
    select_attachments(cw, i);
    uploads.push(rename_selected_cloud_attachment(cw, newNames[i]));
  }

  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerA/testFile1Renamed",
        name: "testFile1Renamed",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceName: "MochiTest A",
        serviceUrl: "https://www.provider-A.org",
        downloadExpiryDate: {
          timestamp: 1639827408073,
          format: { dateStyle: "short" },
        },
      },
      {
        url: "https://www.example.com/providerA/testFile2Renamed",
        name: "testFile2Renamed",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceName: "MochiTest A",
        serviceUrl: "https://www.provider-A.org",
        downloadExpiryDate: {
          timestamp: 1639827408073,
          format: { dateStyle: "short" },
        },
      },
    ],
    `Expected values in uploads array after renaming the files`
  );

  // Add the expected time string.
  uploads[0].downloadExpiryDateString = timeString;
  uploads[1].downloadExpiryDateString = timeString;
  let [, , Urls2] = wait_for_attachment_urls(cw, kFiles.length, uploads);
  Assert.notEqual(Urls1, Urls2, "The original URL should have been replaced");

  close_compose_window(cw);
}

/**
 * Test that if we convert a Filelink to a normal attachment that the
 * Filelink is removed from the message body.
 */
add_task(async function test_converting_filelink_to_normal_removes_url() {
  await try_with_plaintext_and_html_mail(
    subtest_converting_filelink_to_normal_removes_url
  );
});

/**
 * Subtest for test_converting_filelink_to_normal_removes_url that adds
 * some Filelinks to an email, and then converts those Filelinks back into
 * normal attachments, checking to ensure that the links are removed from
 * the body of the email.
 */
async function subtest_converting_filelink_to_normal_removes_url() {
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  provider.init("providerC", {
    serviceName: "MochiTest C",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
    serviceUrl: "https://www.provider-C.org",
  });

  let cw = open_compose_new_mail();
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerC/testFile1",
        name: "testFile1",
        serviceName: "MochiTest C",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-C.org",
      },
      {
        url: "https://www.example.com/providerC/testFile2",
        name: "testFile2",
        serviceName: "MochiTest C",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-C.org",
      },
    ],
    `Expected values in uploads array #7`
  );
  let [root, list] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  for (let i = 0; i < kFiles.length; ++i) {
    let [selectedItem] = select_attachments(cw, i);
    cw.window.convertSelectedToRegularAttachment();

    // Wait until the cloud file entry has been removed.
    utils.waitFor(function () {
      let urls = list.querySelectorAll(".cloudAttachmentItem");
      return urls.length == kFiles.length - (i + 1);
    });

    // Check that the cloud icon has been removed.
    Assert.equal(
      selectedItem.querySelector("img.attachmentcell-icon").src,
      `moz-icon://${selectedItem.attachment.name}?size=16`,
      `CloudIcon should be correctly removed for ${selectedItem.attachment.name}`
    );
  }

  // At this point, the root should also have been removed.
  await new Promise(resolve => setTimeout(resolve));
  let mailBody = get_compose_body(cw);
  root = mailBody.querySelector("#cloudAttachmentListRoot");
  if (root) {
    throw new Error("Should not have found the cloudAttachmentListRoot");
  }

  close_compose_window(cw);
}

/**
 * Tests that if the user manually removes the Filelinks from the message body
 * that it doesn't break future Filelink insertions. Tests both HTML and
 * plaintext composers.
 */
add_task(async function test_filelinks_work_after_manual_removal() {
  await try_with_plaintext_and_html_mail(
    subtest_filelinks_work_after_manual_removal
  );
});

/**
 * Subtest that first adds some Filelinks to the message body, removes them,
 * and then adds another Filelink ensuring that the new URL is successfully
 * inserted.
 */
function subtest_filelinks_work_after_manual_removal() {
  // Insert some Filelinks...
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  provider.init("providerD", {
    serviceName: "MochiTest D",
    serviceIcon: "chrome://messenger/skin/icons/globe.svg",
    serviceUrl: "https://www.provider-D.org",
  });

  let cw = open_compose_new_mail();
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerD/testFile1",
        name: "testFile1",
        serviceName: "MochiTest D",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-D.org",
      },
      {
        url: "https://www.example.com/providerD/testFile2",
        name: "testFile2",
        serviceName: "MochiTest D",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceUrl: "https://www.provider-D.org",
      },
    ],
    `Expected values in uploads array #8`
  );
  let [root] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  // Now remove the root node from the document body
  root.remove();

  gMockFilePicker.returnFiles = collectFiles(["./data/testFile3"]);
  uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerD/testFile3",
        name: "testFile3",
        serviceIcon: "chrome://messenger/skin/icons/globe.svg",
        serviceName: "MochiTest D",
        serviceUrl: "https://www.provider-D.org",
      },
    ],
    `Expected values in uploads array #9`
  );
  [root] = wait_for_attachment_urls(cw, 1, uploads);

  close_compose_window(cw);
}

/**
 * Test that if the users selection caret is on a newline when the URL
 * insertion occurs, that the caret does not move when the insertion is
 * complete. Tests both HTML and plaintext composers.
 */
add_task(async function test_insertion_restores_caret_point() {
  await try_with_plaintext_and_html_mail(
    subtest_insertion_restores_caret_point
  );
});

/**
 * Subtest that types some things into the composer, finishes on two
 * linebreaks, inserts some Filelink URLs, and then types some more,
 * ensuring that the selection is where we expect it to be.
 */
function subtest_insertion_restores_caret_point() {
  // Insert some Filelinks...
  gMockFilePicker.returnFiles = collectFiles(kFiles);
  let provider = new MockCloudfileAccount();
  provider.init("providerE", {
    serviceName: "MochiTest E",
    serviceUrl: "https://www.provider-E.org",
  });

  let cw = open_compose_new_mail();

  // Put the selection at the beginning of the document...
  let editor = cw.window.GetCurrentEditor();
  editor.beginningOfDocument();

  // Do any necessary typing, ending with two linebreaks.
  type_in_composer(cw, ["Line 1", "Line 2", "", ""]);

  // Attach some Filelinks.
  let uploads = add_cloud_attachments(cw, provider);
  test_expected_included(
    uploads,
    [
      {
        url: "https://www.example.com/providerE/testFile1",
        name: "testFile1",
        serviceName: "MochiTest E",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "https://www.provider-E.org",
      },
      {
        url: "https://www.example.com/providerE/testFile2",
        name: "testFile2",
        serviceName: "MochiTest E",
        serviceIcon: "chrome://messenger/content/extension.svg",
        serviceUrl: "https://www.provider-E.org",
      },
    ],
    `Expected values in uploads array #10`
  );
  let [root] = wait_for_attachment_urls(cw, kFiles.length, uploads);

  // Type some text.
  const kTypedIn = "Test";
  type_in_composer(cw, [kTypedIn]);

  // That text should be inserted just above the root attachment URL node.
  let br = assert_previous_nodes("br", root, 1);
  assert_previous_text(br.previousSibling, [kTypedIn]);

  close_compose_window(cw);
}