test.copyrect.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const expect = chai.expect;
  2. import Websock from '../core/websock.js';
  3. import Display from '../core/display.js';
  4. import CopyRectDecoder from '../core/decoders/copyrect.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('CopyRect Decoder', function () {
  23. let decoder;
  24. let display;
  25. before(FakeWebSocket.replace);
  26. after(FakeWebSocket.restore);
  27. beforeEach(function () {
  28. decoder = new CopyRectDecoder();
  29. display = new Display(document.createElement('canvas'));
  30. display.resize(4, 4);
  31. });
  32. it('should handle the CopyRect encoding', function () {
  33. // seed some initial data to copy
  34. display.fillRect(0, 0, 4, 4, [ 0x11, 0x22, 0x33 ]);
  35. display.fillRect(0, 0, 2, 2, [ 0x00, 0x00, 0xff ]);
  36. display.fillRect(2, 0, 2, 2, [ 0x00, 0xff, 0x00 ]);
  37. testDecodeRect(decoder, 0, 2, 2, 2,
  38. [0x00, 0x02, 0x00, 0x00],
  39. display, 24);
  40. testDecodeRect(decoder, 2, 2, 2, 2,
  41. [0x00, 0x00, 0x00, 0x00],
  42. display, 24);
  43. let targetData = new Uint8Array([
  44. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  45. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  46. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  47. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  48. ]);
  49. expect(display).to.have.displayed(targetData);
  50. });
  51. it('should handle empty rects', function () {
  52. display.fillRect(0, 0, 4, 4, [ 0x00, 0x00, 0xff ]);
  53. display.fillRect(2, 0, 2, 2, [ 0x00, 0xff, 0x00 ]);
  54. display.fillRect(0, 2, 2, 2, [ 0x00, 0xff, 0x00 ]);
  55. testDecodeRect(decoder, 1, 2, 0, 0, [0x00, 0x00, 0x00, 0x00], display, 24);
  56. let targetData = new Uint8Array([
  57. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  58. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  59. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  60. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  61. ]);
  62. expect(display).to.have.displayed(targetData);
  63. });
  64. });