summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/test/browser/browser_selectionWidgetController.js
blob: 8e57c64bdc0197898510ed641476e671d44b3973 (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
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
/* 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/. */

var tabInfo;
var win;

add_setup(async () => {
  tabInfo = window.openContentTab(
    "chrome://mochitests/content/browser/comm/mail/base/test/browser/files/selectionWidget.xhtml"
  );
  await BrowserTestUtils.browserLoaded(tabInfo.browser);

  tabInfo.browser.focus();
  win = tabInfo.browser.contentWindow;
});

registerCleanupFunction(() => {
  window.tabmail.closeTab(tabInfo);
});

var selectionModels = ["focus", "browse", "browse-multi"];

/**
 * The selection widget.
 *
 * @type {HTMLElement}
 */
var widget;
/**
 * A focusable item before the widget.
 *
 * @type {HTMLElement}
 */
var before;
/**
 * A focusable item after the widget.
 *
 * @type {HTMLElement}
 */
var after;

/**
 * Reset the page and create a new widget.
 *
 * The "widget", "before" and "after" variables will be reset to the new
 * elements.
 *
 * @param {object} options - Options to set.
 * @param {string} options.model - The selection model to use.
 * @param {string} [options.direction="right-to-left"] - The direction of the
 *   widget.  Choosing "top-to-bottom" will layout items from top to bottom.
 *   Choosing "right-to-left" or "left-to-right" will set the page's direction
 *   to "rtl" or "ltr", respectively, and will layout items in the writing
 *   direction.
 * @param {boolean} [options.draggable=false] - Whether to make the items
 *   draggable.
 */
function reset(options) {
  function createTabStop(text) {
    let el = win.document.createElement("span");
    el.tabIndex = 0;
    el.id = text;
    el.textContent = text;
    return el;
  }
  before = createTabStop("before");
  after = createTabStop("after");

  let { model, direction } = options;
  if (!direction) {
    // Default to a less-common format.
    direction = "right-to-left";
  }
  info(`Creating ${direction} widget with "${model}" model`);

  widget = win.document.createElement("test-selection-widget");
  widget.id = "widget";
  widget.setAttribute("selection-model", model);
  widget.setAttribute(
    "layout-direction",
    direction == "top-to-bottom" ? "vertical" : "horizontal"
  );
  widget.toggleAttribute("items-draggable", options.draggable);

  win.document.body.replaceChildren(before, widget, after);

  win.document.dir = direction == "left-to-right" ? "ltr" : "rtl";

  before.focus();
}

/**
 * Create an array of sequential integers.
 *
 * @param {number} start - The starting integer.
 * @param {number} num - The number of integers.
 *
 * @returns {number[]} - Array of integers between start and (start + num - 1).
 */
function range(start, num) {
  return Array.from({ length: num }, (_, i) => start + i);
}

/**
 * Assert that the specified items are selected in the widget, and nothing else.
 *
 * @param {number[]} indices - The indices of the selected items.
 * @param {string} msg - A message to use for the assertion.
 */
function assertSelection(indices, msg) {
  let selected = widget.selectedIndices();
  Assert.deepEqual(selected, indices, `Selected indices should match: ${msg}`);
  // Test that the return of getSelectionRanges is as expected.
  let expectRanges = [];
  let lastIndex = -2;
  let rangeIndex = -1;
  for (let index of indices) {
    if (index == lastIndex + 1) {
      expectRanges[rangeIndex].end++;
    } else {
      rangeIndex++;
      expectRanges.push({ start: index, end: index + 1 });
    }
    lastIndex = index;
  }
  Assert.deepEqual(
    widget.getSelectionRanges(),
    expectRanges,
    `Selection ranges should match expected: ${msg}`
  );
}

/**
 * Assert that the given element is focused.
 *
 * @param {object} expect - The expected focused element.
 * @param {HTMLElement} [expect.element] - The expected element that will
 *   have focus.
 * @param {number} [expect.index] - If the `element` property is not given, this
 *   specifies the index of the item widget we expect to have focus.
 * @param {string} [expect.text] - Optionally test that the element also has the
 *   given text content.
 * @param {string} msg - A message to use for the assertion.
 */
function assertFocus(expect, msg) {
  let expectElement;
  let name;
  if (expect.element != undefined) {
    expectElement = expect.element;
    name = `Element #${expectElement.id}`;
  } else {
    expectElement = widget.items[expect.index].element;
    name = `Item ${expect.index}`;
  }
  let active = win.document.activeElement;
  let activeIndex = widget.items.findIndex(i => i.element == active);
  if (activeIndex >= 0) {
    active = `"${active.textContent}", index: ${activeIndex}`;
  } else if (active.id) {
    active = `#${active.id}`;
  } else {
    active = `<${active.localName}>`;
  }
  Assert.ok(
    expectElement.matches(":focus"),
    `${name} should have focus (active: ${active}): ${msg}`
  );
}

/**
 * Shift the focus by one step by pressing Tab and assert the new focused
 * element.
 *
 * @param {boolean} forward - Whether to move the focus forward.
 * @param {object} expect - The expected focused element after pressing tab.
 *   Same as passed to {@link assertFocus}.
 * @param {string} msg - A message to use for the assertion.
 */
function stepFocus(forward, expect, msg) {
  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: !forward }, win);
  assertFocus(
    expect,
    `After moving ${forward ? "forward" : "backward"}: ${msg}`
  );
}

/**
 * @typedef {object} ItemState
 * @property {string} text - The text content of the item.
 * @property {boolean} [selected=false] - Whether the item is selected.
 * @property {boolean} [focused=false] - Whether the item is focused.
 */

/**
 * Assert the text order, selection state and focus of the widget items.
 *
 * @param {ItemState[]} expected - The expected state of the widget items, in
 *   the expected order of the items.
 * @param {string} msg - A message to use for the assertion.
 */
function assertState(expected, msg) {
  let textOrder = [];
  let focusIndex;
  let selectedIndices = [];
  for (let [index, state] of expected.entries()) {
    textOrder.push(state.text);
    if (state.selected) {
      selectedIndices.push(index);
    }
    if (state.focused) {
      if (focusIndex != undefined) {
        throw new Error("More than one item specified as having focus");
      }
      focusIndex = index;
    }
  }
  Assert.deepEqual(
    Array.from(widget.items, i => i.element.textContent),
    textOrder,
    `Text order should match: ${msg}`
  );
  assertSelection(selectedIndices, msg);
  if (focusIndex != undefined) {
    assertFocus({ index: focusIndex }, msg);
  } else {
    Assert.ok(
      !widget.querySelector(":focus"),
      `Widget should not contain any focus: ${msg}`
    );
  }
}

/**
 * Click the empty space of the widget.
 *
 * @param {object} mouseEvent - Properties for the click event.
 */
function clickWidgetEmptySpace(mouseEvent) {
  let widgetRect = widget.getBoundingClientRect();
  if (widget.getAttribute("layout-direction") == "vertical") {
    // Try click end, which we assume is empty.
    EventUtils.synthesizeMouse(
      widget,
      widgetRect.width / 2,
      widgetRect.height - 5,
      mouseEvent,
      win
    );
  } else if (widget.matches(":dir(rtl)")) {
    // Try click the left, which we assume is empty.
    EventUtils.synthesizeMouse(
      widget,
      5,
      widgetRect.height / 2,
      mouseEvent,
      win
    );
  } else {
    // Try click the right, which we assume is empty.
    EventUtils.synthesizeMouse(
      widget,
      widgetRect.width - 5,
      widgetRect.height / 2,
      mouseEvent,
      win
    );
  }
}

/**
 * Click the specified widget item.
 *
 * @param {number} index - The index of the item to click.
 * @param {object} mouseEvent - Properties for the click event.
 */
function clickWidgetItem(index, mouseEvent) {
  EventUtils.synthesizeMouseAtCenter(
    widget.items[index].element,
    mouseEvent,
    win
  );
}

/**
 * Trigger the select-all shortcut.
 */
function selectAllShortcut() {
  EventUtils.synthesizeKey(
    "a",
    AppConstants.platform == "macosx" ? { metaKey: true } : { ctrlKey: true },
    win
  );
}

// If the widget is empty, it receives focus on itself.
add_task(function test_empty_widget_focus() {
  for (let model of selectionModels) {
    reset({ model });

    assertFocus({ element: before }, "Initial");

    // Move focus forward.
    stepFocus(true, { element: widget }, "Move into widget");
    stepFocus(true, { element: after }, "Move out of widget");

    // Move focus backward.
    stepFocus(false, { element: widget }, "Move back to widget");
    stepFocus(false, { element: before }, "Move back out of widget");

    // Clicking also gives focus.
    for (let shiftKey of [false, true]) {
      for (let ctrlKey of [false, true]) {
        info(
          `Clicking empty widget: ctrlKey: ${ctrlKey}, shiftKey: ${shiftKey}`
        );
        clickWidgetEmptySpace({ shiftKey, ctrlKey });
        assertFocus({ element: widget }, "Widget receives focus after click");
        // Move focus for the next loop.
        stepFocus(true, { element: after }, "Move back out");
      }
    }
  }
});

/**
 * Test that the initial focus is as expected.
 *
 * @param {string} model - The selection model to use.
 * @param {Function} setup - A callback to set up the widget.
 * @param {number} clickIndex - The index of an item to click.
 * @param {object} expect - The expected states.
 * @param {number} expect.focusIndex - The expected focus index.
 * @param {number[]} expect.selection - The expected initial selection.
 * @param {boolean} expect.selectFocus - Whether we expect the focused item to
 *   become selected.
 */
function subtest_initial_focus(model, setup, expect) {
  let { focusIndex: index, selection, selectFocus } = expect;

  reset({ model });
  setup();

  assertFocus({ element: before }, "Forward start");
  assertSelection(selection, "Initial selection");

  stepFocus(true, { index }, "Move onto selected item");
  if (selectFocus) {
    assertSelection([index], "Focus becomes selected");
  } else {
    assertSelection(selection, "Selection remains when focussing");
  }
  stepFocus(true, { element: after }, "Move out of widget");

  // Reverse.
  reset({ model });
  after.focus();
  setup();

  assertFocus({ element: after }, "Reverse start");
  assertSelection(selection, "Reverse start");

  stepFocus(false, { index }, "Move backward to selected item");
  if (selectFocus) {
    assertSelection([index], "Focus becomes selected");
  } else {
    assertSelection(selection, "Selection remains when focussing");
  }
  stepFocus(false, { element: before }, "Move out of widget");

  // With mouse click.
  for (let shiftKey of [false, true]) {
    for (let ctrlKey of [false, true]) {
      info(`Clicking widget: ctrlKey: ${ctrlKey}, shiftKey: ${shiftKey}`);

      reset({ model });
      setup();

      assertFocus({ element: before }, "Click empty start");
      assertSelection(selection, "Click empty start");
      clickWidgetEmptySpace({ ctrlKey, shiftKey });
      assertFocus(
        { index },
        "Selected item becomes focused with click on empty"
      );
      if (selectFocus) {
        assertSelection([index], "Focus becomes selected on click on empty");
      } else {
        assertSelection(selection, "Selection remains when click on empty");
      }

      // With mouse click on item focus moves to the clicked item instead.
      for (let clickIndex of [
        (index || widget.items.length) - 1,
        index,
        index + 1,
      ]) {
        reset({ model });
        setup();

        assertFocus({ element: before }, "Click first item start");
        assertSelection(selection, "Click first item start");

        clickWidgetItem(clickIndex, { shiftKey, ctrlKey });

        if (
          (shiftKey && ctrlKey) ||
          ((shiftKey || ctrlKey) && (model == "focus" || model == "browse"))
        ) {
          // Both modifiers, or multi-selection not supported, so acts the
          // same as clicking empty.
          assertFocus(
            { index },
            "Selected item becomes focused with click on item"
          );
          if (selectFocus) {
            assertSelection([index], "Focus becomes selected on click on item");
          } else {
            assertSelection(selection, "Selection remains when click on item");
          }
        } else {
          assertFocus(
            { index: clickIndex },
            "Clicked item becomes focused with click on item"
          );
          let clickSelection;
          if (ctrlKey) {
            if (selection.includes(clickIndex)) {
              // Toggle off clicked item.
              clickSelection = selection.filter(index => index != clickIndex);
            } else {
              clickSelection = selection.concat([clickIndex]).sort();
            }
          } else if (shiftKey) {
            // Range selection is always from 0, regardless of the selection
            // before the click.
            clickSelection = range(0, clickIndex + 1);
          } else {
            clickSelection = [clickIndex];
          }
          assertSelection(clickSelection, "Selection after click on item");
        }
      }
    }
  }
}

// If the widget has a selection when we move into it, the selected item is
// focused.
add_task(function test_initial_focus() {
  for (let model of selectionModels) {
    // With no initial selection.
    subtest_initial_focus(
      model,
      () => {
        widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
      },
      { focusIndex: 0, selection: [], selectFocus: true }
    );
    // With call to selectSingleItem
    subtest_initial_focus(
      model,
      () => {
        widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
        widget.selectSingleItem(2);
      },
      { focusIndex: 2, selection: [2], selectFocus: false }
    );

    // Using the setItemSelected API
    if (model == "focus" || model == "browse") {
      continue;
    }

    subtest_initial_focus(
      model,
      () => {
        widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
        widget.setItemSelected(2, true);
      },
      { focusIndex: 2, selection: [2], selectFocus: false }
    );

    // With multiple selected, we move focus to the first selected.
    subtest_initial_focus(
      model,
      () => {
        widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
        widget.setItemSelected(2, true);
        widget.setItemSelected(1, true);
      },
      { focusIndex: 1, selection: [1, 2], selectFocus: false }
    );

    // If we use both methods.
    subtest_initial_focus(
      model,
      () => {
        widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
        widget.selectSingleItem(2, true);
        widget.setItemSelected(1, true);
      },
      { focusIndex: 1, selection: [1, 2], selectFocus: false }
    );

    // If we call selectSingleItem and then unselect it, we act same as the
    // default case.
    subtest_initial_focus(
      model,
      () => {
        widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
        widget.selectSingleItem(2, true);
        widget.setItemSelected(2, false);
      },
      { focusIndex: 0, selection: [], selectFocus: true }
    );
  }
});

// If selectSingleItem API method is called, we select an item and make it the
// focus.
add_task(function test_select_single_item_method() {
  function subTestSelectSingleItem(outside, index) {
    if (outside) {
      stepFocus(true, { element: after }, "Moving focus to outside widget");
    }

    widget.selectSingleItem(index);
    assertSelection([index], "Item becomes selected after call");

    if (outside) {
      assertFocus({ element: after }, "Focus remains outside the widget");
      // Return.
      stepFocus(false, { index }, "Focus moves to selected item on return");
      assertSelection([index], "Item remains selected on return");
    } else {
      assertFocus({ index }, "Focus force moved to selected item");
    }
  }

  for (let model of selectionModels) {
    reset({ model });
    widget.addItems(0, ["First", "Second", "Third", "Fourth"]);

    stepFocus(true, { index: 0 }, "Move onto first item");

    for (let outside of [false, true]) {
      info(`Testing selecting item${outside ? " with focus outside" : ""}`);

      EventUtils.synthesizeKey("KEY_Home", {}, win);
      assertFocus({ index: 0 }, "Focus initially on first item");
      assertSelection([0], "Initial selection on first item");

      subTestSelectSingleItem(outside, 1);
      // Selecting again.
      subTestSelectSingleItem(outside, 1);

      if (model == "focus") {
        continue;
      }

      // Split focus from selection
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item has focus");
      assertSelection([1], "Second item remains selected");

      // Select focused item.
      subTestSelectSingleItem(outside, 2);

      // Split again.
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Second item has focus");
      assertSelection([2], "Third item remains selected");

      // Selecting selected item will still move focus.
      subTestSelectSingleItem(outside, 2);

      // Split again.
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 0 }, "First item has focus");
      assertSelection([2], "Third item remains selected");

      // Select neither focused nor selected.
      subTestSelectSingleItem(outside, 1);
    }

    // With mouse click to focus.
    for (let shiftKey of [false, true]) {
      for (let ctrlKey of [false, true]) {
        info(`Clicking widget: ctrlKey: ${ctrlKey}, shiftKey: ${shiftKey}`);

        reset({ model });
        widget.addItems(0, ["First", "Second", "Third"]);
        stepFocus(true, { index: 0 }, "Move onto first item");
        assertSelection([0], "First item becomes selected");

        // Move focus outside widget.
        stepFocus(true, { element: after }, "Move focus outside");

        // Select an item.
        widget.selectSingleItem(1);

        // Click empty space will focus the selected item.
        clickWidgetEmptySpace({});
        assertFocus(
          { index: 1 },
          "Selected item becomes focused with click on empty"
        );
        assertSelection(
          [1],
          "Second item remains selected with click on empty"
        );

        // With mouse click on selected item.
        stepFocus(false, { element: before }, "Move focus outside");
        widget.selectSingleItem(2);

        clickWidgetItem(2, { shiftKey, ctrlKey });
        assertFocus(
          { index: 2 },
          "Selected item becomes focused with click on selected"
        );
        if (ctrlKey && !shiftKey && model == "browse-multi") {
          assertSelection(
            [],
            "Item becomes unselected with Ctrl+click on selected"
          );
        } else {
          // NOTE: Shift+Click will select from the item to itself.
          assertSelection(
            [2],
            "Selected item remains selected with click on selected"
          );
        }

        // With mouse click on non-selected item.
        stepFocus(false, { element: before }, "Move focus outside");
        widget.selectSingleItem(1);

        clickWidgetItem(2, { shiftKey, ctrlKey });
        if (
          (shiftKey && ctrlKey) ||
          ((shiftKey || ctrlKey) && (model == "focus" || model == "browse"))
        ) {
          // Both modifiers, or multi-selection not supported, so acts the
          // same as clicking empty.
          assertFocus(
            { index: 1 },
            "Selected item becomes focused with click on item"
          );
          assertSelection(
            [1],
            "Selected item remains selected with click on item"
          );
        } else {
          assertFocus(
            { index: 2 },
            "Third item becomes focused with click on item"
          );
          if (ctrlKey) {
            assertSelection(
              [1, 2],
              "Third item becomes selected with Ctrl+click"
            );
          } else if (shiftKey) {
            assertSelection(
              [1, 2],
              "Second to third item become selected with Shift+click"
            );
          } else {
            assertSelection(
              [2],
              "Third item becomes selected with click on item"
            );
          }
        }
      }
    }
  }
});

