]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.hextile.js
feat: add French localization strings
[mirror_novnc.git] / tests / test.hextile.js
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 // Empty messages are filtered at multiple layers, so we need to
21 // do a direct call
22 if (data.length === 0) {
23 decoder.decodeRect(x, y, width, height, sock, display, depth);
24 } else {
25 sock._websocket._receiveData(new Uint8Array(data));
26 }
27
28 display.flip();
29 }
30
31 function push32(arr, num) {
32 arr.push((num >> 24) & 0xFF,
33 (num >> 16) & 0xFF,
34 (num >> 8) & 0xFF,
35 num & 0xFF);
36 }
37
38 describe('Hextile Decoder', function () {
39 let decoder;
40 let display;
41
42 before(FakeWebSocket.replace);
43 after(FakeWebSocket.restore);
44
45 beforeEach(function () {
46 decoder = new HextileDecoder();
47 display = new Display(document.createElement('canvas'));
48 display.resize(4, 4);
49 });
50
51 it('should handle a tile with fg, bg specified, normal subrects', function () {
52 let data = [];
53 data.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
54 push32(data, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
55 data.push(0x00); // becomes 0000ff00 --> #0000FF fg color
56 data.push(0x00);
57 data.push(0xff);
58 data.push(0x00);
59 data.push(2); // 2 subrects
60 data.push(0); // x: 0, y: 0
61 data.push(1 | (1 << 4)); // width: 2, height: 2
62 data.push(2 | (2 << 4)); // x: 2, y: 2
63 data.push(1 | (1 << 4)); // width: 2, height: 2
64
65 testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24);
66
67 let targetData = new Uint8Array([
68 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
69 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
70 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
71 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
72 ]);
73
74 expect(display).to.have.displayed(targetData);
75 });
76
77 it('should handle a raw tile', function () {
78 let targetData = new Uint8Array([
79 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
80 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
81 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
82 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
83 ]);
84
85 let data = [];
86 data.push(0x01); // raw
87 for (let i = 0; i < targetData.length; i += 4) {
88 data.push(targetData[i]);
89 data.push(targetData[i + 1]);
90 data.push(targetData[i + 2]);
91 // Last byte zero to test correct alpha handling
92 data.push(0);
93 }
94
95 testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24);
96
97 expect(display).to.have.displayed(targetData);
98 });
99
100 it('should handle a tile with only bg specified (solid bg)', function () {
101 let data = [];
102 data.push(0x02);
103 push32(data, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
104
105 testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24);
106
107 let expected = [];
108 for (let i = 0; i < 16; i++) {
109 push32(expected, 0x00ff00ff);
110 }
111
112 expect(display).to.have.displayed(new Uint8Array(expected));
113 });
114
115 it('should handle a tile with only bg specified and an empty frame afterwards', function () {
116 // set the width so we can have two tiles
117 display.resize(8, 4);
118
119 let data = [];
120
121 // send a bg frame
122 data.push(0x02);
123 push32(data, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
124
125 // send an empty frame
126 data.push(0x00);
127
128 testDecodeRect(decoder, 0, 0, 32, 4, data, display, 24);
129
130 let expected = [];
131 for (let i = 0; i < 16; i++) {
132 push32(expected, 0x00ff00ff); // rect 1: solid
133 }
134 for (let i = 0; i < 16; i++) {
135 push32(expected, 0x00ff00ff); // rect 2: same bkground color
136 }
137
138 expect(display).to.have.displayed(new Uint8Array(expected));
139 });
140
141 it('should handle a tile with bg and coloured subrects', function () {
142 let data = [];
143 data.push(0x02 | 0x08 | 0x10); // bg spec, anysubrects, colouredsubrects
144 push32(data, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
145 data.push(2); // 2 subrects
146 data.push(0x00); // becomes 0000ff00 --> #0000FF fg color
147 data.push(0x00);
148 data.push(0xff);
149 data.push(0x00);
150 data.push(0); // x: 0, y: 0
151 data.push(1 | (1 << 4)); // width: 2, height: 2
152 data.push(0x00); // becomes 0000ff00 --> #0000FF fg color
153 data.push(0x00);
154 data.push(0xff);
155 data.push(0x00);
156 data.push(2 | (2 << 4)); // x: 2, y: 2
157 data.push(1 | (1 << 4)); // width: 2, height: 2
158
159 testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24);
160
161 let targetData = new Uint8Array([
162 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
163 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
164 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
165 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
166 ]);
167
168 expect(display).to.have.displayed(targetData);
169 });
170
171 it('should carry over fg and bg colors from the previous tile if not specified', function () {
172 display.resize(4, 17);
173
174 let data = [];
175 data.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
176 push32(data, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
177 data.push(0x00); // becomes 0000ffff --> #0000FF fg color
178 data.push(0x00);
179 data.push(0xff);
180 data.push(0xff);
181 data.push(8); // 8 subrects
182 for (let i = 0; i < 4; i++) {
183 data.push((0 << 4) | (i * 4)); // x: 0, y: i*4
184 data.push(1 | (1 << 4)); // width: 2, height: 2
185 data.push((2 << 4) | (i * 4 + 2)); // x: 2, y: i * 4 + 2
186 data.push(1 | (1 << 4)); // width: 2, height: 2
187 }
188 data.push(0x08); // anysubrects
189 data.push(1); // 1 subrect
190 data.push(0); // x: 0, y: 0
191 data.push(1 | (1 << 4)); // width: 2, height: 2
192
193 testDecodeRect(decoder, 0, 0, 4, 17, data, display, 24);
194
195 let targetData = [
196 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
197 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
198 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
199 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
200 ];
201
202 let expected = [];
203 for (let i = 0; i < 4; i++) {
204 expected = expected.concat(targetData);
205 }
206 expected = expected.concat(targetData.slice(0, 16));
207
208 expect(display).to.have.displayed(new Uint8Array(expected));
209 });
210
211 it('should fail on an invalid subencoding', function () {
212 let data = [45]; // an invalid subencoding
213 expect(() => testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24)).to.throw();
214 });
215
216 it('should handle empty rects', function () {
217 display.fillRect(0, 0, 4, 4, [ 0x00, 0x00, 0xff ]);
218 display.fillRect(2, 0, 2, 2, [ 0x00, 0xff, 0x00 ]);
219 display.fillRect(0, 2, 2, 2, [ 0x00, 0xff, 0x00 ]);
220
221 testDecodeRect(decoder, 1, 2, 0, 0, [], display, 24);
222
223 let targetData = new Uint8Array([
224 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
225 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
226 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
227 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
228 ]);
229
230 expect(display).to.have.displayed(targetData);
231 });
232 });