]>
Commit | Line | Data |
---|---|---|
111225fa PO |
1 | const expect = chai.expect; |
2 | ||
3 | import Websock from '../core/websock.js'; | |
4 | import Display from '../core/display.js'; | |
5 | ||
6 | import HextileDecoder from '../core/decoders/hextile.js'; | |
7 | ||
8 | import FakeWebSocket from './fake.websocket.js'; | |
9 | ||
10 | function testDecodeRect(decoder, x, y, width, height, data, display, depth) { | |
11 | let sock; | |
12 | ||
13 | sock = new Websock; | |
14 | sock.open("ws://example.com"); | |
15 | ||
16 | sock.on('message', () => { | |
17 | decoder.decodeRect(x, y, width, height, sock, display, depth); | |
18 | }); | |
19 | ||
20 | sock._websocket._receiveData(new Uint8Array(data)); | |
21 | ||
22 | display.flip(); | |
23 | } | |
24 | ||
25 | function push32(arr, num) { | |
26 | arr.push((num >> 24) & 0xFF, | |
27 | (num >> 16) & 0xFF, | |
28 | (num >> 8) & 0xFF, | |
29 | num & 0xFF); | |
30 | } | |
31 | ||
32 | describe('Hextile Decoder', function () { | |
33 | let decoder; | |
34 | let display; | |
35 | ||
36 | before(FakeWebSocket.replace); | |
37 | after(FakeWebSocket.restore); | |
38 | ||
39 | beforeEach(function () { | |
40 | decoder = new HextileDecoder(); | |
41 | display = new Display(document.createElement('canvas')); | |
42 | display.resize(4, 4); | |
43 | }); | |
44 | ||
45 | it('should handle a tile with fg, bg specified, normal subrects', function () { | |
46 | let data = []; | |
47 | data.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects | |
48 | push32(data, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color | |
49 | data.push(0xff); // becomes ff0000ff --> #0000FF fg color | |
50 | data.push(0x00); | |
51 | data.push(0x00); | |
52 | data.push(0xff); | |
53 | data.push(2); // 2 subrects | |
54 | data.push(0); // x: 0, y: 0 | |
55 | data.push(1 | (1 << 4)); // width: 2, height: 2 | |
56 | data.push(2 | (2 << 4)); // x: 2, y: 2 | |
57 | data.push(1 | (1 << 4)); // width: 2, height: 2 | |
58 | ||
59 | testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24); | |
60 | ||
61 | let targetData = new Uint8Array([ | |
62 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
63 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
64 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, | |
65 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255 | |
66 | ]); | |
67 | ||
68 | expect(display).to.have.displayed(targetData); | |
69 | }); | |
70 | ||
71 | it('should handle a raw tile', function () { | |
72 | let targetData = new Uint8Array([ | |
73 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
74 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
75 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, | |
76 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255 | |
77 | ]); | |
78 | ||
79 | let data = []; | |
80 | data.push(0x01); // raw | |
81 | for (let i = 0; i < targetData.length; i += 4) { | |
82 | data.push(targetData[i + 2]); | |
83 | data.push(targetData[i + 1]); | |
84 | data.push(targetData[i]); | |
85 | data.push(targetData[i + 3]); | |
86 | } | |
87 | ||
88 | testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24); | |
89 | ||
90 | expect(display).to.have.displayed(targetData); | |
91 | }); | |
92 | ||
93 | it('should handle a tile with only bg specified (solid bg)', function () { | |
94 | let data = []; | |
95 | data.push(0x02); | |
96 | push32(data, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color | |
97 | ||
98 | testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24); | |
99 | ||
100 | let expected = []; | |
101 | for (let i = 0; i < 16; i++) { | |
102 | push32(expected, 0xff00ff); | |
103 | } | |
104 | ||
105 | expect(display).to.have.displayed(new Uint8Array(expected)); | |
106 | }); | |
107 | ||
108 | it('should handle a tile with only bg specified and an empty frame afterwards', function () { | |
109 | // set the width so we can have two tiles | |
110 | display.resize(8, 4); | |
111 | ||
112 | let data = []; | |
113 | ||
114 | // send a bg frame | |
115 | data.push(0x02); | |
116 | push32(data, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color | |
117 | ||
118 | // send an empty frame | |
119 | data.push(0x00); | |
120 | ||
121 | testDecodeRect(decoder, 0, 0, 32, 4, data, display, 24); | |
122 | ||
123 | let expected = []; | |
124 | for (let i = 0; i < 16; i++) { | |
125 | push32(expected, 0xff00ff); // rect 1: solid | |
126 | } | |
127 | for (let i = 0; i < 16; i++) { | |
128 | push32(expected, 0xff00ff); // rect 2: same bkground color | |
129 | } | |
130 | ||
131 | expect(display).to.have.displayed(new Uint8Array(expected)); | |
132 | }); | |
133 | ||
134 | it('should handle a tile with bg and coloured subrects', function () { | |
135 | let data = []; | |
136 | data.push(0x02 | 0x08 | 0x10); // bg spec, anysubrects, colouredsubrects | |
137 | push32(data, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color | |
138 | data.push(2); // 2 subrects | |
139 | data.push(0xff); // becomes ff0000ff --> #0000FF fg color | |
140 | data.push(0x00); | |
141 | data.push(0x00); | |
142 | data.push(0xff); | |
143 | data.push(0); // x: 0, y: 0 | |
144 | data.push(1 | (1 << 4)); // width: 2, height: 2 | |
145 | data.push(0xff); // becomes ff0000ff --> #0000FF fg color | |
146 | data.push(0x00); | |
147 | data.push(0x00); | |
148 | data.push(0xff); | |
149 | data.push(2 | (2 << 4)); // x: 2, y: 2 | |
150 | data.push(1 | (1 << 4)); // width: 2, height: 2 | |
151 | ||
152 | testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24); | |
153 | ||
154 | let targetData = new Uint8Array([ | |
155 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
156 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
157 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, | |
158 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255 | |
159 | ]); | |
160 | ||
161 | expect(display).to.have.displayed(targetData); | |
162 | }); | |
163 | ||
164 | it('should carry over fg and bg colors from the previous tile if not specified', function () { | |
165 | display.resize(4, 17); | |
166 | ||
167 | let data = []; | |
168 | data.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects | |
169 | push32(data, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color | |
170 | data.push(0xff); // becomes ff0000ff --> #0000FF fg color | |
171 | data.push(0x00); | |
172 | data.push(0x00); | |
173 | data.push(0xff); | |
174 | data.push(8); // 8 subrects | |
175 | for (let i = 0; i < 4; i++) { | |
176 | data.push((0 << 4) | (i * 4)); // x: 0, y: i*4 | |
177 | data.push(1 | (1 << 4)); // width: 2, height: 2 | |
178 | data.push((2 << 4) | (i * 4 + 2)); // x: 2, y: i * 4 + 2 | |
179 | data.push(1 | (1 << 4)); // width: 2, height: 2 | |
180 | } | |
181 | data.push(0x08); // anysubrects | |
182 | data.push(1); // 1 subrect | |
183 | data.push(0); // x: 0, y: 0 | |
184 | data.push(1 | (1 << 4)); // width: 2, height: 2 | |
185 | ||
186 | testDecodeRect(decoder, 0, 0, 4, 17, data, display, 24); | |
187 | ||
188 | let targetData = [ | |
189 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
190 | 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, | |
191 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, | |
192 | 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255 | |
193 | ]; | |
194 | ||
195 | let expected = []; | |
196 | for (let i = 0; i < 4; i++) { | |
197 | expected = expected.concat(targetData); | |
198 | } | |
199 | expected = expected.concat(targetData.slice(0, 16)); | |
200 | ||
201 | expect(display).to.have.displayed(new Uint8Array(expected)); | |
202 | }); | |
203 | ||
204 | it('should fail on an invalid subencoding', function () { | |
205 | let data = [45]; // an invalid subencoding | |
206 | expect(() => testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24)).to.throw(); | |
207 | }); | |
208 | }); |