// If setItemSelected API method is called, we set the selection state of an
// item but do not change anything else.
add_task(function test_set_item_selected_method() {
  for (let model of selectionModels) {
    reset({ model });
    widget.addItems(0, ["First", "Second", "Third", "Fourth", "Fifth"]);
    stepFocus(true, { index: 0 }, "Initial focus on first item");
    assertSelection([0], "Initial selection on first item");

    if (model == "focus" || model == "browse") {
      // This method always throws.
      Assert.throws(
        () => widget.setItemSelected(2, true),
        /Widget does not support multi-selection/
      );
      // Even if it would not change the single selection state.
      Assert.throws(
        () => widget.setItemSelected(2, false),
        /Widget does not support multi-selection/
      );
      Assert.throws(
        () => widget.setItemSelected(0, true),
        /Widget does not support multi-selection/
      );
      continue;
    }

    // Can select.
    widget.setItemSelected(2, true);
    assertFocus({ index: 0 }, "Same focus");
    assertSelection([0, 2], "Item 2 becomes selected");

    // And unselect.
    widget.setItemSelected(0, false);
    assertFocus({ index: 0 }, "Same focus");
    assertSelection([2], "Item 0 is unselected");

    // Does nothing extra if already selected/unselected.
    widget.setItemSelected(2, true);
    assertFocus({ index: 0 }, "Same focus");
    assertSelection([2], "Same selected");

    widget.setItemSelected(0, false);
    assertFocus({ index: 0 }, "Same focus");
    assertSelection([2], "Same selected");

    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);

    // Select the focused item.
    assertFocus({ index: 3 }, "Focus on item 3");
    assertSelection([2], "Same selected");

    widget.setItemSelected(3, true);
    assertFocus({ index: 3 }, "Same focus");
    assertSelection([2, 3], "Item 3 selected");

    widget.setItemSelected(2, false);
    assertFocus({ index: 3 }, "Same focus");
    assertSelection([3], "Item 2 unselected");

    // Can select none this way.
    widget.setItemSelected(3, false);
    assertFocus({ index: 3 }, "Same focus");
    assertSelection([], "None selected");
  }
});

/**
 * Test navigation for the given direction.
 *
 * @param {string} model - The selection model to use.
 * @param {string} direction - The layout direction of the widget.
 * @param {object} keys - Navigation keys.
 * @param {string} keys.forward - The key to move forward.
 * @param {string} keys.backward - The key to move backward.
 */
function subtest_keyboard_navigation(model, direction, keys) {
  let { forward: forwardKey, backward: backwardKey } = keys;
  reset({ model, direction });
  widget.addItems(0, ["First", "Second", "Third"]);

  stepFocus(true, { index: 0 }, "Initially on first item");

  // Without Ctrl, selection follows focus.

  // Forward.
  EventUtils.synthesizeKey(forwardKey, {}, win);
  assertFocus({ index: 1 }, "Forward to second item");
  assertSelection([1], "Second item becomes selected on focus");
  EventUtils.synthesizeKey(forwardKey, {}, win);
  assertFocus({ index: 2 }, "Forward to third item");
  assertSelection([2], "Third item becomes selected on focus");
  EventUtils.synthesizeKey(forwardKey, {}, win);
  assertFocus({ index: 2 }, "Forward at end remains on third item");
  assertSelection([2], "Third item remains selected");

  // Backward.
  EventUtils.synthesizeKey(backwardKey, {}, win);
  assertFocus({ index: 1 }, "Backward to second item");
  assertSelection([1], "Second item becomes selected on focus");
  EventUtils.synthesizeKey(backwardKey, {}, win);
  assertFocus({ index: 0 }, "Backward to first item");
  assertSelection([0], "First item becomes selected on focus");
  EventUtils.synthesizeKey(backwardKey, {}, win);
  assertFocus({ index: 0 }, "Backward at end remains on first item");
  assertSelection([0], "First item remains selected");

  // End.
  EventUtils.synthesizeKey("KEY_End", {}, win);
  assertFocus({ index: 2 }, "Third becomes focused on End");
  assertSelection([2], "Third becomes selected on End");
  // Move to middle.
  EventUtils.synthesizeKey(backwardKey, {}, win);
  EventUtils.synthesizeKey("KEY_End", {}, win);
  assertFocus({ index: 2 }, "Third becomes focused on End from second");
  assertSelection([2], "Third becomes selected on End from second");
  EventUtils.synthesizeKey("KEY_End", {}, win);
  assertFocus({ index: 2 }, "Third remains focused on End from third");
  assertSelection([2], "Third becomes selected on End from third");

  // Home.
  EventUtils.synthesizeKey("KEY_Home", {}, win);
  assertFocus({ index: 0 }, "First becomes focused on Home");
  assertSelection([0], "First becomes selected on Home");
  // Move to middle.
  EventUtils.synthesizeKey(forwardKey, {}, win);
  EventUtils.synthesizeKey("KEY_Home", {}, win);
  assertFocus({ index: 0 }, "First becomes focused on Home from second");
  assertSelection([0], "First becomes selected on Home from second");
  EventUtils.synthesizeKey("KEY_Home", {}, win);
  assertFocus({ index: 0 }, "First remains focused on Home from first");
  assertSelection([0], "First becomes selected on Home from first");

  // With Ctrl key, selection does not follow focus.
  if (model == "focus") {
    // Disabled in "focus" model.
    // Move to middle item.
    EventUtils.synthesizeKey(forwardKey, {}, win);
    assertFocus({ index: 1 }, "Second item is focused");
    assertFocus({ index: 1 }, "Second item is selected");

    for (let key of [backwardKey, forwardKey, "KEY_Home", "KEY_End"]) {
      for (let shiftKey of [false, true]) {
        info(
          `Pressing Ctrl+${
            shiftKey ? "Shift+" : ""
          }${key} on "focus" model widget`
        );
        EventUtils.synthesizeKey(key, { ctrlKey: true, shiftKey }, win);
        assertFocus({ index: 1 }, "Second item is still focused");
        assertSelection([1], "Second item is still selected");
      }
    }
  } else {
    EventUtils.synthesizeKey(forwardKey, { ctrlKey: true }, win);
    assertFocus({ index: 1 }, "Ctrl+Forward to second item");
    assertSelection([0], "First item remains selected on Ctrl+Forward");

    EventUtils.synthesizeKey(forwardKey, { ctrlKey: true }, win);
    assertFocus({ index: 2 }, "Ctrl+Forward to third item");
    assertSelection([0], "First item remains selected on Ctrl+Forward");

    EventUtils.synthesizeKey(backwardKey, { ctrlKey: true }, win);
    assertFocus({ index: 1 }, "Ctrl+Backward to second item");
    assertSelection([0], "First item remains selected on Ctrl+Backward");

    EventUtils.synthesizeKey(backwardKey, { ctrlKey: true }, win);
    assertFocus({ index: 0 }, "Ctrl+Backward to first item");
    assertSelection([0], "First item remains selected on Ctrl+Backward");

    EventUtils.synthesizeKey("KEY_End", { ctrlKey: true }, win);
    assertFocus({ index: 2 }, "Ctrl+End to third item");
    assertSelection([0], "First item remains selected on Ctrl+End");

    EventUtils.synthesizeKey(backwardKey, {}, win);
    assertFocus({ index: 1 }, "Backward to second item");
    assertSelection([1], "Selection moves with focus when not pressing Ctrl");

    EventUtils.synthesizeKey("KEY_Home", { ctrlKey: true }, win);
    assertFocus({ index: 0 }, "Ctrl+Home to first item");
    assertSelection([1], "Second item remains selected on Ctrl+Home");

    // Does nothing if combined with Shift.
    for (let key of [backwardKey, forwardKey, "KEY_Home", "KEY_End"]) {
      info(`Pressing Ctrl+Shift+${key} on "${model}" model widget`);
      EventUtils.synthesizeKey(key, { ctrlKey: true, shiftKey: true }, win);
      assertFocus({ index: 0 }, "First item is still focused");
      assertSelection([1], "Second item is still selected");
    }

    // Even if focus remains the same, the selection is still updated if we
    // don't press Ctrl.
    EventUtils.synthesizeKey(backwardKey, {}, win);
    assertFocus({ index: 0 }, "Focus remains on first item");
    assertSelection(
      [0],
      "Selection moves to the first item since Ctrl was not pressed"
    );
  }
}

// Navigating with keyboard will move focus, and possibly selection.
add_task(function test_keyboard_navigation() {
  for (let model of selectionModels) {
    subtest_keyboard_navigation(model, "top-to-bottom", {
      forward: "KEY_ArrowDown",
      backward: "KEY_ArrowUp",
    });
    subtest_keyboard_navigation(model, "right-to-left", {
      forward: "KEY_ArrowLeft",
      backward: "KEY_ArrowRight",
    });
    subtest_keyboard_navigation(model, "left-to-right", {
      forward: "KEY_ArrowRight",
      backward: "KEY_ArrowLeft",
    });
  }
});

/**
 * A method to scroll the widget.
 *
 * @callback ScrollMethod
 * @param {number} pos - The position/offset to scroll to.
 */
/**
 * The position of an element, relative to the layout of the widget.
 *
 * @typedef {object} StartEndPositions
 * @property {number} start - The starting position of the element in the
 *   direction of the widget's layout. The value should be a pixel offset
 *   from some fixed point, such that a higher value indicates an element
 *   further from the start of the widget.
 * @property {number} end - The ending position of the element in the
 *   direction of the widget's layout. This should use the same fixed point
 *   as the start.
 * @property {number} xStart - An X position in the client coordinates that
 *   points to the inside of the element, close to the starting corner. I.e.
 *   the block-start and inline-start.
 * @property {number} yStart - A Y position in the client coordinates that
 *   points to the inside of the element, close to the starting corner.
 * @property {number} xEnd - An X position in the client coordinates that
 *   points to the inside of the element, close to the ending corner. I.e.
 *   the block-end and inline-end.
 * @property {number} yEnd - A Y position in the client coordinates that
 *   points to the inside of the element, close to the ending corner.
 */
/**
 * A method to return the starting and ending positions of the bounding
 * client rectangle of an element.
 *
 * @callback GetStartEndMethod
 * @param {DOMRect} rect - The rectangle to get the positions of.
 * @returns {object} positions
/**
 * Test page navigation for the given direction.
 *
 * @param {string} model - The selection model to use on the widget.
 * @param {string} direction - The direction of the widget layout.
 * @param {object} details - Details about the direction.
 * @param {string} details.sizeName - The CSS style name that controls the
 *   widget size in the direction of widget layout.
 * @param {string} details.forwardKey - The key to press to move forward one
 *   item.
 * @param {string} details.backwardKey - The key to press to move backward
 *   one item.
 * @param {ScrollMethod} details.scrollTo - A method to call to scroll the
 *   widget.
 * @param {GetStartEndMethod} details.getStartEnd - A method to get the
 *   positioning of an element.
 */
