summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_font_loading_api.html
blob: 26c5a2a8524b3e4a7b15e4055d3d557e1920c058 (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
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
<!DOCTYPE html>
<meta charset=utf-8>
<title>Test for the CSS Font Loading API</title>
<script src=/tests/SimpleTest/SimpleTest.js></script>
<link rel=stylesheet type=text/css href=/tests/SimpleTest/test.css>

<script src=descriptor_database.js></script>

<body onload="start()">
<iframe id=v src="file_font_loading_api_vframe.html"></iframe>
<iframe id=n style="display: none"></iframe>

<script>
// Map of FontFace descriptor attribute names to @font-face rule descriptor
// names.
var descriptorNames = {
  style: "font-style",
  weight: "font-weight",
  stretch: "font-stretch",
  unicodeRange: "unicode-range",
  variant: "font-variant",
  featureSettings: "font-feature-settings",
  display: "font-display"
};

// Default values for the FontFace descriptor attributes other than family, as
// Gecko currently serializes them.
var defaultValues = {
  style: "normal",
  weight: "normal",
  stretch: "normal",
  unicodeRange: "U+0-10FFFF",
  variant: "normal",
  featureSettings: "normal",
  display: "auto"
};

// Non-default values for the FontFace descriptor attributes other than family
// along with how Gecko currently serializes them.  Each value is chosen to be
// different from the default value and also has a different serialized form.
var nonDefaultValues = {
  style: ["Italic", "italic"],
  weight: ["Bold", "bold"],
  stretch: ["Ultra-Condensed", "ultra-condensed"],
  unicodeRange: ["U+3??", "U+300-3FF"],
  variant: ["Small-Caps", "small-caps"],
  featureSettings: ["'dlig' on", "\"dlig\""],
  display: ["Block", "block"]
};

// Invalid values for the FontFace descriptor attributes other than family.
var invalidValues = {
  style: "initial",
  weight: "bolder",
  stretch: "wider",
  unicodeRange: "U+1????-2????",
  variant: "inherit",
  featureSettings: "dlig",
  display: "normal"
};

// Invalid font family names.
var invalidFontFamilyNames = [
  "", "\"\"", "sans-serif", "A, B", "inherit", "a 1"
];

// Font family list where at least one is likely to be available on
// platforms we care about.
var likelyPlatformFonts = "Helvetica Neue, Bitstream Vera Sans, Bitstream Vera Sans Roman, FreeSans, Free Sans, SwissA, DejaVu Sans, Arial";

// Will hold an ArrayBuffer containing a valid font.
var fontData;

var queue = Promise.resolve();

function is_resolved_with(aPromise, aExpectedValue, aDescription, aTestID) {
  // This assumes that all Promise tasks come from the task source.
  var handled = false;
  return new Promise(function(aResolve, aReject) {
    aPromise.then(function(aValue) {
      if (!handled) {
        handled = true;
        is(aValue, aExpectedValue, aDescription + " should be resolved with the expected value " + aTestID);
        aResolve();
      }
    }, function(aError) {
      if (!handled) {
        handled = true;
        ok(false, aDescription + " should be resolved; instead it was rejected with " + aError + " " + aTestID);
        aResolve();
      }
    });
    Promise.resolve().then(function() {
      if (!handled) {
        handled = true;
        ok(false, aDescription + " should be resolved; instead it is pending " + aTestID);
        aResolve();
      }
    });
  });
}

function is_pending(aPromise, aDescription, aTestID) {
  // This assumes that all Promise tasks come from the task source.
  var handled = false;
  return new Promise(function(aResolve, aReject) {
    aPromise.then(function(aValue) {
      if (!handled) {
        handled = true;
        ok(false, aDescription + " should be pending; instead it was resolved with " + aValue + " " + aTestID);
        aResolve();
      }
    }, function(aError) {
      if (!handled) {
        handled = true;
        ok(false, aDescription + " should be pending; instead it was rejected with " + aError + " " + aTestID);
        aResolve();
      }
    });
    Promise.resolve().then(function() {
      if (!handled) {
        handled = true;
        ok(true, aDescription + " should be pending " + aTestID);
        aResolve();
      }
    });
  });
}

function fetchAsArrayBuffer(aURL) {
  var xhr;
  return new Promise(function(aResolve, aReject) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", aURL);
    xhr.responseType = "arraybuffer";
    xhr.onreadystatechange = function(evt) {
      if (xhr.readyState == 4) {
        if (xhr.status >= 200 && xhr.status <= 299) {
          aResolve(xhr.response);
        } else {
          aReject(new Error("Error fetching file " + aURL + ", status " + xhr.status));
        }
      }
    };
    xhr.send();
  });
}

function setTimeoutZero() {
  return new Promise(function(aResolve, aReject) {
    setTimeout(aResolve, 0);
  });
}

function awaitRefresh() {
  function awaitOneRefresh() {
    return new Promise(function(aResolve, aReject) {
      requestAnimationFrame(aResolve);
    });
  }

  return awaitOneRefresh().then(awaitOneRefresh);
}

