]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.hextile.js
1 const expect
= chai
.expect
;
3 import Websock
from '../core/websock.js';
4 import Display
from '../core/display.js';
6 import HextileDecoder
from '../core/decoders/hextile.js';
8 import FakeWebSocket
from './fake.websocket.js';
10 function testDecodeRect(decoder
, x
, y
, width
, height
, data
, display
, depth
) {
14 sock
.open("ws://example.com");
16 sock
.on('message', () => {
17 decoder
.decodeRect(x
, y
, width
, height
, sock
, display
, depth
);
20 // Empty messages are filtered at multiple layers, so we need to
22 if (data
.length
=== 0) {
23 decoder
.decodeRect(x
, y
, width
, height
, sock
, display
, depth
);
25 sock
._websocket
._receiveData(new Uint8Array(data
));
31 function push32(arr
, num
) {
32 arr
.push((num
>> 24) & 0xFF,
38 describe('Hextile Decoder', function () {
42 before(FakeWebSocket
.replace
);
43 after(FakeWebSocket
.restore
);
45 beforeEach(function () {
46 decoder
= new HextileDecoder();
47 display
= new Display(document
.createElement('canvas'));
51 it('should handle a tile with fg, bg specified, normal subrects', function () {
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
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
65 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
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
74 expect(display
).to
.have
.displayed(targetData
);
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
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
95 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
97 expect(display
).to
.have
.displayed(targetData
);
100 it('should handle a tile with only bg specified (solid bg)', function () {
103 push32(data
, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
105 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
108 for (let i
= 0; i
< 16; i
++) {
109 push32(expected
, 0x00ff00ff);
112 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
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);
123 push32(data
, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
125 // send an empty frame
128 testDecodeRect(decoder
, 0, 0, 32, 4, data
, display
, 24);
131 for (let i
= 0; i
< 16; i
++) {
132 push32(expected
, 0x00ff00ff); // rect 1: solid
134 for (let i
= 0; i
< 16; i
++) {
135 push32(expected
, 0x00ff00ff); // rect 2: same bkground color
138 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
141 it('should handle a tile with bg and coloured subrects', function () {
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
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
156 data
.push(2 | (2 << 4)); // x: 2, y: 2
157 data
.push(1 | (1 << 4)); // width: 2, height: 2
159 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
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
168 expect(display
).to
.have
.displayed(targetData
);
171 it('should carry over fg and bg colors from the previous tile if not specified', function () {
172 display
.resize(4, 17);
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
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
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
193 testDecodeRect(decoder
, 0, 0, 4, 17, data
, display
, 24);
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
203 for (let i
= 0; i
< 4; i
++) {
204 expected
= expected
.concat(targetData
);
206 expected
= expected
.concat(targetData
.slice(0, 16));
208 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
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();
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 ]);
221 testDecodeRect(decoder
, 1, 2, 0, 0, [], display
, 24);
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
230 expect(display
).to
.have
.displayed(targetData
);