function subtest_page_navigation(model, direction, details) {
  let { sizeName, forwardKey, backwardKey, scrollTo, getStartEnd } = details;
  function getStartEndBoundary(element) {
    return getStartEnd(element.getBoundingClientRect());
  }
  function assertInView(expect, msg) {
    let { first, firstClipped, last, lastClipped } = expect;
    if (!firstClipped) {
      firstClipped = 0;
    }
    if (!lastClipped) {
      lastClipped = 0;
    }
    let { start: viewStart, end: viewEnd } = getStartEndBoundary(widget);
    // The widget has a 1px border that should not contribute to the view
    // size.
    viewStart += 1;
    viewEnd -= 1;
    let firstStart = getStartEndBoundary(
      widget.items[expect.first].element
    ).start;
    Assert.equal(
      firstStart,
      viewStart - firstClipped,
      `Item ${first} should be at the start of the view (${viewStart}) clipped by ${firstClipped}: ${msg}`
    );
    if (expect.first > 0) {
      Assert.lessOrEqual(
        getStartEndBoundary(widget.items[expect.first - 1].element).end,
        viewStart,
        `Item ${expect.first - 1} should be out of view: ${msg}`
      );
    }
    let lastEnd = getStartEndBoundary(widget.items[expect.last].element).end;
    Assert.equal(
      lastEnd,
      viewEnd + lastClipped,
      `Item ${last} should be at the end of the view (${viewEnd}) clipped by ${lastClipped}: ${msg}`
    );
    if (expect.last < widget.items.length - 1) {
      Assert.greaterOrEqual(
        getStartEndBoundary(widget.items[expect.last + 1].element).start,
        viewEnd,
        `Item ${expect.last + 1} should be out of view: ${msg}`
      );
    }
  }
  reset({ model, direction });
  widget.addItems(
    0,
    range(0, 70).map(i => `add-${i}`)
  );
  let { start: itemStart, end: itemEnd } = getStartEndBoundary(
    widget.items[0].element
  );
  Assert.equal(itemEnd - itemStart, 30, "Expected item size");

  assertInView({ first: 0, last: 19 }, "First 20 items in view");
  stepFocus(true, { index: 0 }, "Move into widget");
  assertSelection([0], "Fist item selected");
  assertInView({ first: 0, last: 19 }, "First 20 items still in view");

  // PageDown goes to the end of the current page.
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 0, last: 19 }, "First 20 item still in view");
  assertFocus({ index: 19 }, "Focus moves to end of the page");
  assertSelection([19], "Selection at end of the page");

  // Pressing forward key will scroll the next item into view.
  EventUtils.synthesizeKey(forwardKey, {}, win);
  assertInView({ first: 1, last: 20 }, "Items 1 to 20 in view");
  assertFocus({ index: 20 }, "Focus at end of the page");
  assertSelection([20], "Selection at end of the page");

  // Pressing backward will not change the view.
  EventUtils.synthesizeKey(backwardKey, {}, win);
  assertInView({ first: 1, last: 20 }, "Items 1 to 20 still in view");
  assertFocus({ index: 19 }, "Focus moves up to 19");
  assertSelection([19], "Selection moves up to 19");

  // PageDown goes to the end of the current page.
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 1, last: 20 }, "Items 1 to 20 still in view");
  assertFocus({ index: 20 }, "Focus moves to end of page");
  assertSelection([20], "Selection moves to end of page");

  // PageDown when already at the end of the page will move to the next
  // page.
  // The last index from the previous page (20) should still be visible at
  // the top.
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 20, last: 39 }, "Items 20 to 39 in view");
  assertFocus({ index: 39 }, "Focus moves to end of new page");
  assertSelection([39], "Selection moves to end of new page");

  // Another PageDown will do the same.
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 39, last: 58 }, "Items 39 to 58 in view");
  assertFocus({ index: 58 }, "Focus moves to end of new page");
  assertSelection([58], "Selection moves to end of new page");

  // Last PageDown will take us to the end.
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 50, last: 69 }, "Last 20 items in view");
  assertFocus({ index: 69 }, "Focus moves to end");
  assertSelection([69], "Selection moves to end");

  // Same thing in reverse with PageUp.
  // PageUp goes to the start of the current page.
  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 50, last: 69 }, "Last 20 item still in view");
  assertFocus({ index: 50 }, "Focus moves to start of the page");
  assertSelection([50], "Selection at end of the page");

  // Pressing backward will scroll the previous item into view.
  EventUtils.synthesizeKey(backwardKey, {}, win);
  assertInView({ first: 49, last: 68 }, "Items 49 to 68 in view");
  assertFocus({ index: 49 }, "Focus at start of the page");
  assertSelection([49], "Selection at start of the page");

  // Pressing forward will not change the view.
  EventUtils.synthesizeKey(forwardKey, {}, win);
  assertInView({ first: 49, last: 68 }, "Items 49 to 68 still in view");
  assertFocus({ index: 50 }, "Focus moves up to 50");
  assertSelection([50], "Selection moves up to 50");

  // PageUp goes to the start of the current page.
  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 49, last: 68 }, "Items 49 to 68 still in view");
  assertFocus({ index: 49 }, "Focus moves to start of page");
  assertSelection([49], "Selection moves to start of page");

  // PageUp when already at the start of the page will move one page up.
  // The first index from the previously shown page (49) should still be
  // visible at the bottom.
  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 30, last: 49 }, "Items 30 to 49 in view");
  assertFocus({ index: 30 }, "Focus moves to start of new page");
  assertSelection([30], "Selection moves to start of new page");

  // Another PageUp will do the same.
  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 11, last: 30 }, "Items 11 to 30 in view");
  assertFocus({ index: 11 }, "Focus moves to start of new page");
  assertSelection([11], "Selection moves to start of new page");

  // Last PageUp will take us to the start.
  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 0, last: 19 }, "Items 0 to 19 in view");
  assertFocus({ index: 0 }, "Focus moves to start");
  assertSelection([0], "Selection moves to start");

  // PageDown with focus above the view. Focus should move to the end of the
  // visible page.
  scrollTo(120);
  assertInView({ first: 4, last: 23 }, "Items 4 to 23 in view");
  assertFocus({ index: 0 }, "Focus remains above the view");
  assertSelection([0], "Selection remains above the view");

  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 4, last: 23 }, "Same items in view");
  assertFocus({ index: 23 }, "Focus moves to the end of the visible page");
  assertSelection([23], "Selection moves to the end of the visible page");

  // PageDown with focus below the view. Focus should shift by one page,
  // with the previous focus at the top of the page.
  scrollTo(60);
  assertInView({ first: 2, last: 21 }, "Items 2 to 21 in view");
  assertFocus({ index: 23 }, "Focus remains below the view");
  assertSelection([23], "Selection remains below the view");

  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView(
    { first: 23, last: 42 },
    "View shifts by a page relative to focus"
  );
  assertFocus({ index: 42 }, "Focus moves to end of new page");
  assertSelection([42], "Selection moves to end of new page");

  // PageUp with focus below the view. Focus should move to the start of the
  // visible page.
  scrollTo(630);
  assertInView({ first: 21, last: 40 }, "Items 21 to 40 in view");
  assertFocus({ index: 42 }, "Focus remains below the view");
  assertSelection([42], "Selection remains below the view");

  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 21, last: 40 }, "Same items in view");
  assertFocus({ index: 21 }, "Focus moves to the start of the visible page");
  assertSelection([21], "Selection moves to the start of the visible page");

  // PageUp with focus above the view. Focus should shift by one page, with
  // the previous focus at the bottom of the page.
  scrollTo(750);
  assertInView({ first: 25, last: 44 }, "Items 25 to 44 in view");
  assertFocus({ index: 21 }, "Focus remains above the view");
  assertSelection([21], "Selection remains above the view");

  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView(
    { first: 2, last: 21 },
    "View shifts by a page relative to focus"
  );
  assertFocus({ index: 2 }, "Focus moves to start of new page");
  assertSelection([2], "Selection moves to start of new page");

  // Test when view does not exactly fit items.
  for (let sizeDiff of [0, 10, 15, 20]) {
    info(`Reducing widget size by ${sizeDiff}px`);
    widget.style[sizeName] = `${600 - sizeDiff}px`;

    // When we reduce the size of the view by half an item or more, we
    // reduce the page size from 20 to 19.
    // NOTE: At each sizeDiff still fits strictly more than 19 items in its
    // view.
    let pageSize = sizeDiff < 15 ? 20 : 19;

    // Make sure that Home and End keys scroll the view and clip the items
    // as expected.
    EventUtils.synthesizeKey("KEY_Home", {}, win);
    assertInView(
      { first: 0, last: 19, lastClipped: sizeDiff },
      `Start of view with last item clipped by ${sizeDiff}px`
    );
    assertFocus({ index: 0 }, "First item has focus");
    assertSelection([0], "First item is selected");

    EventUtils.synthesizeKey("KEY_End", {}, win);
    assertInView(
      { first: 50, firstClipped: sizeDiff, last: 69 },
      `End of view with first item clipped by ${sizeDiff}px`
    );
    assertFocus({ index: 69 }, "Last item has focus");
    assertSelection([69], "Last item is selected");

    for (let lastClipped of [0, 10, 15, 20]) {
      info(`Testing PageDown with last item clipped by ${lastClipped}px`);
      // Across all sizeDiff and lastClipped values we still want the last
      // item to be index 21 clipped by lastClipped.
      // E.g. when sizeDiff is 10 and lastClipped is 10, then the scroll
      // will be 60px and the first item will be index 2 with no clipping.
      // But when the sizeDiff is 10 and the lastClipped is 20, then the
      // scroll will be 50px and the first item will be index 1 with 20px
      // clipping.
      let scroll = 60 + sizeDiff - lastClipped;
      scrollTo(scroll);
      let first = Math.floor(scroll / 30);
      let firstClipped = scroll % 30;
      clickWidgetItem(3, {});
      assertInView(
        { first, firstClipped, last: 21, lastClipped },
        `Last item 21 in view clipped by ${lastClipped}px`
      );
      assertFocus({ index: 3 }, "Focus on item 3");
      assertSelection([3], "Selection on item 3");

      EventUtils.synthesizeKey("KEY_PageDown", {}, win);
      let pageEnd;
      if (lastClipped < 15) {
        // The last item is more than half in view, so counts as part of the
        // page.
        // NOTE: Index of the first item is always "2", even if it was "1"
        // before the scroll, because the view fits (19, 20] items.
        assertInView(
          { first: 2, firstClipped: sizeDiff, last: 21 },
          "Scrolls down to fully include the last item 21"
        );
        pageEnd = 21;
      } else {
        // The last item is half or less in view, so only the one before it
        // counts as being part of the page.
        assertInView(
          { first, firstClipped, last: 21, lastClipped },
          "Same view"
        );
        pageEnd = 20;
      }
      assertFocus({ index: pageEnd }, "Focus moves to pageEnd");
      assertSelection([pageEnd], "Selection moves to pageEnd");

      // Reset scroll to test scrolling when the focus is already at the
      // pageEnd.
      scrollTo(scroll);
      assertInView(
        { first, firstClipped, last: 21, lastClipped },
        `Last item 21 in view clipped by ${lastClipped}px`
      );

      // PageDown again will move by a page. The new end of the page will be
      // scrolled just into view at the bottom.
      EventUtils.synthesizeKey("KEY_PageDown", {}, win);
      let newPageEnd = pageEnd + pageSize - 1;
      // NOTE: If the previous pageEnd would fit mostly in view, then we
      // expect the first item in the view to be this item. Otherwise, we
      // expect it to be the one before, which will ensure the previous
      // pageEnd is fully visible.
      firstClipped = sizeDiff;
      first = sizeDiff < 15 ? pageEnd : pageEnd - 1;
      assertInView(
        { first, firstClipped, last: newPageEnd },
        "New page end scrolled into view, previous page end mostly visible"
      );
      assertFocus({ index: newPageEnd }, "Focus moves to end of new page");
      assertSelection([newPageEnd], "Selection moves to end of new page");

      // PageUp reverses the focus.
      // We don't test the the view since that is handled lower down.
      EventUtils.synthesizeKey("KEY_PageUp", {}, win);
      assertFocus({ index: pageEnd }, "Focus returns to pageEnd");
      assertSelection([pageEnd], "Selection returns to pageEnd");
    }

    for (let firstClipped of [0, 10, 15, 20]) {
      // Across all sizeDiff and firstClipped values we still want the first
      // item to be index 24 clipped by firstClipped.
      // E.g. when sizeDiff is 10 and firstClipped is 10, then the scroll
      // will be 730px and the last item will be index 44 with no clipping.
      // But when the sizeDiff is 10 and the firstClipped is 0, then the
      // scroll will be 720px and the last item will be index 43 with 10px
      // clipping.
      info(`Testing PageUp with first item clipped by ${firstClipped}px`);
      scrollTo(720 + firstClipped);
      let viewEnd = 720 + firstClipped + 600 - sizeDiff;
      let last = Math.floor(viewEnd / 30);
      let lastClipped = 30 - (viewEnd % 30);
      clickWidgetItem(42, {});
      assertInView(
        { first: 24, firstClipped, last, lastClipped },
        `First item 24 in view clipped by ${firstClipped}px`
      );
      assertFocus({ index: 42 }, "Focus on item 42");
      assertSelection([42], "Selection on item 42");

      EventUtils.synthesizeKey("KEY_PageUp", {}, win);
      let pageStart;
      if (firstClipped < 15) {
        // The first item is more than half in view, so counts as part of
        // the page.
        // NOTE: Index of the last item is always "43", even if it was "44"
        // before the scroll, because the view fits (19, 20] items.
        assertInView(
          { first: 24, last: 43, lastClipped: sizeDiff },
          "Scrolls up to fully include the first item 24"
        );
        pageStart = 24;
      } else {
        // The first item is half or less in view, so only the one after it
        // counts as being part of the page.
        assertInView(
          { first: 24, firstClipped, last, lastClipped },
          "Same view"
        );
        pageStart = 25;
      }
      assertFocus({ index: pageStart }, "Focus moves to pageStart");
      assertSelection([pageStart], "Selection moves to pageStart");

      // Reset scroll.
      scrollTo(720 + firstClipped);
      assertInView(
        { first: 24, firstClipped, last, lastClipped },
        `First item 24 in view clipped by ${firstClipped}px`
      );

      // PageUp again will move by a page. The new start of the page will be
      // scrolled just into view at the top.
      EventUtils.synthesizeKey("KEY_PageUp", {}, win);
      let newPageStart = pageStart - pageSize + 1;
      // NOTE: If the previous pageStart would fit mostly in view, then we
      // expect the last item in the view to be this item. Otherwise, we
      // expect it to be the one after, which will ensure the previous
      // pageStart is fully visible.
      lastClipped = sizeDiff;
      last = sizeDiff < 15 ? pageStart : pageStart + 1;
      assertInView(
        { first: newPageStart, last, lastClipped },
        "New page end scrolled into view, previous page end mostly visible"
      );
      assertFocus({ index: newPageStart }, "Focus moves to start of new page");
      assertSelection([newPageStart], "Selection moves to start of new page");

      // PageDown reverses the focus.
      // We don't test the the view since that is handled further up.
      EventUtils.synthesizeKey("KEY_PageDown", {}, win);
      assertFocus({ index: pageStart }, "Focus returns to pageStart");
      assertSelection([pageStart], "Selection returns to pageStart");
    }
  }

  // When widget only fits 1 visible item or less.
  for (let size of [10, 20, 30, 45, 50]) {
    info(`Resizing widget to ${size}px`);
    widget.style[sizeName] = `${size}px`;

    scrollTo(600);
    // When the view size is less than the size of an item, we cannot always
    // click the center of the item, so we need to click the start instead.
    let { xStart, yStart } = getStartEndBoundary(widget.items[20].element);
    EventUtils.synthesizeMouseAtPoint(xStart, yStart, {}, win);
    let last = size > 30 ? 21 : 20;
    let lastClipped = size > 30 ? 60 - size : 30 - size;
    assertInView({ first: 20, last, lastClipped }, "Small number of items");
    assertFocus({ index: 20 }, "Focus on item 20");
    assertSelection([20], "Item 20 selected");

    EventUtils.synthesizeKey("KEY_PageDown", {}, win);
    if (size <= 45) {
      // Only 1 or 0 items fit on the page, so does nothing.
      assertInView({ first: 20, last, lastClipped }, "Same view");
      assertFocus({ index: 20 }, "Same focus");
      assertSelection([20], "Same selected");
    } else {
      // 2 items fit visibly on the page, so acts as normal.
      assertInView(
        { first: 20, firstClipped: lastClipped, last: 21 },
        "Last item scrolled into view"
      );
      assertFocus({ index: 21 }, "Focus increases by one");
      assertSelection([21], "Selected moves to focus");
    }

    scrollTo(660 - size);
    let { xEnd, yEnd } = getStartEndBoundary(widget.items[21].element);
    EventUtils.synthesizeMouseAtPoint(xEnd, yEnd, {}, win);
    let first = size > 30 ? 20 : 21;
    let firstClipped = size > 30 ? 60 - size : 30 - size;
    assertInView({ first, firstClipped, last: 21 }, "Small number of items");
    assertFocus({ index: 21 }, "Focus on item 21");
    assertSelection([21], "Item 21 selected");

    EventUtils.synthesizeKey("KEY_PageUp", {}, win);
    if (size <= 45) {
      // Only 1 or 0 items fit on the page, so does nothing.
      assertInView({ first, firstClipped, last: 21 }, "Same view");
      assertFocus({ index: 21 }, "Same focus");
      assertSelection([21], "Same selected");
    } else {
      // 2 items fit visibly on the page, so acts as normal.
      assertInView(
        { first: 20, last: 21, lastClipped: firstClipped },
        "First item scrolled into view"
      );
      assertFocus({ index: 20 }, "Focus decreases by one");
      assertSelection([20], "Selected moves to focus");
    }
  }
  widget.style[sizeName] = null;

  // Disable page navigation.
  // This would be used when the item sizes or the page layout do not allow
  // for page navigation, or if PageUp and PageDown should be used for something
  // else.
  widget.toggleAttribute("no-pages", true);

  let gotKeys = [];
  let keydownListener = event => {
    gotKeys.push(event.key);
  };
  win.document.body.addEventListener("keydown", keydownListener);
  scrollTo(600);
  clickWidgetItem(20, {});
  assertInView({ first: 20, last: 39 }, "Items 20 to 39 in view");
  assertFocus({ index: 20 }, "First item focused");
  assertSelection([20], "First item selected");

  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 20, last: 39 }, "Same view");
  assertFocus({ index: 20 }, "Same focus");
  assertSelection([20], "Same selected");
  Assert.deepEqual(gotKeys, ["PageUp"], "PageUp reaches document body");
  gotKeys = [];
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 20, last: 39 }, "Same view");
  assertFocus({ index: 20 }, "Same focus");
  assertSelection([20], "Same selected");
  Assert.deepEqual(gotKeys, ["PageDown"], "PageDown reaches document body");
  gotKeys = [];

  clickWidgetItem(39, {});
  assertInView({ first: 20, last: 39 }, "Items 20 to 39 in view");
  assertFocus({ index: 39 }, "Last item focused");
  assertSelection([39], "Last item selected");

  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertInView({ first: 20, last: 39 }, "Same view");
  assertFocus({ index: 39 }, "Same focus");
  assertSelection([39], "Same selected");
  Assert.deepEqual(gotKeys, ["PageUp"], "PageUp reaches document body");
  gotKeys = [];
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertInView({ first: 20, last: 39 }, "Same view");
  assertFocus({ index: 39 }, "Same focus");
  assertSelection([39], "Same selected");
  Assert.deepEqual(gotKeys, ["PageDown"], "PageDown reaches document body");
  gotKeys = [];

  widget.removeAttribute("no-pages");

  // With page navigation enabled key-presses do not reach the document body.
  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  Assert.deepEqual(gotKeys, [], "No key reaches document body");
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  Assert.deepEqual(gotKeys, [], "No key reaches document body");

  win.document.body.removeEventListener("keydown", keydownListener);

  // Test with modifiers.
  for (let { shiftKey, ctrlKey } of [
    { shiftKey: true, ctrlKey: true },
    { shiftKey: false, ctrlKey: true },
    { shiftKey: true, ctrlKey: false },
  ]) {
    info(
      `Pressing ${ctrlKey ? "Ctrl+" : ""}${shiftKey ? "Shift+" : ""}PageUp/Down`
    );
    EventUtils.synthesizeKey("KEY_Home", {}, win);
    EventUtils.synthesizeKey(forwardKey, {}, win);
    assertInView({ first: 0, last: 19 }, "First 20 items in view");
    assertFocus({ index: 1 }, "Item 1 has focus");
    assertSelection([1], "Item 1 is selected");

    EventUtils.synthesizeKey("KEY_PageDown", { ctrlKey, shiftKey }, win);
    assertInView({ first: 0, last: 19 }, "Same view");
    if (
      (ctrlKey && shiftKey) ||
      model == "focus" ||
      (model == "browse" && shiftKey)
    ) {
      // Does nothing.
      assertFocus({ index: 1 }, "Same focus");
      assertSelection([1], "Same selected");
      // Move focus to the end of the view.
      clickWidgetItem(19, {});
      assertFocus({ index: 19 }, "Focus at end of page");
      assertSelection([19], "Selected at end of page");
    } else {
      assertFocus({ index: 19 }, "Focus moves to end of page");
      if (ctrlKey) {
        // Splits focus from selected.
        assertSelection([1], "Same selected");
      } else {
        assertSelection(range(1, 19), "Range selection from 1 to 19");
      }
    }
    // And again, with focus at the end of the page.
    EventUtils.synthesizeKey("KEY_PageDown", { ctrlKey, shiftKey }, win);
    if (
      (ctrlKey && shiftKey) ||
      model == "focus" ||
      (model == "browse" && shiftKey)
    ) {
      // Does nothing.
      assertInView({ first: 0, last: 19 }, "Same view");
      assertFocus({ index: 19 }, "Same focus");
      assertSelection([19], "Same selected");
    } else {
      assertInView({ first: 19, last: 38 }, "View scrolls to focus");
      assertFocus({ index: 38 }, "Focus moves to end of new page");
      if (ctrlKey) {
        // Splits focus from selected.
        assertSelection([1], "Same selected");
      } else {
        assertSelection(range(1, 38), "Range selection from 1 to 38");
      }
    }

    EventUtils.synthesizeKey("KEY_End", {}, win);
    EventUtils.synthesizeKey(backwardKey, {}, win);
    assertInView({ first: 50, last: 69 }, "Last 20 items in view");
    assertFocus({ index: 68 }, "Item 68 has focus");
    assertSelection([68], "Item 68 is selected");

    EventUtils.synthesizeKey("KEY_PageUp", { ctrlKey, shiftKey }, win);
    assertInView({ first: 50, last: 69 }, "Same view");
    if (
      (ctrlKey && shiftKey) ||
      model == "focus" ||
      (model == "browse" && shiftKey)
    ) {
      // Does nothing.
      assertFocus({ index: 68 }, "Same focus");
      assertSelection([68], "Same selected");
      // Move focus to the end of the view.
      clickWidgetItem(50, {});
      assertFocus({ index: 50 }, "Focus at start of page");
      assertSelection([50], "Selected at start of page");
    } else {
      assertFocus({ index: 50 }, "Focus moves to start of page");
      if (ctrlKey) {
        // Splits focus from selected.
        assertSelection([68], "Same selected");
      } else {
        assertSelection(range(50, 19), "Range selection from 50 to 68");
      }
    }
    // And again, with focus at the start of the page.
    EventUtils.synthesizeKey("KEY_PageUp", { ctrlKey, shiftKey }, win);
    if (
      (ctrlKey && shiftKey) ||
      model == "focus" ||
      (model == "browse" && shiftKey)
    ) {
      // Does nothing.
      assertInView({ first: 50, last: 69 }, "Same view");
      assertFocus({ index: 50 }, "Same focus");
      assertSelection([50], "Same selected");
    } else {
      assertInView({ first: 31, last: 50 }, "View scrolls to focus");
      assertFocus({ index: 31 }, "Focus moves to start of new page");
      if (ctrlKey) {
        // Splits focus from selected.
        assertSelection([68], "Same selected");
      } else {
        assertSelection(range(31, 38), "Range selection from 31 to 68");
      }
    }
  }

  // Does nothing with an empty widget.
  reset({ model, direction });
  stepFocus(true, { element: widget }, "Focus on empty widget");
  assertState([], "Empty");
  EventUtils.synthesizeKey("KEY_PageDown", {}, win);
  assertFocus({ element: widget }, "No change in focus");
  assertState([], "Empty");
  EventUtils.synthesizeKey("KEY_PageUp", {}, win);
  assertFocus({ element: widget }, "No change in focus");
  assertState([], "Empty");
}

// Test that pressing PageUp or PageDown shifts the view according to the
// visible items.
add_task(function test_page_navigation() {
  for (let model of selectionModels) {
    subtest_page_navigation(model, "top-to-bottom", {
      sizeName: "height",
      forwardKey: "KEY_ArrowDown",
      backwardKey: "KEY_ArrowUp",
      scrollTo: pos => {
        widget.scrollTop = pos;
      },
      getStartEnd: rect => {
        return {
          start: rect.top,
          end: rect.bottom,
          xStart: rect.right - 1,
          xEnd: rect.left + 1,
          yStart: rect.top + 1,
          yEnd: rect.bottom - 1,
        };
      },
    });
    subtest_page_navigation(model, "right-to-left", {
      sizeName: "width",
      forwardKey: "KEY_ArrowLeft",
      backwardKey: "KEY_ArrowRight",
      scrollTo: pos => {
        widget.scrollLeft = -pos;
      },
      getStartEnd: rect => {
        return {
          start: -rect.right,
          end: -rect.left,
          xStart: rect.right - 1,
          xEnd: rect.left + 1,
          yStart: rect.top + 1,
          yEnd: rect.bottom - 1,
        };
      },
    });
    subtest_page_navigation(model, "left-to-right", {
      sizeName: "width",
      forwardKey: "KEY_ArrowRight",
      backwardKey: "KEY_ArrowLeft",
      scrollTo: pos => {
        widget.scrollLeft = pos;
      },
      getStartEnd: rect => {
        return {
          start: rect.left,
          end: rect.right,
          xStart: rect.left + 1,
          xEnd: rect.right - 1,
          yStart: rect.top + 1,
          yEnd: rect.bottom - 1,
        };
      },
    });
  }
});

