test.rre.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const expect = chai.expect;
  2. import Websock from '../core/websock.js';
  3. import Display from '../core/display.js';
  4. import RREDecoder from '../core/decoders/rre.js';
  5. import FakeWebSocket from './fake.websocket.js';
  6. function testDecodeRect(decoder, x, y, width, height, data, display, depth) {
  7. let sock;
  8. sock = new Websock;
  9. sock.open("ws://example.com");
  10. sock.on('message', () => {
  11. decoder.decodeRect(x, y, width, height, sock, display, depth);
  12. });
  13. // Empty messages are filtered at multiple layers, so we need to
  14. // do a direct call
  15. if (data.length === 0) {
  16. decoder.decodeRect(x, y, width, height, sock, display, depth);
  17. } else {
  18. sock._websocket._receiveData(new Uint8Array(data));
  19. }
  20. display.flip();
  21. }
  22. function push16(arr, num) {
  23. arr.push((num >> 8) & 0xFF,
  24. num & 0xFF);
  25. }
  26. function push32(arr, num) {
  27. arr.push((num >> 24) & 0xFF,
  28. (num >> 16) & 0xFF,
  29. (num >> 8) & 0xFF,
  30. num & 0xFF);
  31. }
  32. describe('RRE Decoder', function () {
  33. let decoder;
  34. let display;
  35. before(FakeWebSocket.replace);
  36. after(FakeWebSocket.restore);
  37. beforeEach(function () {
  38. decoder = new RREDecoder();
  39. display = new Display(document.createElement('canvas'));
  40. display.resize(4, 4);
  41. });
  42. // TODO(directxman12): test rre_chunk_sz?
  43. it('should handle the RRE encoding', function () {
  44. let data = [];
  45. push32(data, 2); // 2 subrects
  46. push32(data, 0x00ff0000); // becomes 00ff0000 --> #00FF00 bg color
  47. data.push(0x00); // becomes 0000ff00 --> #0000FF fg color
  48. data.push(0x00);
  49. data.push(0xff);
  50. data.push(0x00);
  51. push16(data, 0); // x: 0
  52. push16(data, 0); // y: 0
  53. push16(data, 2); // width: 2
  54. push16(data, 2); // height: 2
  55. data.push(0x00); // becomes 0000ff00 --> #0000FF fg color
  56. data.push(0x00);
  57. data.push(0xff);
  58. data.push(0x00);
  59. push16(data, 2); // x: 2
  60. push16(data, 2); // y: 2
  61. push16(data, 2); // width: 2
  62. push16(data, 2); // height: 2
  63. testDecodeRect(decoder, 0, 0, 4, 4, data, display, 24);
  64. let targetData = new Uint8Array([
  65. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  66. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  67. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  68. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  69. ]);
  70. expect(display).to.have.displayed(targetData);
  71. });
  72. it('should handle empty rects', function () {
  73. display.fillRect(0, 0, 4, 4, [ 0x00, 0x00, 0xff ]);
  74. display.fillRect(2, 0, 2, 2, [ 0x00, 0xff, 0x00 ]);
  75. display.fillRect(0, 2, 2, 2, [ 0x00, 0xff, 0x00 ]);
  76. testDecodeRect(decoder, 1, 2, 0, 0, [ 0x00, 0xff, 0xff, 0xff, 0xff ], display, 24);
  77. let targetData = new Uint8Array([
  78. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  79. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  80. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  81. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  82. ]);
  83. expect(display).to.have.displayed(targetData);
  84. });
  85. });