summaryrefslogtreecommitdiff
path: root/tests/test.rfb.js
blob: 4e2739358390bdd3caba3689553acc0b028c1858 (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
const expect = chai.expect;

import RFB from '../core/rfb.js';
import Websock from '../core/websock.js';
import ZStream from "../vendor/pako/lib/zlib/zstream.js";
import { deflateInit, deflate } from "../vendor/pako/lib/zlib/deflate.js";
import { encodings } from '../core/encodings.js';
import { toUnsigned32bit } from '../core/util/int.js';
import { encodeUTF8 } from '../core/util/strings.js';

import FakeWebSocket from './fake.websocket.js';

/* UIEvent constructor polyfill for IE */
(() => {
    if (typeof window.UIEvent === "function") return;

    function UIEvent( event, params ) {
        params = params || { bubbles: false, cancelable: false, view: window, detail: undefined };
        const evt = document.createEvent( 'UIEvent' );
        evt.initUIEvent( event, params.bubbles, params.cancelable, params.view, params.detail );
        return evt;
    }

    UIEvent.prototype = window.UIEvent.prototype;

    window.UIEvent = UIEvent;
})();

function push8(arr, num) {
    "use strict";
    arr.push(num & 0xFF);
}

function push16(arr, num) {
    "use strict";
    arr.push((num >> 8) & 0xFF,
             num & 0xFF);
}

function push32(arr, num) {
    "use strict";
    arr.push((num >> 24) & 0xFF,
             (num >> 16) & 0xFF,
             (num >>  8) & 0xFF,
             num & 0xFF);
}

function pushString(arr, string) {
    let utf8 = unescape(encodeURIComponent(string));
    for (let i = 0; i < utf8.length; i++) {
        arr.push(utf8.charCodeAt(i));
    }
}

function deflateWithSize(data) {
    // Adds the size of the string in front before deflating

    let unCompData = [];
    unCompData.push((data.length >> 24) & 0xFF,
                    (data.length >> 16) & 0xFF,
                    (data.length >>  8) & 0xFF,
                    (data.length & 0xFF));

    for (let i = 0; i < data.length; i++) {
        unCompData.push(data.charCodeAt(i));
    }

    let strm = new ZStream();
    let chunkSize = 1024 * 10 * 10;
    strm.output = new Uint8Array(chunkSize);
    deflateInit(strm, 5);

    strm.input = unCompData;
    strm.avail_in = strm.input.length;
    strm.next_in = 0;
    strm.next_out = 0;
    strm.avail_out = chunkSize;

    deflate(strm, 3);

    return new Uint8Array(strm.output.buffer, 0, strm.next_out);
}

describe('Remote Frame Buffer Protocol Client', function () {
    let clock;
    let raf;

    before(FakeWebSocket.replace);
    after(FakeWebSocket.restore);

    before(function () {
        this.clock = clock = sinon.useFakeTimers();
        // sinon doesn't support this yet
        raf = window.requestAnimationFrame;
        window.requestAnimationFrame = setTimeout;
        // Use a single set of buffers instead of reallocating to
        // speed up tests
        const sock = new Websock();
        const _sQ = new Uint8Array(sock._sQbufferSize);
        const rQ = new Uint8Array(sock._rQbufferSize);

        Websock.prototype._old_allocate_buffers = Websock.prototype._allocate_buffers;
        Websock.prototype._allocate_buffers = function () {
            this._sQ = _sQ;
            this._rQ = rQ;
        };

    });

    after(function () {
        Websock.prototype._allocate_buffers = Websock.prototype._old_allocate_buffers;
        this.clock.restore();
        window.requestAnimationFrame = raf;
    });

    let container;
    let rfbs;

    beforeEach(function () {
        // Create a container element for all RFB objects to attach to
        container = document.createElement('div');
        container.style.width = "100%";
        container.style.height = "100%";
        document.body.appendChild(container);

        // And track all created RFB objects
        rfbs = [];
    });
    afterEach(function () {
        // Make sure every created RFB object is properly cleaned up
        // or they might affect subsequent tests
        rfbs.forEach(function (rfb) {
            rfb.disconnect();
            expect(rfb._disconnect).to.have.been.called;
        });
        rfbs = [];

        document.body.removeChild(container);
        container = null;
    });

    function make_rfb(url, options) {
        url = url || 'wss://host:8675';
        const rfb = new RFB(container, url, options);
        clock.tick();
        rfb._sock._websocket._open();
        rfb._rfb_connection_state = 'connected';
        sinon.spy(rfb, "_disconnect");
        rfbs.push(rfb);
        return rfb;
    }

    describe('Connecting/Disconnecting', function () {
        describe('#RFB', function () {
            it('should set the current state to "connecting"', function () {
                const client = new RFB(document.createElement('div'), 'wss://host:8675');
                client._rfb_connection_state = '';
                this.clock.tick();
                expect(client._rfb_connection_state).to.equal('connecting');
            });

            it('should actually connect to the websocket', function () {
                const client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
                sinon.spy(client._sock, 'open');
                this.clock.tick();
                expect(client._sock.open).to.have.been.calledOnce;
                expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
            });
        });

        describe('#disconnect', function () {
            let client;
            beforeEach(function () {
                client = make_rfb();
            });

            it('should go to state "disconnecting" before "disconnected"', function () {
                sinon.spy(client, '_updateConnectionState');
                client.disconnect();
                expect(client._updateConnectionState).to.have.been.calledTwice;
                expect(client._updateConnectionState.getCall(0).args[0])
                    .to.equal('disconnecting');
                expect(client._updateConnectionState.getCall(1).args[0])
                    .to.equal('disconnected');
                expect(client._rfb_connection_state).to.equal('disconnected');
            });

            it('should unregister error event handler', function () {
                sinon.spy(client._sock, 'off');
                client.disconnect();
                expect(client._sock.off).to.have.been.calledWith('error');
            });

            it('should unregister message event handler', function () {
                sinon.spy(client._sock, 'off');
                client.disconnect();
                expect(client._sock.off).to.have.been.calledWith('message');
            });

            it('should unregister open event handler', function () {
                sinon.spy(client._sock, 'off');
                client.disconnect();
                expect(client._sock.off).to.have.been.calledWith('open');
            });
        });

        describe('#sendCredentials', function () {
            let client;
            beforeEach(function () {
                client = make_rfb();
                client._rfb_connection_state = 'connecting';
            });

            it('should set the rfb credentials properly"', function () {
                client.sendCredentials({ password: 'pass' });
                expect(client._rfb_credentials).to.deep.equal({ password: 'pass' });
            });

            it('should call init_msg "soon"', function () {
                client._init_msg = sinon.spy();
                client.sendCredentials({ password: 'pass' });
                this.clock.tick(5);
                expect(client._init_msg).to.have.been.calledOnce;
            });
        });
    });

    describe('Public API Basic Behavior', function () {
        let client;
        beforeEach(function () {
            client = make_rfb();
        });

        describe('#sendCtrlAlDel', function () {
            it('should sent ctrl[down]-alt[down]-del[down] then del[up]-alt[up]-ctrl[up]', function () {
                const expected = {_sQ: new Uint8Array(48), _sQlen: 0, flush: () => {}};
                RFB.messages.keyEvent(expected, 0xFFE3, 1);
                RFB.messages.keyEvent(expected, 0xFFE9, 1);
                RFB.messages.keyEvent(expected, 0xFFFF, 1);
                RFB.messages.keyEvent(expected, 0xFFFF, 0);
                RFB.messages.keyEvent(expected, 0xFFE9, 0);
                RFB.messages.keyEvent(expected, 0xFFE3, 0);

                client.sendCtrlAltDel();
                expect(client._sock).to.have.sent(expected._sQ);
            });

            it('should not send the keys if we are not in a normal state', function () {
                sinon.spy(client._sock, 'flush');
                client._rfb_connection_state = "connecting";
                client.sendCtrlAltDel();
                expect(client._sock.flush).to.not.have.been.called;
            });

            it('should not send the keys if we are set as view_only', function () {
                sinon.spy(client._sock, 'flush');
                client._viewOnly = true;
                client.sendCtrlAltDel();
                expect(client._sock.flush).to.not.have.been.called;
            });
        });

        describe('#sendKey', function () {
            it('should send a single key with the given code and state (down = true)', function () {
                const expected = {_sQ: new Uint8Array(8), _sQlen: 0, flush: () => {}};
                RFB.messages.keyEvent(expected, 123, 1);
                client.sendKey(123, 'Key123', true);
                expect(client._sock).to.have.sent(expected._sQ);
            });

            it('should send both a down and up event if the state is not specified', function () {
                const expected = {_sQ: new Uint8Array(16), _sQlen: 0, flush: () => {}};
                RFB.messages.keyEvent(expected, 123, 1);
                RFB.messages.keyEvent(expected, 123, 0);
                client.sendKey(123, 'Key123');
                expect(client._sock).to.have.sent(expected._sQ);
            });

            it('should not send the key if we are not in a normal state', function () {
                sinon.spy(client._sock, 'flush');
                client._rfb_connection_state = "connecting";
                client.sendKey(123, 'Key123');
                expect(client._sock.flush).to.not.have.been.called;
            });

            it('should not send the key if we are set as view_only', function () {
                sinon.spy(client._sock, 'flush');
                client._viewOnly = true;
                client.sendKey(123, 'Key123');
                expect(client._sock.flush).to.not.have.been.called;
            });

            it('should send QEMU extended events if supported', function () {
                client._qemuExtKeyEventSupported = true;
                const expected = {_sQ: new Uint8Array(12), _sQlen: 0, flush: () => {}};
                RFB.messages.QEMUExtendedKeyEvent(expected, 0x20, true, 0x0039);
                client.sendKey(0x20, 'Space', true);
                expect(client._sock).to.have.sent(expected._sQ);
            });

            it('should not send QEMU extended events if unknown key code', function () {
                client._qemuExtKeyEventSupported = true;
                const expected = {_sQ: new Uint8Array(8), _sQlen: 0, flush: () => {}};
                RFB.messages.keyEvent(expected, 123, 1);
                client.sendKey(123, 'FooBar', true);
                expect(client._sock).to.have.sent(expected._sQ);
            });
        });

        describe('#focus', function () {
            it('should move focus to canvas object', function () {
                client._canvas.focus = sinon.spy();
                client.focus();
                expect(client._canvas.focus).to.have.been.calledOnce;
            });
        });

        describe('#blur', function () {
            it('should remove focus from canvas object', function () {
                client._canvas.blur = sinon.spy();
                client.blur();
                expect(client._canvas.blur).to.have.been.calledOnce;
            });
        });

        describe('#clipboardPasteFrom', function () {
            describe('Clipboard update handling', function () {
                beforeEach(function () {
                    sinon.spy(RFB.messages, 'clientCutText');
                    sinon.spy(RFB.messages, 'extendedClipboardNotify');
                });

                afterEach(function () {
                    RFB.messages.clientCutText.restore();
                    RFB.messages.extendedClipboardNotify.restore();
                });

                it('should send the given text in an clipboard update', function () {
                    client.clipboardPasteFrom('abc');

                    expect(RFB.messages.clientCutText).to.have.been.calledOnce;
                    expect(RFB.messages.clientCutText).to.have.been.calledWith(client._sock,
                                                                               new Uint8Array([97, 98, 99]));
                });

                it('should send an notify if extended clipboard is supported by server', function () {
                    // Send our capabilities
                    let data = [3, 0, 0, 0];
                    const flags = [0x1F, 0x00, 0x00, 0x01];
                    let fileSizes = [0x00, 0x00, 0x00, 0x1E];

                    push32(data, toUnsigned32bit(-8));
                    data = data.concat(flags);
                    data = data.concat(fileSizes);
                    client._sock._websocket._receive_data(new Uint8Array(data));

                    client.clipboardPasteFrom('extended test');
                    expect(RFB.messages.extendedClipboardNotify).to.have.been.calledOnce;
                });
            });

            it('should flush multiple times for large clipboards', function () {
                sinon.spy(client._sock, 'flush');
                let long_text = "";
                for (let i = 0; i < client._sock._sQbufferSize + 100; i++) {
                    long_text += 'a';
                }
                client.clipboardPasteFrom(long_text);
                expect(client._sock.flush).to.have.been.calledTwice;
            });

            it('should not send the text if we are not in a normal state', function () {
                sinon.spy(client._sock, 'flush');
                client._rfb_connection_state = "connecting";
                client.clipboardPasteFrom('abc');
                expect(client._sock.flush).to.not.have.been.called;
            });
        });

        describe("XVP operations", function () {
            beforeEach(function () {
                client._rfb_xvp_ver = 1;
            });

            it('should send the shutdown signal on #machineShutdown', function () {
                client.machineShutdown();
                expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x02]));
            });

            it('should send the reboot signal on #machineReboot', function () {
                client.machineReboot();
                expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x03]));
            });

            it('should send the reset signal on #machineReset', function () {
                client.machineReset();
                expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x04]));
            });

            it('should not send XVP operations with higher versions than we support', function () {
                sinon.spy(client._sock, 'flush');
                client._xvpOp(2, 7);
                expect(client._sock.flush).to.not.have.been.called;
            });
        });
    });

    describe('Clipping', function () {
        let client;
        beforeEach(function () {
            client = make_rfb();
            container.style.width = '70px';
            container.style.height = '80px';
            client.clipViewport = true;
        });

        it('should update display clip state when changing the property', function () {
            const spy = sinon.spy(client._display, "clipViewport", ["set"]);

            client.clipViewport = false;
            expect(spy.set).to.have.been.calledOnce;
            expect(spy.set).to.have.been.calledWith(false);
            spy.set.resetHistory();

            client.clipViewport = true;
            expect(spy.set).to.have.been.calledOnce;
            expect(spy.set).to.have.been.calledWith(true);
        });

        it('should update the viewport when the container size changes', function () {
            sinon.spy(client._display, "viewportChangeSize");

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick();

            expect(client._display.viewportChangeSize).to.have.been.calledOnce;
            expect(client._display.viewportChangeSize).to.have.been.calledWith(40, 50);
        });

        it('should update the viewport when the remote session resizes', function () {
            // Simple ExtendedDesktopSize FBU message
            const incoming = [ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0xfe, 0xcc,
                               0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xff,
                               0x00, 0x00, 0x00, 0x00 ];

            sinon.spy(client._display, "viewportChangeSize");

            client._sock._websocket._receive_data(new Uint8Array(incoming));

            // FIXME: Display implicitly calls viewportChangeSize() when
            //        resizing the framebuffer, hence calledTwice.
            expect(client._display.viewportChangeSize).to.have.been.calledTwice;
            expect(client._display.viewportChangeSize).to.have.been.calledWith(70, 80);
        });

        it('should not update the viewport if not clipping', function () {
            client.clipViewport = false;
            sinon.spy(client._display, "viewportChangeSize");

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick();

            expect(client._display.viewportChangeSize).to.not.have.been.called;
        });

        it('should not update the viewport if scaling', function () {
            client.scaleViewport = true;
            sinon.spy(client._display, "viewportChangeSize");

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick();

            expect(client._display.viewportChangeSize).to.not.have.been.called;
        });

        describe('Dragging', function () {
            beforeEach(function () {
                client.dragViewport = true;
                sinon.spy(RFB.messages, "pointerEvent");
            });

            afterEach(function () {
                RFB.messages.pointerEvent.restore();
            });

            it('should not send button messages when initiating viewport dragging', function () {
                client._handleMouseButton(13, 9, 0x001);
                expect(RFB.messages.pointerEvent).to.not.have.been.called;
            });

            it('should send button messages when release without movement', function () {
                // Just up and down
                client._handleMouseButton(13, 9, 0x001);
                client._handleMouseButton(13, 9, 0x000);
                expect(RFB.messages.pointerEvent).to.have.been.calledTwice;

                RFB.messages.pointerEvent.resetHistory();

                // Small movement
                client._handleMouseButton(13, 9, 0x001);
                client._handleMouseMove(15, 14);
                client._handleMouseButton(15, 14, 0x000);
                expect(RFB.messages.pointerEvent).to.have.been.calledTwice;
            });

            it('should send button message directly when drag is disabled', function () {
                client.dragViewport = false;
                client._handleMouseButton(13, 9, 0x001);
                expect(RFB.messages.pointerEvent).to.have.been.calledOnce;
            });

            it('should be initiate viewport dragging on sufficient movement', function () {
                sinon.spy(client._display, "viewportChangePos");

                // Too small movement

                client._handleMouseButton(13, 9, 0x001);
                client._handleMouseMove(18, 9);

                expect(RFB.messages.pointerEvent).to.not.have.been.called;
                expect(client._display.viewportChangePos).to.not.have.been.called;

                // Sufficient movement

                client._handleMouseMove(43, 9);

                expect(RFB.messages.pointerEvent).to.not.have.been.called;
                expect(client._display.viewportChangePos).to.have.been.calledOnce;
                expect(client._display.viewportChangePos).to.have.been.calledWith(-30, 0);

                client._display.viewportChangePos.resetHistory();

                // Now a small movement should move right away

                client._handleMouseMove(43, 14);

                expect(RFB.messages.pointerEvent).to.not.have.been.called;
                expect(client._display.viewportChangePos).to.have.been.calledOnce;
                expect(client._display.viewportChangePos).to.have.been.calledWith(0, -5);
            });

            it('should not send button messages when dragging ends', function () {
                // First the movement

                client._handleMouseButton(13, 9, 0x001);
                client._handleMouseMove(43, 9);
                client._handleMouseButton(43, 9, 0x000);

                expect(RFB.messages.pointerEvent).to.not.have.been.called;
            });

            it('should terminate viewport dragging on a button up event', function () {
                // First the dragging movement

                client._handleMouseButton(13, 9, 0x001);
                client._handleMouseMove(43, 9);
                client._handleMouseButton(43, 9, 0x000);

                // Another movement now should not move the viewport

                sinon.spy(client._display, "viewportChangePos");

                client._handleMouseMove(43, 59);

                expect(client._display.viewportChangePos).to.not.have.been.called;
            });
        });
    });

    describe('Scaling', function () {
        let client;
        beforeEach(function () {
            client = make_rfb();
            container.style.width = '70px';
            container.style.height = '80px';
            client.scaleViewport = true;
        });

        it('should update display scale factor when changing the property', function () {
            const spy = sinon.spy(client._display, "scale", ["set"]);
            sinon.spy(client._display, "autoscale");

            client.scaleViewport = false;
            expect(spy.set).to.have.been.calledOnce;
            expect(spy.set).to.have.been.calledWith(1.0);
            expect(client._display.autoscale).to.not.have.been.called;

            client.scaleViewport = true;
            expect(client._display.autoscale).to.have.been.calledOnce;
            expect(client._display.autoscale).to.have.been.calledWith(70, 80);
        });

        it('should update the clipping setting when changing the property', function () {
            client.clipViewport = true;

            const spy = sinon.spy(client._display, "clipViewport", ["set"]);

            client.scaleViewport = false;
            expect(spy.set).to.have.been.calledOnce;
            expect(spy.set).to.have.been.calledWith(true);

            spy.set.resetHistory();

            client.scaleViewport = true;
            expect(spy.set).to.have.been.calledOnce;
            expect(spy.set).to.have.been.calledWith(false);
        });

        it('should update the scaling when the container size changes', function () {
            sinon.spy(client._display, "autoscale");

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick();

            expect(client._display.autoscale).to.have.been.calledOnce;
            expect(client._display.autoscale).to.have.been.calledWith(40, 50);
        });

        it('should update the scaling when the remote session resizes', function () {
            // Simple ExtendedDesktopSize FBU message
            const incoming = [ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0xfe, 0xcc,
                               0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xff,
                               0x00, 0x00, 0x00, 0x00 ];

            sinon.spy(client._display, "autoscale");

            client._sock._websocket._receive_data(new Uint8Array(incoming));

            expect(client._display.autoscale).to.have.been.calledOnce;
            expect(client._display.autoscale).to.have.been.calledWith(70, 80);
        });

        it('should not update the display scale factor if not scaling', function () {
            client.scaleViewport = false;

            sinon.spy(client._display, "autoscale");

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick();

            expect(client._display.autoscale).to.not.have.been.called;
        });
    });

    describe('Remote resize', function () {
        let client;
        beforeEach(function () {
            client = make_rfb();
            client._supportsSetDesktopSize = true;
            client.resizeSession = true;
            container.style.width = '70px';
            container.style.height = '80px';
            sinon.spy(RFB.messages, "setDesktopSize");
        });

        afterEach(function () {
            RFB.messages.setDesktopSize.restore();
        });

        it('should only request a resize when turned on', function () {
            client.resizeSession = false;
            expect(RFB.messages.setDesktopSize).to.not.have.been.called;
            client.resizeSession = true;
            expect(RFB.messages.setDesktopSize).to.have.been.calledOnce;
        });

        it('should request a resize when initially connecting', function () {
            // Simple ExtendedDesktopSize FBU message
            const incoming = [ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x04, 0x00, 0x04, 0xff, 0xff, 0xfe, 0xcc,
                               0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04,
                               0x00, 0x00, 0x00, 0x00 ];

            // First message should trigger a resize

            client._supportsSetDesktopSize = false;

            client._sock._websocket._receive_data(new Uint8Array(incoming));

            expect(RFB.messages.setDesktopSize).to.have.been.calledOnce;
            expect(RFB.messages.setDesktopSize).to.have.been.calledWith(sinon.match.object, 70, 80, 0, 0);

            RFB.messages.setDesktopSize.resetHistory();

            // Second message should not trigger a resize

            client._sock._websocket._receive_data(new Uint8Array(incoming));

            expect(RFB.messages.setDesktopSize).to.not.have.been.called;
        });

        it('should request a resize when the container resizes', function () {
            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick(1000);

            expect(RFB.messages.setDesktopSize).to.have.been.calledOnce;
            expect(RFB.messages.setDesktopSize).to.have.been.calledWith(sinon.match.object, 40, 50, 0, 0);
        });

        it('should not resize until the container size is stable', function () {
            container.style.width = '20px';
            container.style.height = '30px';
            const event1 = new UIEvent('resize');
            window.dispatchEvent(event1);
            clock.tick(400);

            expect(RFB.messages.setDesktopSize).to.not.have.been.called;

            container.style.width = '40px';
            container.style.height = '50px';
            const event2 = new UIEvent('resize');
            window.dispatchEvent(event2);
            clock.tick(400);

            expect(RFB.messages.setDesktopSize).to.not.have.been.called;

            clock.tick(200);

            expect(RFB.messages.setDesktopSize).to.have.been.calledOnce;
            expect(RFB.messages.setDesktopSize).to.have.been.calledWith(sinon.match.object, 40, 50, 0, 0);
        });

        it('should not resize when resize is disabled', function () {
            client._resizeSession = false;

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick(1000);

            expect(RFB.messages.setDesktopSize).to.not.have.been.called;
        });

        it('should not resize when resize is not supported', function () {
            client._supportsSetDesktopSize = false;

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick(1000);

            expect(RFB.messages.setDesktopSize).to.not.have.been.called;
        });

        it('should not resize when in view only mode', function () {
            client._viewOnly = true;

            container.style.width = '40px';
            container.style.height = '50px';
            const event = new UIEvent('resize');
            window.dispatchEvent(event);
            clock.tick(1000);

            expect(RFB.messages.setDesktopSize).to.not.have.been.called;
        });

        it('should not try to override a server resize', function () {
            // Simple ExtendedDesktopSize FBU message
            const incoming = [ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x04, 0x00, 0x04, 0xff, 0xff, 0xfe, 0xcc,
                               0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04,
                               0x00, 0x00, 0x00, 0x00 ];

            client._sock._websocket._receive_data(new Uint8Array(incoming));

            expect(RFB.messages.setDesktopSize).to.not.have.been.called;
        });
    });

    describe('Misc Internals', function () {
        describe('#_updateConnectionState', function () {
            let client;
            beforeEach(function () {
                client = make_rfb();
            });

            it('should clear the disconnect timer if the state is not "disconnecting"', function () {
                const spy = sinon.spy();
                client._disconnTimer = setTimeout(spy, 50);
                client._rfb_connection_state = 'connecting';
                client._updateConnectionState('connected');
                this.clock.tick(51);
                expect(spy).to.not.have.been.called;
                expect(client._disconnTimer).to.be.null;
            });

            it('should set the rfb_connection_state', function () {
                client._rfb_connection_state = 'connecting';
                client._updateConnectionState('connected');
                expect(client._rfb_connection_state).to.equal('connected');
            });

            it('should not change the state when we are disconnected', function () {
                client.disconnect();
                expect(client._rfb_connection_state).to.equal('disconnected');
                client._updateConnectionState('connecting');
                expect(client._rfb_connection_state).to.not.equal('connecting');
            });

            it('should ignore state changes to the same state', function () {
                const connectSpy = sinon.spy();
                client.addEventListener("connect", connectSpy);

                expect(client._rfb_connection_state).to.equal('connected');
                client._updateConnectionState('connected');
                expect(connectSpy).to.not.have.been.called;

                client.disconnect();

                const disconnectSpy = sinon.spy();
                client.addEventListener("disconnect", disconnectSpy);

                expect(client._rfb_connection_state).to.equal('disconnected');
                client._updateConnectionState('disconnected');
                expect(disconnectSpy).to.not.have.been.called;
            });

            it('should ignore illegal state changes', function () {
                const spy = sinon.spy();
                client.addEventListener("disconnect", spy);
                client._updateConnectionState('disconnected');
                expect(client._rfb_connection_state).to.not.equal('disconnected');
                expect(spy).to.not.have.been.called;
            });
        });

        describe('#_fail', function () {
            let client;
            beforeEach(function () {
                client = make_rfb();
            });

            it('should close the WebSocket connection', function () {
                sinon.spy(client._sock, 'close');
                client._fail();
                expect(client._sock.close).to.have.been.calledOnce;
            });

            it('should transition to disconnected', function () {
                sinon.spy(client, '_updateConnectionState');
                client._fail();
                this.clock.tick(2000);
                expect(client._updateConnectionState).to.have.been.called;
                expect(client._rfb_connection_state).to.equal('disconnected');
            });

            it('should set clean_disconnect variable', function () {
                client._rfb_clean_disconnect = true;
                client._rfb_connection_state = 'connected';
                client._fail();
                expect(client._rfb_clean_disconnect).to.be.false;
            });

            it('should result in disconnect event with clean set to false', function () {
                client._rfb_connection_state = 'connected';
                const spy = sinon.spy();
                client.addEventListener("disconnect", spy);
                client._fail();
                this.clock.tick(2000);
                expect(spy).to.have.been.calledOnce;
                expect(spy.args[0][0].detail.clean).to.be.false;
            });

        });
    });

    describe('Connection States', function () {
        describe('connecting', function () {
            it('should open the websocket connection', function () {
                const client = new RFB(document.createElement('div'),
                                       'ws://HOST:8675/PATH');
                sinon.spy(client._sock, 'open');
                this.clock.tick();
                expect(client._sock.open).to.have.been.calledOnce;
            });
        });

        describe('connected', function () {
            let client;
            beforeEach(function () {
                client = make_rfb();
            });

            it('should result in a connect event if state becomes connected', function () {
                const spy = sinon.spy();
                client.addEventListener("connect", spy);
                client._rfb_connection_state = 'connecting';
                client._updateConnectionState('connected');
                expect(spy).to.have.been.calledOnce;
            });

            it('should not result in a connect event if the state is not "connected"', function () {
                const spy = sinon.spy();
                client.addEventListener("connect", spy);
                client._sock._websocket.open = () => {};  // explicitly don't call onopen
                client._updateConnectionState('connecting');
                expect(spy).to.not.have.been.called;
            });
        });

        describe('disconnecting', function () {
            let client;
            beforeEach(function () {
                client = make_rfb();
            });

            it('should force disconnect if we do not call Websock.onclose within the disconnection timeout', function () {
                sinon.spy(client, '_updateConnectionState');
                client._sock._websocket.close = () => {};  // explicitly don't call onclose
                client._updateConnectionState('disconnecting');
                this.clock.tick(3 * 1000);
                expect(client._updateConnectionState).to.have.been.calledTwice;
                expect(client._rfb_disconnect_reason).to.not.equal("");
                expect(client._rfb_connection_state).to.equal("disconnected");
            });

            it('should not fail if Websock.onclose gets called within the disconnection timeout', function () {
                client._updateConnectionState('disconnecting');
                this.clock.tick(3 * 1000 / 2);
                client._sock._websocket.close();
                this.clock.tick(3 * 1000 / 2 + 1);
                expect(client._rfb_connection_state).to.equal('disconnected');
            });

            it('should close the WebSocket connection', function () {
                sinon.spy(client._sock, 'close');
                client._updateConnectionState('disconnecting');
                expect(client._sock.close).to.have.been.calledOnce;
            });

            it('should not result in a disconnect event', function () {
                const spy = sinon.spy();
                client.addEventListener("disconnect", spy);
                client._sock._websocket.close = () => {};  // explicitly don't call onclose
                client._updateConnectionState('disconnecting');
                expect(spy).to.not.have.been.called;
            });
        });

        describe('disconnected', function () {
            let client;
            beforeEach(function () {
                client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
            });

            it('should result in a disconnect event if state becomes "disconnected"', function () {
                const spy = sinon.spy();
                client.addEventListener("disconnect", spy);
                client._rfb_connection_state = 'disconnecting';
                client._updateConnectionState('disconnected');
                expect(spy).to.have.been.calledOnce;
                expect(spy.args[0][0].detail.clean).to.be.true;
            });

            it('should result in a disconnect event without msg when no reason given', function () {
                const spy = sinon.spy();
                client.addEventListener("disconnect", spy);
                client._rfb_connection_state = 'disconnecting';
                client._rfb_disconnect_reason = "";
                client._updateConnectionState('disconnected');
                expect(spy).to.have.been.calledOnce;
                expect(spy.args[0].length).to.equal(1);
            });
        });
    });

    describe('Protocol Initialization States', function () {
        let client;
        beforeEach(function () {
            client = make_rfb();
            client._rfb_connection_state = 'connecting';
        });

        describe('ProtocolVersion', function () {
            function send_ver(ver, client) {
                const arr = new Uint8Array(12);
                for (let i = 0; i < ver.length; i++) {
                    arr[i+4] = ver.charCodeAt(i);
                }
                arr[0] = 'R'; arr[1] = 'F'; arr[2] = 'B'; arr[3] = ' ';
                arr[11] = '\n';
                client._sock._websocket._receive_data(arr);
            }

            describe('version parsing', function () {
                it('should interpret version 003.003 as version 3.3', function () {
                    send_ver('003.003', client);
                    expect(client._rfb_version).to.equal(3.3);
                });

                it('should interpret version 003.006 as version 3.3', function () {
                    send_ver('003.006', client);
                    expect(client._rfb_version).to.equal(3.3);
                });

                it('should interpret version 003.889 as version 3.3', function () {
                    send_ver('003.889', client);
                    expect(client._rfb_version).to.equal(3.3);
                });

                it('should interpret version 003.007 as version 3.7', function () {
                    send_ver('003.007', client);
                    expect(client._rfb_version).to.equal(3.7);
                });

                it('should interpret version 003.008 as version 3.8', function () {
                    send_ver('003.008', client);
                    expect(client._rfb_version).to.equal(3.8);
                });

                it('should interpret version 004.000 as version 3.8', function () {
                    send_ver('004.000', client);
                    expect(client._rfb_version).to.equal(3.8);
                });

                it('should interpret version 004.001 as version 3.8', function () {
                    send_ver('004.001', client);
                    expect(client._rfb_version).to.equal(3.8);
                });

                it('should interpret version 005.000 as version 3.8', function () {
                    send_ver('005.000', client);
                    expect(client._rfb_version).to.equal(3.8);
                });

                it('should fail on an invalid version', function () {
                    sinon.spy(client, "_fail");
                    send_ver('002.000', client);
                    expect(client._fail).to.have.been.calledOnce;
                });
            });

            it('should send back the interpreted version', function () {
                send_ver('004.000', client);

                const expected_str = 'RFB 003.008\n';
                const expected = [];
                for (let i = 0; i < expected_str.length; i++) {
                    expected[i] = expected_str.charCodeAt(i);
                }

                expect(client._sock).to.have.sent(new Uint8Array(expected));
            });

            it('should transition to the Security state on successful negotiation', function () {
                send_ver('003.008', client);
                expect(client._rfb_init_state).to.equal('Security');
            });

            describe('Repeater', function () {
                beforeEach(function () {
                    client = make_rfb('wss://host:8675', { repeaterID: "12345" });
                    client._rfb_connection_state = 'connecting';
                });

                it('should interpret version 000.000 as a repeater', function () {
                    send_ver('000.000', client);
                    expect(client._rfb_version).to.equal(0);

                    const sent_data = client._sock._websocket._get_sent_data();
                    expect(new Uint8Array(sent_data.buffer, 0, 9)).to.array.equal(new Uint8Array([73, 68, 58, 49, 50, 51, 52, 53, 0]));
                    expect(sent_data).to.have.length(250);
                });

                it('should handle two step repeater negotiation', function () {
                    send_ver('000.000', client);
                    send_ver('003.008', client);
                    expect(client._rfb_version).to.equal(3.8);
                });
            });
        });

        describe('Security', function () {
            beforeEach(function () {
                client._rfb_init_state = 'Security';
            });

            it('should simply receive the auth scheme when for versions < 3.7', function () {
                client._rfb_version = 3.6;
                const auth_scheme_raw = [1, 2, 3, 4];
                const auth_scheme = (auth_scheme_raw[0] << 24) + (auth_scheme_raw[1] << 16) +
                                  (auth_scheme_raw[2] << 8) + auth_scheme_raw[3];
                client._sock._websocket._receive_data(new Uint8Array(auth_scheme_raw));
                expect(client._rfb_auth_scheme).to.equal(auth_scheme);
            });

            it('should prefer no authentication is possible', function () {
                client._rfb_version = 3.7;
                const auth_schemes = [2, 1, 3];
                client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
                expect(client._rfb_auth_scheme).to.equal(1);
                expect(client._sock).to.have.sent(new Uint8Array([1, 1]));
            });

            it('should choose for the most prefered scheme possible for versions >= 3.7', function () {
                client._rfb_version = 3.7;
                const auth_schemes = [2, 22, 16];
                client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
                expect(client._rfb_auth_scheme).to.equal(22);
                expect(client._sock).to.have.sent(new Uint8Array([22]));
            });

            it('should fail if there are no supported schemes for versions >= 3.7', function () {
                sinon.spy(client, "_fail");
                client._rfb_version = 3.7;
                const auth_schemes = [1, 32];
                client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
                expect(client._fail).to.have.been.calledOnce;
            });

            it('should fail with the appropriate message if no types are sent for versions >= 3.7', function () {
                client._rfb_version = 3.7;
                const failure_data = [0, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
                sinon.spy(client, '_fail');
                client._sock._websocket._receive_data(new Uint8Array(failure_data));

                expect(client._fail).to.have.been.calledOnce;
                expect(client._fail).to.have.been.calledWith(
                    'Security negotiation failed on no security types (reason: whoops)');
            });

            it('should transition to the Authentication state and continue on successful negotiation', function () {
                client._rfb_version = 3.7;
                const auth_schemes = [1, 1];
                client._negotiate_authentication = sinon.spy();
                client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
                expect(client._rfb_init_state).to.equal('Authentication');
                expect(client._negotiate_authentication).to.have.been.calledOnce;
            });
        });

        describe('Authentication', function () {
            beforeEach(function () {
                client._rfb_init_state = 'Security';
            });

            function send_security(type, cl) {
                cl._sock._websocket._receive_data(new Uint8Array([1, type]));
            }

            it('should fail on auth scheme 0 (pre 3.7) with the given message', function () {
                client._rfb_version = 3.6;
                const err_msg = "Whoopsies";
                const data = [0, 0, 0, 0];
                const err_len = err_msg.length;
                push32(data, err_len);
                for (let i = 0; i < err_len; i++) {
                    data.push(err_msg.charCodeAt(i));
                }

                sinon.spy(client, '_fail');
                client._sock._websocket._receive_data(new Uint8Array(data));
                expect(client._fail).to.have.been.calledWith(
                    'Security negotiation failed on authentication scheme (reason: Whoopsies)');
            });

            it('should transition straight to SecurityResult on "no auth" (1) for versions >= 3.8', function () {
                client._rfb_version = 3.8;
                send_security(1, client);
                expect(client._rfb_init_state).to.equal('SecurityResult');
            });

            it('should transition straight to ServerInitialisation on "no auth" for versions < 3.8', function () {
                client._rfb_version = 3.7;
                send_security(1, client);
                expect(client._rfb_init_state).to.equal('ServerInitialisation');
            });

            it('should fail on an unknown auth scheme', function () {
                sinon.spy(client, "_fail");
                client._rfb_version = 3.8;
                send_security(57, client);
                expect(client._fail).to.have.been.calledOnce;
            });

            describe('VNC Authentication (type 2) Handler', function () {
                beforeEach(function () {
                    client._rfb_init_state = 'Security';
                    client._rfb_version = 3.8;
                });

                it('should fire the credentialsrequired event if missing a password', function () {
                    const spy = sinon.spy();
                    client.addEventListener("credentialsrequired", spy);
                    send_security(2, client);

                    const challenge = [];
                    for (let i = 0; i < 16; i++) { challenge[i] = i; }
                    client._sock._websocket._receive_data(new Uint8Array(challenge));

                    expect(client._rfb_credentials).to.be.empty;
                    expect(spy).to.have.been.calledOnce;
                    expect(spy.args[0][0].detail.types).to.have.members(["password"]);
                });

                it('should encrypt the password with DES and then send it back', function () {
                    client._rfb_credentials = { password: 'passwd' };
                    send_security(2, client);
                    client._sock._websocket._get_sent_data(); // skip the choice of auth reply

                    const challenge = [];
                    for (let i = 0; i < 16; i++) { challenge[i] = i; }
                    client._sock._websocket._receive_data(new Uint8Array(challenge));

                    const des_pass = RFB.genDES('passwd', challenge);
                    expect(client._sock).to.have.sent(new Uint8Array(des_pass));
                });

                it('should transition to SecurityResult immediately after sending the password', function () {
                    client._rfb_credentials = { password: 'passwd' };
                    send_security(2, client);

                    const challenge = [];
                    for (let i = 0; i < 16; i++) { challenge[i] = i; }
                    client._sock._websocket._receive_data(new Uint8Array(challenge));

                    expect(client._rfb_init_state).to.equal('SecurityResult');
                });
            });

            describe('XVP Authentication (type 22) Handler', function () {
                beforeEach(function () {
                    client._rfb_init_state = 'Security';
                    client._rfb_version = 3.8;
                });

                it('should fall through to standard VNC authentication upon completion', function () {
                    client._rfb_credentials = { username: 'user',
                                                target: 'target',
                                                password: 'password' };
                    client._negotiate_std_vnc_auth = sinon.spy();
                    send_security(22, client);
                    expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
                });

                it('should fire the credentialsrequired event if all credentials are missing', function () {
                    const spy = sinon.spy();
                    client.addEventListener("credentialsrequired", spy);
                    client._rfb_credentials = {};
                    send_security(22, client);

                    expect(client._rfb_credentials).to.be.empty;
                    expect(spy).to.have.been.calledOnce;
                    expect(spy.args[0][0].detail.types).to.have.members(["username", "password", "target"]);
                });

                it('should fire the credentialsrequired event if some credentials are missing', function () {
                    const spy = sinon.spy();
                    client.addEventListener("credentialsrequired", spy);
                    client._rfb_credentials = { username: 'user',
                                                target: 'target' };
                    send_security(22, client);

                    expect(spy).to.have.been.calledOnce;
                    expect(spy.args[0][0].detail.types).to.have.members(["username", "password", "target"]);
                });

                it('should send user and target separately', function () {
                    client._rfb_credentials = { username: 'user',
                                                target: 'target',
                                                password: 'password' };
                    client._negotiate_std_vnc_auth = sinon.spy();

                    send_security(22, client);

                    const expected = [22, 4, 6]; // auth selection, len user, len target
                    for (let i = 0; i < 10; i++) { expected[i+3] = 'usertarget'.charCodeAt(i); }

                    expect(client._sock).to.have.sent(new Uint8Array(expected));
                });
            });

            describe('TightVNC Authentication (type 16) Handler', function () {
                beforeEach(function () {
                    client._rfb_init_state = 'Security';
                    client._rfb_version = 3.8;
                    send_security(16, client);
                    client._sock._websocket._get_sent_data();  // skip the security reply
                });

                function send_num_str_pairs(pairs, client) {
                    const data = [];
                    push32(data, pairs.length);

                    for (let i = 0; i < pairs.length; i++) {
                        push32(data, pairs[i][0]);
                        for (let j = 0; j < 4; j++) {
                            data.push(pairs[i][1].charCodeAt(j));
                        }
                        for (let j = 0; j < 8; j++) {
                            data.push(pairs[i][2].charCodeAt(j));
                        }
                    }

                    client._sock._websocket._receive_data(new Uint8Array(data));
                }

                it('should skip tunnel negotiation if no tunnels are requested', function () {
                    client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
                    expect(client._rfb_tightvnc).to.be.true;
                });

                it('should fail if no supported tunnels are listed', function () {
                    sinon.spy(client, "_fail");
                    send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client);
                    expect(client._fail).to.have.been.calledOnce;
                });

                it('should choose the notunnel tunnel type', function () {
                    send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL'], [123, 'OTHR', 'SOMETHNG']], client);
                    expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 0]));
                });

                it('should choose the notunnel tunnel type for Siemens devices', function () {
                    send_num_str_pairs([[1, 'SICR', 'SCHANNEL'], [2, 'SICR', 'SCHANLPW']], client);
                    expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 0]));
                });

                it('should continue to sub-auth negotiation after tunnel negotiation', function () {
                    send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL']], client);
                    client._sock._websocket._get_sent_data();  // skip the tunnel choice here
                    send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
                    expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
                    expect(client._rfb_init_state).to.equal('SecurityResult');
                });

                /*it('should attempt to use VNC auth over no auth when possible', function () {
                    client._rfb_tightvnc = true;
                    client._negotiate_std_vnc_auth = sinon.spy();
                    send_num_str_pairs([[1, 'STDV', 'NOAUTH__'], [2, 'STDV', 'VNCAUTH_']], client);
                    expect(client._sock).to.have.sent([0, 0, 0, 1]);
                    expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
                    expect(client._rfb_auth_scheme).to.equal(2);
                });*/ // while this would make sense, the original code doesn't actually do this

                it('should accept the "no auth" auth type and transition to SecurityResult', function () {
                    client._rfb_tightvnc = true;
                    send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
                    expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
                    expect(client._rfb_init_state).to.equal('SecurityResult');
                });

                it('should accept VNC authentication and transition to that', function () {
                    client._rfb_tightvnc = true;
                    client._negotiate_std_vnc_auth = sinon.spy();
                    send_num_str_pairs([[2, 'STDV', 'VNCAUTH__']], client);
                    expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 2]));
                    expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
                    expect(client._rfb_auth_scheme).to.equal(2);
                });

                it('should fail if there are no supported auth types', function () {
                    sinon.spy(client, "_fail");
                    client._rfb_tightvnc = true;
                    send_num_str_pairs([[23, 'stdv', 'badval__']], client);
                    expect(client._fail).to.have.been.calledOnce;
                });
            });
        });

        describe('SecurityResult', function () {
            beforeEach(function () {
                client._rfb_init_state = 'SecurityResult';
            });

            it('should fall through to ServerInitialisation on a response code of 0', function () {
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
                expect(client._rfb_init_state).to.equal('ServerInitialisation');
            });

            it('should fail on an error code of 1 with the given message for versions >= 3.8', function () {
                client._rfb_version = 3.8;
                sinon.spy(client, '_fail');
                const failure_data = [0, 0, 0, 1, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
                client._sock._websocket._receive_data(new Uint8Array(failure_data));
                expect(client._fail).to.have.been.calledWith(
                    'Security negotiation failed on security result (reason: whoops)');
            });

            it('should fail on an error code of 1 with a standard message for version < 3.8', function () {
                sinon.spy(client, '_fail');
                client._rfb_version = 3.7;
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 1]));
                expect(client._fail).to.have.been.calledWith(
                    'Security handshake failed');
            });

            it('should result in securityfailure event when receiving a non zero status', function () {
                const spy = sinon.spy();
                client.addEventListener("securityfailure", spy);
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 2]));
                expect(spy).to.have.been.calledOnce;
                expect(spy.args[0][0].detail.status).to.equal(2);
            });

            it('should include reason when provided in securityfailure event', function () {
                client._rfb_version = 3.8;
                const spy = sinon.spy();
                client.addEventListener("securityfailure", spy);
                const failure_data = [0, 0, 0, 1, 0, 0, 0, 12, 115, 117, 99, 104,
                                      32, 102, 97, 105, 108, 117, 114, 101];
                client._sock._websocket._receive_data(new Uint8Array(failure_data));
                expect(spy.args[0][0].detail.status).to.equal(1);
                expect(spy.args[0][0].detail.reason).to.equal('such failure');
            });

            it('should not include reason when length is zero in securityfailure event', function () {
                client._rfb_version = 3.9;
                const spy = sinon.spy();
                client.addEventListener("securityfailure", spy);
                const failure_data = [0, 0, 0, 1, 0, 0, 0, 0];
                client._sock._websocket._receive_data(new Uint8Array(failure_data));
                expect(spy.args[0][0].detail.status).to.equal(1);
                expect('reason' in spy.args[0][0].detail).to.be.false;
            });

            it('should not include reason in securityfailure event for version < 3.8', function () {
                client._rfb_version = 3.6;
                const spy = sinon.spy();
                client.addEventListener("securityfailure", spy);
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 2]));
                expect(spy.args[0][0].detail.status).to.equal(2);
                expect('reason' in spy.args[0][0].detail).to.be.false;
            });
        });

        describe('ClientInitialisation', function () {
            it('should transition to the ServerInitialisation state', function () {
                const client = make_rfb();
                client._rfb_connection_state = 'connecting';
                client._rfb_init_state = 'SecurityResult';
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
                expect(client._rfb_init_state).to.equal('ServerInitialisation');
            });

            it('should send 1 if we are in shared mode', function () {
                const client = make_rfb('wss://host:8675', { shared: true });
                client._rfb_connection_state = 'connecting';
                client._rfb_init_state = 'SecurityResult';
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
                expect(client._sock).to.have.sent(new Uint8Array([1]));
            });

            it('should send 0 if we are not in shared mode', function () {
                const client = make_rfb('wss://host:8675', { shared: false });
                client._rfb_connection_state = 'connecting';
                client._rfb_init_state = 'SecurityResult';
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
                expect(client._sock).to.have.sent(new Uint8Array([0]));
            });
        });

        describe('ServerInitialisation', function () {
            beforeEach(function () {
                client._rfb_init_state = 'ServerInitialisation';
            });

            function send_server_init(opts, client) {
                const full_opts = { width: 10, height: 12, bpp: 24, depth: 24, big_endian: 0,
                                    true_color: 1, red_max: 255, green_max: 255, blue_max: 255,
                                    red_shift: 16, green_shift: 8, blue_shift: 0, name: 'a name' };
                for (let opt in opts) {
                    full_opts[opt] = opts[opt];
                }
                const data = [];

                push16(data, full_opts.width);
                push16(data, full_opts.height);

                data.push(full_opts.bpp);
                data.push(full_opts.depth);
                data.push(full_opts.big_endian);
                data.push(full_opts.true_color);

                push16(data, full_opts.red_max);
                push16(data, full_opts.green_max);
                push16(data, full_opts.blue_max);
                push8(data, full_opts.red_shift);
                push8(data, full_opts.green_shift);
                push8(data, full_opts.blue_shift);

                // padding
                push8(data, 0);
                push8(data, 0);
                push8(data, 0);

                client._sock._websocket._receive_data(new Uint8Array(data));

                const name_data = [];
                let name_len = [];
                pushString(name_data, full_opts.name);
                push32(name_len, name_data.length);

                client._sock._websocket._receive_data(new Uint8Array(name_len));
                client._sock._websocket._receive_data(new Uint8Array(name_data));
            }

            it('should set the framebuffer width and height', function () {
                send_server_init({ width: 32, height: 84 }, client);
                expect(client._fb_width).to.equal(32);
                expect(client._fb_height).to.equal(84);
            });

            // NB(sross): we just warn, not fail, for endian-ness and shifts, so we don't test them

            it('should set the framebuffer name and call the callback', function () {
                const spy = sinon.spy();
                client.addEventListener("desktopname", spy);
                send_server_init({ name: 'som€ nam€' }, client);

                expect(client._fb_name).to.equal('som€ nam€');
                expect(spy).to.have.been.calledOnce;
                expect(spy.args[0][0].detail.name).to.equal('som€ nam€');
            });

            it('should handle the extended init message of the tight encoding', function () {
                // NB(sross): we don't actually do anything with it, so just test that we can
                //            read it w/o throwing an error
                client._rfb_tightvnc = true;
                send_server_init({}, client);

                const tight_data = [];
                push16(tight_data, 1);
                push16(tight_data, 2);
                push16(tight_data, 3);
                push16(tight_data, 0);
                for (let i = 0; i < 16 + 32 + 48; i++) {
                    tight_data.push(i);
                }
                client._sock._websocket._receive_data(new Uint8Array(tight_data));

                expect(client._rfb_connection_state).to.equal('connected');
            });

            it('should resize the display', function () {
                sinon.spy(client._display, 'resize');
                send_server_init({ width: 27, height: 32 }, client);

                expect(client._display.resize).to.have.been.calledOnce;
                expect(client._display.resize).to.have.been.calledWith(27, 32);
            });

            it('should grab the mouse and keyboard', function () {
                sinon.spy(client._keyboard, 'grab');
                sinon.spy(client._mouse, 'grab');
                send_server_init({}, client);
                expect(client._keyboard.grab).to.have.been.calledOnce;
                expect(client._mouse.grab).to.have.been.calledOnce;
            });

            describe('Initial Update Request', function () {
                beforeEach(function () {
                    sinon.spy(RFB.messages, "pixelFormat");
                    sinon.spy(RFB.messages, "clientEncodings");
                    sinon.spy(RFB.messages, "fbUpdateRequest");
                });

                afterEach(function () {
                    RFB.messages.pixelFormat.restore();
                    RFB.messages.clientEncodings.restore();
                    RFB.messages.fbUpdateRequest.restore();
                });

                // TODO(directxman12): test the various options in this configuration matrix
                it('should reply with the pixel format, client encodings, and initial update request', function () {
                    send_server_init({ width: 27, height: 32 }, client);

                    expect(RFB.messages.pixelFormat).to.have.been.calledOnce;
                    expect(RFB.messages.pixelFormat).to.have.been.calledWith(client._sock, 24, true);
                    expect(RFB.messages.pixelFormat).to.have.been.calledBefore(RFB.messages.clientEncodings);
                    expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
                    expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.encodingTight);
                    expect(RFB.messages.clientEncodings).to.have.been.calledBefore(RFB.messages.fbUpdateRequest);
                    expect(RFB.messages.fbUpdateRequest).to.have.been.calledOnce;
                    expect(RFB.messages.fbUpdateRequest).to.have.been.calledWith(client._sock, false, 0, 0, 27, 32);
                });

                it('should reply with restricted settings for Intel AMT servers', function () {
                    send_server_init({ width: 27, height: 32, name: "Intel(r) AMT KVM"}, client);

                    expect(RFB.messages.pixelFormat).to.have.been.calledOnce;
                    expect(RFB.messages.pixelFormat).to.have.been.calledWith(client._sock, 8, true);
                    expect(RFB.messages.pixelFormat).to.have.been.calledBefore(RFB.messages.clientEncodings);
                    expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
                    expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.not.include(encodings.encodingTight);
                    expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.not.include(encodings.encodingHextile);
                    expect(RFB.messages.clientEncodings).to.have.been.calledBefore(RFB.messages.fbUpdateRequest);
                    expect(RFB.messages.fbUpdateRequest).to.have.been.calledOnce;
                    expect(RFB.messages.fbUpdateRequest).to.have.been.calledWith(client._sock, false, 0, 0, 27, 32);
                });
            });

            it('should transition to the "connected" state', function () {
                send_server_init({}, client);
                expect(client._rfb_connection_state).to.equal('connected');
            });
        });
    });

    describe('Protocol Message Processing After Completing Initialization', function () {
        let client;

        beforeEach(function () {
            client = make_rfb();
            client._fb_name = 'some device';
            client._fb_width = 640;
            client._fb_height = 20;
        });

        describe('Framebuffer Update Handling', function () {
            const target_data_arr = [
                0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
                0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
                0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255,
                0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255
            ];
            let target_data;

            const target_data_check_arr = [
                0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
                0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
                0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
                0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
            ];
            let target_data_check;

            before(function () {
                // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray
                target_data = new Uint8Array(target_data_arr);
                target_data_check = new Uint8Array(target_data_check_arr);
            });

            function send_fbu_msg(rect_info, rect_data, client, rect_cnt) {
                let data = [];

                if (!rect_cnt || rect_cnt > -1) {
                    // header
                    data.push(0);  // msg type
                    data.push(0);  // padding
                    push16(data, rect_cnt || rect_data.length);
                }

                for (let i = 0; i < rect_data.length; i++) {
                    if (rect_info[i]) {
                        push16(data, rect_info[i].x);
                        push16(data, rect_info[i].y);
                        push16(data, rect_info[i].width);
                        push16(data, rect_info[i].height);
                        push32(data, rect_info[i].encoding);
                    }
                    data = data.concat(rect_data[i]);
                }

                client._sock._websocket._receive_data(new Uint8Array(data));
            }

            it('should send an update request if there is sufficient data', function () {
                const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
                RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20);

                client._framebufferUpdate = () => true;
                client._sock._websocket._receive_data(new Uint8Array([0]));

                expect(client._sock).to.have.sent(expected_msg._sQ);
            });

            it('should not send an update request if we need more data', function () {
                client._sock._websocket._receive_data(new Uint8Array([0]));
                expect(client._sock._websocket._get_sent_data()).to.have.length(0);
            });

            it('should resume receiving an update if we previously did not have enough data', function () {
                const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
                RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20);

                // just enough to set FBU.rects
                client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 3]));
                expect(client._sock._websocket._get_sent_data()).to.have.length(0);

                client._framebufferUpdate = function () { this._sock.rQskipBytes(1); return true; };  // we magically have enough data
                // 247 should *not* be used as the message type here
                client._sock._websocket._receive_data(new Uint8Array([247]));
                expect(client._sock).to.have.sent(expected_msg._sQ);
            });

            it('should not send a request in continuous updates mode', function () {
                client._enabledContinuousUpdates = true;
                client._framebufferUpdate = () => true;
                client._sock._websocket._receive_data(new Uint8Array([0]));

                expect(client._sock._websocket._get_sent_data()).to.have.length(0);
            });

            it('should fail on an unsupported encoding', function () {
                sinon.spy(client, "_fail");
                const rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
                send_fbu_msg([rect_info], [[]], client);
                expect(client._fail).to.have.been.calledOnce;
            });

            it('should be able to pause and resume receiving rects if not enought data', function () {
                // seed some initial data to copy
                client._fb_width = 4;
                client._fb_height = 4;
                client._display.resize(4, 4);
                client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(target_data_check_arr.slice(0, 32)), 0);

                const info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
                              { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
                // data says [{ old_x: 2, old_y: 0 }, { old_x: 0, old_y: 0 }]
                const rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
                send_fbu_msg([info[0]], [rects[0]], client, 2);
                send_fbu_msg([info[1]], [rects[1]], client, -1);
                expect(client._display).to.have.displayed(target_data_check);
            });

            describe('Message Encoding Handlers', function () {
                beforeEach(function () {
                    // a really small frame
                    client._fb_width = 4;
                    client._fb_height = 4;
                    client._fb_depth = 24;
                    client._display.resize(4, 4);
                });

                it('should handle the RAW encoding', function () {
                    const info = [{ x: 0, y: 0, width: 2, height: 2, encoding: 0x00 },
                                  { x: 2, y: 0, width: 2, height: 2, encoding: 0x00 },
                                  { x: 0, y: 2, width: 4, height: 1, encoding: 0x00 },
                                  { x: 0, y: 3, width: 4, height: 1, encoding: 0x00 }];
                    // data is in bgrx
                    const rects = [
                        [0x00, 0x00, 0xff, 0, 0x00, 0xff, 0x00, 0, 0x00, 0xff, 0x00, 0, 0x00, 0x00, 0xff, 0],
                        [0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0],
                        [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0],
                        [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0]];
                    send_fbu_msg(info, rects, client);
                    expect(client._display).to.have.displayed(target_data);
                });

                it('should handle the RAW encoding in low colour mode', function () {
                    const info = [{ x: 0, y: 0, width: 2, height: 2, encoding: 0x00 },
                                  { x: 2, y: 0, width: 2, height: 2, encoding: 0x00 },
                                  { x: 0, y: 2, width: 4, height: 1, encoding: 0x00 },
                                  { x: 0, y: 3, width: 4, height: 1, encoding: 0x00 }];
                    const rects = [
                        [0x03, 0x03, 0x03, 0x03],
                        [0x0c, 0x0c, 0x0c, 0x0c],
                        [0x0c, 0x0c, 0x03, 0x03],
                        [0x0c, 0x0c, 0x03, 0x03]];
                    client._fb_depth = 8;
                    send_fbu_msg(info, rects, client);
                    expect(client._display).to.have.displayed(target_data_check);
                });

                it('should handle the COPYRECT encoding', function () {
                    // seed some initial data to copy
                    client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(target_data_check_arr.slice(0, 32)), 0);

                    const info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
                                  { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
                    // data says [{ old_x: 0, old_y: 0 }, { old_x: 0, old_y: 0 }]
                    const rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
                    send_fbu_msg(info, rects, client);
                    expect(client._display).to.have.displayed(target_data_check);
                });

                // TODO(directxman12): for encodings with subrects, test resuming on partial send?
                // TODO(directxman12): test rre_chunk_sz (related to above about subrects)?

                it('should handle the RRE encoding', function () {
                    const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x02 }];
                    const rect = [];
                    push32(rect, 2); // 2 subrects
                    push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
                    rect.push(0xff); // becomes ff0000ff --> #0000FF color
                    rect.push(0x00);
                    rect.push(0x00);
                    rect.push(0xff);
                    push16(rect, 0); // x: 0
                    push16(rect, 0); // y: 0
                    push16(rect, 2); // width: 2
                    push16(rect, 2); // height: 2
                    rect.push(0xff); // becomes ff0000ff --> #0000FF color
                    rect.push(0x00);
                    rect.push(0x00);
                    rect.push(0xff);
                    push16(rect, 2); // x: 2
                    push16(rect, 2); // y: 2
                    push16(rect, 2); // width: 2
                    push16(rect, 2); // height: 2

                    send_fbu_msg(info, [rect], client);
                    expect(client._display).to.have.displayed(target_data_check);
                });

                describe('the HEXTILE encoding handler', function () {
                    it('should handle a tile with fg, bg specified, normal subrects', function () {
                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
                        const rect = [];
                        rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
                        rect.push(0x00);
                        rect.push(0x00);
                        rect.push(0xff);
                        rect.push(2); // 2 subrects
                        rect.push(0); // x: 0, y: 0
                        rect.push(1 | (1 << 4)); // width: 2, height: 2
                        rect.push(2 | (2 << 4)); // x: 2, y: 2
                        rect.push(1 | (1 << 4)); // width: 2, height: 2
                        send_fbu_msg(info, [rect], client);
                        expect(client._display).to.have.displayed(target_data_check);
                    });

                    it('should handle a raw tile', function () {
                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
                        const rect = [];
                        rect.push(0x01); // raw
                        for (let i = 0; i < target_data.length; i += 4) {
                            rect.push(target_data[i + 2]);
                            rect.push(target_data[i + 1]);
                            rect.push(target_data[i]);
                            rect.push(target_data[i + 3]);
                        }
                        send_fbu_msg(info, [rect], client);
                        expect(client._display).to.have.displayed(target_data);
                    });

                    it('should handle a tile with only bg specified (solid bg)', function () {
                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
                        const rect = [];
                        rect.push(0x02);
                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
                        send_fbu_msg(info, [rect], client);

                        const expected = [];
                        for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); }
                        expect(client._display).to.have.displayed(new Uint8Array(expected));
                    });

                    it('should handle a tile with only bg specified and an empty frame afterwards', function () {
                        // set the width so we can have two tiles
                        client._fb_width = 8;
                        client._display.resize(8, 4);

                        const info = [{ x: 0, y: 0, width: 32, height: 4, encoding: 0x05 }];

                        const rect = [];

                        // send a bg frame
                        rect.push(0x02);
                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color

                        // send an empty frame
                        rect.push(0x00);

                        send_fbu_msg(info, [rect], client);

                        const expected = [];
                        for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); }     // rect 1: solid
                        for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); }    // rect 2: same bkground color
                        expect(client._display).to.have.displayed(new Uint8Array(expected));
                    });

                    it('should handle a tile with bg and coloured subrects', function () {
                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
                        const rect = [];
                        rect.push(0x02 | 0x08 | 0x10); // bg spec, anysubrects, colouredsubrects
                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
                        rect.push(2); // 2 subrects
                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
                        rect.push(0x00);
                        rect.push(0x00);
                        rect.push(0xff);
                        rect.push(0); // x: 0, y: 0
                        rect.push(1 | (1 << 4)); // width: 2, height: 2
                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
                        rect.push(0x00);
                        rect.push(0x00);
                        rect.push(0xff);
                        rect.push(2 | (2 << 4)); // x: 2, y: 2
                        rect.push(1 | (1 << 4)); // width: 2, height: 2
                        send_fbu_msg(info, [rect], client);
                        expect(client._display).to.have.displayed(target_data_check);
                    });

                    it('should carry over fg and bg colors from the previous tile if not specified', function () {
                        client._fb_width = 4;
                        client._fb_height = 17;
                        client._display.resize(4, 17);

                        const info = [{ x: 0, y: 0, width: 4, height: 17, encoding: 0x05}];
                        const rect = [];
                        rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
                        rect.push(0x00);
                        rect.push(0x00);
                        rect.push(0xff);
                        rect.push(8); // 8 subrects
                        for (let i = 0; i < 4; i++) {
                            rect.push((0 << 4) | (i * 4)); // x: 0, y: i*4
                            rect.push(1 | (1 << 4)); // width: 2, height: 2
                            rect.push((2 << 4) | (i * 4 + 2)); // x: 2, y: i * 4 + 2
                            rect.push(1 | (1 << 4)); // width: 2, height: 2
                        }
                        rect.push(0x08); // anysubrects
                        rect.push(1); // 1 subrect
                        rect.push(0); // x: 0, y: 0
                        rect.push(1 | (1 << 4)); // width: 2, height: 2
                        send_fbu_msg(info, [rect], client);

                        let expected = [];
                        for (let i = 0; i < 4; i++) { expected = expected.concat(target_data_check_arr); }
                        expected = expected.concat(target_data_check_arr.slice(0, 16));
                        expect(client._display).to.have.displayed(new Uint8Array(expected));
                    });

                    it('should fail on an invalid subencoding', function () {
                        sinon.spy(client, "_fail");
                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
                        const rects = [[45]];  // an invalid subencoding
                        send_fbu_msg(info, rects, client);
                        expect(client._fail).to.have.been.calledOnce;
                    });
                });

                it.skip('should handle the TIGHT encoding', function () {
                    // TODO(directxman12): test this
                });

                it.skip('should handle the TIGHT_PNG encoding', function () {
                    // TODO(directxman12): test this
                });

                it('should handle the DesktopSize pseduo-encoding', function () {
                    sinon.spy(client._display, 'resize');
                    send_fbu_msg([{ x: 0, y: 0, width: 20, height: 50, encoding: -223 }], [[]], client);

                    expect(client._fb_width).to.equal(20);
                    expect(client._fb_height).to.equal(50);

                    expect(client._display.resize).to.have.been.calledOnce;
                    expect(client._display.resize).to.have.been.calledWith(20, 50);
                });

                describe('the ExtendedDesktopSize pseudo-encoding handler', function () {
                    beforeEach(function () {
                        // a really small frame
                        client._fb_width = 4;
                        client._fb_height = 4;
                        client._display.resize(4, 4);
                        sinon.spy(client._display, 'resize');
                    });

                    function make_screen_data(nr_of_screens) {
                        const data = [];
                        push8(data, nr_of_screens);   // number-of-screens
                        push8(data, 0);               // padding
                        push16(data, 0);              // padding
                        for (let i=0; i<nr_of_screens; i += 1) {
                            push32(data, 0);  // id
                            push16(data, 0);  // x-position
                            push16(data, 0);  // y-position
                            push16(data, 20); // width
                            push16(data, 50); // height
                            push32(data, 0);  // flags
                        }
                        return data;
                    }

                    it('should handle a resize requested by this client', function () {
                        const reason_for_change = 1; // requested by this client
                        const status_code       = 0; // No error

                        send_fbu_msg([{ x: reason_for_change, y: status_code,
                                        width: 20, height: 50, encoding: -308 }],
                                     make_screen_data(1), client);

                        expect(client._fb_width).to.equal(20);
                        expect(client._fb_height).to.equal(50);

                        expect(client._display.resize).to.have.been.calledOnce;
                        expect(client._display.resize).to.have.been.calledWith(20, 50);
                    });

                    it('should handle a resize requested by another client', function () {
                        const reason_for_change = 2; // requested by another client
                        const status_code       = 0; // No error

                        send_fbu_msg([{ x: reason_for_change, y: status_code,
                                        width: 20, height: 50, encoding: -308 }],
                                     make_screen_data(1), client);

                        expect(client._fb_width).to.equal(20);
                        expect(client._fb_height).to.equal(50);

                        expect(client._display.resize).to.have.been.calledOnce;
                        expect(client._display.resize).to.have.been.calledWith(20, 50);
                    });

                    it('should be able to recieve requests which contain data for multiple screens', function () {
                        const reason_for_change = 2; // requested by another client
                        const status_code       = 0; // No error

                        send_fbu_msg([{ x: reason_for_change, y: status_code,
                                        width: 60, height: 50, encoding: -308 }],
                                     make_screen_data(3), client);

                        expect(client._fb_width).to.equal(60);
                        expect(client._fb_height).to.equal(50);

                        expect(client._display.resize).to.have.been.calledOnce;
                        expect(client._display.resize).to.have.been.calledWith(60, 50);
                    });

                    it('should not handle a failed request', function () {
                        const reason_for_change = 1; // requested by this client
                        const status_code       = 1; // Resize is administratively prohibited

                        send_fbu_msg([{ x: reason_for_change, y: status_code,
                                        width: 20, height: 50, encoding: -308 }],
                                     make_screen_data(1), client);

                        expect(client._fb_width).to.equal(4);
                        expect(client._fb_height).to.equal(4);

                        expect(client._display.resize).to.not.have.been.called;
                    });
                });

                describe('the Cursor pseudo-encoding handler', function () {
                    beforeEach(function () {
                        sinon.spy(client._cursor, 'change');
                    });

                    it('should handle a standard cursor', function () {
                        const info = { x: 5, y: 7,
                                       width: 4, height: 4,
                                       encoding: -239};
                        let rect = [];
                        let expected = [];

                        for (let i = 0;i < info.width*info.height;i++) {
                            push32(rect, 0x11223300);
                        }
                        push32(rect, 0xa0a0a0a0);

                        for (let i = 0;i < info.width*info.height/2;i++) {
                            push32(expected, 0x332211ff);
                            push32(expected, 0x33221100);
                        }
                        expected = new Uint8Array(expected);

                        send_fbu_msg([info], [rect], client);

                        expect(client._cursor.change).to.have.been.calledOnce;
                        expect(client._cursor.change).to.have.been.calledWith(expected, 5, 7, 4, 4);
                    });

                    it('should handle an empty cursor', function () {
                        const info = { x: 0, y: 0,
                                       width: 0, height: 0,
                                       encoding: -239};
                        const rect = [];

                        send_fbu_msg([info], [rect], client);

                        expect(client._cursor.change).to.have.been.calledOnce;
                        expect(client._cursor.change).to.have.been.calledWith(new Uint8Array, 0, 0, 0, 0);
                    });

                    it('should handle a transparent cursor', function () {
                        const info = { x: 5, y: 7,
                                       width: 4, height: 4,
                                       encoding: -239};
                        let rect = [];
                        let expected = [];

                        for (let i = 0;i < info.width*info.height;i++) {
                            push32(rect, 0x11223300);
                        }
                        push32(rect, 0x00000000);

                        for (let i = 0;i < info.width*info.height;i++) {
                            push32(expected, 0x33221100);
                        }
                        expected = new Uint8Array(expected);

                        send_fbu_msg([info], [rect], client);

                        expect(client._cursor.change).to.have.been.calledOnce;
                        expect(client._cursor.change).to.have.been.calledWith(expected, 5, 7, 4, 4);
                    });

                    describe('dot for empty cursor', function () {
                        beforeEach(function () {
                            client.showDotCursor = true;
                            // Was called when we enabled dot cursor
                            client._cursor.change.resetHistory();
                        });

                        it('should show a standard cursor', function () {
                            const info = { x: 5, y: 7,
                                           width: 4, height: 4,
                                           encoding: -239};
                            let rect = [];
                            let expected = [];

                            for (let i = 0;i < info.width*info.height;i++) {
                                push32(rect, 0x11223300);
                            }
                            push32(rect, 0xa0a0a0a0);

                            for (let i = 0;i < info.width*info.height/2;i++) {
                                push32(expected, 0x332211ff);
                                push32(expected, 0x33221100);
                            }
                            expected = new Uint8Array(expected);

                            send_fbu_msg([info], [rect], client);

                            expect(client._cursor.change).to.have.been.calledOnce;
                            expect(client._cursor.change).to.have.been.calledWith(expected, 5, 7, 4, 4);
                        });

                        it('should handle an empty cursor', function () {
                            const info = { x: 0, y: 0,
                                           width: 0, height: 0,
                                           encoding: -239};
                            const rect = [];
                            const dot = RFB.cursors.dot;

                            send_fbu_msg([info], [rect], client);

                            expect(client._cursor.change).to.have.been.calledOnce;
                            expect(client._cursor.change).to.have.been.calledWith(dot.rgbaPixels,
                                                                                  dot.hotx,
                                                                                  dot.hoty,
                                                                                  dot.w,
                                                                                  dot.h);
                        });

                        it('should handle a transparent cursor', function () {
                            const info = { x: 5, y: 7,
                                           width: 4, height: 4,
                                           encoding: -239};
                            let rect = [];
                            const dot = RFB.cursors.dot;

                            for (let i = 0;i < info.width*info.height;i++) {
                                push32(rect, 0x11223300);
                            }
                            push32(rect, 0x00000000);

                            send_fbu_msg([info], [rect], client);

                            expect(client._cursor.change).to.have.been.calledOnce;
                            expect(client._cursor.change).to.have.been.calledWith(dot.rgbaPixels,
                                                                                  dot.hotx,
                                                                                  dot.hoty,
                                                                                  dot.w,
                                                                                  dot.h);
                        });
                    });
                });

                describe('the VMware Cursor pseudo-encoding handler', function () {
                    beforeEach(function () {
                        sinon.spy(client._cursor, 'change');
                    });
                    afterEach(function () {
                        client._cursor.change.resetHistory();
                    });

                    it('should handle the VMware cursor pseudo-encoding', function () {
                        let data = [0x00, 0x00, 0xff, 0,
                                    0x00, 0xff, 0x00, 0,
                                    0x00, 0xff, 0x00, 0,
                                    0x00, 0x00, 0xff, 0];
                        let rect = [];
                        push8(rect, 0);
                        push8(rect, 0);

                        //AND-mask
                        for (let i = 0; i < data.length; i++) {
                            push8(rect, data[i]);
                        }
                        //XOR-mask
                        for (let i = 0; i < data.length; i++) {
                            push8(rect, data[i]);
                        }

                        send_fbu_msg([{ x: 0, y: 0, width: 2, height: 2,
                                        encoding: 0x574d5664}],
                                     [rect], client);
                        expect(client._FBU.rects).to.equal(0);
                    });

                    it('should handle insufficient cursor pixel data', function () {

                        // Specified 14x23 pixels for the cursor,
                        // but only send 2x2 pixels worth of data
                        let w = 14;
                        let h = 23;
                        let data = [0x00, 0x00, 0xff, 0,
                                    0x00, 0xff, 0x00, 0];
                        let rect = [];

                        push8(rect, 0);
                        push8(rect, 0);

                        //AND-mask
                        for (let i = 0; i < data.length; i++) {
                            push8(rect, data[i]);
                        }
                        //XOR-mask
                        for (let i = 0; i < data.length; i++) {
                            push8(rect, data[i]);
                        }

                        send_fbu_msg([{ x: 0, y: 0, width: w, height: h,
                                        encoding: 0x574d5664}],
                                     [rect], client);

                        // expect one FBU to remain unhandled
                        expect(client._FBU.rects).to.equal(1);
                    });

                    it('should update the cursor when type is classic', function () {
                        let and_mask =
                            [0xff, 0xff, 0xff, 0xff,  //Transparent
                             0xff, 0xff, 0xff, 0xff,  //Transparent
                             0x00, 0x00, 0x00, 0x00,  //Opaque
                             0xff, 0xff, 0xff, 0xff]; //Inverted

                        let xor_mask =
                            [0x00, 0x00, 0x00, 0x00,  //Transparent
                             0x00, 0x00, 0x00, 0x00,  //Transparent
                             0x11, 0x22, 0x33, 0x44,  //Opaque
                             0xff, 0xff, 0xff, 0x44]; //Inverted

                        let rect = [];
                        push8(rect, 0); //cursor_type
                        push8(rect, 0); //padding
                        let hotx = 0;
                        let hoty = 0;
                        let w = 2;
                        let h = 2;

                        //AND-mask
                        for (let i = 0; i < and_mask.length; i++) {
                            push8(rect, and_mask[i]);
                        }
                        //XOR-mask
                        for (let i = 0; i < xor_mask.length; i++) {
                            push8(rect, xor_mask[i]);
                        }

                        let expected_rgba = [0x00, 0x00, 0x00, 0x00,
                                             0x00, 0x00, 0x00, 0x00,
                                             0x33, 0x22, 0x11, 0xff,
                                             0x00, 0x00, 0x00, 0xff];

                        send_fbu_msg([{ x: hotx, y: hoty,
                                        width: w, height: h,
                                        encoding: 0x574d5664}],
                                     [rect], client);

                        expect(client._cursor.change)
                            .to.have.been.calledOnce;
                        expect(client._cursor.change)
                            .to.have.been.calledWith(expected_rgba,
                                                     hotx, hoty,
                                                     w, h);
                    });

                    it('should update the cursor when type is alpha', function () {
                        let data = [0xee, 0x55, 0xff, 0x00, // rgba
                                    0x00, 0xff, 0x00, 0xff,
                                    0x00, 0xff, 0x00, 0x22,
                                    0x00, 0xff, 0x00, 0x22,
                                    0x00, 0xff, 0x00, 0x22,
                                    0x00, 0x00, 0xff, 0xee];
                        let rect = [];
                        push8(rect, 1); //cursor_type
                        push8(rect, 0); //padding
                        let hotx = 0;
                        let hoty = 0;
                        let w = 3;
                        let h = 2;

                        for (let i = 0; i < data.length; i++) {
                            push8(rect, data[i]);
                        }

                        let expected_rgba = [0xee, 0x55, 0xff, 0x00,
                                             0x00, 0xff, 0x00, 0xff,
                                             0x00, 0xff, 0x00, 0x22,
                                             0x00, 0xff, 0x00, 0x22,
                                             0x00, 0xff, 0x00, 0x22,
                                             0x00, 0x00, 0xff, 0xee];

                        send_fbu_msg([{ x: hotx, y: hoty,
                                        width: w, height: h,
                                        encoding: 0x574d5664}],
                                     [rect], client);

                        expect(client._cursor.change)
                            .to.have.been.calledOnce;
                        expect(client._cursor.change)
                            .to.have.been.calledWith(expected_rgba,
                                                     hotx, hoty,
                                                     w, h);
                    });

                    it('should not update cursor when incorrect cursor type given', function () {
                        let rect = [];
                        push8(rect, 3); // invalid cursor type
                        push8(rect, 0); // padding

                        client._cursor.change.resetHistory();
                        send_fbu_msg([{ x: 0, y: 0, width: 2, height: 2,
                                        encoding: 0x574d5664}],
                                     [rect], client);

                        expect(client._cursor.change)
                            .to.not.have.been.called;
                    });
                });

                it('should handle the last_rect pseudo-encoding', function () {
                    send_fbu_msg([{ x: 0, y: 0, width: 0, height: 0, encoding: -224}], [[]], client, 100);
                    expect(client._FBU.rects).to.equal(0);
                });

                it('should handle the DesktopName pseudo-encoding', function () {
                    let data = [];
                    push32(data, 13);
                    pushString(data, "som€ nam€");

                    const spy = sinon.spy();
                    client.addEventListener("desktopname", spy);

                    send_fbu_msg([{ x: 0, y: 0, width: 0, height: 0, encoding: -307 }], [data], client);

                    expect(client._fb_name).to.equal('som€ nam€');
                    expect(spy).to.have.been.calledOnce;
                    expect(spy.args[0][0].detail.name).to.equal('som€ nam€');
                });
            });
        });

        describe('XVP Message Handling', function () {
            it('should set the XVP version and fire the callback with the version on XVP_INIT', function () {
                const spy = sinon.spy();
                client.addEventListener("capabilities", spy);
                client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 1]));
                expect(client._rfb_xvp_ver).to.equal(10);
                expect(spy).to.have.been.calledOnce;
                expect(spy.args[0][0].detail.capabilities.power).to.be.true;
                expect(client.capabilities.power).to.be.true;
            });

            it('should fail on unknown XVP message types', function () {
                sinon.spy(client, "_fail");
                client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 237]));
                expect(client._fail).to.have.been.calledOnce;
            });
        });

        describe('Normal Clipboard Handling Receive', function () {
            it('should fire the clipboard callback with the retrieved text on ServerCutText', function () {
                const expected_str = 'cheese!';
                const data = [3, 0, 0, 0];
                push32(data, expected_str.length);
                for (let i = 0; i < expected_str.length; i++) { data.push(expected_str.charCodeAt(i)); }
                const spy = sinon.spy();
                client.addEventListener("clipboard", spy);

                client._sock._websocket._receive_data(new Uint8Array(data));
                expect(spy).to.have.been.calledOnce;
                expect(spy.args[0][0].detail.text).to.equal(expected_str);
            });
        });

        describe('Extended clipboard Handling', function () {

            describe('Extended clipboard initialization', function () {
                beforeEach(function () {
                    sinon.spy(RFB.messages, 'extendedClipboardCaps');
                });

                afterEach(function () {
                    RFB.messages.extendedClipboardCaps.restore();
                });

                it('should update capabilities when receiving a Caps message', function () {
                    let data = [3, 0, 0, 0];
                    const flags = [0x1F, 0x00, 0x00, 0x03];
                    let fileSizes = [0x00, 0x00, 0x00, 0x1E,
                                     0x00, 0x00, 0x00, 0x3C];

                    push32(data, toUnsigned32bit(-12));
                    data = data.concat(flags);
                    data = data.concat(fileSizes);
                    client._sock._websocket._receive_data(new Uint8Array(data));

                    // Check that we give an response caps when we receive one
                    expect(RFB.messages.extendedClipboardCaps).to.have.been.calledOnce;

                    // FIXME: Can we avoid checking internal variables?
                    expect(client._clipboardServerCapabilitiesFormats[0]).to.not.equal(true);
                    expect(client._clipboardServerCapabilitiesFormats[1]).to.equal(true);
                    expect(client._clipboardServerCapabilitiesFormats[2]).to.equal(true);
                    expect(client._clipboardServerCapabilitiesActions[(1 << 24)]).to.equal(true);
                });


            });

            describe('Extended Clipboard Handling Receive', function () {

                beforeEach(function () {
                    // Send our capabilities
                    let data = [3, 0, 0, 0];
                    const flags = [0x1F, 0x00, 0x00, 0x01];
                    let fileSizes = [0x00, 0x00, 0x00, 0x1E];

                    push32(data, toUnsigned32bit(-8));
                    data = data.concat(flags);
                    data = data.concat(fileSizes);
                    client._sock._websocket._receive_data(new Uint8Array(data));
                });

                describe('Handle Provide', function () {
                    it('should update clipboard with correct Unicode data from a Provide message', function () {
                        let expectedData = "Aå漢字!";
                        let data = [3, 0, 0, 0];
                        const flags = [0x10, 0x00, 0x00, 0x01];

                        /* The size 10 (utf8 encoded string size) and the
                        string "Aå漢字!" utf8 encoded and deflated. */
                        let deflatedData = [120, 94, 99, 96, 96, 224, 114, 60,
                                            188, 244, 217, 158, 69, 79, 215,
                                            78, 87, 4, 0, 35, 207, 6, 66];

                        // How much data we are sending.
                        push32(data, toUnsigned32bit(-(4 + deflatedData.length)));

                        data = data.concat(flags);
                        data = data.concat(deflatedData);

                        const spy = sinon.spy();
                        client.addEventListener("clipboard", spy);

                        client._sock._websocket._receive_data(new Uint8Array(data));
                        expect(spy).to.have.been.calledOnce;
                        expect(spy.args[0][0].detail.text).to.equal(expectedData);
                        client.removeEventListener("clipboard", spy);
                    });

                    it('should update clipboard with correct escape characters from a Provide message ', function () {
                        let expectedData = "Oh\nmy!";
                        let data = [3, 0, 0, 0];
                        const flags = [0x10, 0x00, 0x00, 0x01];

                        let text = encodeUTF8("Oh\r\nmy!\0");

                        let deflatedText = deflateWithSize(text);

                        // How much data we are sending.
                        push32(data, toUnsigned32bit(-(4 + deflatedText.length)));

                        data = data.concat(flags);

                        let sendData = new Uint8Array(data.length + deflatedText.length);
                        sendData.set(data);
                        sendData.set(deflatedText, data.length);

                        const spy = sinon.spy();
                        client.addEventListener("clipboard", spy);

                        client._sock._websocket._receive_data(sendData);
                        expect(spy).to.have.been.calledOnce;
                        expect(spy.args[0][0].detail.text).to.equal(expectedData);
                        client.removeEventListener("clipboard", spy);
                    });

                    it('should be able to handle large Provide messages', function () {
                        // repeat() is not supported in IE so a loop is needed instead
                        let expectedData = "hello";
                        for (let i = 1; i <= 100000; i++) {
                            expectedData += "hello";
                        }

                        let data = [3, 0, 0, 0];
                        const flags = [0x10, 0x00, 0x00, 0x01];

                        let text = encodeUTF8(expectedData + "\0");

                        let deflatedText = deflateWithSize(text);

                        // How much data we are sending.
                        push32(data, toUnsigned32bit(-(4 + deflatedText.length)));

                        data = data.concat(flags);

                        let sendData = new Uint8Array(data.length + deflatedText.length);
                        sendData.set(data);
                        sendData.set(deflatedText, data.length);

                        const spy = sinon.spy();
                        client.addEventListener("clipboard", spy);

                        client._sock._websocket._receive_data(sendData);
                        expect(spy).to.have.been.calledOnce;
                        expect(spy.args[0][0].detail.text).to.equal(expectedData);
                        client.removeEventListener("clipboard", spy);
                    });

                });

                describe('Handle Notify', function () {
                    beforeEach(function () {
                        sinon.spy(RFB.messages, 'extendedClipboardRequest');
                    });

                    afterEach(function () {
                        RFB.messages.extendedClipboardRequest.restore();
                    });

                    it('should make a request with supported formats when receiving a notify message', function () {
                        let data = [3, 0, 0, 0];
                        const flags = [0x08, 0x00, 0x00, 0x07];
                        push32(data, toUnsigned32bit(-4));
                        data = data.concat(flags);
                        let expectedData = [0x01];

                        client._sock._websocket._receive_data(new Uint8Array(data));

                        expect(RFB.messages.extendedClipboardRequest).to.have.been.calledOnce;
                        expect(RFB.messages.extendedClipboardRequest).to.have.been.calledWith(client._sock, expectedData);
                    });
                });

                describe('Handle Peek', function () {
                    beforeEach(function () {
                        sinon.spy(RFB.messages, 'extendedClipboardNotify');
                    });

                    afterEach(function () {
                        RFB.messages.extendedClipboardNotify.restore();
                    });

                    it('should send an empty Notify when receiving a Peek and no excisting clipboard data', function () {
                        let data = [3, 0, 0, 0];
                        const flags = [0x04, 0x00, 0x00, 0x00];
                        push32(data, toUnsigned32bit(-4));
                        data = data.concat(flags);
                        let expectedData = [];

                        client._sock._websocket._receive_data(new Uint8Array(data));

                        expect(RFB.messages.extendedClipboardNotify).to.have.been.calledOnce;
                        expect(RFB.messages.extendedClipboardNotify).to.have.been.calledWith(client._sock, expectedData);
                    });

                    it('should send a Notify message with supported formats when receiving a Peek', function () {
                        let data = [3, 0, 0, 0];
                        const flags = [0x04, 0x00, 0x00, 0x00];
                        push32(data, toUnsigned32bit(-4));
                        data = data.concat(flags);
                        let expectedData = [0x01];

                        // Needed to have clipboard data to read.
                        // This will trigger a call to Notify, reset history
                        client.clipboardPasteFrom("HejHej");
                        RFB.messages.extendedClipboardNotify.resetHistory();

                        client._sock._websocket._receive_data(new Uint8Array(data));

                        expect(RFB.messages.extendedClipboardNotify).to.have.been.calledOnce;
                        expect(RFB.messages.extendedClipboardNotify).to.have.been.calledWith(client._sock, expectedData);
                    });
                });

                describe('Handle Request', function () {
                    beforeEach(function () {
                        sinon.spy(RFB.messages, 'extendedClipboardProvide');
                    });

                    afterEach(function () {
                        RFB.messages.extendedClipboardProvide.restore();
                    });

                    it('should send a Provide message with supported formats when receiving a Request', function () {
                        let data = [3, 0, 0, 0];
                        const flags = [0x02, 0x00, 0x00, 0x01];
                        push32(data, toUnsigned32bit(-4));
                        data = data.concat(flags);
                        let expectedData = [0x01];

                        client.clipboardPasteFrom("HejHej");
                        expect(RFB.messages.extendedClipboardProvide).to.not.have.been.called;

                        client._sock._websocket._receive_data(new Uint8Array(data));

                        expect(RFB.messages.extendedClipboardProvide).to.have.been.calledOnce;
                        expect(RFB.messages.extendedClipboardProvide).to.have.been.calledWith(client._sock, expectedData, ["HejHej"]);
                    });
                });
            });

        });

        it('should fire the bell callback on Bell', function () {
            const spy = sinon.spy();
            client.addEventListener("bell", spy);
            client._sock._websocket._receive_data(new Uint8Array([2]));
            expect(spy).to.have.been.calledOnce;
        });

        it('should respond correctly to ServerFence', function () {
            const expected_msg = {_sQ: new Uint8Array(16), _sQlen: 0, flush: () => {}};
            const incoming_msg = {_sQ: new Uint8Array(16), _sQlen: 0, flush: () => {}};

            const payload = "foo\x00ab9";

            // ClientFence and ServerFence are identical in structure
            RFB.messages.clientFence(expected_msg, (1<<0) | (1<<1), payload);
            RFB.messages.clientFence(incoming_msg, 0xffffffff, payload);

            client._sock._websocket._receive_data(incoming_msg._sQ);

            expect(client._sock).to.have.sent(expected_msg._sQ);

            expected_msg._sQlen = 0;
            incoming_msg._sQlen = 0;

            RFB.messages.clientFence(expected_msg, (1<<0), payload);
            RFB.messages.clientFence(incoming_msg, (1<<0) | (1<<31), payload);

            client._sock._websocket._receive_data(incoming_msg._sQ);

            expect(client._sock).to.have.sent(expected_msg._sQ);
        });

        it('should enable continuous updates on first EndOfContinousUpdates', function () {
            const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};

            RFB.messages.enableContinuousUpdates(expected_msg, true, 0, 0, 640, 20);

            expect(client._enabledContinuousUpdates).to.be.false;

            client._sock._websocket._receive_data(new Uint8Array([150]));

            expect(client._enabledContinuousUpdates).to.be.true;
            expect(client._sock).to.have.sent(expected_msg._sQ);
        });

        it('should disable continuous updates on subsequent EndOfContinousUpdates', function () {
            client._enabledContinuousUpdates = true;
            client._supportsContinuousUpdates = true;

            client._sock._websocket._receive_data(new Uint8Array([150]));

            expect(client._enabledContinuousUpdates).to.be.false;
        });

        it('should update continuous updates on resize', function () {
            const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
            RFB.messages.enableContinuousUpdates(expected_msg, true, 0, 0, 90, 700);

            client._resize(450, 160);

            expect(client._sock._websocket._get_sent_data()).to.have.length(0);

            client._enabledContinuousUpdates = true;

            client._resize(90, 700);

            expect(client._sock).to.have.sent(expected_msg._sQ);
        });

        it('should fail on an unknown message type', function () {
            sinon.spy(client, "_fail");
            client._sock._websocket._receive_data(new Uint8Array([87]));
            expect(client._fail).to.have.been.calledOnce;
        });
    });

    describe('Asynchronous Events', function () {
        let client;
        beforeEach(function () {
            client = make_rfb();
        });

        describe('Mouse event handlers', function () {
            it('should not send button messages in view-only mode', function () {
                client._viewOnly = true;
                sinon.spy(client._sock, 'flush');
                client._handleMouseButton(0, 0, 1, 0x001);
                expect(client._sock.flush).to.not.have.been.called;
            });

            it('should not send movement messages in view-only mode', function () {
                client._viewOnly = true;
                sinon.spy(client._sock, 'flush');
                client._handleMouseMove(0, 0);
                expect(client._sock.flush).to.not.have.been.called;
            });

            it('should send a pointer event on mouse button presses', function () {
                client._handleMouseButton(10, 12, 1, 0x001);
                const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
                RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x001);
                expect(client._sock).to.have.sent(pointer_msg._sQ);
            });

            it('should send a mask of 1 on mousedown', function () {
                client._handleMouseButton(10, 12, 1, 0x001);
                const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
                RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x001);
                expect(client._sock).to.have.sent(pointer_msg._sQ);
            });

            it('should send a mask of 0 on mouseup', function () {
                client._mouse_buttonMask = 0x001;
                client._handleMouseButton(10, 12, 0, 0x001);
                const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
                RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000);
                expect(client._sock).to.have.sent(pointer_msg._sQ);
            });

            it('should send a pointer event on mouse movement', function () {
                client._handleMouseMove(10, 12);
                const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
                RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000);
                expect(client._sock).to.have.sent(pointer_msg._sQ);
            });

            it('should set the button mask so that future mouse movements use it', function () {
                client._handleMouseButton(10, 12, 1, 0x010);
                client._handleMouseMove(13, 9);
                const pointer_msg = {_sQ: new Uint8Array(12), _sQlen: 0, flush: () => {}};
                RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x010);
                RFB.messages.pointerEvent(pointer_msg, 13, 9, 0x010);
                expect(client._sock).to.have.sent(pointer_msg._sQ);
            });
        });

        describe('Keyboard Event Handlers', function () {
            it('should send a key message on a key press', function () {
                client._handleKeyEvent(0x41, 'KeyA', true);
                const key_msg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: () => {}};
                RFB.messages.keyEvent(key_msg, 0x41, 1);
                expect(client._sock).to.have.sent(key_msg._sQ);
            });

            it('should not send messages in view-only mode', function () {
                client._viewOnly = true;
                sinon.spy(client._sock, 'flush');
                client._handleKeyEvent('a', 'KeyA', true);
                expect(client._sock.flush).to.not.have.been.called;
            });
        });

        describe('WebSocket event handlers', function () {
            // message events
            it('should do nothing if we receive an empty message and have nothing in the queue', function () {
                client._normal_msg = sinon.spy();
                client._sock._websocket._receive_data(new Uint8Array([]));
                expect(client._normal_msg).to.not.have.been.called;
            });

            it('should handle a message in the connected state as a normal message', function () {
                client._normal_msg = sinon.spy();
                client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
                expect(client._normal_msg).to.have.been.called;
            });

            it('should handle a message in any non-disconnected/failed state like an init message', function () {
                client._rfb_connection_state = 'connecting';
                client._rfb_init_state = 'ProtocolVersion';
                client._init_msg = sinon.spy();
                client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
                expect(client._init_msg).to.have.been.called;
            });

            it('should process all normal messages directly', function () {
                const spy = sinon.spy();
                client.addEventListener("bell", spy);
                client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
                expect(spy).to.have.been.calledTwice;
            });

            // open events
            it('should update the state to ProtocolVersion on open (if the state is "connecting")', function () {
                client = new RFB(document.createElement('div'), 'wss://host:8675');
                this.clock.tick();
                client._sock._websocket._open();
                expect(client._rfb_init_state).to.equal('ProtocolVersion');
            });

            it('should fail if we are not currently ready to connect and we get an "open" event', function () {
                sinon.spy(client, "_fail");
                client._rfb_connection_state = 'connected';
                client._sock._websocket._open();
                expect(client._fail).to.have.been.calledOnce;
            });

            // close events
            it('should transition to "disconnected" from "disconnecting" on a close event', function () {
                const real = client._sock._websocket.close;
                client._sock._websocket.close = () => {};
                client.disconnect();
                expect(client._rfb_connection_state).to.equal('disconnecting');
                client._sock._websocket.close = real;
                client._sock._websocket.close();
                expect(client._rfb_connection_state).to.equal('disconnected');
            });

            it('should fail if we get a close event while connecting', function () {
                sinon.spy(client, "_fail");
                client._rfb_connection_state = 'connecting';
                client._sock._websocket.close();
                expect(client._fail).to.have.been.calledOnce;
            });

            it('should unregister close event handler', function () {
                sinon.spy(client._sock, 'off');
                client.disconnect();
                client._sock._websocket.close();
                expect(client._sock.off).to.have.been.calledWith('close');
            });

            // error events do nothing
        });
    });

    describe('Quality level setting', function () {
        const defaultQuality = 6;

        let client;

        beforeEach(function () {
            client = make_rfb();
            sinon.spy(RFB.messages, "clientEncodings");
        });

        afterEach(function () {
            RFB.messages.clientEncodings.restore();
        });

        it(`should equal ${defaultQuality} by default`, function () {
            expect(client._qualityLevel).to.equal(defaultQuality);
        });

        it('should ignore non-integers when set', function () {
            client.qualityLevel = '1';
            expect(RFB.messages.clientEncodings).to.not.have.been.called;

            RFB.messages.clientEncodings.resetHistory();

            client.qualityLevel = 1.5;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;

            RFB.messages.clientEncodings.resetHistory();

            client.qualityLevel = null;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;

            RFB.messages.clientEncodings.resetHistory();

            client.qualityLevel = undefined;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;

            RFB.messages.clientEncodings.resetHistory();

            client.qualityLevel = {};
            expect(RFB.messages.clientEncodings).to.not.have.been.called;
        });

        it('should ignore integers out of range [0, 9]', function () {
            client.qualityLevel = -1;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;

            RFB.messages.clientEncodings.resetHistory();

            client.qualityLevel = 10;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;
        });

        it('should send clientEncodings with new quality value', function () {
            let newQuality;

            newQuality = 8;
            client.qualityLevel = newQuality;
            expect(client.qualityLevel).to.equal(newQuality);
            expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
            expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingQualityLevel0 + newQuality);
        });

        it('should not send clientEncodings if quality is the same', function () {
            let newQuality;

            newQuality = 2;
            client.qualityLevel = newQuality;
            expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
            expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingQualityLevel0 + newQuality);

            RFB.messages.clientEncodings.resetHistory();

            client.qualityLevel = newQuality;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;
        });

        it('should not send clientEncodings if not in connected state', function () {
            let newQuality;

            client._rfb_connection_state = '';
            newQuality = 2;
            client.qualityLevel = newQuality;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;

            RFB.messages.clientEncodings.resetHistory();

            client._rfb_connection_state = 'connnecting';
            newQuality = 6;
            client.qualityLevel = newQuality;
            expect(RFB.messages.clientEncodings).to.not.have.been.called;

            RFB.messages.clientEncodings.resetHistory();

            client._rfb_connection_state = 'connected';
            newQuality = 5;
            client.qualityLevel = newQuality;
            expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
            expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingQualityLevel0 + newQuality);
        });
    });
});