// Using Space to select items.
add_task(function test_space_selection() {
  for (let model of selectionModels) {
    reset({ model, direction: "right-to-left" });
    widget.addItems(0, ["First", "Second", "Third", "Fourth"]);

    stepFocus(true, { index: 0 }, "Move focus to first item");
    assertSelection([0], "First item is selected");

    // Selecting an already selected item does nothing.
    EventUtils.synthesizeKey(" ", {}, win);
    assertFocus({ index: 0 }, "First item still has focus");
    assertSelection([0], "First item is still selected");

    if (model == "focus") {
      // Just move to second item as set up for the loop.
      EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    } else {
      // Selecting a non-selected item will move selection to it.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Second item has focus");
      assertSelection([0], "First item is still selected");
      EventUtils.synthesizeKey(" ", {}, win);
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([1], "Second item becomes selected");
    }

    // Ctrl + Space will toggle the selection if multi-selection is supported.
    EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
    if (model == "focus") {
      // Did nothing.
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([1], "Second item is still selected");
    } else if (model == "browse") {
      // Did nothing.
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([1], "Second item is still selected");
      // Make sure nothing happens when on a non-selected item as well.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item has focus");
      assertSelection([1], "Second item is still selected");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item still has focus");
      assertSelection([1], "Second item is still selected");
      // Restore the previous state.
      EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    } else {
      // Unselected the item.
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([], "Second item was un-selected");
      // Toggle again.
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([1], "Second item was re-selected");

      // Do on another index.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 3 }, "Fourth item has focus");
      assertSelection([1], "Second item is still selected");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 3 }, "Fourth item still has focus");
      assertSelection([1, 3], "Fourth item becomes selected as well");

      // Move to third without clearing.
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item has focus");
      assertSelection([1, 3], "Fourth and second item remain selected");

      // Merge the two ranges together.
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item still has focus");
      assertSelection([1, 2, 3], "Third item becomes selected");

      // Shrink the range at the end.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 3 }, "Fourth item has focus");
      assertSelection([1, 2, 3], "Same selection");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 3 }, "Fourth item still has focus");
      assertSelection([1, 2], "Fourth item unselected");

      // Shrink the range at the start.
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Second item has focus");
      assertSelection([1, 2], "Same selection");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([2], "Second item unselected");

      // No selection.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item has focus");
      assertSelection([2], "Same selection");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item still has focus");
      assertSelection([], "Third item unselected");

      // Using arrow keys without modifier will re-introduce a single selection.
      EventUtils.synthesizeKey("KEY_ArrowRight");
      assertFocus({ index: 1 }, "Second item has focus");
      assertSelection([1], "Second item becomes selected");

      // Grow range at the start.
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 0 }, "First item has focus");
      assertSelection([1], "Same selection");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 0 }, "First item still has focus");
      assertSelection([0, 1], "First item becomes selected");

      // Grow range at the end.
      EventUtils.synthesizeKey("KEY_End", { ctrlKey: true }, win);
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item has focus");
      assertSelection([0, 1], "Same selection");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item still has focus");
      assertSelection([0, 1, 2], "Third item becomes selected");

      // Split the range in half.
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Second item has focus");
      assertSelection([0, 1, 2], "Same selection");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([0, 2], "Second item unselected");

      // Pressing Space without a modifier clears the multi-selection.
      EventUtils.synthesizeKey(" ", {}, win);
    }

    // Make sure we are in the expected shared state between models.
    assertFocus({ index: 1 }, "Second item has focus");
    assertSelection([1], "Second item is selected");

    // Shift + Space will do nothing.
    for (let ctrlKey of [false, true]) {
      info(`Pressing ${ctrlKey ? "Ctrl+" : ""}Shift+space on item`);
      // On selected item.
      EventUtils.synthesizeKey(" ", { ctrlKey, shiftKey: true }, win);
      assertFocus({ index: 1 }, "Second item still has focus");
      assertSelection([1], "Second item is still selected");

      if (model == "focus") {
        continue;
      }

      // On non-selected item.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Third item has focus");
      assertSelection([1], "Second item is still selected");
      EventUtils.synthesizeKey(" ", { ctrlKey, shiftKey: true }, win);
      assertFocus({ index: 2 }, "Third item still has focus");
      assertSelection([1], "Second item is still selected");

      // Restore for next loop.
      EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
      assertFocus({ index: 1 }, "Second item has focus");
      assertSelection([1], "Second item is selected");
    }
  }
});

// Clicking an item will focus and select it.
add_task(function test_clicking_items() {
  for (let model of selectionModels) {
    reset({ model, direction: "right-to-left" });
    widget.addItems(0, [
      "First",
      "Second",
      "Third",
      "Fourth",
      "Fifth",
      "Sixth",
      "Seventh",
      "Eighth",
    ]);

    assertFocus({ element: before }, "Focus initially outside widget");
    assertSelection([], "No initial selection");

    // Focus moves into widget, onto the clicked item.
    clickWidgetItem(1, {});
    assertFocus({ index: 1 }, "Focus clicked second item");
    assertSelection([1], "Selected clicked second item");

    // Focus moves to different item.
    clickWidgetItem(2, {});
    assertFocus({ index: 2 }, "Focus clicked third item");
    assertSelection([2], "Selected clicked third item");

    // Click same item.
    clickWidgetItem(2, {});
    assertFocus({ index: 2 }, "Focus remains on third item");
    assertSelection([2], "Selected remains on third item");

    // Focus outside widget, focus moves but selection remains.
    before.focus();
    assertFocus({ element: before }, "Focus outside widget");
    assertSelection([2], "Selected remains on third item");

    // Clicking same item will return focus to it.
    clickWidgetItem(2, {});
    assertFocus({ index: 2 }, "Focus returns to third item");
    assertSelection([2], "Selected remains on third item");

    // Do the same, but return to a different item.
    before.focus();
    assertFocus({ element: before }, "Focus outside widget");
    assertSelection([2], "Selected remains on third item");

    // Clicking same item will return focus to it.
    clickWidgetItem(1, {});
    assertFocus({ index: 1 }, "Focus moves to second item");
    assertSelection([1], "Selected moves to second item");

    // Switching to keyboard works.
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    assertFocus({ index: 0 }, "Focus moves to first item");
    assertSelection([0], "Selected moves to first item");

    // Returning to mouse works.
    clickWidgetItem(1, {});
    assertFocus({ index: 1 }, "Focus moves to second item");
    assertSelection([1], "Selected moves to second item");

    // Toggle selection with Ctrl+Click.
    clickWidgetItem(3, { ctrlKey: true });
    if (model == "browse-multi") {
      assertFocus({ index: 3 }, "Focus moves to fourth item");
      assertSelection([1, 3], "Fourth item is selected");

      clickWidgetItem(7, { ctrlKey: true });
      assertFocus({ index: 7 }, "Focus moves to eighth item");
      assertSelection([1, 3, 7], "Eighth item selected");

      // Extend selection range by one after.
      clickWidgetItem(4, { ctrlKey: true });
      assertFocus({ index: 4 }, "Focus moves to fifth item");
      assertSelection([1, 3, 4, 7], "Fifth item is selected");

      // Extend selection range by one before.
      clickWidgetItem(6, { ctrlKey: true });
      assertFocus({ index: 6 }, "Focus moves to seventh item");
      assertSelection([1, 3, 4, 6, 7], "Seventh item is selected");

      // Merge the two ranges together.
      clickWidgetItem(5, { ctrlKey: true });
      assertFocus({ index: 5 }, "Focus moves to sixth item");
      assertSelection([1, 3, 4, 5, 6, 7], "Sixth item is selected");

      // Reverse by unselecting.
      clickWidgetItem(7, { ctrlKey: true });
      assertFocus({ index: 7 }, "Focus moves to eight item");
      assertSelection([1, 3, 4, 5, 6], "Eight item is unselected");

      clickWidgetItem(3, { ctrlKey: true });
      assertFocus({ index: 3 }, "Focus moves to fourth item");
      assertSelection([1, 4, 5, 6], "Fourth item is unselected");

      // Split a range.
      clickWidgetItem(5, { ctrlKey: true });
      assertFocus({ index: 5 }, "Focus moves to sixth item");
      assertSelection([1, 4, 6], "Sixth item is unselected");

      clickWidgetItem(1, { ctrlKey: true });
      assertFocus({ index: 1 }, "Focus moves to second item");
      assertSelection([4, 6], "Second item is unselected");

      clickWidgetItem(6, { ctrlKey: true });
      assertFocus({ index: 6 }, "Focus moves to seventh item");
      assertSelection([4], "Seventh item is unselected");

      // Can get zero-selection.
      clickWidgetItem(4, { ctrlKey: true });
      assertFocus({ index: 4 }, "Focus moves to fifth item");
      assertSelection([], "None selected");

      // Get into the same state as the other case.
      clickWidgetItem(1, { ctrlKey: true });
      assertFocus({ index: 1 }, "Focus moves to second item");
      assertSelection([1], "Second item is selected");
    } else {
      // No multi-selection, so does nothing.
      assertFocus({ index: 1 }, "Focus remains on second item");
      assertSelection([1], "Second item remains selected");
    }

    // Ctrl+Shift+Click does nothing in all models.
    clickWidgetItem(2, { ctrlKey: true, shiftKey: true });
    assertFocus({ index: 1 }, "Focus remains on second item");
    assertSelection([1], "Second item remains selected");
  }
});

add_task(function test_select_all() {
  for (let model of selectionModels) {
    reset({ model, direction: "right-to-left" });
    widget.addItems(0, ["First", "Second", "Third", "Fourth", "Fifth"]);

    stepFocus(true, { index: 0 }, "Move focus to first item");
    assertSelection([0], "First item is selected");

    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    assertFocus({ index: 1 }, "Focus on second item");
    assertSelection([1], "Second item is selected");

    selectAllShortcut();

    assertFocus({ index: 1 }, "Focus remains on second item");
    if (model == "browse-multi") {
      assertSelection([0, 1, 2, 3, 4], "All items are selected");
      // Can insert a hole.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Focus moves to third item");
      assertSelection([0, 1, 2, 3, 4], "All items are still selected");
      EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
      assertFocus({ index: 2 }, "Focus remains on third item");
      assertSelection([0, 1, 3, 4], "Third item was unselected");
      EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
      assertFocus({ index: 1 }, "Focus moves to the second item");
      assertSelection([0, 1, 3, 4], "Selection remains the same");
      EventUtils.synthesizeKey(" ", {}, win);
      assertFocus({ index: 1 }, "Focus remains on the second item");
      assertSelection([1], "Only the second item is selected");
    } else {
      // Did nothing.
      assertSelection([1], "Second item is still selected");
    }

    // Wrong platform modifier does nothing.
    EventUtils.synthesizeKey(
      "a",
      AppConstants.platform == "macosx" ? { ctrlKey: true } : { metaKey: true },
      win
    );
    assertFocus({ index: 1 }, "Focus remains on second item");
    assertSelection([1], "Second item still selected");
  }
});

// Holding the shift key should perform a range selection if multi-selection is
// supported by the model.
add_task(function test_range_selection() {
  for (let model of selectionModels) {
    reset({ model, direction: "right-to-left" });
    widget.addItems(0, ["First", "Second", "Third", "Fourth", "Fifth"]);

    stepFocus(true, { index: 0 }, "Move focus to first item");
    assertSelection([0], "First item is selected");

    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);

    assertFocus({ index: 2 }, "Focus on third item");
    assertSelection([2], "Third item is selected");

    // Nothing happens with Ctrl+Shift in any model.
    EventUtils.synthesizeKey(
      "KEY_ArrowLeft",
      { shiftKey: true, ctrlKey: true },
      win
    );
    assertFocus({ index: 2 }, "Focus remains on third item");
    assertSelection([2], "Only second item is selected");
    EventUtils.synthesizeKey(
      "KEY_ArrowRight",
      { shiftKey: true, ctrlKey: true },
      win
    );
    assertFocus({ index: 2 }, "Focus remains on third item");
    assertSelection([2], "Only second item is selected");

    // With just Shift modifier.
    if (model == "focus" || model == "browse") {
      // No range selection.
      EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
      assertFocus({ index: 2 }, "Focus remains on third item");
      assertSelection([2], "Only second item is selected");

      EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
      assertFocus({ index: 2 }, "Focus remains on third item");
      assertSelection([2], "Only second item is selected");

      clickWidgetItem(3, { shiftKey: true });
      assertFocus({ index: 2 }, "Focus remains on third item");
      assertSelection([2], "Only second item is selected");

      clickWidgetItem(1, { shiftKey: true });
      assertFocus({ index: 2 }, "Focus remains on third item");
      assertSelection([2], "Only second item is selected");
      continue;
    }

    // Range selection with shift key.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 3 }, "Focus on fourth item");
    assertSelection([2, 3], "Select from third to fourth item");

    // Reverse
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 2 }, "Focus on third item");
    assertSelection([2], "Select from third to same item");

    // Go back another step.
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 1 }, "Focus on second item");
    assertSelection([1, 2], "Third to second items are selected");

    // Split focus from selection.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertFocus({ index: 2 }, "Focus on third item");
    assertSelection([1, 2], "Third to second items are still selected");

    // Back to range selection.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 3 }, "Focus on fourth item");
    assertSelection([2, 3], "Third to fourth items are selected");

    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 4 }, "Focus on fifth item");
    assertSelection([2, 3, 4], "Third to fifth items are selected");

    // Moving without a modifier breaks the range.
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    assertFocus({ index: 4 }, "Focus remains on final fifth item");
    assertSelection([4], "Fifth item is selected");

    // Again at the middle.
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([3, 4], "Fifth to fourth items are selected");
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    assertFocus({ index: 2 }, "Focus moves to third item");
    assertSelection([2], "Only third item is selected");

    // Home and End also work.
    EventUtils.synthesizeKey("KEY_Home", { shiftKey: true }, win);
    assertFocus({ index: 0 }, "Focus moves to first item");
    assertSelection([0, 1, 2], "Up to third item is selected");

    EventUtils.synthesizeKey("KEY_End", { shiftKey: true }, win);
    assertFocus({ index: 4 }, "Focus moves to last item");
    assertSelection([2, 3, 4], "Third item and above is selected");

    // Ctrl+A breaks range selection sequence, so we no longer select around the
    // third item when we go back to using Shift+Arrow.
    selectAllShortcut();
    assertFocus({ index: 4 }, "Focus remains on last item");
    assertSelection([0, 1, 2, 3, 4], "All items are selected");
    // The new shift+range will be from the focus index (the fifth item) rather
    // than the third item used for the previous range.
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([3, 4], "Fifth to fourth item are selected");

    // Ctrl+Space also breaks range selection sequence.
    EventUtils.synthesizeKey("KEY_Home", { ctrlKey: true }, win);
    assertFocus({ index: 0 }, "Focus moves to first item");
    assertSelection([3, 4], "Range selection remains");
    EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
    assertFocus({ index: 0 }, "Focus still on first item");
    assertSelection([0, 3, 4], "First item added to selection");
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 1 }, "Focus moves to second item");
    assertSelection([0, 1], "First to second item are selected");

    // Same when unselecting.
    EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
    assertFocus({ index: 1 }, "Focus remains on second item");
    assertSelection([0], "Second item is no longer selected");

    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 2 }, "Focus moves to third item");
    assertSelection([1, 2], "Second to third item are selected");

    // Same when using setItemSelected API
    widget.setItemSelected(4, true);
    assertFocus({ index: 2 }, "Focus remains on third item");
    assertSelection([1, 2, 4], "Fifth item becomes selected");

    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([2, 3], "Third to fourth item are selected");
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 4 }, "Focus moves to fifth item");
    assertSelection([2, 3, 4], "Third to fifth item are selected");

    widget.setItemSelected(3, false);
    assertFocus({ index: 4 }, "Focus remains on fifth item");
    assertSelection([2, 4], "Fourth item becomes unselected");

    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([3, 4], "Fifth to fourth item are selected");

    // Even when the selection state does not change.
    widget.setItemSelected(3, true);
    assertFocus({ index: 3 }, "Focus remains on fourth item");
    assertSelection([3, 4], "Same selection");
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 2 }, "Focus moves to third item");
    assertSelection([2, 3], "Fourth to third item are selected");
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 1 }, "Focus moves to second item");
    assertSelection([1, 2, 3], "Fourth to second item are selected");

    widget.setItemSelected(4, false);
    assertFocus({ index: 1 }, "Focus remains on second item");
    assertSelection([1, 2, 3], "Same selection");
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 2 }, "Focus moves to third item");
    assertSelection([1, 2], "Second to third item are selected");

    // Same when selecting with space (no modifier).
    EventUtils.synthesizeKey(" ", {}, win);
    assertFocus({ index: 2 }, "Focus remains on third item");
    assertSelection([2], "Third item is selected");

    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([2, 3], "Third to fourth item are selected");

    // Same when using the selectSingleItem API.
    widget.selectSingleItem(1);
    assertFocus({ index: 1 }, "Focus moves to second item");
    assertSelection([1], "Second item is selected");

    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 0 }, "Focus moves to first item");
    assertSelection([0, 1], "Second to first item are selected");

    // If focus goes out and we return, the range origin is remembered.
    stepFocus(true, { element: after }, "Move focus outside the widget");
    assertSelection([0, 1], "Second to first item are still selected");
    stepFocus(false, { index: 0 }, "Focus returns to the widget");
    assertSelection([0, 1], "Second to first item are still selected");

    EventUtils.synthesizeKey("KEY_End", { shiftKey: true }, win);
    assertFocus({ index: 4 }, "Focus moves to last item");
    assertSelection([1, 2, 3, 4], "Second to fifth item are selected");

    // Clicking empty space does not clear it.
    clickWidgetEmptySpace({});
    assertFocus({ index: 4 }, "Focus remains on last item");
    assertSelection([1, 2, 3, 4], "Second to fifth item are still selected");

    // Shift+Click an item will use the same range origin established by the
    // current selection.
    clickWidgetItem(3, { shiftKey: true });
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([1, 2, 3], "Second to fourth item are selected");

    // Clicking without the modifier breaks the range selection sequence.
    clickWidgetItem(2, {});
    assertFocus({ index: 2 }, "Focus moves to third item");
    assertSelection([2], "Only the third item is selected");

    // Shift click will select between the third item and the the clicked item.
    clickWidgetItem(4, { shiftKey: true });
    assertFocus({ index: 4 }, "Focus moves to fifth item");
    assertSelection([2, 3, 4], "Third to fifth item are selected");

    // Reverse direction about the same point.
    clickWidgetItem(0, { shiftKey: true });
    assertFocus({ index: 0 }, "Focus moves to first item");
    assertSelection([0, 1, 2], "Third to first item are selected");

    // Ctrl+Click breaks the range selection sequence.
    clickWidgetItem(1, { ctrlKey: true });
    assertFocus({ index: 1 }, "Focus moves to second item");
    assertSelection([0, 2], "Second item is unselected");
    clickWidgetItem(3, { shiftKey: true });
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([1, 2, 3], "Second to fourth item are selected");

    // Same when Ctrl+Click on non-selected.
    clickWidgetItem(4, { ctrlKey: true });
    assertFocus({ index: 4 }, "Focus moves to fifth item");
    assertSelection([1, 2, 3, 4], "Fifth item is selected");
    clickWidgetItem(3, { shiftKey: true });
    assertFocus({ index: 3 }, "Focus moves to fourth item");
    assertSelection([3, 4], "Fifth to fourth item are selected");

    // Selecting-all also breaks range selection sequence.
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertFocus({ index: 2 }, "Focus moves to third item");
    assertSelection([3, 4], "Same selection");

    selectAllShortcut();
    assertFocus({ index: 2 }, "Focus remains on third item");
    assertSelection([0, 1, 2, 3, 4], "All items selected");

    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertFocus({ index: 1 }, "Focus moves to second item");
    assertSelection([1, 2], "Third to second item selected");
  }
});