function runTest() {
  // Document and window from inside the display:none iframe.
  var nframe = document.getElementById("n");
  var ndocument = nframe.contentDocument;
  var nwindow = nframe.contentWindow;

  // Document and window from inside the visible iframe.
  var vframe = document.getElementById("v");
  var vdocument = vframe.contentDocument;
  var vwindow = vframe.contentWindow;

  // For iterating over different combinations of documents and windows
  // to test with.
  var sources = [
    { win: window,  doc: document,  what: "window/document"   },
    { win: vwindow, doc: vdocument, what: "vwindow/vdocument" },
    { win: nwindow, doc: ndocument, what: "nwindow/ndocument" },
    { win: window,  doc: vdocument, what: "window/vdocument"  },
    { win: window,  doc: ndocument, what: "window/ndocument"  },
    { win: vwindow, doc: document,  what: "vwindow/document"  },
    { win: vwindow, doc: ndocument, what: "vwindow/ndocument" },
    { win: nwindow, doc: document,  what: "nwindow/document"  },
    { win: nwindow, doc: vdocument, what: "nwindow/vdocument" },
  ];

  var sourceDocuments = [
    { doc: document,  what: "document"  },
    { doc: vdocument, what: "vdocument" },
    { doc: ndocument, what: "ndocument" },
  ];

  var sourceWindows = [
    { win: window,  what: "window"  },
    { win: vwindow, what: "vwindow" },
    { win: nwindow, what: "nwindow" },
  ];

  queue = queue.then(function() {

    // First, initialize fontData.
    return fetchAsArrayBuffer("BitPattern.woff")
             .then(function(aResult) { fontData = aResult; });

  }).then(function() {

    // (TEST 1) Some miscellaneous tests for FontFaceSet and FontFace.
    ok(window.FontFaceSet, "FontFaceSet interface object should be present (TEST 1)");
    is(Object.getPrototypeOf(FontFaceSet.prototype), EventTarget.prototype, "FontFaceSet should inherit from EventTarget (TEST 1)");
    ok(document.fonts instanceof FontFaceSet, "document.fonts should be a a FontFaceSet (TEST 1)");
    ok(window.FontFace, "FontFace interface object should be present (TEST 1)");
    is(Object.getPrototypeOf(FontFace.prototype), Object.prototype, "FontFace should inherit from Object (TEST 1)");

    // (TEST 2) Some miscellaneous tests for FontFaceSetLoadEvent.
    ok(window.FontFaceSetLoadEvent, "FontFaceSetLoadEvent interface object should be present (TEST 2)");
    is(Object.getPrototypeOf(FontFaceSetLoadEvent.prototype), Event.prototype, "FontFaceSetLoadEvent should inherit from Event (TEST 2)");

  }).then(function() {

    // (TEST 3) Test that document.fonts.ready starts out resolved with the
    // FontFaceSet.
    var p = Promise.resolve();
    sourceDocuments.forEach(function({ doc, what }) {
      p = p.then(function() {
        return is_resolved_with(doc.fonts.ready, doc.fonts, "initial value of document.fonts", "(TEST 3) (" + what + ")");
      });
    });
    return p;

  }).then(function() {

    // (TEST 4) Test that document.fonts in this test document starts out with no
    // FontFace objects in it.
    sourceDocuments.forEach(function({ doc, what }) {
      is(Array.from(doc.fonts).length, 0, "initial number of FontFace objects in document.fonts (TEST 4) (" + what + ")");
    });

    // (TEST 5) Test that document.fonts.status starts off as loaded.
    sourceDocuments.forEach(function({ doc, what }) {
      is(doc.fonts.status, "loaded", "initial value of document.fonts.status (TEST 5) (" + what + ")");
    });

    // (TEST 6) Test initial value of FontFace.status when a url() source is
    // used.
    sourceWindows.forEach(function({ win, what }) {
      is(new win.FontFace("test", "url(x)").status, "unloaded", "initial value of FontFace.status when a url() source is used (TEST 6) (" + what + ")");
    });

    // (TEST 7) Test initial value of FontFace.status when an invalid
    // ArrayBuffer source is used.  Because it has an implicit initial
    // load() call, it should either be "loading" if the browser is
    // asynchronously parsing the font data, or "error" if it parsed
    // it immediately.
    sourceWindows.forEach(function({ win, what }) {
      var status = new win.FontFace("test", new ArrayBuffer(0)).status;
      ok(status == "loading" || status == "error", "initial value of FontFace.status when an invalid ArrayBuffer source is used (TEST 7) (" + what + ")");
    });

    // (TEST 8) Test initial value of FontFace.status when a valid ArrayBuffer
    // source is used.  Because it has an implicit initial load() call, it
    // should either be "loading" if the browser is asynchronously parsing the
    // font data, or "loaded" if it parsed it immediately.
    sourceWindows.forEach(function({ win, what }) {
      status = new win.FontFace("test", fontData).status;
      ok(status == "loading" || status == "loaded", "initial value of FontFace.status when a valid ArrayBuffer source is used (TEST 8) (" + what + ")");
    });

    // (TEST 9) (old test became redundant with TEST 19)

  }).then(function() {

    // (TEST 10) Test initial value of FontFace.loaded when a valid url()
    // source is used.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        return is_pending(new win.FontFace("test", "url(x)").loaded, "initial value of FontFace.loaded when a valid url() source is used", "(TEST 10) (" + what + ")");
      });
    });
    return p;

  }).then(function() {

    // (TEST 11) (old test became redundant with TEST 21)

  }).then(function() {

    // (TEST 12) (old test became redundant with TEST 20)

  }).then(function() {

    // (TEST 13) Test initial values of the descriptor attributes on FontFace
    // objects.
    sourceWindows.forEach(function({ win, what }) {
      var face = new win.FontFace("test", fontData);
      // XXX Spec issue: what values do the descriptor attributes have before the
      // constructor's dictionary argument is parsed?
      for (var desc in defaultValues) {
        is(face[desc], defaultValues[desc], "initial value of FontFace." + desc + " (TEST 13) (" + what + ")");
      }
    });

    // (TEST 14) Test default values of the FontFaceDescriptors dictionary.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var face = new win.FontFace("test", fontData);
        return face.loaded.then(function() {
          for (var desc in defaultValues) {
            is(face[desc], defaultValues[desc], "default value of FontFace." + desc + " (TEST 14) (" + what + ")");
          }
        }, function(aError) {
          ok(false, "FontFace should have loaded succesfully (TEST 14) (" + what + ")");
        });
      });
    });
    return p;

  }).then(function() {

    // (TEST 15) Test passing non-default descriptor values to the FontFace
    // constructor.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var descriptorTests = Promise.resolve();
        Object.keys(nonDefaultValues).forEach(function(aDesc) {
          descriptorTests = descriptorTests.then(function() {
            var init = {};
            init[aDesc] = nonDefaultValues[aDesc][0];
            var face = new win.FontFace("test", fontData, init);
            var ok_todo = aDesc == "variant" ? todo : ok;
            ok_todo(face[aDesc] == nonDefaultValues[aDesc][1], "specified valid non-default value of FontFace." + aDesc + " immediately after construction (TEST 15) (" + what + ")");
            return face.loaded.then(function() {
              ok_todo(face[aDesc] == nonDefaultValues[aDesc][1], "specified valid non-default value of FontFace." + aDesc + " (TEST 15) (" + what + ")");
            }, function(aError) {
              ok(false, "FontFace should have loaded succesfully (TEST 15) (" + what + ")");
            });
          });
        });
        return descriptorTests;
      });
    });
    return p;

  }).then(function() {

    // (TEST 16) Test passing invalid descriptor values to the FontFace
    // constructor.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var descriptorTests = Promise.resolve();
        Object.keys(invalidValues).forEach(function(aDesc) {
          descriptorTests = descriptorTests.then(function() {
            var init = {};
            init[aDesc] = invalidValues[aDesc];
            var face = new win.FontFace("test", fontData, init);
            var ok_todo = aDesc == "variant" ? todo : ok;
            ok_todo(face.status == "error", "FontFace should be error immediately after construction with invalid value of FontFace." + aDesc + " (TEST 16) (" + what + ")");
            return face.loaded.then(function() {
              ok_todo(false, "FontFace should not load after invalid value of FontFace." + aDesc + " specified (TEST 16) (" + what + ")");
            }, function(aError) {
              ok(true, "FontFace should not load after invalid value of FontFace." + aDesc + " specified (TEST 16) (" + what + ")");
              is(aError.name, "SyntaxError", "FontFace.loaded with invalid value of FontFace." + aDesc + " should be rejected with a SyntaxError (TEST 16) (" + what + ")");
            });
          });
        });
        return descriptorTests;
      });
    });
    return p;

  }).then(function() {

    // (TEST 17) Test passing an invalid font family name to the FontFace
    // constructor.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var familyTests = Promise.resolve();
        invalidFontFamilyNames.forEach(function(aFamilyName) {
          familyTests = familyTests.then(function() {
            var face = new win.FontFace(aFamilyName, fontData);
            is(face.status, "error", "FontFace should be error immediately after construction with invalid family name " + aFamilyName + " (TEST 17) (" + what + ")");
            is(face.family, "", "FontFace.family should be the empty string after construction with invalid family name " + aFamilyName + " (TEST 17) (" + what + ")");
            return face.loaded.then(function() {
              ok(false, "FontFace should not load after invalid family name " + aFamilyName + " specified (TEST 17) (" + what + ")");
            }, function(aError) {
              ok(true, "FontFace should not load after invalid family name " + aFamilyName + " specified (TEST 17) (" + what + ")");
              is(aError.name, "SyntaxError", "FontFace.loaded with invalid family name " + aFamilyName + " should be rejected with a SyntaxError (TEST 17) (" + what + ")");
            });
          });
        });
        return familyTests;
      });
    });
    return p;

  }).then(function() {

    // XXX Disabled this sub-test due to intermittent failures (bug 1076803).
    return;

    // (TEST 18) Test passing valid url() source strings to the FontFace
    // constructor.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var srcTests = Promise.resolve();
        gCSSFontFaceDescriptors.src.values.forEach(function(aSrc) {
          srcTests = srcTests.then(function() {
            var face = new win.FontFace("test", aSrc);
            return face.load().then(function() {
              ok(true, "FontFace should load with valid url() src " + aSrc + " (TEST 18) (" + what + ")");
            }, function(aError) {
              is(aError.name, "NetworkError", "FontFace had NetworkError when loading with valid url() src " + aSrc + " (TEST 18) (" + what + ")");
            });
          });
        });
        return srcTests;
      });
    });
    return p;

  }).then(function() {

    // (TEST 19) Test passing invalid url() source strings to the FontFace
    // constructor.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var srcTests = Promise.resolve();
        gCSSFontFaceDescriptors.src.invalid_values.forEach(function(aSrc) {
          srcTests = srcTests.then(function() {
            var face = new win.FontFace("test", aSrc);
            is(face.status, "error", "FontFace.status should be \"error\" when constructed with an invalid url() src " + aSrc + " (TEST 19) (" + what + ")");
            return face.loaded.then(function() {
              ok(false, "FontFace should not load with invalid url() src " + aSrc + " (TEST 19) (" + what + ")");
            }, function(aError) {
              is(aError.name, "SyntaxError", "FontFace.ready should have been rejected with a SyntaxError when constructed with an invalid url() src " + aSrc + " (TEST 19) (" + what + ")");
            });
          });
        });
        return srcTests;
      });
    });
    return p;

  }).then(function() {

    // (TEST 20) Test that the status of a FontFace constructed with a valid
    // ArrayBuffer source eventually becomes "loaded".
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var face = new win.FontFace("test", fontData);
        return face.loaded.then(function(aFace) {
          is(face.status, "loaded", "status of FontFace constructed with a valid ArrayBuffer source should eventually be \"loaded\" (TEST 20) (" + what + ")");
          is(face, aFace, "FontFace.loaded was resolved with the FontFace object once loaded (TEST 20) (" + what + ")");
        }, function(aError) {
          ok(false, "FontFace constructed with a valid ArrayBuffer should eventually load (TEST 20) (" + what + ")");
        });
      });
    });
    return p;

  }).then(function() {

    // (TEST 21) Test that the status of a FontFace constructed with an invalid
    // ArrayBuffer source eventually becomes "error".
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var face = new win.FontFace("test", new ArrayBuffer(0));
        return face.loaded.then(function() {
          ok(false, "FontFace constructed with an invalid ArrayBuffer should not load (TEST 21) (" + what + ")");
        }, function(aError) {
          is(aError.name, "SyntaxError", "loaded of FontFace constructed with an invalid ArrayBuffer source should be rejected with TypeError (TEST 21) (" + what + ")");
          is(face.status, "error", "status of FontFace constructed with an invalid ArrayBuffer source should eventually be \"error\" (TEST 21) (" + what + ")");
        });
      });
    });
    return p;

  }).then(function() {

    // (TEST 22) Test assigning non-default descriptor values on the FontFace.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var descriptorTests = Promise.resolve();
        Object.keys(nonDefaultValues).forEach(function(aDesc) {
          descriptorTests = descriptorTests.then(function() {
            var face = new win.FontFace("test", fontData);
            return face.loaded.then(function() {
              var ok_todo = aDesc == "variant" ? todo : ok;
              face[aDesc] = nonDefaultValues[aDesc][0];
              ok_todo(face[aDesc] == nonDefaultValues[aDesc][1], "assigned valid non-default value to FontFace." + aDesc + " (TEST 22) (" + what + ")");
            }, function(aError) {
              ok(false, "FontFace should have loaded succesfully (TEST 22) (" + what + ")");
            });
          });
        });
        return descriptorTests;
      });
    });
    return p;

  }).then(function() {

    // (TEST 23) Test assigning invalid descriptor values on the FontFace.
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var descriptorTests = Promise.resolve();
        Object.keys(invalidValues).forEach(function(aDesc) {
          descriptorTests = descriptorTests.then(function() {
            var face = new win.FontFace("test", fontData);
            return face.loaded.then(function() {
              var ok_todo = aDesc == "variant" ? todo : ok;
              var exceptionName = "";
              try {
                face[aDesc] = invalidValues[aDesc];
              } catch (ex) {
                exceptionName = ex.name;
              }
              ok_todo(exceptionName == "SyntaxError", "assigning invalid value to FontFace." + aDesc + " should throw a SyntaxError (TEST 23) (" + what + ")");
            }, function(aError) {
              ok(false, "FontFace should have loaded succesfully (TEST 23) (" + what + ")");
            });
          });
        });
        return descriptorTests;
      });
    });
    return p;

  }).then(function() {

    // (TEST 24) Test that the status of a FontFace with a non-existing url()
    // source is set to "loading" right after load() is called, that its .loaded
    // Promise is returned, and that the Promise is eventually rejected with a
    // NetworkError and its status is set to "error".
    var p = Promise.resolve();
    sourceWindows.forEach(function({ win, what }) {
      p = p.then(function() {
        var face = new win.FontFace("test", "url(x)");
        var result = face.load();
        is(face.status, "loading", "FontFace.status should be \"loading\" right after load() is called (TEST 24) (" + what + ")");
        is(result, face.loaded, "FontFace.load() should return the .loaded Promise (TEST 24) (" + what + ")");

        return result.then(function() {
          ok(false, "FontFace with a non-existing url() source should not load (TEST 24) (" + what + ")");
        }, function(aError) {
          is(aError.name, "NetworkError", "FontFace with a non-existing url() source should result in its .loaded Promise being rejected with a NetworkError (TEST 24) (" + what + ")");
          is(face.status, "error", "FontFace with a non-existing url() source should result in its .status being set to \"error\" (TEST 24) (" + what + ")");
        });
      });
    });
    return p;

  }).then(function() {

    // (TEST 25) Test simple manipulation of the FontFaceSet.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        var face, face2, all;
        face = new win.FontFace("test", "url(x)");
        face2 = new win.FontFace("test2", "url(x)");
        ok(!doc.fonts.has(face), "newly created FontFace should not be in document.fonts (TEST 25) (" + what + ")");
        doc.fonts.add(face);
        ok(doc.fonts.has(face), "should be able to add a FontFace to document.fonts (TEST 25) (" + what + ")");
        doc.fonts.add(face);
        ok(doc.fonts.has(face), "should be able to repeatedly add a FontFace to document.fonts (TEST 25) (" + what + ")");
        ok(doc.fonts.delete(face), "FontFaceSet.delete should return true when it succeeds (TEST 25) (" + what + ")");
        ok(!doc.fonts.has(face), "FontFace should be gone from document.fonts after delete is called (TEST 25) (" + what + ")");
        ok(!doc.fonts.delete(face), "FontFaceSet.delete should return false when it fails (TEST 25) (" + what + ")");
        doc.fonts.add(face);
        doc.fonts.add(face2);
        ok(doc.fonts.has(face2), "should be able to add a second FontFace to document.fonts (TEST 25) (" + what + ")");
        doc.fonts.clear();
        ok(!doc.fonts.has(face) && !doc.fonts.has(face2), "FontFaces should be gone from document.fonts after clear is called (TEST 25) (" + what + ")");
        doc.fonts.add(face);
        doc.fonts.add(face2);
        all = Array.from(doc.fonts);
        is(all[0], face, "FontFaces should be returned in the same order as insertion (TEST 25) (" + what + ")");
        is(all[1], face2, "FontFaces should be returned in the same order as insertion (TEST 25) (" + what + ")");
        doc.fonts.add(face);
        all = Array.from(doc.fonts);
        is(all[0], face, "FontFaces should be not be reordered when a duplicate entry is added (TEST 25) (" + what + ")");
        is(all[1], face2, "FontFaces should be not be reordered when a duplicate entry is added (TEST 25) (" + what + ")");
        doc.fonts.clear();
        return doc.fonts.ready;
      });
    });
    return p;

  }).then(function() {

    // (TEST 26) Test that FontFaceSet.ready is replaced, .status is set to
    // "loading", and a loading event is dispatched when a loading FontFace is
    // added to it.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        var awaitEvents = new Promise(function(aResolve, aReject) {

          var onloadingTriggered = false, loadingDispatched = false;

          function check() {
            if (onloadingTriggered && loadingDispatched) {
              doc.fonts.onloading = null;
              doc.fonts.removeEventListener("loading", listener);
              aResolve();
            }
          }

          var listener = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.Event.prototype, "loading event should be a plain Event object (TEST 26) (" + what + ")");
            loadingDispatched = true;
            check();
          };
          doc.fonts.addEventListener("loading", listener);
          doc.fonts.onloading = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.Event.prototype, "loading event should be a plain Event object (TEST 26) (" + what + ")");
            onloadingTriggered = true;
            check();
          };
        });

        is(doc.fonts.status, "loaded", "FontFaceSet.status initially (TEST 26) (" + what + ")");

        var oldReady = doc.fonts.ready;
        var face = new win.FontFace("test", "url(neverending_font_load.sjs)");
        face.load();
        doc.fonts.add(face);

        var newReady = doc.fonts.ready;
        isnot(newReady, oldReady, "FontFaceSet.ready should be replaced when a loading FontFace is added to it (TEST 26) (" + what + ")");
        is(doc.fonts.status, "loading", "FontFaceSet.status should be set to \"loading\" when a loading FontFace is added to it (TEST 26) (" + what + ")");

        return awaitEvents
            .then(function() {
              return is_pending(newReady, "FontFaceSet.ready should be replaced with a fresh pending Promise when a loading FontFace is added to it", "(TEST 26) (" + what + ")");
            })
            .then(function() {
              doc.fonts.clear();
              return doc.fonts.ready;
            });
      });
    });
    return p;

  }).then(function() {

    // (TEST 27) Test that FontFaceSet.ready is resolved, .status is set to
    // "loaded", and a loadingdone event (but no loadingerror event) is
    // dispatched when the only loading FontFace in it is removed.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        var awaitEvents = new Promise(function(aResolve, aReject) {

          var onloadingdoneTriggered = false, loadingdoneDispatched = false;
          var onloadingerrorTriggered = false, loadingerrorDispatched = false;

          function check() {
            doc.fonts.onloadingdone = null;
            doc.fonts.onloadingerror = null;
            doc.fonts.removeEventListener("loadingdone", doneListener);
            doc.fonts.removeEventListener("loadingerror", errorListener);
            aResolve();
          }

          var doneListener = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.FontFaceSetLoadEvent.prototype, "loadingdone event should be a FontFaceSetLoadEvent object (TEST 27) (" + what + ")");
            is(aEvent.fontfaces.length, 0, "the FontFaceSetLoadEvent should have an empty fontfaces array (TEST 27) (" + what + ")");
            loadingdoneDispatched = true;
            check();
          };
          doc.fonts.addEventListener("loadingdone", doneListener);
          doc.fonts.onloadingdone = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.FontFaceSetLoadEvent.prototype, "loadingdone event should be a FontFaceSetLoadEvent object (TEST 27) (" + what + ")");
            is(aEvent.fontfaces.length, 0, "the FontFaceSetLoadEvent should have an empty fontfaces array (TEST 27) (" + what + ")");
            onloadingdoneTriggered = true;
            check();
          };
          var errorListener = function(aEvent) {
            loadingerrorDispatched = true;
            check();
          }
          doc.fonts.addEventListener("loadingerror", errorListener);
          doc.fonts.onloadingerror = function(aEvent) {
            onloadingdoneTriggered = true;
            check();
          };
        });

        is(doc.fonts.status, "loaded", "FontFaceSet.status should be \"loaded\" initially (TEST 27) (" + what + ")");

        var f = new win.FontFace("test", "url(neverending_font_load.sjs)");
        f.load();
        doc.fonts.add(f);

        is(doc.fonts.status, "loading", "FontFaceSet.status should be \"loading\" when a loading FontFace is in it (TEST 27) (" + what + ")");

        doc.fonts.clear();

        return awaitEvents
            .then(function() {
              return is_resolved_with(doc.fonts.ready, doc.fonts, "FontFaceSet.ready when the FontFaceSet is cleared", "(TEST 27) (" + what + ")");
            })
            .then(function() {
              is(doc.fonts.status, "loaded", "FontFaceSet.status should be set to \"loaded\" when it is cleared (TEST 27) (" + what + ")");
              return doc.fonts.ready;
            });
      });
    });
    return p;

  }).then(function() {

    // (TEST 28) Test that FontFaceSet.ready is replaced, .status is set to
    // "loading", and a loading event is dispatched when a FontFace in it
    // starts loading.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        var awaitEvents = new Promise(function(aResolve, aReject) {

          var onloadingTriggered = false, loadingDispatched = false;

          function check() {
            if (onloadingTriggered && loadingDispatched) {
              doc.fonts.onloading = null;
              doc.fonts.removeEventListener("loading", listener);
              aResolve();
            }
          }

          var listener = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.Event.prototype, "loading event should be a plain Event object (TEST 28) (" + what + ")");
            loadingDispatched = true;
            check();
          };
          doc.fonts.addEventListener("loading", listener);
          doc.fonts.onloading = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.Event.prototype, "loading event should be a plain Event object (TEST 28) (" + what + ")");
            onloadingTriggered = true;
            check();
          };
        });

        var oldReady = doc.fonts.ready;
        var face = new win.FontFace("test", "url(neverending_font_load.sjs)");
        doc.fonts.add(face);
        face.load();

        var newReady = doc.fonts.ready;
        isnot(newReady, oldReady, "FontFaceSet.ready should be replaced when its only FontFace starts loading (TEST 28) (" + what + ")");
        is(doc.fonts.status, "loading", "FontFaceSet.status should be set to \"loading\" when its only FontFace starts loading (TEST 28) (" + what + ")");

        return awaitEvents
            .then(function() {
              return is_pending(newReady, "FontFaceSet.ready when the FontFaceSet's only FontFace starts loading", "(TEST 28) (" + what + ")");
            })
            .then(function() {
              doc.fonts.clear();
              return doc.fonts.ready;
            });
      });
    });
    return p;

  }).then(function() {

    // (TEST 29) Test that a loadingdone and a loadingerror event is dispatched
    // when a FontFace that eventually becomes status "error" is added to the
    // FontFaceSet.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        var face;
        var awaitEvents = new Promise(function(aResolve, aReject) {

          var onloadingdoneTriggered = false, loadingdoneDispatched = false;
          var onloadingerrorTriggered = false, loadingerrorDispatched = false;

          function check() {
            if (onloadingdoneTriggered && loadingdoneDispatched &&
                onloadingerrorTriggered && loadingerrorDispatched) {
              doc.fonts.onloadingdone = null;
              doc.fonts.onloadingerror = null;
              doc.fonts.removeEventListener("loadingdone", doneListener);
              doc.fonts.removeEventListener("loadingerror", errorListener);
              aResolve();
            }
          }

          var doneListener = function(aEvent) {
            loadingdoneDispatched = true;
            check();
          };
          doc.fonts.addEventListener("loadingdone", doneListener);
          doc.fonts.onloadingdone = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.FontFaceSetLoadEvent.prototype, "loadingdone event should be a FontFaceSetLoadEvent object (TEST 29) (" + what + ")");
            is(aEvent.fontfaces.length, 0, "the FontFaceSetLoadEvent should have an empty fontfaces array (TEST 29) (" + what + ")");
            onloadingdoneTriggered = true;
            check();
          };
          var errorListener = function(aEvent) {
            is(Object.getPrototypeOf(aEvent), doc.defaultView.FontFaceSetLoadEvent.prototype, "loadingerror event should be a FontFaceSetLoadEvent object (TEST 29) (" + what + ")");
            is(aEvent.fontfaces[0], face, "the FontFaceSetLoadEvent should have a fontfaces array with the FontFace in it (TEST 29) (" + what + ")");
            loadingerrorDispatched = true;
            check();
          }
          doc.fonts.addEventListener("loadingerror", errorListener);
          doc.fonts.onloadingerror = function(aEvent) {
            onloadingerrorTriggered = true;
            check();
          };
        });

        face = new win.FontFace("test", "url(x)");
        face.load();
        is(face.status, "loading", "FontFace should have status \"loading\" (TEST 29) (" + what + ")");
        doc.fonts.add(face);

        return face.loaded
          .then(function() {
            ok(false, "the FontFace should not load (TEST 29) (" + what + ")");
          }, function(aError) {
            is(face.status, "error", "FontFace should have status \"error\" (TEST 29) (" + what + ")");
            return awaitEvents;
          })
          .then(function() {
            doc.fonts.clear();
            return doc.fonts.ready;
          });
      });
    });
    return p;

  }).then(function() {

    // (TEST 30) Test that a loadingdone event is dispatched when a FontFace
    // that eventually becomes status "loaded" is added to the FontFaceSet.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }, i) {
      p = p.then(function() {
        var face;
        var awaitEvents = new Promise(function(aResolve, aReject) {

          var onloadingdoneTriggered = false, loadingdoneDispatched = false;

          function check() {
            if (onloadingdoneTriggered && loadingdoneDispatched) {
              doc.fonts.onloadingdone = null;
              doc.fonts.removeEventListener("loadingdone", doneListener);
              aResolve();
            }
          }

          var doneListener = function(aEvent) {
            loadingdoneDispatched = true;
            check();
          };
          doc.fonts.addEventListener("loadingdone", doneListener);
          doc.fonts.onloadingdone = function(aEvent) {
            is(aEvent.fontfaces[0], face, "the FontFaceSetLoadEvent should have a fontfaces array with the FontFace in it (TEST 30) (" + what + ")");
            onloadingdoneTriggered = true;
            check();
          };
        });

        face = new win.FontFace("test", "url(BitPattern.woff?test30." + i + ")");
        face.load();
        is(face.status, "loading", "FontFace should have status \"loading\" (TEST 30) (" + what + ")");
        doc.fonts.add(face);

        return face.loaded
          .then(function() {
            is(face.status, "loaded", "FontFace should have status \"loaded\" (TEST 30) (" + what + ")");
            return awaitEvents;
          })
          .then(function() {
            doc.fonts.clear();
          });
      });
    });
    return p;

  }).then(function() {

    // (TEST 31) Test that a loadingdone event is dispatched when a FontFace
    // with status "unloaded" is added to the FontFaceSet and load() is called
    // on it.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }, i) {
      p = p.then(function() {
        var face;
        var awaitEvents = new Promise(function(aResolve, aReject) {

          var onloadingdoneTriggered = false, loadingdoneDispatched = false;

          function check() {
            if (onloadingdoneTriggered && loadingdoneDispatched) {
              doc.fonts.onloadingdone = null;
              doc.fonts.removeEventListener("loadingdone", doneListener);
              aResolve();
            }
          }

          var doneListener = function(aEvent) {
            loadingdoneDispatched = true;
            check();
          };
          doc.fonts.addEventListener("loadingdone", doneListener);
          doc.fonts.onloadingdone = function(aEvent) {
            is(aEvent.fontfaces[0], face, "the FontFaceSetLoadEvent should have a fontfaces array with the FontFace in it (TEST 31) (" + what + ")");
            onloadingdoneTriggered = true;
            check();
          };
        });

        face = new win.FontFace("test", "url(BitPattern.woff?test31." + i + ")");
        is(face.status, "unloaded", "FontFace should have status \"unloaded\" (TEST 31) (" + what + ")");
        doc.fonts.add(face);

        return face.load()
            .then(function() {
              return awaitEvents;
            })
            .then(function() {
              is(face.status, "loaded", "FontFace should have status \"loaded\" (TEST 31) (" + what + ")");
              doc.fonts.clear();
              return doc.fonts.ready;
            });
      });
    });
    return p;

  }).then(function() {

    // (TEST 32) Test that pending restyles prevent document.fonts.status
    // from becoming loaded.
    var face = new FontFace("test", "url(neverending_font_load.sjs)");
    face.load();
    document.fonts.add(face);

    is(document.fonts.status, "loading", "FontFaceSet.status after adding a loading FontFace (TEST 32)");

    document.fonts.clear();

    is(document.fonts.status, "loaded", "FontFaceSet.status after clearing (TEST 32)");

    document.fonts.add(face);

    is(document.fonts.status, "loading", "FontFaceSet.status after adding a loading FontFace again (TEST 32)");

    var div = document.querySelector("div");
    div.style.color = "blue";

    document.fonts.clear();
    is(document.fonts.status, "loading", "FontFaceSet.status after clearing but when there is a pending restyle (TEST 32)");

    return awaitRefresh()  // wait for a refresh driver tick
        .then(function() {
          is(document.fonts.status, "loaded", "FontFaceSet.status after clearing and the restyle has been flushed (TEST 32)");
          return document.fonts.ready;
        });

  }).then(function() {

    // (TEST 33) Test that CSS-connected FontFace objects are created
    // for @font-face rules in the document.

    is(document.fonts.status, "loaded", "document.fonts.status should initially be loaded (TEST 33)");

    var style = document.querySelector("style");
    var ruleText = "@font-face { font-family: something; src: url(x); ";
    Object.keys(nonDefaultValues).forEach(function(aDesc) {
      ruleText += descriptorNames[aDesc] + ": " + nonDefaultValues[aDesc][0] + "; ";
    });
    ruleText += "}";

    style.textContent = ruleText;

    var rule = style.sheet.cssRules[0];

    var all = Array.from(document.fonts);
    is(all.length, 1, "document.fonts should contain one FontFace (TEST 33)");

    var face = all[0];
    is(face.family, "\"something\"", "FontFace should have correct family value (TEST 33)");
    Object.keys(nonDefaultValues).forEach(function(aDesc) {
      var ok_todo = aDesc == "variant" ? todo : ok;
      ok_todo(face[aDesc] == nonDefaultValues[aDesc][1], "FontFace should have correct " + aDesc + " value (TEST 33)");
    });

    is(document.fonts.status, "loaded", "document.fonts.status should still be loaded (TEST 33)");
    is(face.status, "unloaded", "FontFace.status should be unloaded (TEST 33)");

    document.fonts.clear();
    ok(document.fonts.has(face), "CSS-connected FontFace should not be removed from document.fonts when clear is called (TEST 33)");

    is(document.fonts.delete(face), false, "attempting to remove CSS-connected FontFace from document.fonts should return false (TEST 33)");
    ok(document.fonts.has(face), "CSS-connected FontFace should not be removed from document.fonts when delete is called (TEST 33)");

    style.textContent = "";

    ok(!document.fonts.has(face), "CSS-connected FontFace should be removed from document.fonts once the rule has been removed (TEST 33)");

    is(document.fonts.status, "loaded", "document.fonts.status should still be loaded after rule is removed (TEST 33)");
    is(face.status, "unloaded", "FontFace.status should still be unloaded after rule is removed (TEST 33)");

    document.fonts.add(face);
    ok(document.fonts.has(face), "previously CSS-connected FontFace should be able to be added to document.fonts (TEST 33)");

    is(document.fonts.status, "loaded", "document.fonts.status should still be loaded after now disconnected FontFace is added (TEST 33)");
    is(face.status, "unloaded", "FontFace.status should still be unloaded after now disconnected FontFace is added (TEST 33)");

    document.fonts.delete(face);
    ok(!document.fonts.has(face), "previously CSS-connected FontFace should be able to be removed from document.fonts (TEST 33)");

  }).then(function() {

    // (TEST 34) Test that descriptor getters for unspecified descriptors on
    // CSS-connected FontFace objects return their default values.
    var style = document.querySelector("style");
    var ruleText = "@font-face { font-family: something; src: url(x); }";

    style.textContent = ruleText;

    var all = Array.from(document.fonts);
    var face = all[0];

    Object.keys(defaultValues).forEach(function(aDesc) {
      is(face[aDesc], defaultValues[aDesc], "FontFace should return default value for " + aDesc + " (TEST 34)");
    });

    style.textContent = "";

  }).then(function() {

    // (TEST 35) Test that no loadingdone event is dispatched when a FontFace
    // with "loaded" status is added to a "loaded" FontFaceSet.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        var gotLoadingDone = false;
        doc.fonts.onloadingdone = function(aEvent) {
          gotLoadingDone = true;
        };

        is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" (TEST 35) (" + what + ")");
        var face = new win.FontFace("test", fontData);

        return face.loaded
          .then(function() {
            is(face.status, "loaded", "FontFace should have status \"loaded\" (TEST 35) (" + what + ")");
            doc.fonts.add(face);
            is(doc.fonts.status, "loaded", "document.fonts.status should still have status \"loaded\" (TEST 35) (" + what + ")");
            return doc.fonts.ready;
          })
          .then(function() {
            ok(!gotLoadingDone, "loadingdone event should not be dispatched (TEST 35) (" + what + ")");
            doc.fonts.onloadingdone = null;
            doc.fonts.clear();
          });
      });
    });
    return p;

  }).then(function() {

    // (TEST 36) Test that no loadingdone or loadingerror event is dispatched
    // when a FontFace with "error" status is added to a "loaded" FontFaceSet.
    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      var doc = win.document;
      p = p.then(function() {
        var gotLoadingDone = false, gotLoadingError = false;
        doc.fonts.onloadingdone = function(aEvent) {
          gotLoadingDone = true;
        };
        doc.fonts.onloadingerror = function(aEvent) {
          gotLoadingError = true;
        };

        is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" (TEST 36) (" + what + ")");
        var face = new win.FontFace("test", new ArrayBuffer(0));

        return face.loaded
          .then(function() {
            ok(false, "FontFace should not have loaded (TEST 36) (" + what + ")");
          }, function() {
            is(face.status, "error", "FontFace should have status \"error\" (TEST 36) (" + what + ")");
            doc.fonts.add(face);
            is(doc.fonts.status, "loaded", "document.fonts.status should still have status \"loaded\" (TEST 36) (" + what + ")");
            return doc.fonts.ready;
          })
          .then(function() {
            ok(!gotLoadingDone, "loadingdone event should not be dispatched (TEST 36) (" + what + ")");
            ok(!gotLoadingError, "loadingerror event should not be dispatched (TEST 36) (" + what + ")");
            doc.fonts.onloadingdone = null;
            doc.fonts.onloadingerror = null;
            doc.fonts.clear();
          });
      });
    });
    return p;

  }).then(function() {

    // (TEST 37) Test that a FontFace only has one loadingdone event dispatched
    // at the FontFaceSet containing it.

    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what}, i) {
      p = p.then(function() {
        return setTimeoutZero();  // wait for any previous events to be dispatched
      }).then(function() {
        var events = [], face, face2;

        var awaitEvents = new Promise(function(aResolve, aReject) {
          doc.fonts.onloadingdone = doc.fonts.onloadingerror = function(e) {
            events.push(e);
            if (events.length == 2) {
              aResolve();
            }
          };
        });

        is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" (TEST 37) (" + what + ")");

        face = new win.FontFace("test", "url(BitPattern.woff?test37." + i + "a)");
        face.load();
        doc.fonts.add(face);
        is(doc.fonts.status, "loading", "document.fonts.status should have status \"loading\" after first font added (TEST 37) (" + what + ")");

        return doc.fonts.ready
          .then(function() {
            is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" after first font loaded (TEST 37) (" + what + ")");
            is(face.status, "loaded", "first FontFace should have status \"loaded\" (TEST 37) (" + what + ")");

            face2 = new win.FontFace("test2", "url(BitPattern.woff?test37." + i + "b)");
            face2.load();
            doc.fonts.add(face2);
            is(doc.fonts.status, "loading", "document.fonts.status should have status \"loading\" after second font added (TEST 37) (" + what + ")");

            return doc.fonts.ready;
          }).then(function() {
            return awaitEvents;
          }).then(function() {
            is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" after second font loaded (TEST 37) (" + what + ")");
            is(face2.status, "loaded", "second FontFace should have status \"loaded\" (TEST 37) (" + what + ")");

            is(events.length, 2, "should receive two events (TEST 37) (" + what + ")");

            is(events[0].type, "loadingdone", "first event should be \"loadingdone\" (TEST 37) (" + what + ")");
            is(events[0].fontfaces.length, 1, "first event should have 1 FontFace (TEST 37) (" + what + ")");
            is(events[0].fontfaces[0], face, "first event should have the first FontFace");

            is(events[1].type, "loadingdone", "second event should be \"loadingdone\" (TEST 37) (" + what + ")");
            is(events[1].fontfaces.length, 1, "second event should only have 1 FontFace (TEST 37) (" + what + ")");
            is(events[1].fontfaces[0], face2, "second event should have the second FontFace (TEST 37) (" + what + ")");

            doc.fonts.onloadingdone = null;
            doc.fonts.onloadingerror = null;
            doc.fonts.clear();
            return doc.fonts.ready;
          });
      });
    });
    return p;

  }).then(function() {

    // (TEST 38) Test that a FontFace only has one loadingerror event dispatched
    // at the FontFaceSet containing it.

    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        return setTimeoutZero();  // wait for any previous events to be dispatched
      }).then(function() {
        var events = [], face, face2;

        var awaitEvents = new Promise(function(aResolve, aReject) {
          doc.fonts.onloadingdone = doc.fonts.onloadingerror = function(e) {
            events.push(e);
            if (events.length == 4) {
              aResolve();
            }
          };
        });

        is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" (TEST 38) (" + what + ")");

        face = new win.FontFace("test", "url(x)");
        face.load();
        doc.fonts.add(face);
        is(doc.fonts.status, "loading", "document.fonts.status should have status \"loading\" after first font added (TEST 38) (" + what + ")");

        return doc.fonts.ready
          .then(function() {
            is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" after first font failed to load (TEST 38) (" + what + ")");
            is(face.status, "error", "first FontFace should have status \"error\" (TEST 38) (" + what + ")");

            face2 = new win.FontFace("test2", "url(x)");
            face2.load();
            doc.fonts.add(face2);
            is(doc.fonts.status, "loading", "document.fonts.status should have status \"loading\" after second font added (TEST 38) (" + what + ")");

            return doc.fonts.ready;
          }).then(function() {
            return awaitEvents;
          }).then(function() {
            is(doc.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" after second font failed to load (TEST 38) (" + what + ")");
            is(face2.status, "error", "second FontFace should have status \"error\" (TEST 38) (" + what + ")");

            is(events.length, 4, "should receive four events (TEST 38) (" + what + ")");

            is(events[0].type, "loadingdone", "first event should be \"loadingdone\" (TEST 38) (" + what + ")");
            is(events[0].fontfaces.length, 0, "first event should have no FontFaces (TEST 38) (" + what + ")");

            is(events[1].type, "loadingerror", "second event should be \"loadingerror\" (TEST 38) (" + what + ")");
            is(events[1].fontfaces.length, 1, "second event should have 1 FontFace (TEST 38) (" + what + ")");
            is(events[1].fontfaces[0], face, "second event should have the first FontFace");

            is(events[2].type, "loadingdone", "third event should be \"loadingdone\" (TEST 38) (" + what + ")");
            is(events[2].fontfaces.length, 0, "third event should have no FontFaces (TEST 38) (" + what + ")");

            is(events[3].type, "loadingerror", "third event should be \"loadingerror\" (TEST 38) (" + what + ")");
            is(events[3].fontfaces.length, 1, "third event should only have 1 FontFace (TEST 38) (" + what + ")");
            is(events[3].fontfaces[0], face2, "third event should have the second FontFace");

            doc.fonts.onloadingdone = null;
            doc.fonts.onloadingerror = null;
            doc.fonts.clear();
            return doc.fonts.ready;
          });
      });
    });
    return p;

  }).then(function() {

    // (TEST 39) Test that a FontFace for an @font-face rule only has one
    // loadingdone event dispatched at the FontFaceSet containing it.

    var style, all, events, awaitEvents;

    return setTimeoutZero()  // wait for any previous events to be dispatched
      .then(function() {
        style = document.querySelector("style");
        var ruleText = "@font-face { font-family: test; src: url(BitPattern.woff?test39a); } " +
                       "@font-face { font-family: test2; src: url(BitPattern.woff?test39b); }";

        style.textContent = ruleText;

        all = Array.from(document.fonts);
        events = [];

        awaitEvents = new Promise(function(aResolve, aReject) {
          document.fonts.onloadingdone = document.fonts.onloadingerror = function(e) {
            events.push(e);
            if (events.length == 2) {
              aResolve();
            }
          };
        });

        is(document.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" (TEST 39)");

        all[0].load();
        is(document.fonts.status, "loading", "document.fonts.status should have status \"loading\" after first font loading (TEST 39)");

        return document.fonts.ready
      }).then(function() {
        is(document.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" after first font loaded (TEST 39)");
        is(all[0].status, "loaded", "first FontFace should have status \"loaded\" (TEST 39)");
        is(all[1].status, "unloaded", "second FontFace should have status \"unloaded\" (TEST 39)");

        all[1].load();
        is(document.fonts.status, "loading", "document.fonts.status should have status \"loading\" after second font loading (TEST 39)");

        return document.fonts.ready;
      }).then(function() {
        return awaitEvents;
      }).then(function() {
        is(document.fonts.status, "loaded", "document.fonts.status should have status \"loaded\" after second font loaded (TEST 39)");
        is(all[1].status, "loaded", "second FontFace should have status \"loaded\" (TEST 39)");

        is(events.length, 2, "should receive two events (TEST 39)");

        is(events[0].type, "loadingdone", "first event should be \"loadingdone\" (TEST 39)");
        is(events[0].fontfaces.length, 1, "first event should have 1 FontFace (TEST 39)");
        is(events[0].fontfaces[0], all[0], "first event should have the first FontFace");

        is(events[1].type, "loadingdone", "second event should be \"loadingdone\" (TEST 39)");
        is(events[1].fontfaces.length, 1, "second event should only have 1 FontFace (TEST 39)");
        is(events[1].fontfaces[0], all[1], "second event should have the second FontFace (TEST 39)");

        style.textContent = "";

        document.fonts.onloadingdone = null;
        document.fonts.onloadingerror = null;
        document.fonts.clear();
        return document.fonts.ready;
      });

  }).then(function() {

    // (TEST 40) Test that an attempt to add the same FontFace object a second
    // time to a FontFaceSet (where one of the FontFace objects is reflecting
    // an @font-face rule) will be ignored.

    // First set up a @font-face rule.
    var style = document.querySelector("style");
    style.textContent = "@font-face { font-family: something; src: url(x); }";

    // Then add a couple of non-connected FontFace objects.
    var f1 = new FontFace("test1", "url(x)");
    var f2 = new FontFace("test2", "url(x)");

    document.fonts.add(f1);
    document.fonts.add(f2);

    var all = Array.from(document.fonts);
    var ruleFontFace = all[0];

    is(all.length, 3, "number of FontFace objects in the FontFaceSet before duplicate add (TEST 40)");
    is(all[1], f1, "first non-connected FontFace object in the FontFaceSet before duplicate add (TEST 40)");
    is(all[2], f2, "second non-connected FontFace object in the FontFaceSet before duplicate add (TEST 40)");

    document.fonts.add(f1);

    all = Array.from(document.fonts);
    is(all.length, 3, "number of FontFace objects in the FontFaceSet after duplicate add #1 (TEST 40)");
    is(all[0], ruleFontFace, "rule-based FontFace object in the FontFaceSEt after duplicate add #1 (TEST 40)");
    is(all[1], f1, "first non-connected FontFace object in the FontFaceSet after duplicate add #1 (TEST 40)");
    is(all[2], f2, "second non-connected FontFace object in the FontFaceSet after duplicate add #1 (TEST 40)");

    document.fonts.add(ruleFontFace);

    all = Array.from(document.fonts);
    is(all.length, 3, "number of FontFace objects in the FontFaceSet after duplicate add #2 (TEST 40)");
    is(all[0], ruleFontFace, "rule-based FontFace object in the FontFaceSEt after duplicate add #2 (TEST 40)");
    is(all[1], f1, "first non-connected FontFace object in the FontFaceSet after duplicate add #2 (TEST 40)");
    is(all[2], f2, "second non-connected FontFace object in the FontFaceSet after duplicate add #2 (TEST 40)");

    style.textContent = "";

    document.fonts.clear();

  }).then(function() {

    // (TEST 41) Test that an attempt to add the same FontFace object a second
    // time to a FontFaceSet (where none of the FontFace objects are reflecting
    // an @font-face rule) will be ignored.

    sources.forEach(function({ win, doc, what }) {
      // Add a couple of non-connected FontFace objects.
      var f1 = new win.FontFace("test1", "url(x)");
      var f2 = new win.FontFace("test2", "url(x)");

      doc.fonts.add(f1);
      doc.fonts.add(f2);

      var all = Array.from(doc.fonts);

      is(all.length, 2, "number of FontFace objects in the FontFaceSet before duplicate add (TEST 41) (" + what + ")");
      is(all[0], f1, "first non-connected FontFace object in the FontFaceSet before duplicate add (TEST 41) (" + what + ")");
      is(all[1], f2, "second non-connected FontFace object in the FontFaceSet before duplicate add (TEST 41) (" + what + ")");

      doc.fonts.add(f1);

      all = Array.from(doc.fonts);
      is(all.length, 2, "number of FontFace objects in the FontFaceSet after duplicate add (TEST 41) (" + what + ")");
      is(all[0], f1, "first non-connected FontFace object in the FontFaceSet after duplicate add (TEST 41) (" + what + ")");
      is(all[1], f2, "second non-connected FontFace object in the FontFaceSet after duplicate add (TEST 41) (" + what + ")");

      doc.fonts.clear();
    });

  }).then(function() {

    // (TEST 42) Test that adding a FontFace to multiple FontFaceSets and then
    // loading it updates the status of all FontFaceSets.

    var face = new FontFace("test", "url(x)");

    sourceDocuments.forEach(function({ doc, what }) {
      doc.fonts.add(face);
    });

    sourceDocuments.forEach(function({ doc, what }) {
      is(doc.fonts.status, "loaded", what + ".fonts.status before loading (TEST 42)");
    });

    face.load();

    sourceDocuments.forEach(function({ doc, what }) {
      is(doc.fonts.status, "loading", what + ".fonts.status after loading started (TEST 42)");
    });

    return Promise.all(sourceDocuments.map(function({ doc }) { return doc.fonts.ready; }))
      .then(function() {
        is(face.status, "error", "FontFace.status after loading finished (TEST 42)");
        sourceDocuments.forEach(function({ doc, what }) {
          is(doc.fonts.status, "loaded", what + ".fonts.status after loading finished (TEST 42)");
        });

        sourceDocuments.forEach(function({ doc, what }) {
          doc.fonts.clear();
        });
      });

  }).then(function() {

    // (TEST 43) Test the check method with platform fonts and some
    // degenerate cases.

    sourceDocuments.forEach(function({ doc, what }) {
      // Invalid font shorthands should throw a SyntaxError.
      try {
        doc.fonts.check("Helvetica");
        ok(false, "check should throw when a syntactically invalid font shorthand is given (TEST 43) (" + what + ")");
      } catch (ex) {
        is(ex.name, "SyntaxError", "exception name when check is called with a syntactically invalid font shorthand (TEST 43) (" + what + ")");
      }

      // System fonts should throw a SyntaxError.
      try {
        doc.fonts.check("caption");
        ok(false, "check should throw when a system font value is given (TEST 43) (" + what + ")");
      } catch (ex) {
        is(ex.name, "SyntaxError", "exception name when check is called with a system font value (TEST 43) (" + what + ")");
      }

      // CSS-wide keywords should throw a SyntaxError.
      try {
        doc.fonts.check("inherit");
        ok(false, "check should throw when a CSS-wide keyword is given (TEST 43) (" + what + ")");
      } catch (ex) {
        is(ex.name, "SyntaxError", "exception name when check is called with a CSS-wide keyword (TEST 43) (" + what + ")");
      }

      // CSS variables should throw a SyntaxError.
      try {
        doc.fonts.check("16px var(--family)");
        ok(false, "check should throw when CSS variables are used (TEST 43) (" + what + ")");
      } catch (ex) {
        is(ex.name, "SyntaxError", "exception name when check is called with CSS variables (TEST 43) (" + what + ")");
      }

      // No matching font family names => return true.
      is(doc.fonts.check("16px NonExistentFont1, NonExistentFont2"), true, "check return value when no matching font family names are used (TEST 43) (" + what + ")");

      // Matching platform font family name => return true.
      is(doc.fonts.check("16px NonExistentFont, " + likelyPlatformFonts), true, "check return value when a matching platform font family name is used (TEST 43) (" + what + ")");

      // Matching platform font family name, but using a different test
      // strings.  (Platform fonts always return true from check, regardless
      // of the actual glyphs present.)
      [
        { test: "\0",  desc: "a single non-matching glyph" },
        { test: "A\0", desc: "a matching and a non-matching glyph" },
        { test: "A",   desc: "a matching glyph" },
        { test: "AB",  desc: "multiple matching glyphs" }
      ].forEach(function({ test, desc }) {
        is(doc.fonts.check("16px " + likelyPlatformFonts, test), true, "check return value when a matching platform font family name is used but with " + desc + " (TEST 43) (" + what + ")");
      });

      // No matching font family name, but an empty test string.
      is(doc.fonts.check("16px NonExistentFont", ""), true, "check return value with a non-matching font family name and an empty test string (TEST 43) (" + what + ")");

      // Matching platform font family name, but empty test string.
      is(doc.fonts.check("16px " + likelyPlatformFonts, ""), true, "check return value with an empty test string (TEST 43) (" + what + ")");
    });

  }).then(function() {

    // (TEST 44) Test the check method with script-created FontFaces.

    var tests = [
      // at least one matching FontFace is not loaded ==> false
      { result: false, font: "16px Test", faces: [{ family: "Test", status: "unloaded" }] },
      { result: false, font: "16px Test", faces: [{ family: "SecondTest", status: "loaded" }, { family: "Test", status: "unloaded" }] },
      { result: false, font: "16px Test", faces: [{ family: "Test", status: "unloaded" }, { family: "Test", status: "loaded" }] },
      { result: false, font: "16px Test", faces: [{ family: "Test", status: "loading" }] },
      { result: false, font: "16px Test", faces: [{ family: "Test", status: "error" }] },
      { result: false, font: "16px Test", faces: [{ family: "Test", status: "unloaded", style: "italic" }] },
      { result: false, font: "bold 16px Test", faces: [{ family: "Test", status: "loaded", weight: "600" }, { family: "Test", status: "unloaded", weight: "bold" }] },
      { result: false, font: "16px Test, SecondTest", faces: [{ family: "Test", status: "loaded" }, { family: "SecondTest", status: "unloaded" }] },
      { result: false, font: "16px Test, " + likelyPlatformFonts + ", SecondTest, sans-serif", faces: [{ family: "Test", status: "loaded" }, { family: "SecondTest", status: "unloaded" }] },

      // all matching FontFaces are loaded ==> true
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded" }, { family: "Test", status: "loaded" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded" }, { family: "Test", status: "unloaded", unicodeRange: "U+4E0A" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded" }, { family: "Test", status: "error", unicodeRange: "U+4E0A" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded" }, { family: "Irrelevant", status: "unloaded" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", style: "italic" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", weight: "bold" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", stretch: "condensed" }] },
      { result: true,  font: "bold 16px Test", faces: [{ family: "Test", status: "loaded", weight: "bold" }, { family: "Test", status: "unloaded", weight: "600" }] },
      { result: true,  font: "16px Test, " + likelyPlatformFonts + ", SecondTest, sans-serif", faces: [{ family: "Test", status: "loaded" }, { family: "SecondTest", status: "loaded" }] },

      // no matching FontFaces at all ==> true
      { result: true,  font: "16px Test", faces: [] },
      { result: true,  font: "16px Test", faces: [{ family: "Irrelevant", status: "unloaded" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "unloaded", unicodeRange: "U+4E0A" }] },
      { result: true,  font: "16px Test, " + likelyPlatformFonts + ", SecondTest, sans-serif", faces: [{ family: "ThirdTest", status: "loaded" }] },

      // matching FontFace for one sample text character is loaded but
      // not the other ==> false
      { result: false, font: "16px Test", faces: [{ family: "Test", status: "loaded", unicodeRange: "U+61" }, { family: "Test", status: "unloaded", unicodeRange: "U+62" }] },

      // matching FontFaces for separate sample text characters are all
      // loaded ==> true
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", unicodeRange: "U+61" }, { family: "Test", status: "loaded", unicodeRange: "U+62" }] },
    ];

    sources.forEach(function({ win, doc, what }, i) {
      tests.forEach(function({ result, font, faces }, j) {
        faces.forEach(function(f, k) {
          var fontFace;
          if (f.status == "loaded") {
            fontFace = new win.FontFace(f.family, fontData, f);
          } else if (f.status == "error") {
            fontFace = new win.FontFace(f.family, new ArrayBuffer(0), f);
          } else {
            fontFace = new win.FontFace(f.family, "url(BitPattern.woff?test44." + [i, j, k] + ")", f);
            if (f.status == "loading") {
              fontFace.load();
            }
          }
          is(fontFace.status, f.status, "status of newly created FontFace " + [j, k] + " (TEST 44) (" + what + ")");
          doc.fonts.add(fontFace);
        });
        is(doc.fonts.check(font, "ab"), result, "check return value for subtest " + j + " (TEST 44) (" + what + ")");
        doc.fonts.clear();
      });
    });

  }).then(function() {

    // (TEST 45) Test the load method with platform fonts and some
    // degenerate cases.

    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }) {
      p = p.then(function() {
        // Invalid font shorthands should reject the promise with a SyntaxError.
        return doc.fonts.load("Helvetica").then(function() {
          ok(false, "load should reject when a syntactically invalid font shorthand is given (TEST 45) (" + what + ")");
        }, function(ex) {
          is(ex.name, "SyntaxError", "exception name when load is called with a syntactically invalid font shorthand (TEST 45) (" + what + ")");
        });
      });

      p = p.then(function() {
        // System fonts should reject with a SyntaxError.
        return doc.fonts.load("caption").then(function() {
          ok(false, "load should throw when a system font value is given (TEST 45) (" + what + ")");
        }, function(ex) {
          is(ex.name, "SyntaxError", "exception name when load is called with a system font value (TEST 45) (" + what + ")");
        });
      });

      p = p.then(function() {
        // CSS-wide keywords should reject with a SyntaxError.
        return doc.fonts.load("inherit").then(function() {
          ok(false, "load should throw when a CSS-wide keyword is given (TEST 45) (" + what + ")");
        }, function(ex) {
          is(ex.name, "SyntaxError", "exception name when load is called with a CSS-wide keyword (TEST 45) (" + what + ")");
        });
      });

      p = p.then(function() {
        // CSS variables should throw a SyntaxError.
        return doc.fonts.load("16px var(--family)").then(function() {
          ok(false, "load should throw when CSS variables are used (TEST 45) (" + what + ")");
        }, function(ex) {
          is(ex.name, "SyntaxError", "exception name when load is called with CSS variables (TEST 45) (" + what + ")");
        });
      });

      p = p.then(function() {
        // No matching font family names => return true.
        return doc.fonts.load("16px NonExistentFont1, NonExistentFont2").then(function(result) {
          is(result.length, 0, "load resolves with an emtpy array when no matching font family names are used (TEST 45) (" + what + ")");
        });
      });

      p = p.then(function() {
        // Matching platform font family name => return true.
        return doc.fonts.load("16px NonExistentFont1, " + likelyPlatformFonts).then(function(result) {
          is(result.length, 0, "load resolves with an emtpy array when no matching font family names are used (TEST 45) (" + what + ")");
        });
      });

      // Matching platform font family name, but using a different test
      // strings.  (Platform fonts always return true from load, regardless
      // of the actual glyphs present.)
      [
        { sample: "\0",  desc: "a single non-matching glyph" },
        { sample: "A\0", desc: "a matching and a non-matching glyph" },
        { sample: "A",   desc: "a matching glyph" },
        { sample: "AB",  desc: "multiple matching glyphs" }
      ].forEach(function({ sample, desc }) {
        p = p.then(function() {
          return doc.fonts.load("16px " + likelyPlatformFonts, sample).then(function(result) {
            is(result.length, 0, "load resolves with an empty array when a matching platform font family name is used but with " + desc + " (TEST 45) (" + what + ")");
          });
        });
      });

      p = p.then(function() {
        // No matching font family name, but an empty test string.
        return doc.fonts.load("16px NonExistentFont", "").then(function(result) {
          is(result.length, 0, "load resolves with an empty array when a non-matching platform font family name and an empty test string is used (TEST 45) (" + what + ")");
        });
      });

      p = p.then(function() {
        // Matching font family name, but an empty test string.
        return doc.fonts.load("16px " + likelyPlatformFonts, "").then(function(result) {
          is(result.length, 0, "load resolves with an empty array when a matching platform font family name and an empty test string is used (TEST 45) (" + what + ")");
        });
      });
    });
    return p;

  }).then(function() {

    // (TEST 46) Test the load method with script-created FontFaces.

    var tests = [
      // at least one matching FontFace is not yet loaded, but will load ==> resolve
      { result: true, font: "16px Test", faces: [{ family: "Test", status: "unloaded", included: true }] },
      { result: true, font: "16px Test", faces: [{ family: "SecondTest", status: "loaded" }, { family: "Test", status: "unloaded", included: true }] },
      { result: true, font: "16px Test", faces: [{ family: "Test", status: "unloaded", included: true }, { family: "Test", status: "loaded", included: true }] },
      { result: true, font: "16px Test", faces: [{ family: "Test", status: "loading", included: true }] },
      { result: true, font: "16px Test", faces: [{ family: "Test", status: "unloaded", style: "italic", included: true }] },
      { result: true, font: "bold 16px Test", faces: [{ family: "Test", status: "unloaded", weight: "600", included: true }] },
      { result: true, font: "bold 16px Test", faces: [{ family: "Test", status: "unloaded", weight: "600" }, { family: "Test", status: "unloaded", weight: "bold", included: true }] },
      { result: true, font: "16px Test, SecondTest", faces: [{ family: "Test", status: "loaded", included: true }, { family: "SecondTest", status: "unloaded", included: true }] },
      { result: true, font: "16px Test, " + likelyPlatformFonts + ", SecondTest, sans-serif", faces: [{ family: "Test", status: "loaded", included: true }, { family: "SecondTest", status: "unloaded", included: true }] },

      // at least one matching FontFace is in an error state ==> reject
      { result: false, font: "16px Test", faces: [{ family: "Test", status: "unloaded" }, { family: "Test", status: "error" }] },

      // all matching FontFaces are already loaded ==> resolve
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", included: true }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", included: true }, { family: "Test", status: "loaded", included: true }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", included: true }, { family: "Test", status: "unloaded", unicodeRange: "U+4E0A" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", included: true }, { family: "Test", status: "error", unicodeRange: "U+4E0A" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", included: true }, { family: "Irrelevant", status: "unloaded" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", style: "italic", included: true }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", weight: "bold", included: true }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", stretch: "condensed", included: true }] },
      { result: true,  font: "bold 16px Test", faces: [{ family: "Test", status: "loaded", weight: "bold", included: true }, { family: "Test", status: "loaded", weight: "600" }] },
      { result: true,  font: "bold 16px Test", faces: [{ family: "Test", status: "loaded", weight: "600" }, { family: "Test", status: "loaded", weight: "bold", included: true }] },
      { result: true,  font: "16px Test, " + likelyPlatformFonts + ", SecondTest, sans-serif", faces: [{ family: "Test", status: "loaded", included: true }, { family: "SecondTest", status: "loaded", included: true }] },

      // no matching FontFaces at all ==> resolve
      { result: true,  font: "16px Test", faces: [] },
      { result: true,  font: "16px Test", faces: [{ family: "Irrelevant", status: "unloaded" }] },
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "unloaded", unicodeRange: "U+4E0A" }] },
      { result: true,  font: "16px Test, " + likelyPlatformFonts + ", SecondTest, sans-serif", faces: [{ family: "ThirdTest", status: "loaded" }] },

      // matching FontFace for one sample text character is already loaded but
      // the other is not (but will) ==> resolve
      { result: true, font: "16px Test", faces: [{ family: "Test", status: "loaded", unicodeRange: "U+61", included: true }, { family: "Test", status: "unloaded", unicodeRange: "U+62", included: true }] },

      // matching FontFaces for separate sample text characters are all
      // loaded ==> resolve
      { result: true,  font: "16px Test", faces: [{ family: "Test", status: "loaded", unicodeRange: "U+61", included: true }, { family: "Test", status: "loaded", unicodeRange: "U+62", included: true }] },
    ];

    var p = Promise.resolve();
    sources.forEach(function({ win, doc, what }, i) {
      tests.forEach(function({ result, font, faces }, j) {
        p = p.then(function() {
          var fontFaces = [];
          faces.forEach(function(f, k) {
            var fontFace;
            if (f.status == "loaded") {
              fontFace = new win.FontFace(f.family, fontData, f);
            } else if (f.status == "error") {
              fontFace = new win.FontFace(f.family, new ArrayBuffer(0), f);
            } else {
              fontFace = new win.FontFace(f.family, "url(BitPattern.woff?test46." + [i, j, k] + ")", f);
              if (f.status == "loading") {
                fontFace.load();
              }
            }
            is(fontFace.status, f.status, "status of newly created FontFace " + [j, k] + " (TEST 46) (" + what + ")");
            doc.fonts.add(fontFace);
            fontFaces.push(fontFace);
          });
          return doc.fonts.load(font, "ab").then(function(array) {
            ok(result, "load should resolve for subtest " + j + " (TEST 46) (" + what + ")");
            var expected = [];
            for (var k = 0; k < faces.length; k++) {
              if (faces[k].included) {
                expected.push(fontFaces[k]);
              }
            }
            is(array.length, expected.length, "length of array load resolves with for subtest " + j + " (TEST 46) (" + what + ")");
            for (var k = 0; k < array.length; k++) {
              is(array[k], expected[k], "value in array[" + k + "] load resolves with for subtest " + j + " (TEST 46) (" + what + ")");
            }
          }, function(ex) {
            ok(!result, "load should not resolve for subtest " + j + " (TEST 46) (" + what + ")");
            is(ex.name, "SyntaxError", "exception load's return value is rejected with for subtest " + j + " (TEST 46) (" + what + ")");
          }).then(function() {
            doc.fonts.clear();
          });
        });
      });
    });
    return p;

  }).then(function() {

    // (TEST 47) Test that CSS-connected FontFaces can't be added to other
    // FontFaceSets.

    var style = document.querySelector("style");
    style.textContent = "@font-face { font-family: something; src: url(x); }";

    var rule = style.sheet.cssRules[0];

    var all = Array.from(document.fonts);
    is(all.length, 1, "document.fonts should contain one FontFace (TEST 47)");

    var face = all[0];

    sourceDocuments.forEach(function({ doc, what }) {
      if (doc == document) {
        return;
      }

      var exceptionName;
      try {
        doc.fonts.add(face);
        ok(false, "add should throw when attempting to add a CSS-connected FontFace to another FontFaceSet (TEST 47) (" + what + ")");
      } catch (ex) {
        is(ex.name, "InvalidModificationError", "exception name when add is called with a CSS-connected FontFace from another FontFaceSet (TEST 47) (" + what + ")");
      }
    });

    style.textContent = "";
    document.body.offsetTop;

    sourceDocuments.forEach(function({ doc, what }) {
      if (doc == document) {
        return;
      }

      ok(!doc.fonts.has(face), "FontFaceSet initially doesn't have the FontFace (TEST 47) (" + what + ")");
      doc.fonts.add(face);
      ok(doc.fonts.has(face), "add should allow a previously CSS-connected FontFace to be added to another FontFaceSet (TEST 47) (" + what + ")");
      doc.fonts.clear();
    });

    document.fonts.clear();

  }).then(function() {

    // (TEST 48) Test that FontFaceSets that hold a combination of FontFaces
    // from different documents expose the right set of FontFaces.

    // Expected FontFaceSet contents.
    var expected = {
      document: [],
      vdocument: [],
      ndocument: [],
    };

    // Create a CSS-connected FontFace in the top-level document.
    var style = document.querySelector("style");
    style.textContent = "@font-face { font-family: something; src: url(x); }";

    var all = Array.from(document.fonts);
    is(all.length, 1, "document.fonts should contain one FontFace (TEST 48)");

    all[0]._description = "CSS-connected in document";
    expected.document.push(all[0]);

    // Create a CSS-connected FontFace in the visible iframe.
    var vstyle = vdocument.querySelector("style");
    vstyle.textContent = "@font-face { font-family: somethingelse; src: url(x); }";

    all = Array.from(vdocument.fonts);
    all[0]._description = "CSS-connected in vdocument";
    is(all.length, 1, "vdocument.fonts should contain one FontFace (TEST 48)");

    expected.vdocument.push(all[0]);

    // Create a FontFace in each window and add it to each document's FontFaceSet.
    var i = 0;
    var faces = [];
    sourceWindows.forEach(function({ win, what: whatWin }) {
      var f = new win.FontFace("test" + ++i, "url(x)");
      sourceDocuments.forEach(function({ doc, what: whatDoc }) {
        doc.fonts.add(f);
        expected[whatDoc].push(f);
        f._description = whatWin + "/" + whatDoc;
      });
    });

    sourceDocuments.forEach(function({ doc, what }) {
      var all = Array.from(doc.fonts);
      is(expected[what].length, all.length, "expected FontFaceSet size (TEST 48) (" + what + ")");
      for (var i = 0; i < expected[what].length; i++) {
        is(expected[what][i], all[i], "expected FontFace (" + expected[what][i]._description + ") at index " + i + " (TEST 48) (" + what + ")");
      }
    });

    vstyle.textContent = "";
    style.textContent = "";

    sourceDocuments.forEach(function({ doc }) { doc.fonts.clear(); });

  }).then(function() {

    // (TEST LAST) Test that a pending style sheet load prevents
    // document.fonts.status from being set to "loaded".

    // First, add a FontFace to document.fonts that will load soon.
    var face = new FontFace("test", "url(BitPattern.woff?testlast)");
    face.load();
    document.fonts.add(face);

    // Next, add a style sheet reference.
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.href = "neverending_stylesheet_load.sjs";
    link.type = "text/css";
    document.head.appendChild(link);

    return setTimeoutZero()  // wait for the style sheet to start loading
        .then(function() {
          document.fonts.clear();
          is(document.fonts.status, "loading", "FontFaceSet.status when the FontFaceSet has been cleared of loading FontFaces but there is a pending style sheet load (TEST LAST)");
          document.head.removeChild(link);
          // XXX Removing the <link> element won't cancel the load of the
          // style sheet, so we can't do that to test that
          // document.fonts.ready is resolved once there are no more
          // loading style sheets.
        });

    // NOTE: It is important that this style sheet test comes last in the file,
    // as the neverending style sheet load will interfere with subsequent
    // sub-tests.

  }).then(function() {

    // End of the tests.
    SimpleTest.finish();

  }, function(aError) {

    // Something failed.
    ok(false, "Something failed: " + aError);
    SimpleTest.finish();

  });
}

function start() {
  if (SpecialPowers.getBoolPref("layout.css.font-loading-api.enabled")) {
    SpecialPowers.pushPrefEnv({ set: [["layout.css.font-display.enabled", true]] },
                              runTest);
  } else {
    ok(true, "CSS Font Loading API is not enabled.");
  }
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestLongerTimeout(5);

</script>

<style></style>
<div></div>