describe('RFB messages', function () {
    let sock;

    before(function () {
        FakeWebSocket.replace();
        sock = new Websock();
        sock.open();
    });

    after(function () {
        FakeWebSocket.restore();
    });

    describe('Extended Clipboard Handling Send', function () {
        beforeEach(function () {
            sinon.spy(RFB.messages, 'clientCutText');
        });

        afterEach(function () {
            RFB.messages.clientCutText.restore();
        });

        it('should call clientCutText with correct Caps data', function () {
            let formats = {
                0: 2,
                2: 4121
            };
            let expectedData = new Uint8Array([0x1F, 0x00, 0x00, 0x05,
                                               0x00, 0x00, 0x00, 0x02,
                                               0x00, 0x00, 0x10, 0x19]);
            let actions = [
                1 << 24,  // Caps
                1 << 25,  // Request
                1 << 26,  // Peek
                1 << 27,  // Notify
                1 << 28   // Provide
            ];

            RFB.messages.extendedClipboardCaps(sock, actions, formats);
            expect(RFB.messages.clientCutText).to.have.been.calledOnce;
            expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData);
        });

        it('should call clientCutText with correct Request data', function () {
            let formats = new Uint8Array([0x01]);
            let expectedData = new Uint8Array([0x02, 0x00, 0x00, 0x01]);

            RFB.messages.extendedClipboardRequest(sock, formats);
            expect(RFB.messages.clientCutText).to.have.been.calledOnce;
            expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData);
        });

        it('should call clientCutText with correct Notify data', function () {
            let formats = new Uint8Array([0x01]);
            let expectedData = new Uint8Array([0x08, 0x00, 0x00, 0x01]);

            RFB.messages.extendedClipboardNotify(sock, formats);
            expect(RFB.messages.clientCutText).to.have.been.calledOnce;
            expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData);
        });

        it('should call clientCutText with correct Provide data', function () {
            let testText = "Test string";
            let expectedText = encodeUTF8(testText + "\0");

            let deflatedData =  deflateWithSize(expectedText);

            // Build Expected with flags and deflated data
            let expectedData = new Uint8Array(4 + deflatedData.length);
            expectedData[0] = 0x10; // The client capabilities
            expectedData[1] = 0x00; // Reserved flags
            expectedData[2] = 0x00; // Reserved flags
            expectedData[3] = 0x01; // The formats client supports
            expectedData.set(deflatedData, 4);

            RFB.messages.extendedClipboardProvide(sock, [0x01], [testText]);
            expect(RFB.messages.clientCutText).to.have.been.calledOnce;
            expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData, true);

        });

        describe('End of line characters', function () {
            it('Carriage return', function () {

                let testText = "Hello\rworld\r\r!";
                let expectedText = encodeUTF8("Hello\r\nworld\r\n\r\n!\0");

                let deflatedData =  deflateWithSize(expectedText);

                // Build Expected with flags and deflated data
                let expectedData = new Uint8Array(4 + deflatedData.length);
                expectedData[0] = 0x10; // The client capabilities
                expectedData[1] = 0x00; // Reserved flags
                expectedData[2] = 0x00; // Reserved flags
                expectedData[3] = 0x01; // The formats client supports
                expectedData.set(deflatedData, 4);

                RFB.messages.extendedClipboardProvide(sock, [0x01], [testText]);
                expect(RFB.messages.clientCutText).to.have.been.calledOnce;
                expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData, true);
            });

            it('Carriage return Line feed', function () {

                let testText = "Hello\r\n\r\nworld\r\n!";
                let expectedText = encodeUTF8(testText + "\0");

                let deflatedData =  deflateWithSize(expectedText);

                // Build Expected with flags and deflated data
                let expectedData = new Uint8Array(4 + deflatedData.length);
                expectedData[0] = 0x10; // The client capabilities
                expectedData[1] = 0x00; // Reserved flags
                expectedData[2] = 0x00; // Reserved flags
                expectedData[3] = 0x01; // The formats client supports
                expectedData.set(deflatedData, 4);

                RFB.messages.extendedClipboardProvide(sock, [0x01], [testText]);
                expect(RFB.messages.clientCutText).to.have.been.calledOnce;
                expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData, true);
            });

            it('Line feed', function () {
                let testText = "Hello\n\n\nworld\n!";
                let expectedText = encodeUTF8("Hello\r\n\r\n\r\nworld\r\n!\0");

                let deflatedData =  deflateWithSize(expectedText);

                // Build Expected with flags and deflated data
                let expectedData = new Uint8Array(4 + deflatedData.length);
                expectedData[0] = 0x10; // The client capabilities
                expectedData[1] = 0x00; // Reserved flags
                expectedData[2] = 0x00; // Reserved flags
                expectedData[3] = 0x01; // The formats client supports
                expectedData.set(deflatedData, 4);

                RFB.messages.extendedClipboardProvide(sock, [0x01], [testText]);
                expect(RFB.messages.clientCutText).to.have.been.calledOnce;
                expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData, true);
            });

            it('Carriage return and Line feed mixed', function () {
                let testText = "\rHello\r\n\rworld\n\n!";
                let expectedText = encodeUTF8("\r\nHello\r\n\r\nworld\r\n\r\n!\0");

                let deflatedData =  deflateWithSize(expectedText);

                // Build Expected with flags and deflated data
                let expectedData = new Uint8Array(4 + deflatedData.length);
                expectedData[0] = 0x10; // The client capabilities
                expectedData[1] = 0x00; // Reserved flags
                expectedData[2] = 0x00; // Reserved flags
                expectedData[3] = 0x01; // The formats client supports
                expectedData.set(deflatedData, 4);

                RFB.messages.extendedClipboardProvide(sock, [0x01], [testText]);
                expect(RFB.messages.clientCutText).to.have.been.calledOnce;
                expect(RFB.messages.clientCutText).to.have.been.calledWith(sock, expectedData, true);
            });
        });
    });
});