// Adding items to widget with existing items, should not change the selected
// item.
add_task(function test_add_items_to_nonempty() {
  for (let model of selectionModels) {
    reset({ model, direction: "right-to-left" });
    assertState([], "Empty");

    widget.addItems(0, ["0-add"]);
    stepFocus(true, { index: 0, text: "0-add" }, "Move focus to 0-add");
    assertState([{ text: "0-add", selected: true, focused: true }], "One item");

    // Add item after.
    widget.addItems(1, ["1-add"]);
    assertState(
      [{ text: "0-add", selected: true, focused: true }, { text: "1-add" }],
      "0-add still focused and selected"
    );

    // Add item before. 0-add moves to index 1.
    widget.addItems(0, ["2-add"]);
    assertState(
      [
        { text: "2-add" },
        { text: "0-add", selected: true, focused: true },
        { text: "1-add" },
      ],
      "0-add still focused and selected"
    );

    // Add several before.
    widget.addItems(1, ["3-add", "4-add", "5-add"]);
    assertState(
      [
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
        { text: "0-add", selected: true, focused: true },
        { text: "1-add" },
      ],
      "0-add still focused and selected"
    );

    // Key navigation works.
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    assertState(
      [
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add", selected: true, focused: true },
        { text: "0-add" },
        { text: "1-add" },
      ],
      "5-add becomes focused and selected"
    );

    // With focus outside the widget.
    reset({ model, direction: "right-to-left" });
    assertState([], "Empty");

    widget.addItems(0, ["0-add"]);
    stepFocus(true, { index: 0 }, "Move focus to 0-add");
    assertState([{ text: "0-add", selected: true, focused: true }], "One item");

    stepFocus(true, { element: after }, "Move focus to after widget");
    // Add after.
    widget.addItems(1, ["1-add", "2-add"]);
    assertState(
      [{ text: "0-add", selected: true }, { text: "1-add" }, { text: "2-add" }],
      "0-add still selected but not focused"
    );
    stepFocus(false, { index: 0 }, "Move focus back to 0-add");
    assertState(
      [
        { text: "0-add", selected: true, focused: true },
        { text: "1-add" },
        { text: "2-add" },
      ],
      "0-add selected and focused"
    );

    stepFocus(false, { element: before }, "Move focus to before widget");
    // Add before.
    widget.addItems(0, ["3-add", "4-add"]);
    assertState(
      [
        { text: "3-add" },
        { text: "4-add" },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add" },
      ],
      "0-add selected but not focused"
    );
    stepFocus(true, { index: 2 }, "Move focus back to 0-add");
    assertState(
      [
        { text: "3-add" },
        { text: "4-add" },
        { text: "0-add", selected: true, focused: true },
        { text: "1-add" },
        { text: "2-add" },
      ],
      "0-add selected and focused"
    );

    // With focus separate from selection.
    if (model == "focus") {
      continue;
    }

    reset({ model, direction: "right-to-left" });
    assertState([], "Empty");

    widget.addItems(0, ["0-add", "1-add", "2-add"]);
    assertState(
      [{ text: "0-add" }, { text: "1-add" }, { text: "2-add" }],
      "None selected or focused"
    );
    stepFocus(true, { index: 0 }, "Move focus to 0-add");

    // With selection after focus.
    EventUtils.synthesizeKey("KEY_End", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertState(
      [
        { text: "0-add" },
        { text: "1-add", focused: true },
        { text: "2-add", selected: true },
      ],
      "Selection after focus"
    );

    // Add after both selection and focus.
    widget.addItems(3, ["3-add"]);
    assertState(
      [
        { text: "0-add" },
        { text: "1-add", focused: true },
        { text: "2-add", selected: true },
        { text: "3-add" },
      ],
      "Same items selected and focused"
    );

    // Add before both selection and focus.
    widget.addItems(1, ["4-add"]);
    assertState(
      [
        { text: "0-add" },
        { text: "4-add" },
        { text: "1-add", focused: true },
        { text: "2-add", selected: true },
        { text: "3-add" },
      ],
      "Same items selected and focused"
    );

    // Before selection, after focus.
    widget.addItems(3, ["5-add"]);
    assertState(
      [
        { text: "0-add" },
        { text: "4-add" },
        { text: "1-add", focused: true },
        { text: "5-add" },
        { text: "2-add", selected: true },
        { text: "3-add" },
      ],
      "Same items selected and focused"
    );

    // Swap selection to be before focus.
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "0-add" },
        { text: "4-add", selected: true },
        { text: "1-add" },
        { text: "5-add", focused: true },
        { text: "2-add" },
        { text: "3-add" },
      ],
      "Selection before focus"
    );

    // After selection, before focus.
    widget.addItems(2, ["6-add", "7-add", "8-add"]);
    assertState(
      [
        { text: "0-add" },
        { text: "4-add", selected: true },
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add" },
        { text: "1-add" },
        { text: "5-add", focused: true },
        { text: "2-add" },
        { text: "3-add" },
      ],
      "Same items selected and focused"
    );

    // With multi-selection.
    if (model == "browse") {
      continue;
    }

    reset({ model, direction: "right-to-left" });
    assertState([], "Empty");

    widget.addItems(0, ["0-add", "1-add", "2-add"]);
    assertState(
      [{ text: "0-add" }, { text: "1-add" }, { text: "2-add" }],
      "None selected"
    );
    stepFocus(true, { index: 0 }, "Move focus to 0-add");

    // Select all.
    EventUtils.synthesizeKey("KEY_End", { shiftKey: true }, win);
    assertState(
      [
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true, focused: true },
      ],
      "All selected"
    );

    // Add after all.
    widget.addItems(3, ["3-add", "4-add", "5-add"]);
    assertState(
      [
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true, focused: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Same range selected"
    );

    // Can continue shift selection to newly added item
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true },
        { text: "3-add", selected: true, focused: true },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Range extended to new item"
    );

    // Add before all.
    widget.addItems(0, ["6-add", "7-add"]);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true },
        { text: "3-add", selected: true, focused: true },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Same range selected"
    );

    // Can continue shift selection about the "0-add" item.
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true, focused: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Range extended backward"
    );

    // And change direction of shift selection range.
    EventUtils.synthesizeKey("KEY_Home", { ctrlKey: true }, win);
    assertState(
      [
        { text: "6-add", focused: true },
        { text: "7-add" },
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Focus moves to first item"
    );
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add", selected: true, focused: true },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Selection pivoted about 0-add"
    );

    // Add items in the middle of the range. Selection in the range is not added
    // initially.
    widget.addItems(2, ["8-add", "9-add", "10-add"]);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add", selected: true, focused: true },
        { text: "8-add" },
        { text: "9-add" },
        { text: "10-add" },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Backward range selection with single gap"
    );

    // But continuing the shift selection will fill in the holes again.
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertState(
      [
        { text: "6-add", selected: true, focused: true },
        { text: "7-add", selected: true },
        { text: "8-add", selected: true },
        { text: "9-add", selected: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Backward range selection with no gap"
    );

    // Do the same but with a selection range moving forward and two holes.
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "9-add", selected: true },
        { text: "10-add", selected: true, focused: true },
        { text: "0-add" },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Forward range selection"
    );

    widget.addItems(3, ["11-add"]);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "11-add" },
        { text: "9-add", selected: true },
        { text: "10-add", selected: true, focused: true },
        { text: "0-add" },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Forward range selection with one gap"
    );

    widget.addItems(5, ["12-add", "13-add"]);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "11-add" },
        { text: "9-add", selected: true },
        { text: "12-add" },
        { text: "13-add" },
        { text: "10-add", selected: true, focused: true },
        { text: "0-add" },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Forward range selection with two gaps"
    );

    // Continuing the shift selection will fill in the holes.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "13-add", selected: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true, focused: true },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Extended range forward with no gaps"
    );

    // With multi-selection via toggling.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "13-add", selected: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add", selected: true, focused: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Selected 2-add"
    );
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "13-add", focused: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add", selected: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "De-selected 13-add"
    );

    widget.addItems(6, ["14-add", "15-add"]);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "14-add" },
        { text: "15-add" },
        { text: "13-add", focused: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add", selected: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Same selected items"
    );

    widget.addItems(3, ["16-add"]);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "8-add", selected: true },
        { text: "16-add" },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "14-add" },
        { text: "15-add" },
        { text: "13-add", focused: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add" },
        { text: "2-add", selected: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "Same selected items"
    );

    // With select-all
    selectAllShortcut();
    assertState(
      [
        { text: "6-add", selected: true },
        { text: "7-add", selected: true },
        { text: "8-add", selected: true },
        { text: "16-add", selected: true },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "14-add", selected: true },
        { text: "15-add", selected: true },
        { text: "13-add", selected: true, focused: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true },
        { text: "3-add", selected: true },
        { text: "4-add", selected: true },
        { text: "5-add", selected: true },
      ],
      "All items selected"
    );

    // Added items do not become selected.
    widget.addItems(4, ["17-add", "18-add"]);
    assertState(
      [
        { text: "6-add", selected: true },
        { text: "7-add", selected: true },
        { text: "8-add", selected: true },
        { text: "16-add", selected: true },
        { text: "17-add" },
        { text: "18-add" },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "14-add", selected: true },
        { text: "15-add", selected: true },
        { text: "13-add", selected: true, focused: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true },
        { text: "3-add", selected: true },
        { text: "4-add", selected: true },
        { text: "5-add", selected: true },
      ],
      "Added items not selected"
    );

    // Added items will be selected if we select-all again.
    selectAllShortcut();
    assertState(
      [
        { text: "6-add", selected: true },
        { text: "7-add", selected: true },
        { text: "8-add", selected: true },
        { text: "16-add", selected: true },
        { text: "17-add", selected: true },
        { text: "18-add", selected: true },
        { text: "11-add", selected: true },
        { text: "9-add", selected: true },
        { text: "12-add", selected: true },
        { text: "14-add", selected: true },
        { text: "15-add", selected: true },
        { text: "13-add", selected: true, focused: true },
        { text: "10-add", selected: true },
        { text: "0-add", selected: true },
        { text: "1-add", selected: true },
        { text: "2-add", selected: true },
        { text: "3-add", selected: true },
        { text: "4-add", selected: true },
        { text: "5-add", selected: true },
      ],
      "All items selected"
    );
  }
});

/**
 * Test that pressing a key on a non-empty widget that has focus on itself will
 * move to the expected index.
 *
 * @param {object} initialState - The initial state of the widget to set up.
 * @param {string} initialState.model - The selection model to use.
 * @param {string} initialState.direction - The layout direction of the widget.
 * @param {number} initialState.numItems - The number of items in the widget.
 * @param {Function} [initialState.scroll] - A method to call to scroll the
 *   widget.
 * @param {string} key - The key to press once the widget is set up.
 * @param {number} index - The expected index for the item that will receive
 *   focus after the key press.
 */
function subtest_keypress_on_focused_widget(initialState, key, index) {
  let { model, direction, numItems, scroll } = initialState;
  for (let ctrlKey of [false, true]) {
    for (let shiftKey of [false, true]) {
      info(
        `Adding items to empty ${direction} widget and then pressing ${
          ctrlKey ? "Ctrl+" : ""
        }${shiftKey ? "Shift+" : ""}${key}`
      );
      reset({ model, direction });

      stepFocus(true, { element: widget }, "Move focus onto empty widget");
      widget.addItems(
        0,
        range(0, numItems).map(i => `add-${i}`)
      );
      scroll?.();

      assertFocus(
        { element: widget },
        "Focus remains on the widget after adding items"
      );
      assertSelection([], "No items are selected yet");

      EventUtils.synthesizeKey(key, { ctrlKey, shiftKey }, win);
      if (
        (ctrlKey && shiftKey) ||
        (model == "browse" && shiftKey) ||
        (model == "focus" && (ctrlKey || shiftKey))
      ) {
        // Does nothing.
        assertFocus({ element: widget }, "Focus remains on widget");
        assertSelection([], "No change in selection");
        continue;
      }

      assertFocus({ index }, `Focus moves to ${index} after ${key}`);
      if (ctrlKey) {
        assertSelection([], `No selection if pressing Ctrl+${key}`);
      } else if (shiftKey) {
        assertSelection(
          range(0, index + 1),
          `Range selection from 0 to ${index} if pressing Shift+${key}`
        );
      } else {
        assertSelection([index], `Item selected after ${key}`);
      }
    }
  }
}

// If items are added to an empty widget that has focus, nothing happens
// initially. Arrow keys will focus the first item.
add_task(function test_add_items_to_empty_with_focus() {
  for (let model of selectionModels) {
    // Step navigation always takes us to the first item.
    subtest_keypress_on_focused_widget(
      { model, direction: "top-to-bottom", numItems: 3 },
      "KEY_ArrowUp",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "top-to-bottom", numItems: 3 },
      "KEY_ArrowDown",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "right-to-left", numItems: 3 },
      "KEY_ArrowRight",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "right-to-left", numItems: 3 },
      "KEY_ArrowLeft",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "left-to-right", numItems: 3 },
      "KEY_ArrowLeft",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "left-to-right", numItems: 3 },
      "KEY_ArrowRight",
      0
    );
    // Home also takes us to the first item.
    subtest_keypress_on_focused_widget(
      { model, direction: "top-to-bottom", numItems: 3 },
      "KEY_Home",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "right-to-left", numItems: 3 },
      "KEY_Home",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "left-to-right", numItems: 3 },
      "KEY_Home",
      0
    );
    // End takes us to the last item.
    subtest_keypress_on_focused_widget(
      { model, direction: "top-to-bottom", numItems: 3 },
      "KEY_End",
      2
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "right-to-left", numItems: 3 },
      "KEY_End",
      2
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "left-to-right", numItems: 3 },
      "KEY_End",
      2
    );
    // PageUp and PageDown take us to the start or end of the visible page.
    subtest_keypress_on_focused_widget(
      { model, direction: "top-to-bottom", numItems: 3 },
      "KEY_PageUp",
      0
    );
    subtest_keypress_on_focused_widget(
      { model, direction: "top-to-bottom", numItems: 3 },
      "KEY_PageDown",
      2
    );
    subtest_keypress_on_focused_widget(
      {
        model,
        direction: "top-to-bottom",
        numItems: 30,
        scroll: () => {
          widget.scrollTop = 270;
        },
      },
      "KEY_PageUp",
      9
    );
    subtest_keypress_on_focused_widget(
      {
        model,
        direction: "top-to-bottom",
        numItems: 30,
        scroll: () => {
          widget.scrollTop = 60;
        },
      },
      "KEY_PageDown",
      21
    );

    // Arrow keys in other directions do nothing.
    reset({ model, direction: "top-to-bottom" });
    stepFocus(true, { element: widget }, "Move focus onto empty widget");
    widget.addItems(0, ["First", "Second"]);
    for (let key of ["KEY_ArrowRight", "KEY_ArrowLeft"]) {
      EventUtils.synthesizeKey(key, {}, win);
      assertFocus({ element: widget }, `Focus remains on widget after ${key}`);
      assertSelection([], `No items become selected after ${key}`);
    }

    reset({ model, direction: "right-to-left" });
    stepFocus(true, { element: widget }, "Move focus onto empty widget");
    widget.addItems(0, ["First", "Second"]);
    for (let key of ["KEY_ArrowUp", "KEY_ArrowDown"]) {
      EventUtils.synthesizeKey(key, {}, win);
      assertFocus({ element: widget }, `Focus remains on widget after ${key}`);
      assertSelection([], `No items become selected after ${key}`);
    }

    // Pressing Space does nothing.
    reset({ model });
    stepFocus(true, { element: widget }, "Move focus onto empty widget");
    widget.addItems(0, ["First", "Second"]);
    for (let ctrlKey of [false, true]) {
      for (let shiftKey of [false, true]) {
        info(
          `Pressing ${ctrlKey ? "Ctrl+" : ""}${shiftKey ? "Shift+" : ""}Space`
        );
        EventUtils.synthesizeKey(" ", {}, win);
        assertFocus({ element: widget }, "Focus remains on widget after Space");
        assertSelection([], "No items become selected after Space");
      }
    }

    // Selecting all
    reset({ model });
    stepFocus(true, { element: widget }, "Move focus onto empty widget");
    widget.addItems(0, ["First", "Second", "Third"]);

    selectAllShortcut();
    assertFocus({ element: widget }, "Focus remains on the widget");
    if (model == "browse-multi") {
      assertSelection([0, 1, 2], "All items selected");
    } else {
      assertSelection([], "still no selection");
    }

    // Adding and then removing items does not set focus.
    reset({ model });
    stepFocus(true, { element: widget }, "Move focus onto empty widget");
    widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
    widget.removeItems(2, 2);
    assertState(
      [{ text: "First" }, { text: "Second" }],
      "No item focused or selected"
    );
    assertFocus({ element: widget }, "Focus remains on the widget");
    widget.removeItems(0, 1);
    assertState([{ text: "Second" }], "No item focused or selected");
    assertFocus({ element: widget }, "Focus remains on the widget");

    // Moving items does not set focus.
    reset({ model });
    stepFocus(true, { element: widget }, "Move focus onto empty widget");
    widget.addItems(0, ["First", "Second", "Third", "Fourth"]);
    widget.moveItems(1, 0, 2, false);
    assertState(
      [
        { text: "Second" },
        { text: "Third" },
        { text: "First" },
        { text: "Fourth" },
      ],
      "No item focused or selected"
    );
    assertFocus({ element: widget }, "Focus remains on the widget");
    widget.moveItems(0, 1, 3, true);
    assertState(
      [
        { text: "Fourth" },
        { text: "Second" },
        { text: "Third" },
        { text: "First" },
      ],
      "No item focused or selected"
    );
    assertFocus({ element: widget }, "Focus remains on the widget");

    // This does not effect clicking.
    // NOTE: case where widget does not initially have focus on clicking is
    // handled by test_initial_no_select_focus
    for (let ctrlKey of [false, true]) {
      for (let shiftKey of [false, true]) {
        info(
          `Adding items to empty focused widget and then ${
            ctrlKey ? "Ctrl+" : ""
          }${shiftKey ? "Shift+" : ""}Click`
        );
        reset({ model });
        stepFocus(true, { element: widget }, "Move focus onto empty widget");
        widget.addItems(0, ["First", "Second", "Third"]);

        // Clicking empty space does nothing.
        clickWidgetEmptySpace({ ctrlKey, shiftKey });
        assertFocus({ element: widget }, "Focus remains on widget");
        assertSelection([], "No item selected");

        // Clicking an item can change focus and selection.
        clickWidgetItem(1, { ctrlKey, shiftKey });
        if (
          (ctrlKey && shiftKey) ||
          ((model == "focus" || model == "browse") && (ctrlKey || shiftKey))
        ) {
          assertFocus({ element: widget }, "Focus remains on widget");
          assertSelection([], "No selection");
          continue;
        }
        assertFocus({ index: 1 }, "Focus moves to second item");
        if (shiftKey) {
          assertSelection([0, 1], "First and second item selected");
        } else {
          assertSelection([1], "Second item selected");
        }
      }
    }
  }
});

