]>
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 sock
._websocket
._receiveData(new Uint8Array(data
));
25 function push32(arr
, num
) {
26 arr
.push((num
>> 24) & 0xFF,
32 describe('Hextile Decoder', function () {
36 before(FakeWebSocket
.replace
);
37 after(FakeWebSocket
.restore
);
39 beforeEach(function () {
40 decoder
= new HextileDecoder();
41 display
= new Display(document
.createElement('canvas'));
45 it('should handle a tile with fg, bg specified, normal subrects', function () {
47 data
.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
48 push32(data
, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
49 data
.push(0xff); // becomes ff000000 --> #0000FF fg color
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
59 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
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
68 expect(display
).to
.have
.displayed(targetData
);
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
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 // Last byte zero to test correct alpha handling
89 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
91 expect(display
).to
.have
.displayed(targetData
);
94 it('should handle a tile with only bg specified (solid bg)', function () {
97 push32(data
, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
99 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
102 for (let i
= 0; i
< 16; i
++) {
103 push32(expected
, 0x00ff00ff);
106 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
109 it('should handle a tile with only bg specified and an empty frame afterwards', function () {
110 // set the width so we can have two tiles
111 display
.resize(8, 4);
117 push32(data
, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
119 // send an empty frame
122 testDecodeRect(decoder
, 0, 0, 32, 4, data
, display
, 24);
125 for (let i
= 0; i
< 16; i
++) {
126 push32(expected
, 0x00ff00ff); // rect 1: solid
128 for (let i
= 0; i
< 16; i
++) {
129 push32(expected
, 0x00ff00ff); // rect 2: same bkground color
132 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
135 it('should handle a tile with bg and coloured subrects', function () {
137 data
.push(0x02 | 0x08 | 0x10); // bg spec, anysubrects, colouredsubrects
138 push32(data
, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
139 data
.push(2); // 2 subrects
140 data
.push(0xff); // becomes ff000000 --> #0000FF fg color
144 data
.push(0); // x: 0, y: 0
145 data
.push(1 | (1 << 4)); // width: 2, height: 2
146 data
.push(0xff); // becomes ff000000 --> #0000FF fg color
150 data
.push(2 | (2 << 4)); // x: 2, y: 2
151 data
.push(1 | (1 << 4)); // width: 2, height: 2
153 testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24);
155 let targetData
= new Uint8Array([
156 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
157 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
158 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
159 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
162 expect(display
).to
.have
.displayed(targetData
);
165 it('should carry over fg and bg colors from the previous tile if not specified', function () {
166 display
.resize(4, 17);
169 data
.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
170 push32(data
, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
171 data
.push(0xff); // becomes ff0000ff --> #0000FF fg color
175 data
.push(8); // 8 subrects
176 for (let i
= 0; i
< 4; i
++) {
177 data
.push((0 << 4) | (i
* 4)); // x: 0, y: i*4
178 data
.push(1 | (1 << 4)); // width: 2, height: 2
179 data
.push((2 << 4) | (i
* 4 + 2)); // x: 2, y: i * 4 + 2
180 data
.push(1 | (1 << 4)); // width: 2, height: 2
182 data
.push(0x08); // anysubrects
183 data
.push(1); // 1 subrect
184 data
.push(0); // x: 0, y: 0
185 data
.push(1 | (1 << 4)); // width: 2, height: 2
187 testDecodeRect(decoder
, 0, 0, 4, 17, data
, display
, 24);
190 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
191 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
192 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
193 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
197 for (let i
= 0; i
< 4; i
++) {
198 expected
= expected
.concat(targetData
);
200 expected
= expected
.concat(targetData
.slice(0, 16));
202 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
205 it('should fail on an invalid subencoding', function () {
206 let data
= [45]; // an invalid subencoding
207 expect(() => testDecodeRect(decoder
, 0, 0, 4, 4, data
, display
, 24)).to
.throw();