summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/test/browser/browser_treeView.js
blob: bf1eeda35aa6e5f3b623042afe0101b04db2431a (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
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

let tabmail = document.getElementById("tabmail");
registerCleanupFunction(() => {
  tabmail.closeOtherTabs(tabmail.tabInfo[0]);
});

// We wish to run several variants of each test with minor differences, based on
// changes in the source file, in order to verify that certain variables don't
// impact behavior.
const TEST_VARIANTS = ["header", "no-header"];

/**
 * Run a given test in a new tab and script sandbox.
 *
 * @param {Function} test - The test function to run in the sandbox.
 * @param {string} filenameFragment - The fragment of the filename representing
 *   the test variant to run.
 * @param {object[]} sandboxArgs - Arguments to the sandbox spawner to pass to
 *   the test function.
 */
async function runTestInSandbox(test, filenameFragment, sandboxArgs = []) {
  // Create a new tab with our custom content.
  const tab = tabmail.openTab("contentTab", {
    url: `chrome://mochitests/content/browser/comm/mail/base/test/browser/files/tree-element-test-${filenameFragment}.xhtml`,
  });

  await BrowserTestUtils.browserLoaded(tab.browser);
  tab.browser.focus();

  // Spawn a new JavaScript sandbox for the tab and run the test function inside
  // of it.
  await SpecialPowers.spawn(tab.browser, sandboxArgs, test);

  tabmail.closeTab(tab);
}

/**
 * Checks that interactions with the widget do as expected.
 */
add_task(async function testKeyboardAndMouse() {
  for (const variant of TEST_VARIANTS) {
    info(`Running keyboard and mouse test for ${variant}`);
    await runTestInSandbox(subtestKeyboardAndMouse, variant, [variant]);
  }
});

async function subtestKeyboardAndMouse(variant) {
  let doc = content.document;

  let list = doc.getElementById("testTree");
  Assert.ok(!!list, "the list exists");

  async function doListActionAndWaitForRowBuffer(actionFn) {
    // Filling the row buffer is fiddly timing, so provide an event to indicate
    // that actions which may trigger changes in the row buffer have finished.
    const eventName = "_treerowbufferfill";
    list._rowBufferReadyEvent = new content.CustomEvent(eventName);

    const promise = new Promise(resolve =>
      list.addEventListener(eventName, resolve, { once: true })
    );

    await actionFn();
    await promise;

    list._rowBufferReadyEvent = null;
  }

  async function scrollListToPosition(topOfScroll) {
    await doListActionAndWaitForRowBuffer(() => {
      list.scrollTo(0, topOfScroll);
    });
  }

  Assert.equal(list._rowElementName, "test-row");
  Assert.equal(list._rowElementClass, content.customElements.get("test-row"));
  Assert.equal(
    list._toleranceSize,
    26,
    "list should have tolerance twice the number of visible rows"
  );

  // We should be scrolled to the top already, but this will ensure we get an
  // event fire one way or another.
  await scrollListToPosition(0);

  let rows = list.querySelectorAll(`tr[is="test-row"]`);
  // Count is calculated from the height of `list` divided by
  // TestCardRow.ROW_HEIGHT, plus list._toleranceSize.
  Assert.equal(rows.length, 13 + 26, "the list has the right number of rows");

  Assert.equal(doc.activeElement, doc.body);

  // Verify the tab order of list elements by tabbing both forward and backward
  // through them.
  EventUtils.synthesizeKey("VK_TAB", {}, content);
  Assert.equal(
    doc.activeElement.id,
    "before",
    "the element before the list should have focus"
  );

  if (variant == "header") {
    // Tab order changes slightly if the table has a header versus if it
    // doesn't, so we need to account for that variation.
    EventUtils.synthesizeKey("VK_TAB", {}, content);
    Assert.equal(
      doc.activeElement.id,
      "testColButton",
      "the list header button should have focus"
    );
  }

  EventUtils.synthesizeKey("VK_TAB", {}, content);
  Assert.equal(doc.activeElement.id, "testBody", "the list should have focus");

  EventUtils.synthesizeKey("VK_TAB", {}, content);
  Assert.equal(
    doc.activeElement.id,
    "after",
    "the element after the list should have focus"
  );

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, content);
  Assert.equal(doc.activeElement.id, "testBody", "the list should have focus");

  if (variant == "header") {
    // Tab order changes slightly if the table has a header versus if it
    // doesn't, so we need to account for that variation.
    EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, content);
    Assert.equal(
      doc.activeElement.id,
      "testColButton",
      "the list header button should have focus"
    );
  }

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, content);
  Assert.equal(
    doc.activeElement.id,
    "before",
    "the element before the list should have focus"
  );

  // Check initial selection.

  let selectHandler = {
    seenEvent: null,
    currentAtEvent: null,
    selectedAtEvent: null,
    t0: Date.now(),
    time: 0,

    reset() {
      this.seenEvent = null;
      this.currentAtEvent = null;
      this.selectedAtEvent = null;
      this.t0 = Date.now();
    },
    handleEvent(event) {
      this.seenEvent = event;
      this.currentAtEvent = list.currentIndex;
      this.selectedAtEvent = list.selectedIndices;
      this.time = Date.now() - this.t0;
    },
  };

  /**
   * Check if the spacerTop TBODY of the TreeViewTable is properly allocating
   * the height of non existing rows.
   *
   * @param {int} rows - The number of rows that the spacerTop should be
   * simulating their height allocation.
   */
  function checkTopSpacerHeight(rows) {
    let table = doc.querySelector(`[is="tree-view-table"]`);
    // -26 to account for the tolerance buffer.
    Assert.equal(
      table.spacerTop.clientHeight,
      list.getRowAtIndex(rows).clientHeight * (rows - 26),
      "The top spacer has the correct height"
    );
  }

  function checkCurrent(expectedIndex) {
    Assert.equal(list.currentIndex, expectedIndex, "currentIndex is correct");
    if (selectHandler.currentAtEvent !== null) {
      Assert.equal(
        selectHandler.currentAtEvent,
        expectedIndex,
        "currentIndex was correct at the last 'select' event"
      );
    }

    let current = list.querySelectorAll(".current");
    if (expectedIndex == -1) {
      Assert.equal(current.length, 0, "no rows have the 'current' class");
    } else {
      Assert.equal(current.length, 1, "only one row has the 'current' class");
      Assert.equal(
        current[0].index,
        expectedIndex,
        "correct row has the 'current' class"
      );
    }
  }

  function checkSelected(...expectedIndices) {
    Assert.deepEqual(
      list.selectedIndices,
      expectedIndices,
      "selectedIndices are correct"
    );

    if (selectHandler.selectedAtEvent !== null) {
      // Check the value was already set when the select event fired.
      Assert.deepEqual(
        selectHandler.selectedAtEvent,
        expectedIndices,
        "selectedIndices were correct at the last 'select' event"
      );
    }

    let selected = [...list.querySelectorAll(".selected")].map(
      row => row.index
    );
    expectedIndices.sort((a, b) => a - b);
    Assert.deepEqual(
      selected,
      expectedIndices,
      "correct rows have the 'selected' class"
    );
  }

  checkCurrent(0);
  checkSelected(0);

  // Click on some individual rows.

  const { TestUtils } = ChromeUtils.importESModule(
    "resource://testing-common/TestUtils.sys.mjs"
  );
  const { AppConstants } = ChromeUtils.importESModule(
    "resource://gre/modules/AppConstants.sys.mjs"
  );

  async function clickOnRow(index, modifiers = {}, expectEvent = true) {
    if (modifiers.shiftKey) {
      info(`clicking on row ${index} with shift key`);
    } else if (modifiers.accelKey) {
      info(`clicking on row ${index} with ctrl key`);
    } else {
      info(`clicking on row ${index}`);
    }

    let x = list.clientWidth / 2;
    let y = list.table.header.clientHeight + index * 50 + 25;

    selectHandler.reset();
    list.addEventListener("select", selectHandler, { once: true });
    EventUtils.synthesizeMouse(list, x, y, modifiers, content);
    await TestUtils.waitForCondition(
      () => !!selectHandler.seenEvent == expectEvent,
      `'select' event should ${expectEvent ? "" : "not "}get fired`
    );
  }

  await clickOnRow(0, {}, false);
  checkCurrent(0);
  checkSelected(0);

  await clickOnRow(1);
  checkCurrent(1);
  checkSelected(1);

  await clickOnRow(2);
  checkCurrent(2);
  checkSelected(2);

  // Select multiple rows by shift-clicking.

  await clickOnRow(4, { shiftKey: true });
  checkCurrent(4);
  checkSelected(2, 3, 4);

  // Holding ctrl and shift should always produce a range selection.
  await clickOnRow(6, { accelKey: true, shiftKey: true });
  checkCurrent(6);
  checkSelected(2, 3, 4, 5, 6);

  await clickOnRow(0, { shiftKey: true });
  checkCurrent(0);
  checkSelected(0, 1, 2);

  await clickOnRow(2, { shiftKey: true });
  checkCurrent(2);
  checkSelected(2);

  // Select multiple rows by ctrl-clicking.

  await clickOnRow(5, { accelKey: true });
  checkCurrent(5);
  checkSelected(2, 5);

  await clickOnRow(1, { accelKey: true });
  checkCurrent(1);
  checkSelected(1, 2, 5);

  await clickOnRow(5, { accelKey: true });
  checkCurrent(5);
  checkSelected(1, 2);

  await clickOnRow(1, { accelKey: true });
  checkCurrent(1);
  checkSelected(2);

  await clickOnRow(2, { accelKey: true });
  checkCurrent(2);
  checkSelected();

  // Move around by pressing keys.

  async function pressKey(key, modifiers = {}, expectEvent = true) {
    if (modifiers.shiftKey) {
      info(`pressing ${key} with shift key`);
    } else if (modifiers.accelKey) {
      info(`pressing ${key} with accel key`);
    } else {
      info(`pressing ${key}`);
    }

    selectHandler.reset();
    list.addEventListener("select", selectHandler, { once: true });
    EventUtils.synthesizeKey(key, modifiers, content);
    await TestUtils.waitForCondition(
      () => !!selectHandler.seenEvent == expectEvent,
      `'select' event should ${expectEvent ? "" : "not "}get fired`
    );
    // We don't enforce any delay on multiselection.
    let multiselect =
      (AppConstants.platform == "macosx" && key == " ") ||
      modifiers.shiftKey ||
      modifiers.accelKey;
    if (expectEvent && !multiselect) {
      // We have data-select-delay="250" in treeView.xhtml
      Assert.greater(selectHandler.time, 240, "should select only after delay");
    }
  }

  await pressKey("VK_UP");
  checkCurrent(1);
  checkSelected(1);

  await pressKey("VK_UP", { accelKey: true }, false);
  checkCurrent(0);
  checkSelected(1);

  // Without Ctrl selection moves with focus again.
  await pressKey("VK_UP");
  checkCurrent(0);
  checkSelected(0);

  // Does nothing.
  await pressKey("VK_UP", {}, false);
  checkCurrent(0);
  checkSelected(0);

  await pressKey("VK_DOWN", { accelKey: true }, false);
  checkCurrent(1);
  checkSelected(0);

  await pressKey("VK_DOWN", { accelKey: true }, false);
  checkCurrent(2);
  checkSelected(0);

  // Multi select with only Space on macOS on a focused row, since Cmd+Space is
  // captured by the OS.
  if (AppConstants.platform == "macosx") {
    await pressKey(" ");
  } else {
    // Multi select with Ctrl+Space for Windows and Linux.
    await pressKey(" ", { accelKey: true });
  }
  checkCurrent(2);
  checkSelected(0, 2);

  await pressKey("VK_DOWN", { accelKey: true }, false);
  checkCurrent(3);
  checkSelected(0, 2);

  await pressKey("VK_DOWN", { accelKey: true }, false);
  checkCurrent(4);
  checkSelected(0, 2);

  if (AppConstants.platform == "macosx") {
    await pressKey(" ");
  } else {
    await pressKey(" ", { accelKey: true });
  }
  checkCurrent(4);
  checkSelected(0, 2, 4);

  // Single selection restored with normal navigation.
  await pressKey("VK_UP");
  checkCurrent(3);
  checkSelected(3);

  // We don't allow unselecting a selected row with Space on macOS due to
  // conflict with the `mail.advance_on_spacebar` pref.
  if (AppConstants.platform != "macosx") {
    // Can select none using Ctrl+Space.
    await pressKey(" ", { accelKey: true });
    checkCurrent(3);
    checkSelected();
  }

  await pressKey("VK_DOWN");
  checkCurrent(4);
  checkSelected(4);

  await pressKey("VK_HOME", { accelKey: true }, false);
  checkCurrent(0);
  checkSelected(4);

  if (AppConstants.platform == "macosx") {
    // We can't clear the selection with only Space on macOS, simulate a Arrow
    // Up to force clear selection and only select the top most row.
    await pressKey("VK_UP");
  } else {
    // Select only the current item with Space (no modifier).
    await pressKey(" ");
  }
  checkCurrent(0);
  checkSelected(0);

  // The list is 630px high, so rows 0-12 are fully or partly visible.

  await doListActionAndWaitForRowBuffer(async () => {
    await pressKey("VK_PAGE_DOWN");
  });
  checkCurrent(13);
  checkSelected(13);
  Assert.equal(
    list.getFirstVisibleIndex(),
    1,
    "should have scrolled down a page"
  );

  await doListActionAndWaitForRowBuffer(async () => {
    await pressKey("VK_PAGE_UP", { shiftKey: true });
  });
  checkCurrent(0);
  checkSelected(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
  Assert.equal(
    list.getFirstVisibleIndex(),
    0,
    "should have scrolled up a page"
  );

  // Shrink shift selection.
  await pressKey("VK_DOWN", { shiftKey: true });
  checkCurrent(1);
  checkSelected(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);

  await pressKey("VK_DOWN", { accelKey: true }, false);
  checkCurrent(2);
  checkSelected(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);

  await pressKey("VK_DOWN", { accelKey: true }, false);
  checkCurrent(3);
  checkSelected(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);

  if (AppConstants.platform == "macosx") {
    // We don't allow unselecting a selected row with Space on macOS due to
    // conflict with the `mail.advance_on_spacebar` pref, so simulate a
    // CMD+click to unselect it.
    await clickOnRow(3, { accelKey: true });
  } else {
    // Break the shift sequence by Ctrl+Space.
    await pressKey(" ", { accelKey: true });
  }
  checkCurrent(3);
  checkSelected(1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);

  await pressKey("VK_DOWN", { shiftKey: true });
  checkCurrent(4);
  checkSelected(3, 4);

  // Reverse selection direction.
  await pressKey("VK_HOME", { shiftKey: true });
  checkCurrent(0);
  checkSelected(0, 1, 2, 3);

  // Now rows 138-149 are fully visible.

  await doListActionAndWaitForRowBuffer(async () => {
    await pressKey("VK_END");
  });
  checkCurrent(149);
  checkSelected(149);
  Assert.equal(
    list.getFirstVisibleIndex(),
    137,
    "should have scrolled to the end"
  );
  checkTopSpacerHeight(137);

  // Does nothing.
  await pressKey("VK_DOWN", {}, false);
  checkCurrent(149);
  checkSelected(149);
  Assert.equal(
    list.getFirstVisibleIndex(),
    137,
    "should not have changed view"
  );
  checkTopSpacerHeight(137);

  await doListActionAndWaitForRowBuffer(async () => {
    await pressKey("VK_PAGE_UP");
  });
  checkCurrent(136);
  checkSelected(136);
  Assert.equal(
    list.getFirstVisibleIndex(),
    136,
    "should have scrolled up a page"
  );

  await doListActionAndWaitForRowBuffer(async () => {
    await pressKey("VK_PAGE_DOWN", { shiftKey: true });
  });
  checkCurrent(149);
  checkSelected(
    136,
    137,
    138,
    139,
    140,
    141,
    142,
    143,
    144,
    145,
    146,
    147,
    148,
    149
  );
  Assert.equal(
    list.getFirstVisibleIndex(),
    137,
    "should have scrolled down a page"
  );
  checkTopSpacerHeight(137);

  await doListActionAndWaitForRowBuffer(async () => {
    await pressKey("VK_HOME");
  });
  checkCurrent(0);
  checkSelected(0);
  Assert.equal(
    list.getFirstVisibleIndex(),
    0,
    "should have scrolled to the beginning"
  );

  // Scroll around. Which rows are current and selected should be remembered
  // even if the row element itself disappears.

  selectHandler.reset();
  await scrollListToPosition(125);
  checkCurrent(0);
  checkSelected(0);
  Assert.equal(
    list.getFirstVisibleIndex(),
    2,
    "getFirstVisibleIndex is correct"
  );

  await scrollListToPosition(2525);
  Assert.equal(list.currentIndex, 0, "currentIndex is still set");
  Assert.ok(
    !list.querySelector(".current"),
    "no visible rows have the 'current' class"
  );
  Assert.deepEqual(list.selectedIndices, [0], "selectedIndices are still set");
  Assert.ok(
    !list.querySelector(".selected"),
    "no visible rows have the 'selected' class"
  );
  Assert.equal(
    list.getFirstVisibleIndex(),
    50,
    "getFirstVisibleIndex is correct"
  );
  Assert.ok(!selectHandler.seenEvent, "should not have fired 'select' event");
  checkTopSpacerHeight(50);

  await doListActionAndWaitForRowBuffer(async () => {
    await pressKey("VK_DOWN");
  });
  checkCurrent(1);
  checkSelected(1);
  Assert.equal(
    list.getFirstVisibleIndex(),
    1,
    "should have scrolled so that the second row is in view"
  );

  selectHandler.reset();
  await scrollListToPosition(0);
  checkCurrent(1);
  checkSelected(1);
  Assert.equal(
    list.getFirstVisibleIndex(),
    0,
    "getFirstVisibleIndex is correct"
  );
  Assert.ok(
    !selectHandler.seenEvent,
    "'select' event did not fire as expected"
  );

  await pressKey("VK_UP");
  checkCurrent(0);
  checkSelected(0);
  Assert.equal(
    list.getFirstVisibleIndex(),
    0,
    "should have scrolled so that the first row is in view"
  );

  // Some literal edge cases. Clicking on a partially visible row should
  // scroll it into view.

  // Calculate the visible area in order to verify that rows appear where they
  // are intended to appear.
  const listRect = list.getBoundingClientRect();
  const headerHeight = list.table.header.clientHeight;

  const visibleRect = new content.DOMRect(
    listRect.x,
    listRect.y + headerHeight,
    listRect.width,
    listRect.height - headerHeight
  );

  Assert.equal(visibleRect.height, 630, "the table body should be 630px tall");

  rows = list.querySelectorAll(`tr[is="test-row"]`);
  let bcr = rows[12].getBoundingClientRect();
  Assert.less(
    Math.round(bcr.top),
    Math.round(visibleRect.bottom),
    "top of row 12 is visible"
  );
  Assert.greater(
    Math.round(bcr.bottom),
    Math.round(visibleRect.bottom),
    "bottom of row 12 is not visible"
  );

  await doListActionAndWaitForRowBuffer(async () => {
    await clickOnRow(12);
  });
  rows = list.querySelectorAll(`tr[is="test-row"]`);
  bcr = rows[12].getBoundingClientRect();
  Assert.less(
    Math.round(bcr.top),
    Math.round(visibleRect.bottom),
    "the top of row 12 should be visible"
  );
  Assert.equal(
    Math.round(bcr.bottom),
    Math.round(visibleRect.bottom),
    "row 12 should be at the bottom of the visible area"
  );

  bcr = rows[0].getBoundingClientRect();
  Assert.less(
    Math.round(bcr.top),
    Math.round(visibleRect.top),
    "top of row 0 is not visible"
  );
  Assert.greater(
    Math.round(bcr.bottom),
    Math.round(visibleRect.top),
    "bottom of row 0 is visible"
  );

  await doListActionAndWaitForRowBuffer(async () => {
    await clickOnRow(0);
  });
  rows = list.querySelectorAll(`tr[is="test-row"]`);
  bcr = rows[0].getBoundingClientRect();
  Assert.equal(
    Math.round(bcr.top),
    Math.round(visibleRect.top),
    "row 0 should be at the top of the visible area"
  );
  Assert.greater(
    Math.round(bcr.bottom),
    Math.round(visibleRect.top),
    "the bottom of row 0 should be visible"
  );
}