// Removing items from the widget with existing items, may change focus or
// selection if the corresponding item was removed.
add_task(function test_remove_items_nonempty() {
  for (let model of selectionModels) {
    reset({ model, direction: "right-to-left" });

    widget.addItems(0, ["0-add", "1-add", "2-add", "3-add", "4-add", "5-add"]);
    assertState(
      [
        { text: "0-add" },
        { text: "1-add" },
        { text: "2-add" },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "No initial focus or selection"
    );

    clickWidgetItem(2, {});
    assertState(
      [
        { text: "0-add" },
        { text: "1-add" },
        { text: "2-add", selected: true, focused: true },
        { text: "3-add" },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "2-add focused and selected"
    );

    // Remove one after.
    widget.removeItems(3, 1);
    assertState(
      [
        { text: "0-add" },
        { text: "1-add" },
        { text: "2-add", selected: true, focused: true },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "2-add still focused and selected"
    );

    // Remove one before.
    widget.removeItems(0, 1);
    assertState(
      [
        { text: "1-add" },
        { text: "2-add", selected: true, focused: true },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "2-add still focused and selected"
    );

    widget.addItems(0, ["6-add", "7-add"]);
    assertState(
      [
        { text: "6-add" },
        { text: "7-add" },
        { text: "1-add" },
        { text: "2-add", selected: true, focused: true },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "2-add still focused and selected"
    );

    // Remove several before.
    widget.removeItems(1, 2);
    assertState(
      [
        { text: "6-add" },
        { text: "2-add", selected: true, focused: true },
        { text: "4-add" },
        { text: "5-add" },
      ],
      "2-add still focused and selected"
    );

    // Remove selected and focused. Focus should move to the next item.
    widget.removeItems(1, 1);
    assertState(
      [
        { text: "6-add" },
        { text: "4-add", selected: true, focused: true },
        { text: "5-add" },
      ],
      "Selection and focus move to 4-add"
    );

    widget.addItems(0, ["8-add"]);
    widget.addItems(3, ["9-add", "10-add"]);
    assertState(
      [
        { text: "8-add" },
        { text: "6-add" },
        { text: "4-add", selected: true, focused: true },
        { text: "9-add" },
        { text: "10-add" },
        { text: "5-add" },
      ],
      "Selection and focus still on 4-add"
    );

    // Remove selected and focused, not at boundary.
    widget.removeItems(1, 3);
    assertState(
      [
        { text: "8-add" },
        { text: "10-add", selected: true, focused: true },
        { text: "5-add" },
      ],
      "Selection and focus move to 10-add"
    );

    // Remove last item whilst it has focus. Focus should move to the new last
    // item.
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    assertState(
      [
        { text: "8-add" },
        { text: "10-add" },
        { text: "5-add", selected: true, focused: true },
      ],
      "Last item is focused and selected"
    );

    widget.removeItems(2, 1);
    assertState(
      [{ text: "8-add" }, { text: "10-add", selected: true, focused: true }],
      "New last item is focused and selected"
    );

    // Delete focused whilst outside widget.
    widget.addItems(2, ["11-add"]);
    assertState(
      [
        { text: "8-add" },
        { text: "10-add", selected: true, focused: true },
        { text: "11-add" },
      ],
      "10-add is focused and selected"
    );
    stepFocus(false, { element: before });

    widget.removeItems(1, 1);
    assertFocus({ element: before }, "Focus remains outside widget");
    assertState(
      [{ text: "8-add" }, { text: "11-add", selected: true }],
      "11-add becomes selected"
    );

    stepFocus(true, { index: 1 }, "11-add becomes focused");
    assertState(
      [{ text: "8-add" }, { text: "11-add", selected: true, focused: true }],
      "11-add is selected"
    );

    // With focus separate from selected.
    if (model == "focus") {
      continue;
    }

    // Move selection to be before focus.
    widget.addItems(2, ["12-add", "13-add", "14-add"]);
    assertState(
      [
        { text: "8-add" },
        { text: "11-add", selected: true, focused: true },
        { text: "12-add" },
        { text: "13-add" },
        { text: "14-add" },
      ],
      "11-add is selected and focused"
    );

    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "8-add" },
        { text: "11-add", selected: true },
        { text: "12-add", focused: true },
        { text: "13-add" },
        { text: "14-add" },
      ],
      "Selection before focus"
    );

    // Remove focused, but not selected.
    widget.removeItems(2, 1);
    assertState(
      [
        { text: "8-add" },
        { text: "11-add", selected: true },
        { text: "13-add", focused: true },
        { text: "14-add" },
      ],
      "Focus moves to 13-add, but selection is the same"
    );

    // Remove focused and selected.
    widget.removeItems(1, 2);
    assertState(
      [{ text: "8-add" }, { text: "14-add", selected: true, focused: true }],
      "Focus moves to 14-add and becomes selected"
    );

    // Restore selection before focus.
    widget.addItems(0, ["15-add"]);
    assertState(
      [
        { text: "15-add" },
        { text: "8-add" },
        { text: "14-add", selected: true, focused: true },
      ],
      "14-add has focus and selection"
    );
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    assertState(
      [
        { text: "15-add" },
        { text: "8-add", selected: true, focused: true },
        { text: "14-add" },
      ],
      "8-add is focused and selected"
    );
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "15-add" },
        { text: "8-add", selected: true },
        { text: "14-add", focused: true },
      ],
      "Selection before focus again"
    );

    // Remove selected, but not focused.
    widget.removeItems(1, 1);
    assertState(
      [{ text: "15-add" }, { text: "14-add", focused: true }],
      "14-add still has focus, but selection is lost"
    );

    // Move selection to be after focus.
    widget.addItems(1, ["16-add", "17-add"]);
    widget.addItems(4, ["18-add"]);
    assertState(
      [
        { text: "15-add" },
        { text: "16-add" },
        { text: "17-add" },
        { text: "14-add", focused: true },
        { text: "18-add" },
      ],
      "Still no selection"
    );
    // Select focused.
    EventUtils.synthesizeKey(" ", {}, win);
    assertFocus({ index: 3 }, "14-add has focus");
    assertSelection([3], "14-add is selected");
    assertState(
      [
        { text: "15-add" },
        { text: "16-add" },
        { text: "17-add" },
        { text: "14-add", selected: true, focused: true },
        { text: "18-add" },
      ],
      "14-add is selected and focused"
    );
    // Move focus.
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertState(
      [
        { text: "15-add" },
        { text: "16-add", focused: true },
        { text: "17-add" },
        { text: "14-add", selected: true },
        { text: "18-add" },
      ],
      "Selection after focus"
    );

    // Remove focused, but not selected.
    widget.removeItems(1, 1);
    assertState(
      [
        { text: "15-add" },
        { text: "17-add", focused: true },
        { text: "14-add", selected: true },
        { text: "18-add" },
      ],
      "Focus moves to 17-add, selection stays on 14-add"
    );

    // Remove focused and selected.
    widget.removeItems(1, 2);
    assertState(
      [{ text: "15-add" }, { text: "18-add", selected: true, focused: true }],
      "Focus and selection moves to 18-add"
    );

    // Restore selection after focus.
    widget.addItems(2, ["19-add", "20-add"]);
    assertState(
      [
        { text: "15-add" },
        { text: "18-add", selected: true, focused: true },
        { text: "19-add" },
        { text: "20-add" },
      ],
      "Still no selection"
    );
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    assertState(
      [
        { text: "15-add" },
        { text: "18-add" },
        { text: "19-add", selected: true, focused: true },
        { text: "20-add" },
      ],
      "19-add focused and selected"
    );
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertState(
      [
        { text: "15-add" },
        { text: "18-add", focused: true },
        { text: "19-add", selected: true },
        { text: "20-add" },
      ],
      "Selection after focus again"
    );

    // Remove selected, but not focused.
    widget.removeItems(2, 2);
    assertState(
      [{ text: "15-add" }, { text: "18-add", focused: true }],
      "18-add still has focus, but selection is lost"
    );

    // With multi-selection
    if (model == "browse") {
      continue;
    }

    widget.addItems(0, ["21-add", "22-add", "23-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "22-add" },
        { text: "23-add" },
        { text: "15-add" },
        { text: "18-add", focused: true },
      ],
      "18-add focused, no selection yet"
    );
    widget.addItems(5, [
      "24-add",
      "25-add",
      "26-add",
      "27-add",
      "28-add",
      "29-add",
      "30-add",
      "31-add",
    ]);
    assertState(
      [
        { text: "21-add" },
        { text: "22-add" },
        { text: "23-add" },
        { text: "15-add" },
        { text: "18-add", focused: true },
        { text: "24-add" },
        { text: "25-add" },
        { text: "26-add" },
        { text: "27-add" },
        { text: "28-add" },
        { text: "29-add" },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "18-add focused, no selection yet"
    );

    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);

    assertState(
      [
        { text: "21-add" },
        { text: "22-add" },
        { text: "23-add" },
        { text: "15-add" },
        { text: "18-add", selected: true },
        { text: "24-add", selected: true },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true, focused: true },
        { text: "27-add" },
        { text: "28-add" },
        { text: "29-add" },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "Forward range selection from 18-add to 26-add"
    );

    // Delete after the selection range
    widget.removeItems(10, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "22-add" },
        { text: "23-add" },
        { text: "15-add" },
        { text: "18-add", selected: true },
        { text: "24-add", selected: true },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true, focused: true },
        { text: "27-add" },
        { text: "28-add" },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "Same range selection"
    );

    // Delete before the selection range.
    widget.removeItems(1, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "15-add" },
        { text: "18-add", selected: true },
        { text: "24-add", selected: true },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true, focused: true },
        { text: "27-add" },
        { text: "28-add" },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "Same range selection"
    );

    // Delete the start of the selection range.
    widget.removeItems(2, 3);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true, focused: true },
        { text: "27-add" },
        { text: "28-add" },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "Selection range from 25-add to 26-add"
    );

    // Selection pivot is now around 25-add.
    EventUtils.synthesizeKey("KEY_Home", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add", selected: true, focused: true },
        { text: "23-add", selected: true },
        { text: "25-add", selected: true },
        { text: "26-add" },
        { text: "27-add" },
        { text: "28-add" },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "Selection range from 25-add to 21-add"
    );
    EventUtils.synthesizeKey("KEY_End", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true },
        { text: "27-add", selected: true },
        { text: "28-add", selected: true },
        { text: "30-add", selected: true },
        { text: "31-add", selected: true, focused: true },
      ],
      "Selection range from 25-add to 31-add"
    );
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true },
        { text: "27-add", selected: true },
        { text: "28-add", selected: true, focused: true },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "Selection range from 25-add to 28-add"
    );

    // Delete the end of the selection.
    // As a special case, the focus moves to the end of the selection, rather
    // than to the next item.
    widget.removeItems(4, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true, focused: true },
        { text: "30-add" },
        { text: "31-add" },
      ],
      "Selection range from 25-add to 26-add"
    );

    // Do same with a gap.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "26-add", selected: true },
        { text: "30-add", selected: true, focused: true },
        { text: "31-add" },
      ],
      "Continue selection range from 25-add to 30-add"
    );

    widget.addItems(3, ["32-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add" },
        { text: "26-add", selected: true },
        { text: "30-add", selected: true, focused: true },
        { text: "31-add" },
      ],
      "Selection range from 25-add to 30-add with gap"
    );

    widget.removeItems(5, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add" },
        { text: "26-add", selected: true, focused: true },
        { text: "31-add" },
      ],
      "Focus moves to the end of the range, after the gap"
    );

    // Do the same with a gap and all items after the gap are removed.
    widget.addItems(6, ["33-add", "34-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add" },
        { text: "26-add", selected: true, focused: true },
        { text: "31-add" },
        { text: "33-add" },
        { text: "34-add" },
      ],
      "Added 33-add and 34-add"
    );

    clickWidgetItem(5, { shiftKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add", selected: true },
        { text: "26-add", selected: true },
        { text: "31-add", selected: true, focused: true },
        { text: "33-add" },
        { text: "34-add" },
      ],
      "Selection extended to 31-add and gap filled"
    );

    widget.addItems(4, ["35-add", "36-add", "37-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add", selected: true },
        { text: "35-add" },
        { text: "36-add" },
        { text: "37-add" },
        { text: "26-add", selected: true },
        { text: "31-add", selected: true, focused: true },
        { text: "33-add" },
        { text: "34-add" },
      ],
      "Selection from 25-add to 31-add with gap"
    );

    widget.removeItems(6, 3);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add", selected: true, focused: true },
        { text: "35-add" },
        { text: "36-add" },
        { text: "33-add" },
        { text: "34-add" },
      ],
      "Focus jumps gap to what is left of the selection range"
    );

    // Same, with entire gap also removed.
    clickWidgetItem(6, { shiftKey: true });
    widget.addItems(5, ["38-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add", selected: true },
        { text: "35-add", selected: true },
        { text: "38-add" },
        { text: "36-add", selected: true },
        { text: "33-add", selected: true, focused: true },
        { text: "34-add" },
      ],
      "Selection from 25-add to 33-add with gap"
    );

    widget.removeItems(4, 4);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "32-add", selected: true, focused: true },
        { text: "34-add" },
      ],
      "Focus moves to end of what is left of the selection range"
    );

    // Test deleting the end of the selection with focus in a gap.
    // We don't expect to follow the special treatment because the user has
    // explicitly moved the focus "outside" of the selected range.
    widget.addItems(3, ["39-add"]);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "39-add", focused: true },
        { text: "32-add", selected: true },
        { text: "34-add" },
      ],
      "Focus in range gap"
    );

    widget.removeItems(3, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "34-add", focused: true },
      ],
      "Focus moves from gap to 34-add, outside the selection range"
    );

    // Same, but deleting the start of the range.
    widget.addItems(4, ["40-add", "41-add", "42-add"]);
    clickWidgetItem(5, { shiftKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true },
        { text: "41-add", selected: true, focused: true },
        { text: "42-add" },
      ],
      "Selection from 25-add to 41-add"
    );

    widget.addItems(3, ["43-add", "44-add", "45-add"]);
    EventUtils.synthesizeKey("KEY_Home", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "23-add" },
        { text: "25-add", selected: true },
        { text: "43-add", focused: true },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true },
        { text: "41-add", selected: true },
        { text: "42-add" },
      ],
      "Focus in gap"
    );

    widget.removeItems(1, 3);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add", focused: true },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true },
        { text: "41-add", selected: true },
        { text: "42-add" },
      ],
      "Focus moves to next item, rather than the selection start"
    );

    // Test deleting the end of the selection with the focus towards the end of
    // the range.
    widget.addItems(7, ["46-add", "47-add", "48-add", "49-add", "50-add"]);
    clickWidgetItem(7, { shiftKey: true });
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true },
        { text: "41-add", selected: true },
        { text: "42-add", selected: true, focused: true },
        { text: "46-add", selected: true },
        { text: "47-add" },
        { text: "48-add" },
        { text: "49-add" },
        { text: "50-add" },
      ],
      "Range selection from 34-add to 46-add, with focus on 42-add"
    );

    widget.removeItems(6, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true },
        { text: "41-add", selected: true, focused: true },
        { text: "47-add" },
        { text: "48-add" },
        { text: "49-add" },
        { text: "50-add" },
      ],
      "Focus still moves to the end of the selection"
    );

    // Test deleting with focus after the end of the range.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true },
        { text: "41-add", selected: true },
        { text: "47-add", focused: true },
        { text: "48-add" },
        { text: "49-add" },
        { text: "50-add" },
      ],
      "Focus still moves to the end of the selection"
    );

    widget.removeItems(5, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true },
        { text: "48-add", focused: true },
        { text: "49-add" },
        { text: "50-add" },
      ],
      "Focus remains outside the range"
    );

    // Test deleting with focus in the middle of the range, and end of the range
    // is not deleted.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true });
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true });
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "40-add", selected: true, focused: true },
        { text: "48-add", selected: true },
        { text: "49-add", selected: true },
        { text: "50-add" },
      ],
      "Focus in the middle of the range"
    );

    widget.removeItems(4, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "48-add", selected: true, focused: true },
        { text: "49-add", selected: true },
        { text: "50-add" },
      ],
      "Focus moves to next item, rather than the end of the range"
    );

    // With focus just before a gap.
    widget.addItems(5, ["51-add", "52-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "48-add", selected: true, focused: true },
        { text: "51-add" },
        { text: "52-add" },
        { text: "49-add", selected: true },
        { text: "50-add" },
      ],
      "Focus just before a gap"
    );

    widget.removeItems(4, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "51-add", focused: true },
        { text: "52-add" },
        { text: "49-add", selected: true },
        { text: "50-add" },
      ],
      "Focus moves forward into the gap"
    );

    // Selection pivot is about 34-add
    clickWidgetItem(1, { shiftKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "44-add", selected: true, focused: true },
        { text: "45-add", selected: true },
        { text: "34-add", selected: true },
        { text: "51-add" },
        { text: "52-add" },
        { text: "49-add" },
        { text: "50-add" },
      ],
      "Selection from 34-add backward to 44-add"
    );
    clickWidgetItem(5, { shiftKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "34-add", selected: true },
        { text: "51-add", selected: true },
        { text: "52-add", selected: true, focused: true },
        { text: "49-add" },
        { text: "50-add" },
      ],
      "Selection from 34-add forward to 52-add"
    );

    // Delete the whole range.
    widget.removeItems(3, 3);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add" },
        { text: "45-add" },
        { text: "49-add", selected: true, focused: true },
        { text: "50-add" },
      ],
      "Focus and selection moves to after the selection range"
    );

    // Do the same with focus outside the range.
    widget.addItems(5, ["53-add", "54-add", "55-add"]);
    EventUtils.synthesizeKey("KEY_Home", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    clickWidgetItem(2, { shiftKey: true });
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "44-add", selected: true },
        { text: "45-add", selected: true },
        { text: "49-add", focused: true },
        { text: "50-add" },
        { text: "53-add" },
        { text: "54-add" },
        { text: "55-add" },
      ],
      "Focus outside selection"
    );

    widget.removeItems(1, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "49-add", focused: true },
        { text: "50-add" },
        { text: "53-add" },
        { text: "54-add" },
        { text: "55-add" },
      ],
      "Focus remains on same item and unselected"
    );

    // Do the same, but the focus is also removed.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "49-add", selected: true },
        { text: "50-add", selected: true },
        { text: "53-add", focused: true },
        { text: "54-add" },
        { text: "55-add" },
      ],
      "Focus outside selection"
    );

    widget.removeItems(1, 3);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add", selected: true, focused: true },
        { text: "55-add" },
      ],
      "Focus and selection moves to 49-add"
    );

    // * Do the same tests but with selection travelling backwards. *
    widget.addItems(3, [
      "56-add",
      "57-add",
      "58-add",
      "59-add",
      "60-add",
      "61-add",
      "62-add",
      "63-add",
      "64-add",
      "65-add",
      "66-add",
    ]);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add", selected: true, focused: true },
        { text: "55-add" },
        { text: "56-add" },
        { text: "57-add" },
        { text: "58-add" },
        { text: "59-add" },
        { text: "60-add" },
        { text: "61-add" },
        { text: "62-add" },
        { text: "63-add" },
        { text: "64-add" },
        { text: "65-add" },
        { text: "66-add" },
      ],
      "Same selection and focus"
    );
    EventUtils.synthesizeKey("KEY_End", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add" },
        { text: "55-add" },
        { text: "56-add" },
        { text: "57-add", selected: true, focused: true },
        { text: "58-add", selected: true },
        { text: "59-add", selected: true },
        { text: "60-add", selected: true },
        { text: "61-add", selected: true },
        { text: "62-add", selected: true },
        { text: "63-add" },
        { text: "64-add" },
        { text: "65-add" },
        { text: "66-add" },
      ],
      "Backward range selection from 62-add to 57-add"
    );

    // Delete after the selection range
    widget.removeItems(11, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add" },
        { text: "55-add" },
        { text: "56-add" },
        { text: "57-add", selected: true, focused: true },
        { text: "58-add", selected: true },
        { text: "59-add", selected: true },
        { text: "60-add", selected: true },
        { text: "61-add", selected: true },
        { text: "62-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Same range selection"
    );

    // Delete before the selection range.
    widget.removeItems(2, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add" },
        { text: "57-add", selected: true, focused: true },
        { text: "58-add", selected: true },
        { text: "59-add", selected: true },
        { text: "60-add", selected: true },
        { text: "61-add", selected: true },
        { text: "62-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Same range selection"
    );

    // Delete the end of the selection range.
    widget.removeItems(7, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add" },
        { text: "57-add", selected: true, focused: true },
        { text: "58-add", selected: true },
        { text: "59-add", selected: true },
        { text: "60-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backwards from 61-add to 57-add"
    );

    // Selection pivot is now around 61-add.
    EventUtils.synthesizeKey("KEY_End", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add" },
        { text: "57-add" },
        { text: "58-add" },
        { text: "59-add" },
        { text: "60-add" },
        { text: "61-add", selected: true },
        { text: "63-add", selected: true },
        { text: "66-add", selected: true, focused: true },
      ],
      "Selection range forwards from 61-add to 66-add"
    );
    EventUtils.synthesizeKey("KEY_Home", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add", selected: true, focused: true },
        { text: "54-add", selected: true },
        { text: "57-add", selected: true },
        { text: "58-add", selected: true },
        { text: "59-add", selected: true },
        { text: "60-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backwards from 61-add to 21-add"
    );
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "54-add" },
        { text: "57-add", selected: true, focused: true },
        { text: "58-add", selected: true },
        { text: "59-add", selected: true },
        { text: "60-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backwards from 61-add to 57-add"
    );

    // Delete the start of the selection.
    widget.removeItems(1, 3);
    assertState(
      [
        { text: "21-add" },
        { text: "59-add", selected: true, focused: true },
        { text: "60-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range shrinks to 59-add"
    );

    // Do the same with a gap after the focus and its next item.
    widget.addItems(3, ["67-add", "68-add", "69-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "59-add", selected: true, focused: true },
        { text: "60-add", selected: true },
        { text: "67-add" },
        { text: "68-add" },
        { text: "69-add" },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backwards from 61-add to 59-add with gap"
    );

    widget.removeItems(1, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "60-add", selected: true, focused: true },
        { text: "67-add" },
        { text: "68-add" },
        { text: "69-add" },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus moves to the next item, before gap"
    );

    // Do the same with a gap and all items before the gap are removed.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "60-add" },
        { text: "67-add", selected: true, focused: true },
        { text: "68-add", selected: true },
        { text: "69-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backward reduced to 67-add with gap filled"
    );
    widget.addItems(4, ["70-add", "71-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "60-add" },
        { text: "67-add", selected: true, focused: true },
        { text: "68-add", selected: true },
        { text: "70-add" },
        { text: "71-add" },
        { text: "69-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backward from 61-add to 67-add with gap"
    );

    widget.removeItems(2, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "60-add" },
        { text: "70-add" },
        { text: "71-add" },
        { text: "69-add", selected: true, focused: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus jumps gap to selection range"
    );

    // Same, with entire gap also removed.
    clickWidgetItem(1, { shiftKey: true });
    widget.addItems(2, ["72-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "60-add", selected: true, focused: true },
        { text: "72-add" },
        { text: "70-add", selected: true },
        { text: "71-add", selected: true },
        { text: "69-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backwards from 60-add to 70-add"
    );

    widget.removeItems(1, 3);
    assertState(
      [
        { text: "21-add" },
        { text: "71-add", selected: true, focused: true },
        { text: "69-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus moves to the start of what is left of the selection range"
    );

    // Test deleting the start of the selection with focus in a gap.
    // We don't expect to follow the special treatment because the user has
    // explicitly moved the focus "outside" of the selected range.
    widget.addItems(2, ["73-add", "74-add", "75-add"]);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "71-add", selected: true },
        { text: "73-add", focused: true },
        { text: "74-add" },
        { text: "75-add" },
        { text: "69-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus in range gap"
    );

    widget.removeItems(1, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "74-add", focused: true },
        { text: "75-add" },
        { text: "69-add", selected: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus moves to the next item, rather than the selection range"
    );

    // Same, but deleting the end of the range.
    clickWidgetItem(1, { shiftKey: true });
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "74-add", selected: true },
        { text: "75-add", selected: true },
        { text: "69-add", selected: true, focused: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backward from 61-add to 74-add, with focus shifted"
    );
    widget.addItems(3, ["76-add", "77-add"]);
    assertState(
      [
        { text: "21-add" },
        { text: "74-add", selected: true },
        { text: "75-add", selected: true },
        { text: "76-add" },
        { text: "77-add" },
        { text: "69-add", selected: true, focused: true },
        { text: "61-add", selected: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus in gap"
    );

    widget.removeItems(5, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "74-add", selected: true },
        { text: "75-add", selected: true },
        { text: "76-add" },
        { text: "77-add" },
        { text: "63-add", focused: true },
        { text: "66-add" },
      ],
      "Focus moves to next item, rather than selection range end"
    );

    // Selection pivot now about 75-add.
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "74-add" },
        { text: "75-add", selected: true },
        { text: "76-add", selected: true },
        { text: "77-add", selected: true, focused: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range forward from 75-add to 77-add"
    );

    widget.addItems(1, ["78-add", "79-add", "80-add"]);
    clickWidgetItem(2, { shiftKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "78-add" },
        { text: "79-add", selected: true, focused: true },
        { text: "80-add", selected: true },
        { text: "74-add", selected: true },
        { text: "75-add", selected: true },
        { text: "76-add" },
        { text: "77-add" },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backward from 75-add to 79-add"
    );

    // Move focus to the end of the range and delete again, but with no gap this
    // time.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertState(
      [
        { text: "21-add" },
        { text: "78-add" },
        { text: "79-add", selected: true },
        { text: "80-add", selected: true },
        { text: "74-add", selected: true },
        { text: "75-add", selected: true, focused: true },
        { text: "76-add" },
        { text: "77-add" },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus moved to the end of the selection"
    );

    widget.removeItems(5, 2);
    assertState(
      [
        { text: "21-add" },
        { text: "78-add" },
        { text: "79-add", selected: true },
        { text: "80-add", selected: true },
        { text: "74-add", selected: true },
        { text: "77-add", focused: true },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus moved to the next item rather than the selection start"
    );

    // Deleting with focus before the selection start.
    EventUtils.synthesizeKey("KEY_Home", { ctrlKey: true });
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true });
    assertState(
      [
        { text: "21-add" },
        { text: "78-add", focused: true },
        { text: "79-add", selected: true },
        { text: "80-add", selected: true },
        { text: "74-add", selected: true },
        { text: "77-add" },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus before the selection start"
    );

    widget.removeItems(1, 1);
    assertState(
      [
        { text: "21-add" },
        { text: "79-add", selected: true, focused: true },
        { text: "80-add", selected: true },
        { text: "74-add", selected: true },
        { text: "77-add" },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus moves to the next item, which happens to be in the selection range"
    );

    // Test deleting with focus in the middle of the range.
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true });
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true });
    assertState(
      [
        { text: "21-add", selected: true },
        { text: "79-add", selected: true, focused: true },
        { text: "80-add", selected: true },
        { text: "74-add", selected: true },
        { text: "77-add" },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Selection range backwards from 74-add to 21-add, with focus in middle"
    );

    widget.removeItems(1, 1);
    assertState(
      [
        { text: "21-add", selected: true },
        { text: "80-add", selected: true, focused: true },
        { text: "74-add", selected: true },
        { text: "77-add" },
        { text: "63-add" },
        { text: "66-add" },
      ],
      "Focus moves to the next item, rather than the selection start or end"
    );

    // Delete the whole range.
    widget.removeItems(0, 4);
    assertState(
      [{ text: "63-add", selected: true, focused: true }, { text: "66-add" }],
      "Focus and selection move to the next remaining item"
    );

    // Do the same with focus outside the range.
    widget.addItems(0, ["81-add", "82-add", "83-add", "84-add", "85-add"]);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertState(
      [
        { text: "81-add" },
        { text: "82-add" },
        { text: "83-add" },
        { text: "84-add", focused: true },
        { text: "85-add", selected: true },
        { text: "63-add", selected: true },
        { text: "66-add" },
      ],
      "Focus outside backward selection range"
    );

    widget.removeItems(4, 2);
    assertState(
      [
        { text: "81-add" },
        { text: "82-add" },
        { text: "83-add" },
        { text: "84-add", focused: true },
        { text: "66-add" },
      ],
      "Focus remains the same and is not selected"
    );

    // Same, but with focus also removed.
    widget.addItems(5, ["86-add"]);
    EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertState(
      [
        { text: "81-add" },
        { text: "82-add", focused: true },
        { text: "83-add", selected: true },
        { text: "84-add", selected: true },
        { text: "66-add" },
        { text: "86-add" },
      ],
      "Focus outside backwards selection range"
    );

    widget.removeItems(1, 3);
    assertState(
      [
        { text: "81-add" },
        { text: "66-add", selected: true, focused: true },
        { text: "86-add" },
      ],
      "Focus moves to next item and selected"
    );

    // With multi-selection via toggling.

    widget.addItems(3, [
      "87-add",
      "88-add",
      "89-add",
      "90-add",
      "91-add",
      "92-add",
      "93-add",
      "94-add",
    ]);
    EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, win);
    assertState(
      [
        { text: "81-add" },
        { text: "66-add" },
        { text: "86-add" },
        { text: "87-add", selected: true },
        { text: "88-add", selected: true },
        { text: "89-add", selected: true },
        { text: "90-add", selected: true, focused: true },
        { text: "91-add" },
        { text: "92-add" },
        { text: "93-add" },
        { text: "94-add" },
      ],
      "Start with range selection forward from 87-add to 90-add"
    );

    clickWidgetItem(4, { ctrlKey: true });
    clickWidgetItem(5, { ctrlKey: true });
    assertState(
      [
        { text: "81-add" },
        { text: "66-add" },
        { text: "86-add" },
        { text: "87-add", selected: true },
        { text: "88-add" },
        { text: "89-add", focused: true },
        { text: "90-add", selected: true },
        { text: "91-add" },
        { text: "92-add" },
        { text: "93-add" },
        { text: "94-add" },
      ],
      "Range selection from 87-add to 90-add, with 88-add and 89-add unselected"
    );

    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true });
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true });
    EventUtils.synthesizeKey(" ", { ctrlKey: true });
    assertState(
      [
        { text: "81-add" },
        { text: "66-add" },
        { text: "86-add" },
        { text: "87-add", selected: true },
        { text: "88-add" },
        { text: "89-add" },
        { text: "90-add", selected: true },
        { text: "91-add", selected: true, focused: true },
        { text: "92-add" },
        { text: "93-add" },
        { text: "94-add" },
      ],
      "Mixed selection"
    );

    // Remove before the selected items
    widget.removeItems(0, 2);
    assertState(
      [
        { text: "86-add" },
        { text: "87-add", selected: true },
        { text: "88-add" },
        { text: "89-add" },
        { text: "90-add", selected: true },
        { text: "91-add", selected: true, focused: true },
        { text: "92-add" },
        { text: "93-add" },
        { text: "94-add" },
      ],
      "Same selection and focus"
    );

    // Remove after
    widget.removeItems(6, 1);
    assertState(
      [
        { text: "86-add" },
        { text: "87-add", selected: true },
        { text: "88-add" },
        { text: "89-add" },
        { text: "90-add", selected: true },
        { text: "91-add", selected: true, focused: true },
        { text: "93-add" },
        { text: "94-add" },
      ],
      "Same selection and focus"
    );

    // Removed the focused item, unlike a simple range selection, the focused
    // item is not bound to stay within the selected items.
    widget.removeItems(5, 1);
    assertState(
      [
        { text: "86-add" },
        { text: "87-add", selected: true },
        { text: "88-add" },
        { text: "89-add" },
        { text: "90-add", selected: true },
        { text: "93-add", focused: true },
        { text: "94-add" },
      ],
      "Focus moves to next item and not selected"
    );

    // Remove the unselected items, merging the two ranges together.
    widget.removeItems(2, 2);
    assertState(
      [
        { text: "86-add" },
        { text: "87-add", selected: true },
        { text: "90-add", selected: true },
        { text: "93-add", focused: true },
        { text: "94-add" },
      ],
      "Focus remains the same"
    );

    // Remove the selected items.
    widget.removeItems(1, 2);
    assertState(
      [
        { text: "86-add" },
        { text: "93-add", focused: true },
        { text: "94-add" },
      ],
      "Focus remains the same, and not selected"
    );

    // Remove all selected items, including the focused item.
    widget.addItems(0, [
      "95-add",
      "96-add",
      "97-add",
      "98-add",
      "99-add",
      "100-add",
      "101-add",
    ]);
    EventUtils.synthesizeKey(" ", {}, win);
    assertState(
      [
        { text: "95-add" },
        { text: "96-add" },
        { text: "97-add" },
        { text: "98-add" },
        { text: "99-add" },
        { text: "100-add" },
        { text: "101-add" },
        { text: "86-add" },
        { text: "93-add", selected: true, focused: true },
        { text: "94-add" },
      ],
      "Single selection"
    );

    clickWidgetItem(6, { ctrlKey: true });
    clickWidgetItem(5, { ctrlKey: true });
    assertState(
      [
        { text: "95-add" },
        { text: "96-add" },
        { text: "97-add" },
        { text: "98-add" },
        { text: "99-add" },
        { text: "100-add", selected: true, focused: true },
        { text: "101-add", selected: true },
        { text: "86-add" },
        { text: "93-add", selected: true },
        { text: "94-add" },
      ],
      "Mixed selection with focus selected"
    );

    widget.removeItems(5, 4);
    assertState(
      [
        { text: "95-add" },
        { text: "96-add" },
        { text: "97-add" },
        { text: "98-add" },
        { text: "99-add" },
        { text: "94-add", selected: true, focused: true },
      ],
      "Focus moves to next item and selected"
    );

    // Remove all selected, with focus outside the selection
    clickWidgetItem(1, {});
    clickWidgetItem(3, { ctrlKey: true });
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true });
    assertState(
      [
        { text: "95-add" },
        { text: "96-add", selected: true },
        { text: "97-add", focused: true },
        { text: "98-add", selected: true },
        { text: "99-add" },
        { text: "94-add" },
      ],
      "Mixed selection with focus not selected"
    );

    widget.removeItems(1, 3);
    assertState(
      [
        { text: "95-add" },
        { text: "99-add", selected: true, focused: true },
        { text: "94-add" },
      ],
      "Focus moves to next item and selected"
    );

    // With select all.
    widget.addItems(0, ["102-add", "103-add", "104-add"]);
    widget.addItems(6, ["105-add", "106-add"]);
    selectAllShortcut();
    assertState(
      [
        { text: "102-add", selected: true },
        { text: "103-add", selected: true },
        { text: "104-add", selected: true },
        { text: "95-add", selected: true },
        { text: "99-add", selected: true, focused: true },
        { text: "94-add", selected: true },
        { text: "105-add", selected: true },
        { text: "106-add", selected: true },
      ],
      "All selected"
    );

    // Remove middle and focused.
    widget.removeItems(4, 1);
    assertState(
      [
        { text: "102-add", selected: true },
        { text: "103-add", selected: true },
        { text: "104-add", selected: true },
        { text: "95-add", selected: true },
        { text: "94-add", selected: true, focused: true },
        { text: "105-add", selected: true },
        { text: "106-add", selected: true },
      ],
      "Focus moves to the next item, selections remain"
    );

    // Remove before focused.
    widget.removeItems(1, 1);
    assertState(
      [
        { text: "102-add", selected: true },
        { text: "104-add", selected: true },
        { text: "95-add", selected: true },
        { text: "94-add", selected: true, focused: true },
        { text: "105-add", selected: true },
        { text: "106-add", selected: true },
      ],
      "Focus and selection remain"
    );

    // Remove after the focus.
    widget.removeItems(4, 2);
    assertState(
      [
        { text: "102-add", selected: true },
        { text: "104-add", selected: true },
        { text: "95-add", selected: true },
        { text: "94-add", selected: true, focused: true },
      ],
      "Focus and selection remain"
    );

    // Remove end and focused.
    widget.removeItems(3, 1);
    assertState(
      [
        { text: "102-add", selected: true },
        { text: "104-add", selected: true },
        { text: "95-add", selected: true, focused: true },
      ],
      "Focus moves to the last item, selection remains"
    );

    // Remove start and focused.
    EventUtils.synthesizeKey("KEY_Home", { ctrlKey: true }, win);
    assertState(
      [
        { text: "102-add", selected: true, focused: true },
        { text: "104-add", selected: true },
        { text: "95-add", selected: true },
      ],
      "Focus on first item"
    );

    widget.removeItems(0, 1);
    assertState(
      [
        { text: "104-add", selected: true, focused: true },
        { text: "95-add", selected: true },
      ],
      "Focus moves to next item"
    );

    // Remove items to cause two distinct ranges to merge together, with
    // in-between ranges removed.
    widget.addItems(2, [
      "107-add",
      "108-add",
      "109-add",
      "110-add",
      "111-add",
      "112-add",
      "113-add",
    ]);
    clickWidgetItem(0, { ctrlKey: true });
    assertState(
      [
        { text: "104-add", focused: true },
        { text: "95-add", selected: true },
        { text: "107-add" },
        { text: "108-add" },
        { text: "109-add" },
        { text: "110-add" },
        { text: "111-add" },
        { text: "112-add" },
        { text: "113-add" },
      ],
      "Added items and de-selected 104-add"
    );

    clickWidgetItem(3, { ctrlKey: true });
    clickWidgetItem(4, { ctrlKey: true });
    clickWidgetItem(6, { ctrlKey: true });
    clickWidgetItem(8, { ctrlKey: true });
    assertState(
      [
        { text: "104-add" },
        { text: "95-add", selected: true },
        { text: "107-add" },
        { text: "108-add", selected: true },
        { text: "109-add", selected: true },
        { text: "110-add" },
        { text: "111-add", selected: true },
        { text: "112-add" },
        { text: "113-add", selected: true, focused: true },
      ],
      "Several selection ranges"
    );

    widget.removeItems(2, 6);
    assertState(
      [
        { text: "104-add" },
        { text: "95-add", selected: true },
        { text: "113-add", selected: true, focused: true },
      ],
      "End ranges merged together"
    );

    // Do the same, but where parts of the end ranges are also removed.
    widget.addItems(3, [
      "114-add",
      "115-add",
      "116-add",
      "117-add",
      "118-add",
      "119-add",
      "120-add",
      "121-add",
      "122-add",
      "123-add",
      "124-add",
      "125-add",
      "126-add",
      "127-add",
    ]);
    clickWidgetItem(4, { ctrlKey: true });
    clickWidgetItem(5, { ctrlKey: true });
    clickWidgetItem(7, { ctrlKey: true });
    clickWidgetItem(10, { ctrlKey: true });
    clickWidgetItem(12, { ctrlKey: true });
    clickWidgetItem(13, { ctrlKey: true });
    clickWidgetItem(14, { ctrlKey: true });
    clickWidgetItem(16, { ctrlKey: true });

    clickWidgetItem(9, { ctrlKey: true });
    assertState(
      [
        { text: "104-add" },
        { text: "95-add", selected: true },
        { text: "113-add", selected: true },
        { text: "114-add" },
        { text: "115-add", selected: true },
        { text: "116-add", selected: true },
        { text: "117-add" },
        { text: "118-add", selected: true },
        { text: "119-add" },
        { text: "120-add", selected: true, focused: true },
        { text: "121-add", selected: true },
        { text: "122-add" },
        { text: "123-add", selected: true },
        { text: "124-add", selected: true },
        { text: "125-add", selected: true },
        { text: "126-add" },
        { text: "127-add", selected: true },
      ],
      "Several ranges"
    );

    widget.removeItems(5, 8);
    assertState(
      [
        { text: "104-add" },
        { text: "95-add", selected: true },
        { text: "113-add", selected: true },
        { text: "114-add" },
        { text: "115-add", selected: true },
        { text: "124-add", selected: true, focused: true },
        { text: "125-add", selected: true },
        { text: "126-add" },
        { text: "127-add", selected: true },
      ],
      "Two ranges merged and rest removed, focus moves to next item"
    );
  }
});

