]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.display.js
Remove non-JavaScript render code
[mirror_novnc.git] / tests / test.display.js
1 /* jshint expr: true */
2 var expect = chai.expect;
3
4 import Base64 from '../core/base64.js';
5 import Display from '../core/display.js';
6 import { _forceCursorURIs, browserSupportsCursorURIs } from '../core/util/browsers.js';
7
8 import sinon from '../vendor/sinon.js';
9
10 describe('Display/Canvas Helper', function () {
11 var checked_data = [
12 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
13 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
14 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
15 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
16 ];
17 checked_data = new Uint8Array(checked_data);
18
19 var basic_data = [0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0xff, 0xff, 0xff, 255];
20 basic_data = new Uint8Array(basic_data);
21
22 function make_image_canvas (input_data) {
23 var canvas = document.createElement('canvas');
24 canvas.width = 4;
25 canvas.height = 4;
26 var ctx = canvas.getContext('2d');
27 var data = ctx.createImageData(4, 4);
28 for (var i = 0; i < checked_data.length; i++) { data.data[i] = input_data[i]; }
29 ctx.putImageData(data, 0, 0);
30 return canvas;
31 }
32
33 function make_image_png (input_data) {
34 var canvas = make_image_canvas(input_data);
35 var url = canvas.toDataURL();
36 var data = url.split(",")[1];
37 return Base64.decode(data);
38 }
39
40 describe('checking for cursor uri support', function () {
41 it('should disable cursor URIs if there is no support', function () {
42 _forceCursorURIs(false);
43 var display = new Display(document.createElement('canvas'), { viewport: false });
44 expect(display._cursor_uri).to.be.false;
45 });
46
47 it('should enable cursor URIs if there is support', function () {
48 _forceCursorURIs(true);
49 var display = new Display(document.createElement('canvas'), { viewport: false });
50 expect(display._cursor_uri).to.be.true;
51 });
52
53 it('respect the cursor_uri option if there is support', function () {
54 _forceCursorURIs(false);
55 var display = new Display(document.createElement('canvas'), { viewport: false, cursor_uri: false });
56 expect(display._cursor_uri).to.be.false;
57 });
58 });
59
60 describe('viewport handling', function () {
61 var display;
62 beforeEach(function () {
63 display = new Display(document.createElement('canvas'), { viewport: true });
64 display.resize(5, 5);
65 display.viewportChangeSize(3, 3);
66 display.viewportChangePos(1, 1);
67 });
68
69 it('should take viewport location into consideration when drawing images', function () {
70 display.resize(4, 4);
71 display.viewportChangeSize(2, 2);
72 display.drawImage(make_image_canvas(basic_data), 1, 1);
73 display.flip();
74
75 var expected = new Uint8Array(16);
76 var i;
77 for (i = 0; i < 8; i++) { expected[i] = basic_data[i]; }
78 for (i = 8; i < 16; i++) { expected[i] = 0; }
79 expect(display).to.have.displayed(expected);
80 });
81
82 it('should resize the target canvas when resizing the viewport', function() {
83 display.viewportChangeSize(2, 2);
84 expect(display._target.width).to.equal(2);
85 expect(display._target.height).to.equal(2);
86 });
87
88 it('should move the viewport if necessary', function() {
89 display.viewportChangeSize(5, 5);
90 expect(display.absX(0)).to.equal(0);
91 expect(display.absY(0)).to.equal(0);
92 expect(display._target.width).to.equal(5);
93 expect(display._target.height).to.equal(5);
94 });
95
96 it('should limit the viewport to the framebuffer size', function() {
97 display.viewportChangeSize(6, 6);
98 expect(display._target.width).to.equal(5);
99 expect(display._target.height).to.equal(5);
100 });
101
102 it('should redraw when moving the viewport', function () {
103 display.flip = sinon.spy();
104 display.viewportChangePos(-1, 1);
105 expect(display.flip).to.have.been.calledOnce;
106 });
107
108 it('should redraw when resizing the viewport', function () {
109 display.flip = sinon.spy();
110 display.viewportChangeSize(2, 2);
111 expect(display.flip).to.have.been.calledOnce;
112 });
113
114 it('should report clipping when framebuffer > viewport', function () {
115 var clipping = display.clippingDisplay();
116 expect(clipping).to.be.true;
117 });
118
119 it('should report not clipping when framebuffer = viewport', function () {
120 display.viewportChangeSize(5, 5);
121 var clipping = display.clippingDisplay();
122 expect(clipping).to.be.false;
123 });
124
125 it('should show the entire framebuffer when disabling the viewport', function() {
126 display.set_viewport(false);
127 expect(display.absX(0)).to.equal(0);
128 expect(display.absY(0)).to.equal(0);
129 expect(display._target.width).to.equal(5);
130 expect(display._target.height).to.equal(5);
131 });
132
133 it('should ignore viewport changes when the viewport is disabled', function() {
134 display.set_viewport(false);
135 display.viewportChangeSize(2, 2);
136 display.viewportChangePos(1, 1);
137 expect(display.absX(0)).to.equal(0);
138 expect(display.absY(0)).to.equal(0);
139 expect(display._target.width).to.equal(5);
140 expect(display._target.height).to.equal(5);
141 });
142
143 it('should show the entire framebuffer just after enabling the viewport', function() {
144 display.set_viewport(false);
145 display.set_viewport(true);
146 expect(display.absX(0)).to.equal(0);
147 expect(display.absY(0)).to.equal(0);
148 expect(display._target.width).to.equal(5);
149 expect(display._target.height).to.equal(5);
150 });
151 });
152
153 describe('resizing', function () {
154 var display;
155 beforeEach(function () {
156 display = new Display(document.createElement('canvas'), { viewport: false });
157 display.resize(4, 4);
158 });
159
160 it('should change the size of the logical canvas', function () {
161 display.resize(5, 7);
162 expect(display._fb_width).to.equal(5);
163 expect(display._fb_height).to.equal(7);
164 });
165
166 it('should keep the framebuffer data', function () {
167 display.fillRect(0, 0, 4, 4, [0, 0, 0xff]);
168 display.resize(2, 2);
169 display.flip();
170 var expected = [];
171 for (var i = 0; i < 4 * 2*2; i += 4) {
172 expected[i] = 0xff;
173 expected[i+1] = expected[i+2] = 0;
174 expected[i+3] = 0xff;
175 }
176 expect(display).to.have.displayed(new Uint8Array(expected));
177 });
178
179 describe('viewport', function () {
180 beforeEach(function () {
181 display.set_viewport(true);
182 display.viewportChangeSize(3, 3);
183 display.viewportChangePos(1, 1);
184 });
185
186 it('should keep the viewport position and size if possible', function () {
187 display.resize(6, 6);
188 expect(display.absX(0)).to.equal(1);
189 expect(display.absY(0)).to.equal(1);
190 expect(display._target.width).to.equal(3);
191 expect(display._target.height).to.equal(3);
192 });
193
194 it('should move the viewport if necessary', function () {
195 display.resize(3, 3);
196 expect(display.absX(0)).to.equal(0);
197 expect(display.absY(0)).to.equal(0);
198 expect(display._target.width).to.equal(3);
199 expect(display._target.height).to.equal(3);
200 });
201
202 it('should shrink the viewport if necessary', function () {
203 display.resize(2, 2);
204 expect(display.absX(0)).to.equal(0);
205 expect(display.absY(0)).to.equal(0);
206 expect(display._target.width).to.equal(2);
207 expect(display._target.height).to.equal(2);
208 });
209 });
210 });
211
212 describe('rescaling', function () {
213 var display;
214 var canvas;
215
216 beforeEach(function () {
217 canvas = document.createElement('canvas');
218 display = new Display(canvas, { viewport: true });
219 display.resize(4, 4);
220 display.viewportChangeSize(3, 3);
221 display.viewportChangePos(1, 1);
222 document.body.appendChild(canvas);
223 });
224
225 afterEach(function () {
226 document.body.removeChild(canvas);
227 });
228
229 it('should not change the bitmap size of the canvas', function () {
230 display.set_scale(2.0);
231 expect(canvas.width).to.equal(3);
232 expect(canvas.height).to.equal(3);
233 });
234
235 it('should change the effective rendered size of the canvas', function () {
236 display.set_scale(2.0);
237 expect(canvas.clientWidth).to.equal(6);
238 expect(canvas.clientHeight).to.equal(6);
239 });
240
241 it('should not change when resizing', function () {
242 display.set_scale(2.0);
243 display.resize(5, 5);
244 expect(display.get_scale()).to.equal(2.0);
245 expect(canvas.width).to.equal(3);
246 expect(canvas.height).to.equal(3);
247 expect(canvas.clientWidth).to.equal(6);
248 expect(canvas.clientHeight).to.equal(6);
249 });
250 });
251
252 describe('autoscaling', function () {
253 var display;
254 var canvas;
255
256 beforeEach(function () {
257 canvas = document.createElement('canvas');
258 display = new Display(canvas, { viewport: true });
259 display.resize(4, 3);
260 document.body.appendChild(canvas);
261 });
262
263 afterEach(function () {
264 document.body.removeChild(canvas);
265 });
266
267 it('should preserve aspect ratio while autoscaling', function () {
268 display.autoscale(16, 9);
269 expect(canvas.clientWidth / canvas.clientHeight).to.equal(4 / 3);
270 });
271
272 it('should use width to determine scale when the current aspect ratio is wider than the target', function () {
273 display.autoscale(9, 16);
274 expect(display.absX(9)).to.equal(4);
275 expect(display.absY(18)).to.equal(8);
276 expect(canvas.clientWidth).to.equal(9);
277 expect(canvas.clientHeight).to.equal(7); // round 9 / (4 / 3)
278 });
279
280 it('should use height to determine scale when the current aspect ratio is taller than the target', function () {
281 display.autoscale(16, 9);
282 expect(display.absX(9)).to.equal(3);
283 expect(display.absY(18)).to.equal(6);
284 expect(canvas.clientWidth).to.equal(12); // 16 * (4 / 3)
285 expect(canvas.clientHeight).to.equal(9);
286
287 });
288
289 it('should not change the bitmap size of the canvas', function () {
290 display.autoscale(16, 9);
291 expect(canvas.width).to.equal(4);
292 expect(canvas.height).to.equal(3);
293 });
294
295 it('should not upscale when downscaleOnly is true', function () {
296 display.autoscale(2, 2, true);
297 expect(display.absX(9)).to.equal(18);
298 expect(display.absY(18)).to.equal(36);
299 expect(canvas.clientWidth).to.equal(2);
300 expect(canvas.clientHeight).to.equal(2);
301
302 display.autoscale(16, 9, true);
303 expect(display.absX(9)).to.equal(9);
304 expect(display.absY(18)).to.equal(18);
305 expect(canvas.clientWidth).to.equal(4);
306 expect(canvas.clientHeight).to.equal(3);
307 });
308 });
309
310 describe('drawing', function () {
311
312 // TODO(directxman12): improve the tests for each of the drawing functions to cover more than just the
313 // basic cases
314 var display;
315 beforeEach(function () {
316 display = new Display(document.createElement('canvas'));
317 display.resize(4, 4);
318 });
319
320 it('should clear the screen on #clear without a logo set', function () {
321 display.fillRect(0, 0, 4, 4, [0x00, 0x00, 0xff]);
322 display._logo = null;
323 display.clear();
324 display.resize(4, 4);
325 var empty = [];
326 for (var i = 0; i < 4 * display._fb_width * display._fb_height; i++) { empty[i] = 0; }
327 expect(display).to.have.displayed(new Uint8Array(empty));
328 });
329
330 it('should draw the logo on #clear with a logo set', function (done) {
331 display._logo = { width: 4, height: 4, type: "image/png", data: make_image_png(checked_data) };
332 display.clear();
333 display.set_onFlush(function () {
334 expect(display).to.have.displayed(checked_data);
335 expect(display._fb_width).to.equal(4);
336 expect(display._fb_height).to.equal(4);
337 done();
338 });
339 display.flush();
340 });
341
342 it('should not draw directly on the target canvas', function () {
343 display.fillRect(0, 0, 4, 4, [0, 0, 0xff]);
344 display.flip();
345 display.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
346 var expected = [];
347 for (var i = 0; i < 4 * display._fb_width * display._fb_height; i += 4) {
348 expected[i] = 0xff;
349 expected[i+1] = expected[i+2] = 0;
350 expected[i+3] = 0xff;
351 }
352 expect(display).to.have.displayed(new Uint8Array(expected));
353 });
354
355 it('should support filling a rectangle with particular color via #fillRect', function () {
356 display.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
357 display.fillRect(0, 0, 2, 2, [0xff, 0, 0]);
358 display.fillRect(2, 2, 2, 2, [0xff, 0, 0]);
359 display.flip();
360 expect(display).to.have.displayed(checked_data);
361 });
362
363 it('should support copying an portion of the canvas via #copyImage', function () {
364 display.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
365 display.fillRect(0, 0, 2, 2, [0xff, 0, 0x00]);
366 display.copyImage(0, 0, 2, 2, 2, 2);
367 display.flip();
368 expect(display).to.have.displayed(checked_data);
369 });
370
371 it('should support drawing images via #imageRect', function (done) {
372 display.imageRect(0, 0, "image/png", make_image_png(checked_data));
373 display.flip();
374 display.set_onFlush(function () {
375 expect(display).to.have.displayed(checked_data);
376 done();
377 });
378 display.flush();
379 });
380
381 it('should support drawing tile data with a background color and sub tiles', function () {
382 display.startTile(0, 0, 4, 4, [0, 0xff, 0]);
383 display.subTile(0, 0, 2, 2, [0xff, 0, 0]);
384 display.subTile(2, 2, 2, 2, [0xff, 0, 0]);
385 display.finishTile();
386 display.flip();
387 expect(display).to.have.displayed(checked_data);
388 });
389
390 it('should support drawing BGRX blit images with true color via #blitImage', function () {
391 var data = [];
392 for (var i = 0; i < 16; i++) {
393 data[i * 4] = checked_data[i * 4 + 2];
394 data[i * 4 + 1] = checked_data[i * 4 + 1];
395 data[i * 4 + 2] = checked_data[i * 4];
396 data[i * 4 + 3] = checked_data[i * 4 + 3];
397 }
398 display.blitImage(0, 0, 4, 4, data, 0);
399 display.flip();
400 expect(display).to.have.displayed(checked_data);
401 });
402
403 it('should support drawing RGB blit images with true color via #blitRgbImage', function () {
404 var data = [];
405 for (var i = 0; i < 16; i++) {
406 data[i * 3] = checked_data[i * 4];
407 data[i * 3 + 1] = checked_data[i * 4 + 1];
408 data[i * 3 + 2] = checked_data[i * 4 + 2];
409 }
410 display.blitRgbImage(0, 0, 4, 4, data, 0);
411 display.flip();
412 expect(display).to.have.displayed(checked_data);
413 });
414
415 it('should support drawing an image object via #drawImage', function () {
416 var img = make_image_canvas(checked_data);
417 display.drawImage(img, 0, 0);
418 display.flip();
419 expect(display).to.have.displayed(checked_data);
420 });
421 });
422
423 describe('the render queue processor', function () {
424 var display;
425 beforeEach(function () {
426 display = new Display(document.createElement('canvas'));
427 display.resize(4, 4);
428 sinon.spy(display, '_scan_renderQ');
429 });
430
431 afterEach(function () {
432 window.requestAnimationFrame = this.old_requestAnimationFrame;
433 });
434
435 it('should try to process an item when it is pushed on, if nothing else is on the queue', function () {
436 display._renderQ_push({ type: 'noop' }); // does nothing
437 expect(display._scan_renderQ).to.have.been.calledOnce;
438 });
439
440 it('should not try to process an item when it is pushed on if we are waiting for other items', function () {
441 display._renderQ.length = 2;
442 display._renderQ_push({ type: 'noop' });
443 expect(display._scan_renderQ).to.not.have.been.called;
444 });
445
446 it('should wait until an image is loaded to attempt to draw it and the rest of the queue', function () {
447 var img = { complete: false, addEventListener: sinon.spy() }
448 display._renderQ = [{ type: 'img', x: 3, y: 4, img: img },
449 { type: 'fill', x: 1, y: 2, width: 3, height: 4, color: 5 }];
450 display.drawImage = sinon.spy();
451 display.fillRect = sinon.spy();
452
453 display._scan_renderQ();
454 expect(display.drawImage).to.not.have.been.called;
455 expect(display.fillRect).to.not.have.been.called;
456 expect(img.addEventListener).to.have.been.calledOnce;
457
458 display._renderQ[0].img.complete = true;
459 display._scan_renderQ();
460 expect(display.drawImage).to.have.been.calledOnce;
461 expect(display.fillRect).to.have.been.calledOnce;
462 expect(img.addEventListener).to.have.been.calledOnce;
463 });
464
465 it('should call callback when queue is flushed', function () {
466 display.set_onFlush(sinon.spy());
467 display.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
468 expect(display.get_onFlush()).to.not.have.been.called;
469 display.flush();
470 expect(display.get_onFlush()).to.have.been.calledOnce;
471 });
472
473 it('should draw a blit image on type "blit"', function () {
474 display.blitImage = sinon.spy();
475 display._renderQ_push({ type: 'blit', x: 3, y: 4, width: 5, height: 6, data: [7, 8, 9] });
476 expect(display.blitImage).to.have.been.calledOnce;
477 expect(display.blitImage).to.have.been.calledWith(3, 4, 5, 6, [7, 8, 9], 0);
478 });
479
480 it('should draw a blit RGB image on type "blitRgb"', function () {
481 display.blitRgbImage = sinon.spy();
482 display._renderQ_push({ type: 'blitRgb', x: 3, y: 4, width: 5, height: 6, data: [7, 8, 9] });
483 expect(display.blitRgbImage).to.have.been.calledOnce;
484 expect(display.blitRgbImage).to.have.been.calledWith(3, 4, 5, 6, [7, 8, 9], 0);
485 });
486
487 it('should copy a region on type "copy"', function () {
488 display.copyImage = sinon.spy();
489 display._renderQ_push({ type: 'copy', x: 3, y: 4, width: 5, height: 6, old_x: 7, old_y: 8 });
490 expect(display.copyImage).to.have.been.calledOnce;
491 expect(display.copyImage).to.have.been.calledWith(7, 8, 3, 4, 5, 6);
492 });
493
494 it('should fill a rect with a given color on type "fill"', function () {
495 display.fillRect = sinon.spy();
496 display._renderQ_push({ type: 'fill', x: 3, y: 4, width: 5, height: 6, color: [7, 8, 9]});
497 expect(display.fillRect).to.have.been.calledOnce;
498 expect(display.fillRect).to.have.been.calledWith(3, 4, 5, 6, [7, 8, 9]);
499 });
500
501 it('should draw an image from an image object on type "img" (if complete)', function () {
502 display.drawImage = sinon.spy();
503 display._renderQ_push({ type: 'img', x: 3, y: 4, img: { complete: true } });
504 expect(display.drawImage).to.have.been.calledOnce;
505 expect(display.drawImage).to.have.been.calledWith({ complete: true }, 3, 4);
506 });
507 });
508 });