test.raw.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const expect = chai.expect;
  2. import Websock from '../core/websock.js';
  3. import Display from '../core/display.js';
  4. import RawDecoder from '../core/decoders/raw.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. describe('Raw Decoder', function () {
  23. let decoder;
  24. let display;
  25. before(FakeWebSocket.replace);
  26. after(FakeWebSocket.restore);
  27. beforeEach(function () {
  28. decoder = new RawDecoder();
  29. display = new Display(document.createElement('canvas'));
  30. display.resize(4, 4);
  31. });
  32. it('should handle the Raw encoding', function () {
  33. testDecodeRect(decoder, 0, 0, 2, 2,
  34. [0xff, 0x00, 0x00, 0, 0x00, 0xff, 0x00, 0,
  35. 0x00, 0xff, 0x00, 0, 0xff, 0x00, 0x00, 0],
  36. display, 24);
  37. testDecodeRect(decoder, 2, 0, 2, 2,
  38. [0x00, 0x00, 0xff, 0, 0x00, 0x00, 0xff, 0,
  39. 0x00, 0x00, 0xff, 0, 0x00, 0x00, 0xff, 0],
  40. display, 24);
  41. testDecodeRect(decoder, 0, 2, 4, 1,
  42. [0xee, 0x00, 0xff, 0, 0x00, 0xee, 0xff, 0,
  43. 0xaa, 0xee, 0xff, 0, 0xab, 0xee, 0xff, 0],
  44. display, 24);
  45. testDecodeRect(decoder, 0, 3, 4, 1,
  46. [0xee, 0x00, 0xff, 0, 0x00, 0xee, 0xff, 0,
  47. 0xaa, 0xee, 0xff, 0, 0xab, 0xee, 0xff, 0],
  48. display, 24);
  49. let targetData = new Uint8Array([
  50. 0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  51. 0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  52. 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255,
  53. 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255
  54. ]);
  55. expect(display).to.have.displayed(targetData);
  56. });
  57. it('should handle the Raw encoding in low colour mode', function () {
  58. testDecodeRect(decoder, 0, 0, 2, 2,
  59. [0x30, 0x30, 0x30, 0x30],
  60. display, 8);
  61. testDecodeRect(decoder, 2, 0, 2, 2,
  62. [0x0c, 0x0c, 0x0c, 0x0c],
  63. display, 8);
  64. testDecodeRect(decoder, 0, 2, 4, 1,
  65. [0x0c, 0x0c, 0x30, 0x30],
  66. display, 8);
  67. testDecodeRect(decoder, 0, 3, 4, 1,
  68. [0x0c, 0x0c, 0x30, 0x30],
  69. display, 8);
  70. let targetData = new Uint8Array([
  71. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  72. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  73. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  74. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  75. ]);
  76. expect(display).to.have.displayed(targetData);
  77. });
  78. it('should handle empty rects', function () {
  79. display.fillRect(0, 0, 4, 4, [ 0x00, 0x00, 0xff ]);
  80. display.fillRect(2, 0, 2, 2, [ 0x00, 0xff, 0x00 ]);
  81. display.fillRect(0, 2, 2, 2, [ 0x00, 0xff, 0x00 ]);
  82. testDecodeRect(decoder, 1, 2, 0, 0, [], display, 24);
  83. let targetData = new Uint8Array([
  84. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  85. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  86. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  87. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  88. ]);
  89. expect(display).to.have.displayed(targetData);
  90. });
  91. it('should handle empty rects in low colour mode', function () {
  92. display.fillRect(0, 0, 4, 4, [ 0x00, 0x00, 0xff ]);
  93. display.fillRect(2, 0, 2, 2, [ 0x00, 0xff, 0x00 ]);
  94. display.fillRect(0, 2, 2, 2, [ 0x00, 0xff, 0x00 ]);
  95. testDecodeRect(decoder, 1, 2, 0, 0, [], display, 8);
  96. let targetData = new Uint8Array([
  97. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  98. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  99. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  100. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  101. ]);
  102. expect(display).to.have.displayed(targetData);
  103. });
  104. });