// If widget is emptied whilst focused, focus moves to widget.
add_task(function test_emptying_widget() {
  for (let model of selectionModels) {
    // Empty with focused widget.
    reset({ model });
    stepFocus(true, { element: widget }, "Initial");
    widget.addItems(0, ["First", "Second"]);
    assertFocus({ element: widget }, "Focus still on widget after adding");
    widget.removeItems(0, 2);
    assertFocus({ element: widget }, "Focus still on widget after removing");

    // Empty with focused item.
    widget.addItems(0, ["First", "Second"]);
    EventUtils.synthesizeKey("KEY_Home", {}, win);
    assertFocus({ index: 0 }, "Focus on first item");
    widget.removeItems(0, 2);
    assertFocus({ element: widget }, "Focus moves to widget after removing");

    // Empty with focus elsewhere.
    widget.addItems(0, ["First", "Second"]);
    stepFocus(false, { element: before }, "Focus elsewhere");
    widget.removeItems(0, 2);
    assertFocus({ element: before }, "Focus still elsewhere after removing");
    stepFocus(true, { element: widget }, "Widget becomes focused");

    // Empty with focus elsewhere, but active item.
    widget.addItems(0, ["First", "Second"]);
    // Move away from and back to widget to focus second item.
    stepFocus(true, { element: after }, "Focus elsewhere");
    widget.selectSingleItem(1);
    stepFocus(false, { index: 1 }, "Focus on second item");
    stepFocus(false, { element: before }, "Return focus to elsewhere");
    widget.removeItems(0, 2);
    assertFocus({ element: before }, "Focus still elsewhere after removing");
    stepFocus(true, { element: widget }, "Widget becomes focused");
  }
});