/**
 * Checks that changes in the view are propagated to the list.
 */
add_task(async function testRowCountChange() {
  for (const variant of TEST_VARIANTS) {
    info(`Running row count change test for ${variant}`);
    await runTestInSandbox(subtestRowCountChange, variant);
  }
});

async function subtestRowCountChange() {
  let doc = content.document;

  let ROW_HEIGHT = 50;
  let list = doc.getElementById("testTree");

  async function doListActionAndWaitForRowBuffer(actionFn) {
    // Filling the row buffer is fiddly timing, so provide an event to indicate
    // that actions which may trigger changes in the row buffer have finished.
    const eventName = "_treerowbufferfill";
    list._rowBufferReadyEvent = new content.CustomEvent(eventName);

    const promise = new Promise(resolve =>
      list.addEventListener(eventName, resolve, { once: true })
    );

    await actionFn();
    await promise;

    list._rowBufferReadyEvent = null;
  }

  let view = list.view;
  let rows;

  // Check the initial state.

  function checkRows(first, last) {
    let expectedIndices = [];
    for (let i = first; i <= last; i++) {
      expectedIndices.push(i);
    }
    rows = list.querySelectorAll(`tr[is="test-row"]`);
    Assert.deepEqual(
      Array.from(rows, r => r.index),
      expectedIndices,
      "the list has the right rows"
    );
    Assert.deepEqual(
      Array.from(rows, r => r.dataset.value),
      view.values.slice(first, last + 1),
      "the list has the right rows"
    );
  }

  function checkSelected(indices, existingIndices) {
    Assert.deepEqual(list.selectedIndices, indices);
    let selectedRows = list.querySelectorAll(`tr[is="test-row"].selected`);
    Assert.deepEqual(
      Array.from(selectedRows, r => r.index),
      existingIndices
    );
  }

  let expectedCount = 150;

  // Select every tenth row. We'll check what is selected remains selected.

  list.selectedIndices = [4, 14, 24, 34, 44];

  function getRowsHeight() {
    return list.scrollHeight - list.table.header.clientHeight;
  }

  async function addValues(index, values) {
    view.values.splice(index, 0, ...values);
    info(`Added ${values.join(", ")} at ${index}`);
    info(view.values);

    expectedCount += values.length;
    Assert.equal(
      view.rowCount,
      expectedCount,
      "the view has the right number of rows"
    );

    await doListActionAndWaitForRowBuffer(() => {
      list.rowCountChanged(index, values.length);
    });

    Assert.equal(
      getRowsHeight(),
      expectedCount * ROW_HEIGHT,
      "space for all rows is allocated"
    );
  }

  async function removeValues(index, count, expectedRemoved) {
    let values = view.values.splice(index, count);
    info(`Removed ${values.join(", ")} from ${index}`);
    info(view.values);

    Assert.deepEqual(values, expectedRemoved);

    expectedCount -= values.length;
    Assert.equal(
      view.rowCount,
      expectedCount,
      "the view has the right number of rows"
    );

    await doListActionAndWaitForRowBuffer(() => {
      list.rowCountChanged(index, -count);
    });

    Assert.equal(
      getRowsHeight(),
      expectedCount * ROW_HEIGHT,
      "space for all rows is allocated"
    );
  }

  async function scrollListToPosition(topOfScroll) {
    await doListActionAndWaitForRowBuffer(() => {
      list.scrollTo(0, topOfScroll);
    });
  }

  Assert.equal(
    view.rowCount,
    expectedCount,
    "the view has the right number of rows"
  );
  Assert.equal(list.scrollTop, 0, "the list is scrolled to the top");
  Assert.equal(
    getRowsHeight(),
    expectedCount * ROW_HEIGHT,
    "space for all rows is allocated"
  );

  // We should be scrolled to the top already, but this will ensure we get an
  // event fire one way or another.
  await scrollListToPosition(0);

  checkRows(0, 38);
  checkSelected([4, 14, 24, 34, 44], [4, 14, 24, 34]);
  Assert.equal(getRowsHeight(), 150 * 50);

  // Add a value at the end. Only the scroll height should change.

  await addValues(150, [150]);
  checkRows(0, 38);
  checkSelected([4, 14, 24, 34, 44], [4, 14, 24, 34]);
  Assert.equal(getRowsHeight(), 151 * 50);

  // Add more values at the end. Only the scroll height should change.

  await addValues(151, [151, 152, 153]);
  checkRows(0, 38);
  checkSelected([4, 14, 24, 34, 44], [4, 14, 24, 34]);
  Assert.equal(getRowsHeight(), 154 * 50);

  // Add values between the last row and the end.
  // Only the scroll height should change.

  await addValues(40, ["39a", "39b"]);
  checkRows(0, 38);
  checkSelected([4, 14, 24, 34, 46], [4, 14, 24, 34]);
  Assert.equal(getRowsHeight(), 156 * 50);

  // Add values between the last visible row and the last row.
  // The changed rows and those below them should be updated.

  await addValues(18, ["17a", "17b", "17c"]);
  checkRows(0, 38);
  // Hard-coded sanity checks to prove checkRows is working as intended.
  Assert.equal(rows[17].dataset.value, "17");
  Assert.equal(rows[18].dataset.value, "17a");
  Assert.equal(rows[19].dataset.value, "17b");
  Assert.equal(rows[20].dataset.value, "17c");
  Assert.equal(rows[21].dataset.value, "18");
  checkSelected([4, 14, 27, 37, 49], [4, 14, 27, 37]);
  Assert.equal(getRowsHeight(), 159 * 50);

  // Add values in the visible rows.
  // The changed rows and those below them should be updated.

  await addValues(8, ["7a", "7b"]);
  checkRows(0, 38);
  Assert.equal(rows[7].dataset.value, "7");
  Assert.equal(rows[8].dataset.value, "7a");
  Assert.equal(rows[9].dataset.value, "7b");
  Assert.equal(rows[10].dataset.value, "8");
  Assert.equal(rows[22].dataset.value, "17c");
  checkSelected([4, 16, 29, 39, 51], [4, 16, 29]);
  Assert.equal(getRowsHeight(), 161 * 50);

  // Add a value at the start. All rows should be updated.

  await addValues(0, [-1]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-1");
  Assert.equal(rows[1].dataset.value, "0");
  Assert.equal(rows[22].dataset.value, "17b");
  checkSelected([5, 17, 30, 40, 52], [5, 17, 30]);
  Assert.equal(getRowsHeight(), 162 * 50);

  // Add more values at the start. All rows should be updated.

  await addValues(0, [-3, -2]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-3");
  Assert.equal(rows[1].dataset.value, "-2");
  Assert.equal(rows[2].dataset.value, "-1");
  Assert.equal(rows[22].dataset.value, "17");
  checkSelected([7, 19, 32, 42, 54], [7, 19, 32]);
  Assert.equal(getRowsHeight(), 164 * 50);

  Assert.equal(list.scrollTop, 0, "the list is still scrolled to the top");

  // Remove values in the order we added them.

  await removeValues(160, 1, [150]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-3");
  Assert.equal(rows[22].dataset.value, "17");
  checkSelected([7, 19, 32, 42, 54], [7, 19, 32]);
  Assert.equal(getRowsHeight(), 163 * 50);

  await removeValues(160, 3, [151, 152, 153]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-3");
  Assert.equal(rows[22].dataset.value, "17");
  checkSelected([7, 19, 32, 42, 54], [7, 19, 32]);
  Assert.equal(getRowsHeight(), 160 * 50);

  await removeValues(48, 2, ["39a", "39b"]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-3");
  Assert.equal(rows[22].dataset.value, "17");
  checkSelected([7, 19, 32, 42, 52], [7, 19, 32]);
  Assert.equal(getRowsHeight(), 158 * 50);

  await removeValues(23, 3, ["17a", "17b", "17c"]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-3");
  Assert.equal(rows[22].dataset.value, "17");
  checkSelected([7, 19, 29, 39, 49], [7, 19, 29]);
  Assert.equal(getRowsHeight(), 155 * 50);

  await removeValues(11, 2, ["7a", "7b"]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-3");
  Assert.equal(rows[10].dataset.value, "7");
  Assert.equal(rows[11].dataset.value, "8");
  Assert.equal(rows[22].dataset.value, "19");
  checkSelected([7, 17, 27, 37, 47], [7, 17, 27, 37]);
  Assert.equal(getRowsHeight(), 153 * 50);

  await removeValues(2, 1, [-1]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "-3");
  Assert.equal(rows[1].dataset.value, "-2");
  Assert.equal(rows[2].dataset.value, "0");
  Assert.equal(rows[22].dataset.value, "20");
  checkSelected([6, 16, 26, 36, 46], [6, 16, 26, 36]);
  Assert.equal(getRowsHeight(), 152 * 50);

  await removeValues(0, 2, [-3, -2]);
  checkRows(0, 38);
  Assert.equal(rows[0].dataset.value, "0");
  Assert.equal(rows[1].dataset.value, "1");
  Assert.equal(rows[22].dataset.value, "22");
  checkSelected([4, 14, 24, 34, 44], [4, 14, 24, 34]);
  Assert.equal(getRowsHeight(), 150 * 50);

  Assert.equal(list.scrollTop, 0, "the list is still scrolled to the top");

  // Now scroll to the middle and repeat.

  await scrollListToPosition(1735);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "8");
  Assert.equal(rows[65].dataset.value, "73");
  checkSelected([4, 14, 24, 34, 44], [14, 24, 34, 44]);

  await addValues(150, [150]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "8");
  Assert.equal(rows[65].dataset.value, "73");
  checkSelected([4, 14, 24, 34, 44], [14, 24, 34, 44]);

  await addValues(38, ["37a"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "8");
  Assert.equal(rows[29].dataset.value, "37");
  Assert.equal(rows[30].dataset.value, "37a");
  Assert.equal(rows[31].dataset.value, "38");
  Assert.equal(rows[65].dataset.value, "72");
  checkSelected([4, 14, 24, 34, 45], [14, 24, 34, 45]);

  await addValues(25, ["24a"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "8");
  Assert.equal(rows[16].dataset.value, "24");
  Assert.equal(rows[17].dataset.value, "24a");
  Assert.equal(rows[18].dataset.value, "25");
  Assert.equal(rows[65].dataset.value, "71");
  checkSelected([4, 14, 24, 35, 46], [14, 24, 35, 46]);

  await addValues(11, ["10a"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "8");
  Assert.equal(rows[2].dataset.value, "10");
  Assert.equal(rows[3].dataset.value, "10a");
  Assert.equal(rows[4].dataset.value, "11");
  Assert.equal(rows[65].dataset.value, "70");
  checkSelected([4, 15, 25, 36, 47], [15, 25, 36, 47]);

  await addValues(0, ["-1"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "7");
  Assert.equal(rows[65].dataset.value, "69");
  checkSelected([5, 16, 26, 37, 48], [16, 26, 37, 48]);

  Assert.equal(
    list.scrollTop,
    1735,
    "the list is still scrolled to the middle"
  );

  await removeValues(154, 1, [150]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "7");
  Assert.equal(rows[65].dataset.value, "69");
  checkSelected([5, 16, 26, 37, 48], [16, 26, 37, 48]);

  await removeValues(41, 1, ["37a"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "7");
  Assert.equal(rows[65].dataset.value, "70");
  checkSelected([5, 16, 26, 37, 47], [16, 26, 37, 47]);

  await removeValues(27, 1, ["24a"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "7");
  Assert.equal(rows[65].dataset.value, "71");
  checkSelected([5, 16, 26, 36, 46], [16, 26, 36, 46]);

  await removeValues(12, 1, ["10a"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "7");
  Assert.equal(rows[65].dataset.value, "72");
  checkSelected([5, 15, 25, 35, 45], [15, 25, 35, 45]);

  await removeValues(0, 1, ["-1"]);
  checkRows(8, 73);
  Assert.equal(rows[0].dataset.value, "8");
  Assert.equal(rows[65].dataset.value, "73");
  checkSelected([4, 14, 24, 34, 44], [14, 24, 34, 44]);

  Assert.equal(
    list.scrollTop,
    1735,
    "the list is still scrolled to the middle"
  );

  // Now scroll to the bottom and repeat.

  await scrollListToPosition(6870);
  checkRows(111, 149);
  Assert.equal(rows[0].dataset.value, "111");
  Assert.equal(rows[38].dataset.value, "149");
  checkSelected([4, 14, 24, 34, 44], []);

  await addValues(50, [50]);
  checkRows(111, 150);
  Assert.equal(rows[0].dataset.value, "110");
  Assert.equal(rows[39].dataset.value, "149");
  checkSelected([4, 14, 24, 34, 44], []);

  await addValues(49, ["48a"]);
  checkRows(111, 151);
  Assert.equal(rows[0].dataset.value, "109");
  Assert.equal(rows[40].dataset.value, "149");
  checkSelected([4, 14, 24, 34, 44], []);

  await addValues(30, ["29a"]);
  checkRows(111, 152);
  Assert.equal(rows[0].dataset.value, "108");
  Assert.equal(rows[41].dataset.value, "149");
  checkSelected([4, 14, 24, 35, 45], []);

  await addValues(0, ["-1"]);
  checkRows(111, 153);
  Assert.equal(rows[0].dataset.value, "107");
  Assert.equal(rows[42].dataset.value, "149");
  checkSelected([5, 15, 25, 36, 46], []);

  Assert.equal(
    list.scrollTop,
    6870,
    "the list is still scrolled to the bottom"
  );

  await removeValues(53, 1, [50]);
  checkRows(111, 152);
  Assert.equal(rows[0].dataset.value, "108");
  Assert.equal(rows[41].dataset.value, "149");
  checkSelected([5, 15, 25, 36, 46], []);

  await removeValues(51, 1, ["48a"]);
  checkRows(111, 151);
  Assert.equal(rows[0].dataset.value, "109");
  Assert.equal(rows[40].dataset.value, "149");
  checkSelected([5, 15, 25, 36, 46], []);

  await removeValues(31, 1, ["29a"]);
  checkRows(111, 150);
  Assert.equal(rows[0].dataset.value, "110");
  Assert.equal(rows[39].dataset.value, "149");
  checkSelected([5, 15, 25, 35, 45], []);

  await removeValues(0, 1, ["-1"]);
  checkRows(111, 149);
  Assert.equal(rows[0].dataset.value, "111");
  Assert.equal(rows[38].dataset.value, "149");
  checkSelected([4, 14, 24, 34, 44], []);

  Assert.equal(
    list.scrollTop,
    6870,
    "the list is still scrolled to the bottom"
  );

  // Remove a selected row and check the selection changes.

  await scrollListToPosition(0);

  checkSelected([4, 14, 24, 34, 44], [4, 14, 24, 34]);

  await removeValues(3, 3, [3, 4, 5]); // 4 is selected.
  checkSelected([11, 21, 31, 41], [11, 21, 31]);

  await addValues(3, [3, 4, 5]);
  checkSelected([14, 24, 34, 44], [14, 24, 34]);

  // Remove some consecutive selected rows.

  list.selectedIndices = [6, 7, 8, 9];
  checkSelected([6, 7, 8, 9], [6, 7, 8, 9]);

  await removeValues(7, 1, [7]);
  checkSelected([6, 7, 8], [6, 7, 8]);

  await removeValues(7, 1, [8]);
  checkSelected([6, 7], [6, 7]);

  await removeValues(7, 1, [9]);
  checkSelected([6], [6]);

  // Reset the list.

  await addValues(7, [7, 8, 9]);
  list.selectedIndex = -1;
}

/**
 * Checks that expanding and collapsing works. Twisties in the test file are
 * styled as coloured squares: red for collapsed, green for expanded.
 *
 * @note This is practically the same test as in browser_treeListbox.js, but
 * for TreeView instead of TreeListbox. If you make changes here you
 * may want to make changes there too.
 */
add_task(async function testExpandCollapse() {
  await runTestInSandbox(subtestExpandCollapse, "levels");
});

async function subtestExpandCollapse() {
  let doc = content.document;
  let list = doc.getElementById("testTree");
  let allIds = [
    "row-1",
    "row-2",
    "row-2-1",
    "row-2-2",
    "row-3",
    "row-3-1",
    "row-3-1-1",
    "row-3-1-2",
  ];
  let idsWithoutChildren = [
    "row-1",
    "row-2-1",
    "row-2-2",
    "row-3-1-1",
    "row-3-1-2",
  ];

  let listener = {
    reset() {
      this.collapsedIndex = null;
      this.expandedIndex = null;
    },
    handleEvent(event) {
      if (event.type == "collapsed") {
        this.collapsedIndex = event.detail;
      } else if (event.type == "expanded") {
        this.expandedIndex = event.detail;
      }
    },
  };
  list.addEventListener("collapsed", listener);
  list.addEventListener("expanded", listener);

  let selectHandler = {
    seenEvent: null,
    selectedAtEvent: null,

    reset() {
      this.seenEvent = null;
      this.selectedAtEvent = null;
    },
    handleEvent(event) {
      this.seenEvent = event;
      this.selectedAtEvent = list.selectedIndex;
    },
  };

  Assert.equal(
    list.querySelectorAll("collapsed").length,
    0,
    "no rows are collapsed"
  );
  Assert.equal(list.view.rowCount, 8, "row count");
  Assert.deepEqual(
    Array.from(list.table.body.children, r => r.id),
    [
      "row-1",
      "row-2",
      "row-2-1",
      "row-2-2",
      "row-3",
      "row-3-1",
      "row-3-1-1",
      "row-3-1-2",
    ],
    "rows property"
  );

  function checkCurrent(expectedIndex) {
    Assert.equal(list.currentIndex, expectedIndex, "currentIndex is correct");
    const current = list.querySelectorAll(".current");
    if (expectedIndex == -1) {
      Assert.equal(current.length, 0, "no rows have the 'current' class");
    } else {
      Assert.equal(current.length, 1, "only one row has the 'current' class");
      Assert.equal(
        current[0].index,
        expectedIndex,
        "correct row has the 'current' class"
      );
    }
  }

  function checkMultiSelect(...expectedIds) {
    let selected = [...list.querySelectorAll(".selected")].map(row => row.id);
    Assert.deepEqual(selected, expectedIds, "selection should be correct");
  }

  function checkSelectedAndCurrent(expectedIndex, expectedId) {
    Assert.equal(list.selectedIndex, expectedIndex, "selectedIndex is correct");
    let selected = [...list.querySelectorAll(".selected")].map(row => row.id);
    Assert.deepEqual(
      selected,
      [expectedId],
      "correct rows have the 'selected' class"
    );
    checkCurrent(expectedIndex);
  }

  list.selectedIndex = 0;
  checkSelectedAndCurrent(0, "row-1");

  // Click the twisties of rows without children.

  function performChange(id, expectedChange, changeCallback) {
    listener.reset();
    let row = doc.getElementById(id);
    let before = row.classList.contains("collapsed");

    changeCallback(row);

    row = doc.getElementById(id);
    if (expectedChange == "collapsed") {
      Assert.ok(!before, `${id} was expanded`);
      Assert.ok(row.classList.contains("collapsed"), `${id} collapsed`);
      Assert.notEqual(
        listener.collapsedIndex,
        null,
        `${id} fired 'collapse' event`
      );
      Assert.ok(!listener.expandedIndex, `${id} did not fire 'expand' event`);
    } else if (expectedChange == "expanded") {
      Assert.ok(before, `${id} was collapsed`);
      Assert.ok(!row.classList.contains("collapsed"), `${id} expanded`);
      Assert.ok(
        !listener.collapsedIndex,
        `${id} did not fire 'collapse' event`
      );
      Assert.notEqual(
        listener.expandedIndex,
        null,
        `${id} fired 'expand' event`
      );
    } else {
      Assert.equal(
        row.classList.contains("collapsed"),
        before,
        `${id} state did not change`
      );
    }
  }

  function clickTwisty(id, expectedChange) {
    info(`clicking the twisty on ${id}`);
    performChange(id, expectedChange, row =>
      EventUtils.synthesizeMouseAtCenter(
        row.querySelector(".twisty"),
        {},
        content
      )
    );
  }

  function clickThread(id, expectedChange) {
    info(`clicking the thread on ${id}`);
    performChange(id, expectedChange, row => {
      EventUtils.synthesizeMouseAtCenter(
        row.querySelector(".tree-button-thread"),
        {},
        content
      );
    });
  }

  for (let id of idsWithoutChildren) {
    clickTwisty(id, null);
    Assert.equal(list.querySelector(".selected").id, id);
  }

  checkSelectedAndCurrent(7, "row-3-1-2");

  // Click the twisties of rows with children.

  function checkRowsAreHidden(...hiddenIds) {
    let remainingIds = allIds.slice();

    for (let id of allIds) {
      if (hiddenIds.includes(id)) {
        Assert.ok(!doc.getElementById(id), `${id} is hidden`);
        remainingIds.splice(remainingIds.indexOf(id), 1);
      } else {
        Assert.greater(
          doc.getElementById(id).clientHeight,
          0,
          `${id} is visible`
        );
      }
    }

    Assert.equal(list.view.rowCount, 8 - hiddenIds.length, "row count");
    Assert.deepEqual(
      Array.from(list.table.body.children, r => r.id),
      remainingIds,
      "rows property"
    );
  }

  // Collapse row 2.

  clickTwisty("row-2", "collapsed");
  checkRowsAreHidden("row-2-1", "row-2-2");
  checkSelectedAndCurrent(5, "row-3-1-2");

  // Collapse row 3.

  clickTwisty("row-3", "collapsed");
  checkRowsAreHidden("row-2-1", "row-2-2", "row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(2, "row-3");

  // Expand row 2.

  clickTwisty("row-2", "expanded");
  checkRowsAreHidden("row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  // Expand row 3.

  clickTwisty("row-3", "expanded");
  checkRowsAreHidden();
  checkSelectedAndCurrent(4, "row-3");

  // Collapse row 3-1.

  clickTwisty("row-3-1", "collapsed");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  // Collapse row 3.

  clickTwisty("row-3", "collapsed");
  checkRowsAreHidden("row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  // Expand row 3.

  clickTwisty("row-3", "expanded");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  // Expand row 3-1.

  clickTwisty("row-3-1", "expanded");
  checkRowsAreHidden();
  checkSelectedAndCurrent(4, "row-3");

  // Test key presses.

  function pressKey(id, key, expectedChange) {
    info(`pressing ${key}`);
    performChange(id, expectedChange, row => {
      EventUtils.synthesizeKey(key, {}, content);
    });
  }

  // Row 0 has no children or parent, nothing should happen.

  list.selectedIndex = 0;
  pressKey("row-1", "VK_LEFT");
  checkSelectedAndCurrent(0, "row-1");
  pressKey("row-1", "VK_RIGHT");
  checkSelectedAndCurrent(0, "row-1");

  // Collapse row 2.

  list.selectedIndex = 1;
  pressKey("row-2", "VK_LEFT", "collapsed");
  checkRowsAreHidden("row-2-1", "row-2-2");
  checkSelectedAndCurrent(1, "row-2");

  pressKey("row-2", "VK_LEFT");
  checkRowsAreHidden("row-2-1", "row-2-2");
  checkSelectedAndCurrent(1, "row-2");

  // Collapse row 3.

  list.selectedIndex = 2;
  pressKey("row-3", "VK_LEFT", "collapsed");
  checkRowsAreHidden("row-2-1", "row-2-2", "row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(2, "row-3");

  pressKey("row-3", "VK_LEFT");
  checkRowsAreHidden("row-2-1", "row-2-2", "row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(2, "row-3");

  // Expand row 2.

  list.selectedIndex = 1;
  pressKey("row-2", "VK_RIGHT", "expanded");
  checkRowsAreHidden("row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(1, "row-2");

  // Expand row 3.

  list.selectedIndex = 4;
  pressKey("row-3", "VK_RIGHT", "expanded");
  checkRowsAreHidden();
  checkSelectedAndCurrent(4, "row-3");

  // Go down the tree to row 3-1-1.

  pressKey("row-3", "VK_RIGHT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(5, "row-3-1");

  pressKey("row-3", "VK_RIGHT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  pressKey("row-3-1-1", "VK_RIGHT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  // Collapse row 3-1.

  pressKey("row-3-1-1", "VK_LEFT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(5, "row-3-1");

  pressKey("row-3-1", "VK_LEFT", "collapsed");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(5, "row-3-1");

  // Collapse row 3.

  pressKey("row-3-1", "VK_LEFT");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  pressKey("row-3", "VK_LEFT", "collapsed");
  checkRowsAreHidden("row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  // Expand row 3.

  pressKey("row-3", "VK_RIGHT", "expanded");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  pressKey("row-3", "VK_RIGHT");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(5, "row-3-1");

  // Expand row 3-1.

  pressKey("row-3-1", "VK_RIGHT", "expanded");
  checkRowsAreHidden();
  checkSelectedAndCurrent(5, "row-3-1");

  pressKey("row-3-1", "VK_RIGHT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  pressKey("row-3-1-1", "VK_RIGHT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  // Same again, with a RTL tree.

  info("switching to RTL");
  doc.documentElement.dir = "rtl";

  // Row 0 has no children or parent, nothing should happen.

  list.selectedIndex = 0;
  pressKey("row-1", "VK_RIGHT");
  checkSelectedAndCurrent(0, "row-1");
  pressKey("row-1", "VK_LEFT");
  checkSelectedAndCurrent(0, "row-1");

  // Collapse row 2.

  list.selectedIndex = 1;
  pressKey("row-2", "VK_RIGHT", "collapsed");
  checkRowsAreHidden("row-2-1", "row-2-2");
  checkSelectedAndCurrent(1, "row-2");

  pressKey("row-2", "VK_RIGHT");
  checkRowsAreHidden("row-2-1", "row-2-2");
  checkSelectedAndCurrent(1, "row-2");

  // Collapse row 3.

  list.selectedIndex = 2;
  pressKey("row-3", "VK_RIGHT", "collapsed");
  checkRowsAreHidden("row-2-1", "row-2-2", "row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(2, "row-3");

  pressKey("row-3", "VK_RIGHT");
  checkRowsAreHidden("row-2-1", "row-2-2", "row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(2, "row-3");

  // Expand row 2.

  list.selectedIndex = 1;
  pressKey("row-2", "VK_LEFT", "expanded");
  checkRowsAreHidden("row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(1, "row-2");

  // Expand row 3.

  list.selectedIndex = 4;
  pressKey("row-3", "VK_LEFT", "expanded");
  checkRowsAreHidden();
  checkSelectedAndCurrent(4, "row-3");

  // Go down the tree to row 3-1-1.

  pressKey("row-3", "VK_LEFT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(5, "row-3-1");

  pressKey("row-3", "VK_LEFT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  pressKey("row-3-1-1", "VK_LEFT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  // Collapse row 3-1.

  pressKey("row-3-1-1", "VK_RIGHT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(5, "row-3-1");

  pressKey("row-3-1", "VK_RIGHT", "collapsed");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(5, "row-3-1");

  // Collapse row 3.

  pressKey("row-3-1", "VK_RIGHT");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  pressKey("row-3", "VK_RIGHT", "collapsed");
  checkRowsAreHidden("row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  // Expand row 3.

  pressKey("row-3", "VK_LEFT", "expanded");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");

  pressKey("row-3", "VK_LEFT");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(5, "row-3-1");

  // Expand row 3-1.

  pressKey("row-3-1", "VK_LEFT", "expanded");
  checkRowsAreHidden();
  checkSelectedAndCurrent(5, "row-3-1");

  pressKey("row-3-1", "VK_LEFT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  pressKey("row-3-1-1", "VK_LEFT");
  checkRowsAreHidden();
  checkSelectedAndCurrent(6, "row-3-1-1");

  // Use the class methods for expanding and collapsing.

  selectHandler.reset();
  list.addEventListener("select", selectHandler);
  listener.reset();

  list.collapseRowAtIndex(6); // No children, no effect.
  Assert.ok(!selectHandler.seenEvent, "'select' event did not fire");
  Assert.ok(!listener.collapsedIndex, "'collapsed' event did not fire");

  list.expandRowAtIndex(6); // No children, no effect.
  Assert.ok(!selectHandler.seenEvent, "'select' event did not fire");
  Assert.ok(!listener.expandedIndex, "'expanded' event did not fire");

  list.collapseRowAtIndex(1); // Item with children that aren't selected.
  Assert.ok(!selectHandler.seenEvent, "'select' event did not fire");
  Assert.equal(listener.collapsedIndex, 1, "row-2 fired 'collapsed' event");
  listener.reset();

  list.expandRowAtIndex(1); // Item with children that aren't selected.
  Assert.ok(!selectHandler.seenEvent, "'select' event did not fire");
  Assert.equal(listener.expandedIndex, 1, "row-2 fired 'expanded' event");
  listener.reset();

  list.collapseRowAtIndex(5); // Item with children that are selected.
  Assert.ok(selectHandler.seenEvent, "'select' event fired");
  Assert.equal(
    selectHandler.selectedAtEvent,
    5,
    "selectedIndex was correct when 'select' event fired"
  );
  Assert.equal(listener.collapsedIndex, 5, "row-3-1 fired 'collapsed' event");
  checkRowsAreHidden("row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(5, "row-3-1");
  selectHandler.reset();
  listener.reset();

  list.expandRowAtIndex(5); // Selected item with children.
  Assert.ok(!selectHandler.seenEvent, "'select' event did not fire");
  Assert.equal(listener.expandedIndex, 5, "row-3-1 fired 'expanded' event");
  checkRowsAreHidden();
  checkSelectedAndCurrent(5, "row-3-1");
  listener.reset();

  list.selectedIndex = 7;
  selectHandler.reset();

  list.collapseRowAtIndex(4); // Item with grandchildren that are selected.
  Assert.ok(selectHandler.seenEvent, "'select' event fired");
  Assert.equal(
    selectHandler.selectedAtEvent,
    4,
    "selectedIndex was correct when 'select' event fired"
  );
  Assert.equal(listener.collapsedIndex, 4, "row-3 fired 'collapsed' event");
  checkRowsAreHidden("row-3-1", "row-3-1-1", "row-3-1-2");
  checkSelectedAndCurrent(4, "row-3");
  selectHandler.reset();
  listener.reset();

  list.expandRowAtIndex(4); // Selected item with grandchildren.
  Assert.ok(!selectHandler.seenEvent, "'select' event did not fire");
  Assert.equal(listener.expandedIndex, 4, "row-3 fired 'expanded' event");
  checkRowsAreHidden();
  checkSelectedAndCurrent(4, "row-3");
  listener.reset();

  // Click thread for already expanded thread. Should select all in thread.
  selectHandler.reset();
  clickThread("row-3"); // Item with grandchildren.
  Assert.ok(selectHandler.seenEvent, "'select' event fired");
  Assert.equal(
    selectHandler.selectedAtEvent,
    4,
    "selectedIndex was correct when 'select' event fired"
  );
  checkRowsAreHidden();
  checkMultiSelect("row-3", "row-3-1", "row-3-1-1", "row-3-1-2");
  checkCurrent(4);

  // Click thread for collapsed thread. Should expand the thread and select all
  // children.
  list.collapseRowAtIndex(1); // Item with children that aren't selected.
  Assert.equal(listener.collapsedIndex, 1, "row-2 fired 'collapsed' event");
  checkRowsAreHidden("row-2-1", "row-2-2");
  clickThread("row-2", "expanded");
  Assert.equal(listener.expandedIndex, 1, "row-2 fired 'expanded' event");
  checkMultiSelect("row-2", "row-2-1", "row-2-2");
  checkCurrent(1);

  // Select multiple messages in an expanded thread by keyboard, ending with a
  // child message, then collapse the thread. After that, currentIndex should
  // be the root message.
  selectHandler.reset();
  list.selectedIndex = 1;
  checkSelectedAndCurrent(1, "row-2");
  info(`pressing VK_DOWN with shift key twice`);
  EventUtils.synthesizeKey("VK_DOWN", { shiftKey: true }, content);
  EventUtils.synthesizeKey("VK_DOWN", { shiftKey: true }, content);
  checkMultiSelect("row-2", "row-2-1", "row-2-2");
  checkCurrent(3);
  clickTwisty("row-2", "collapsed");
  checkSelectedAndCurrent(1, "row-2");

  list.removeEventListener("collapsed", listener);
  list.removeEventListener("expanded", listener);
  list.removeEventListener("select", selectHandler);
  doc.documentElement.dir = null;
}

/**
 * Checks that the row widget can be changed, redrawing the rows and
 * maintaining the selection.
 */
add_task(async function testRowClassChange() {
  for (const variant of TEST_VARIANTS) {
    info(`Running row class change test for ${variant}`);
    await runTestInSandbox(subtestRowClassChange, variant);
  }
});

async function subtestRowClassChange() {
  let doc = content.document;
  let list = doc.getElementById("testTree");
  let indices = (list.selectedIndices = [1, 2, 3, 5, 8, 13, 21, 34]);
  list.currentIndex = 5;

  for (let row of list.table.body.children) {
    Assert.equal(row.getAttribute("is"), "test-row");
    Assert.equal(row.clientHeight, 50);
    Assert.equal(
      row.classList.contains("selected"),
      indices.includes(row.index)
    );
    Assert.equal(row.classList.contains("current"), row.index == 5);
  }

  info("switching row class to AlternativeCardRow");
  list.setAttribute("rows", "alternative-row");
  Assert.deepEqual(list.selectedIndices, indices);
  Assert.equal(list.currentIndex, 5);

  for (let row of list.table.body.children) {
    Assert.equal(row.getAttribute("is"), "alternative-row");
    Assert.equal(row.clientHeight, 80);
    Assert.equal(
      row.classList.contains("selected"),
      indices.includes(row.index)
    );
    Assert.equal(row.classList.contains("current"), row.index == 5);
  }

  list.selectedIndex = -1;
  Assert.deepEqual(list.selectedIndices, []);
  Assert.equal(list.currentIndex, -1);

  info("switching row class to TestCardRow");
  list.setAttribute("rows", "test-row");
  Assert.deepEqual(list.selectedIndices, []);
  Assert.equal(list.currentIndex, -1);

  for (let row of list.table.body.children) {
    Assert.equal(row.getAttribute("is"), "test-row");
    Assert.equal(row.clientHeight, 50);
    Assert.ok(!row.classList.contains("selected"));
    Assert.ok(!row.classList.contains("current"));
  }
}

/**
 * Checks that resizing the widget automatically adds more rows if necessary.
 */
add_task(async function testResize() {
  for (const variant of TEST_VARIANTS) {
    info(`Running resize test for ${variant}`);
    await runTestInSandbox(subtestResize, variant);
  }
});

async function subtestResize() {
  let doc = content.document;

  let list = doc.getElementById("testTree");
  Assert.ok(!!list, "the list exists");

  async function doListActionAndWaitForRowBuffer(actionFn) {
    // Filling the row buffer is fiddly timing, so provide an event to indicate
    // that actions which may trigger changes in the row buffer have finished.
    const eventName = "_treerowbufferfill";
    list._rowBufferReadyEvent = new content.CustomEvent(eventName);

    const promise = new Promise(resolve =>
      list.addEventListener(eventName, resolve, { once: true })
    );

    await actionFn();
    await promise;

    list._rowBufferReadyEvent = null;
  }

  async function scrollVerticallyBy(scrollDistance) {
    await doListActionAndWaitForRowBuffer(() => {
      list.scrollBy(0, scrollDistance);
    });
  }

  async function changeHeightTo(newHeight) {
    await doListActionAndWaitForRowBuffer(() => {
      list.style.height = `${newHeight}px`;
    });
  }

  let rowCount = function () {
    return list.querySelectorAll(`tr[is="test-row"]`).length;
  };

  let originalHeight = list.clientHeight;

  // We should already be at the top, but this will force us to have finished
  // loading before we trigger another scroll. Otherwise, we may get back a fill
  // event for the initial fill when we expect an event in response to a scroll.
  await doListActionAndWaitForRowBuffer(() => {
    list.scrollTo(0, 0);
  });

  // Start by scrolling to somewhere in the middle of the list, so that we
  // don't have to think about buffer rows that don't exist at the ends.
  await scrollVerticallyBy(2650);

  // The list has enough space for 13 visible rows, and 26 buffer rows should
  // exist above and below.
  Assert.equal(
    rowCount(),
    13 + 26 + 26,
    "the list should contain the right number of rows"
  );

  // Make the list shorter by 5 rows. This should not affect the number of rows,
  // but this is a bit flaky, so check we have at least the minimum required.
  await changeHeightTo(originalHeight - 250);
  Assert.equal(list._toleranceSize, 16);
  Assert.greaterOrEqual(
    rowCount(),
    8 + 26 + 26,
    "making the list shorter should not change the number of rows"
  );

  // Scrolling the list by any amount should remove excess rows.
  await scrollVerticallyBy(50);
  Assert.equal(
    rowCount(),
    8 + 16 + 16,
    "scrolling the list after resize should remove the excess rows"
  );

  // Return to the original height. More buffer rows should be added. We have
  // to wait for the ResizeObserver to be triggered.
  await changeHeightTo(originalHeight);
  Assert.equal(list._toleranceSize, 26);
  Assert.equal(
    rowCount(),
    13 + 26 + 26,
    "making the list taller should change the number of rows"
  );

  // Make the list taller by 5 rows. We have to wait for the ResizeObserver
  // to be triggered.
  await changeHeightTo(originalHeight + 250);
  Assert.equal(list._toleranceSize, 36);
  Assert.equal(
    rowCount(),
    18 + 36 + 36,
    "making the list taller should change the number of rows"
  );

  // Scrolling the list should not affect the number of rows.
  await scrollVerticallyBy(50);
  Assert.equal(
    rowCount(),
    18 + 36 + 36,
    "scrolling the list should not change the number of rows"
  );
}