|
| 1 | +import Websock from '../core/websock.js'; |
| 2 | +import Display from '../core/display.js'; |
| 3 | + |
| 4 | +import ZlibDecoder from '../core/decoders/zlib.js'; |
| 5 | + |
| 6 | +import FakeWebSocket from './fake.websocket.js'; |
| 7 | + |
| 8 | +function testDecodeRect(decoder, x, y, width, height, data, display, depth) { |
| 9 | + let sock; |
| 10 | + let done = false; |
| 11 | + |
| 12 | + sock = new Websock; |
| 13 | + sock.open("ws://example.com"); |
| 14 | + |
| 15 | + sock.on('message', () => { |
| 16 | + done = decoder.decodeRect(x, y, width, height, sock, display, depth); |
| 17 | + }); |
| 18 | + |
| 19 | + // Empty messages are filtered at multiple layers, so we need to |
| 20 | + // do a direct call |
| 21 | + if (data.length === 0) { |
| 22 | + done = decoder.decodeRect(x, y, width, height, sock, display, depth); |
| 23 | + } else { |
| 24 | + sock._websocket._receiveData(new Uint8Array(data)); |
| 25 | + } |
| 26 | + |
| 27 | + display.flip(); |
| 28 | + |
| 29 | + return done; |
| 30 | +} |
| 31 | + |
| 32 | +describe('Zlib Decoder', function () { |
| 33 | + let decoder; |
| 34 | + let display; |
| 35 | + |
| 36 | + before(FakeWebSocket.replace); |
| 37 | + after(FakeWebSocket.restore); |
| 38 | + |
| 39 | + beforeEach(function () { |
| 40 | + decoder = new ZlibDecoder(); |
| 41 | + display = new Display(document.createElement('canvas')); |
| 42 | + display.resize(4, 4); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should handle the Zlib encoding', function () { |
| 46 | + let done; |
| 47 | + |
| 48 | + let zlibData = new Uint8Array([ |
| 49 | + 0x00, 0x00, 0x00, 0x23, /* length */ |
| 50 | + 0x78, 0x01, 0xfa, 0xcf, 0x00, 0x04, 0xff, 0x61, 0x04, 0x90, 0x01, 0x41, 0x50, 0xc1, 0xff, 0x0c, |
| 51 | + 0xef, 0x40, 0x02, 0xef, 0xfe, 0x33, 0xac, 0x02, 0xe2, 0xd5, 0x40, 0x8c, 0xce, 0x07, 0x00, 0x00, |
| 52 | + 0x00, 0xff, 0xff, |
| 53 | + ]); |
| 54 | + done = testDecodeRect(decoder, 0, 0, 4, 4, zlibData, display, 24); |
| 55 | + expect(done).to.be.true; |
| 56 | + |
| 57 | + let targetData = new Uint8ClampedArray([ |
| 58 | + 0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, |
| 59 | + 0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, |
| 60 | + 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255, |
| 61 | + 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255 |
| 62 | + ]); |
| 63 | + |
| 64 | + expect(display).to.have.displayed(targetData); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle empty rects', function () { |
| 68 | + display.fillRect(0, 0, 4, 4, [0x00, 0x00, 0xff]); |
| 69 | + display.fillRect(2, 0, 2, 2, [0x00, 0xff, 0x00]); |
| 70 | + display.fillRect(0, 2, 2, 2, [0x00, 0xff, 0x00]); |
| 71 | + |
| 72 | + let done = testDecodeRect(decoder, 1, 2, 0, 0, [], display, 24); |
| 73 | + |
| 74 | + let targetData = new Uint8Array([ |
| 75 | + 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, |
| 76 | + 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, |
| 77 | + 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, |
| 78 | + 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255 |
| 79 | + ]); |
| 80 | + |
| 81 | + expect(done).to.be.true; |
| 82 | + expect(display).to.have.displayed(targetData); |
| 83 | + }); |
| 84 | +}); |
0 commit comments