/**
 * Test moving items in the widget.
 *
 * @param {string} model - The selection model to use.
 * @param {boolean} reCreate - Whether the widget should reCreate the items when
 *   moving them.
 */
function subtest_move_items(model, reCreate) {
  reset({ model, direction: "right-to-left" });

  widget.addItems(0, [
    "0-add",
    "1-add",
    "2-add",
    "3-add",
    "4-add",
    "5-add",
    "6-add",
    "7-add",
    "8-add",
    "9-add",
    "10-add",
    "11-add",
    "12-add",
    "13-add",
  ]);
  clickWidgetItem(5, {});

  assertState(
    [
      { text: "0-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "3-add" },
      { text: "4-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "6-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Item 5 selected and focused"
  );

  // Move items before focus.
  widget.moveItems(4, 3, 1, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "6-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection"
  );
  widget.moveItems(1, 3, 2, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "6-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection"
  );

  // Move items after focus.
  widget.moveItems(6, 8, 2, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "7-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection"
  );
  widget.moveItems(9, 6, 1, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection"
  );

  // Move from before focus to after focus.
  widget.moveItems(2, 3, 3, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "3-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection, but moved"
  );

  // Move from after focus to before focus.
  widget.moveItems(3, 2, 5, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection, but moved"
  );

  // Move selected and focused up.
  widget.moveItems(7, 3, 1, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "1-add" },
      { text: "2-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Focus and selection moved to index 3"
  );

  // Move down.
  widget.moveItems(3, 5, 1, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Focus and selection moved to index 5"
  );

  // Move in a group.
  widget.moveItems(4, 5, 3, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add" },
      { text: "8-add" },
      { text: "2-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "7-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Focus and selection moved to index 6"
  );
  widget.moveItems(5, 4, 3, reCreate);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Focus and selection moved back to index 5"
  );

  // With focus split from selection.
  if (model == "focus") {
    return;
  }

  // Focus before selection.
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  assertState(
    [
      { text: "0-add" },
      { text: "4-add" },
      { text: "3-add" },
      { text: "1-add", focused: true },
      { text: "2-add" },
      { text: "5-add", selected: true },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Focus before selection"
  );

  // Move before both.
  widget.moveItems(0, 1, 1, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "0-add" },
      { text: "3-add" },
      { text: "1-add", focused: true },
      { text: "2-add" },
      { text: "5-add", selected: true },
      { text: "7-add" },
      { text: "8-add" },
      { text: "9-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection"
  );

  // Move after both.
  widget.moveItems(8, 7, 1, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "0-add" },
      { text: "3-add" },
      { text: "1-add", focused: true },
      { text: "2-add" },
      { text: "5-add", selected: true },
      { text: "7-add" },
      { text: "9-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Same focus and selection"
  );

  // Move focus to after selected.
  widget.moveItems(3, 6, 2, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "0-add" },
      { text: "3-add" },
      { text: "5-add", selected: true },
      { text: "7-add" },
      { text: "9-add" },
      { text: "1-add", focused: true },
      { text: "2-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Focus moved to after selection"
  );

  // Move focus before selected.
  widget.moveItems(5, 2, 3, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "0-add" },
      { text: "9-add" },
      { text: "1-add", focused: true },
      { text: "2-add" },
      { text: "3-add" },
      { text: "5-add", selected: true },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Focus moved to before selection"
  );

  // Move selection before focus.
  widget.moveItems(5, 1, 5, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "5-add", selected: true },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "0-add" },
      { text: "9-add" },
      { text: "1-add", focused: true },
      { text: "2-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Selected moved to before focus"
  );

  // Move selection after focus.
  widget.moveItems(2, 8, 1, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "0-add" },
      { text: "9-add" },
      { text: "1-add", focused: true },
      { text: "5-add", selected: true },
      { text: "2-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Selected moved to after focus"
  );

  // Navigation still works.
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "0-add" },
      { text: "9-add", focused: true },
      { text: "1-add" },
      { text: "5-add", selected: true },
      { text: "2-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Selected moved to after focus"
  );

  // Test with multi-selection.
  if (model == "browse") {
    return;
  }

  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add", selected: true, focused: true },
      { text: "0-add", selected: true },
      { text: "9-add", selected: true },
      { text: "1-add" },
      { text: "5-add" },
      { text: "2-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Range selection from 9-add to 6-add"
  );

  // Move non-selected into the middle of the selected.
  widget.moveItems(8, 5, 2, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add", selected: true, focused: true },
      { text: "5-add" },
      { text: "2-add" },
      { text: "0-add", selected: true },
      { text: "9-add", selected: true },
      { text: "1-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Non-selected gap"
  );

  // Moving an item always ends a Shift range selection.
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "5-add" },
      { text: "2-add" },
      { text: "0-add" },
      { text: "9-add" },
      { text: "1-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Range selection from 6-add to 8-add"
  );

  clickWidgetItem(9, { shiftKey: true }, win);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add", selected: true },
      { text: "5-add", selected: true },
      { text: "2-add", selected: true },
      { text: "0-add", selected: true },
      { text: "9-add", selected: true },
      { text: "1-add", selected: true, focused: true },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Range selection from 6-add to 1-add"
  );

  // Move selected to middle of selected.
  widget.moveItems(8, 6, 2, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add", selected: true },
      { text: "5-add", selected: true },
      { text: "9-add", selected: true },
      { text: "1-add", selected: true, focused: true },
      { text: "2-add", selected: true },
      { text: "0-add", selected: true },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Selection block"
  );

  // Also ends a Shift range selection.
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "9-add", selected: true },
      { text: "1-add", selected: true },
      { text: "2-add" },
      { text: "0-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Range selection from 1-add to 5-add"
  );

  // Move from start of selection to end.
  widget.moveItems(5, 7, 1, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "9-add", selected: true },
      { text: "1-add", selected: true },
      { text: "5-add", selected: true, focused: true },
      { text: "2-add" },
      { text: "0-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Moved to end"
  );

  // And reverse.
  widget.moveItems(7, 5, 1, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "8-add" },
      { text: "6-add" },
      { text: "5-add", selected: true, focused: true },
      { text: "9-add", selected: true },
      { text: "1-add", selected: true },
      { text: "2-add" },
      { text: "0-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Moved back to start"
  );

  // Also broke Shift range selection.
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, win);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add", selected: true, focused: true },
      { text: "7-add", selected: true },
      { text: "8-add", selected: true },
      { text: "6-add", selected: true },
      { text: "5-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
      { text: "2-add" },
      { text: "0-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "13-add" },
    ],
    "Range selection from 5-add to 3-add"
  );

  EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
  EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
  EventUtils.synthesizeKey("KEY_End", { ctrlKey: true }, win);
  EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
  EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
  EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);

  assertState(
    [
      { text: "4-add" },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "8-add", selected: true },
      { text: "6-add", selected: true },
      { text: "5-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
      { text: "2-add", selected: true, focused: true },
      { text: "0-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "13-add", selected: true },
    ],
    "Multi-selection"
  );

  // Move selected with gap into middle of a selection block.
  widget.moveItems(8, 4, 6, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "8-add", selected: true },
      { text: "2-add", selected: true, focused: true },
      { text: "0-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "13-add", selected: true },
      { text: "6-add", selected: true },
      { text: "5-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
    ],
    "Merged ranges together on both sides"
  );

  // Move selected with gap to start of a selection block.
  widget.moveItems(5, 1, 5, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "0-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "13-add", selected: true },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "8-add", selected: true },
      { text: "2-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "5-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
    ],
    "Merged ranges together at start"
  );

  // Move selected with gap to end of a selection block.
  widget.moveItems(1, 4, 8, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "2-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "5-add", selected: true },
      { text: "0-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "13-add", selected: true },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "8-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
    ],
    "Merged ranges together at end"
  );

  // Move block with non-selected boundaries into middle of selected.
  widget.moveItems(5, 3, 6, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "2-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "13-add", selected: true },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "5-add", selected: true },
      { text: "0-add", selected: true },
      { text: "8-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
    ],
    "Split range block"
  );

  // Move block with selected at start into middle of selected.
  widget.moveItems(1, 6, 5, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "13-add", selected: true },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "5-add", selected: true },
      { text: "0-add", selected: true },
      { text: "2-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "8-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
    ],
    "Merged ranges together at start"
  );

  // Move block with selected at end into middle of selected.
  widget.moveItems(8, 6, 4, reCreate);
  assertState(
    [
      { text: "4-add" },
      { text: "13-add", selected: true },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "5-add", selected: true },
      { text: "0-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "8-add", selected: true },
      { text: "2-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
    ],
    "Merged ranges together at end"
  );

  // Move selected into non-selected region and move to start.
  widget.moveItems(4, 0, 6, reCreate);
  assertState(
    [
      { text: "5-add", selected: true },
      { text: "0-add", selected: true },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "8-add", selected: true },
      { text: "4-add" },
      { text: "13-add", selected: true },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "2-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
    ],
    "Merged ranges together at end"
  );

  // Remove gap between two selections and move to end.
  widget.moveItems(2, 9, 5, reCreate);
  assertState(
    [
      { text: "5-add", selected: true },
      { text: "0-add", selected: true },
      { text: "13-add", selected: true },
      { text: "3-add", selected: true },
      { text: "7-add" },
      { text: "2-add", selected: true, focused: true },
      { text: "6-add", selected: true },
      { text: "9-add" },
      { text: "1-add" },
      { text: "10-add" },
      { text: "11-add", selected: true },
      { text: "12-add" },
      { text: "8-add", selected: true },
      { text: "4-add" },
    ],
    "Merged ranges together"
  );

  // Navigation still works.
  EventUtils.synthesizeKey("KEY_ArrowLeft", {}, win);
  assertState(
    [
      { text: "5-add" },
      { text: "0-add" },
      { text: "13-add" },
      { text: "3-add" },
      { text: "7-add" },
      { text: "2-add" },
      { text: "6-add", selected: true, focused: true },
      { text: "9-add" },
      { text: "1-add" },
      { text: "10-add" },
      { text: "11-add" },
      { text: "12-add" },
      { text: "8-add" },
      { text: "4-add" },
    ],
    "Move by one index and single select"
  );
}

// Moving items in the widget will move focus and selection with the moved
// items.
add_task(function test_move_items() {
  for (let model of selectionModels) {
    // We want to be sure the methods work with or without re-creating the
    // item elements.
    subtest_move_items(model, false);
    subtest_move_items(model, true);
  }
});

// Test that dragging is possible.
add_task(function test_can_drag_items() {
  /**
   * Assert that dragging can occur and takes place with the expected selection.
   *
   * @param {number} index - The index of the item to start dragging on. We also
   *   expect this item to have focus during and after dragging.
   * @param {number[]} selection - The expected selection during and after
   *   dragging.
   * @param {string} msg - A message to use in assertions.
   */
  function assertDragstart(index, selection, msg) {
    let element = widget.items[index].element;
    let eventFired = false;

    let dragstartListener = event => {
      eventFired = true;
      Assert.ok(
        element.contains(event.target),
        `Item ${index} contains the dragstart target`
      );
      assertFocus({ index }, `Item ${index} has focus in dragstart: ${msg}`);
      assertSelection(selection, `Selection in dragstart: ${msg}`);
    };
    widget.addEventListener("dragstart", dragstartListener, true);

    // Synthesize the start of a drag.
    let rect = element.getBoundingClientRect();
    let x = rect.left + rect.width / 2;
    let y = rect.top + rect.height / 2;
    EventUtils.synthesizeMouseAtPoint(x, y, { type: "mousedown" }, win);
    EventUtils.synthesizeMouseAtPoint(x, y, { type: "mousemove" }, win);
    EventUtils.synthesizeMouseAtPoint(x, y + 60, { type: "mousemove" }, win);
    // Don't care about ending the drag.

    Assert.ok(eventFired, `dragstart event fired: ${msg}`);
    widget.removeEventListener("dragstart", dragstartListener, true);
    assertSelection(selection, `Same selection after dragging: ${msg}`);
    assertFocus(
      { index },
      `Item ${index} still has focus after dragging: ${msg}`
    );
  }

  for (let model of selectionModels) {
    reset({ model, draggable: true });
    widget.addItems(0, ["First", "Second", "Third"]);
    assertFocus({ element: before }, "Focus outside widget");
    assertSelection([], "No initial selection");
    assertDragstart(1, [1], "First drag with no focus or selection");

    assertDragstart(1, [1], "Already selected item");
    assertDragstart(2, [2], "Non-selected item");

    reset({ model, draggable: true });
    widget.addItems(0, ["First", "Second", "Third"]);
    widget.selectSingleItem(1);
    assertFocus({ element: before }, "Focus outside widget");
    assertSelection([1], "Initial selection on item 1");
    assertDragstart(1, [1], "First drag on selected item");

    reset({ model, draggable: true });
    widget.addItems(0, ["First", "Second", "Third", "Fourth", "Fifth"]);
    widget.selectSingleItem(3);
    assertFocus({ element: before }, "Focus outside widget");
    assertSelection([3], "Initial selection on item 3");
    assertDragstart(2, [2], "First drag on non-selected item");

    // With focus split from selected.
    if (model == "focus") {
      continue;
    }
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    assertFocus({ index: 3 }, "Focus on item 3");
    assertSelection([2], "Item 2 is selected");
    assertDragstart(3, [3], "Non-selected but focused item");

    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    assertFocus({ index: 2 }, "Focus on item 2");
    assertSelection([3], "Item 3 is selected");
    assertDragstart(3, [3], "Selected but non-focused item");

    // With mutli-selection.
    if (model == "browse") {
      continue;
    }

    // Clicking a non-selected item will change to selection to the single item
    // before dragging.
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowRight", { ctrlKey: true }, win);
    EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
    assertFocus({ index: 1 }, "Focus on item 1");
    assertSelection([1, 3], "Multi selection");
    assertDragstart(2, [2], "Selection moves to item 2 before drag");

    // Clicking a selected item will keep the same selection for dragging.
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey("KEY_ArrowLeft", { ctrlKey: true }, win);
    EventUtils.synthesizeKey(" ", { ctrlKey: true }, win);
    assertFocus({ index: 4 }, "Focus on item 4");
    assertSelection([2, 4], "Multi selection");
    assertDragstart(
      4,
      [2, 4],
      "Selection same when dragging selected and focused"
    );
    assertDragstart(
      2,
      [2, 4],
      "Selection same when dragging selected and non-focussed"
    );
  }
});