1 /* jshint expr: true */
2 var expect
= chai
.expect
;
4 import Base64
from '../core/base64.js';
5 import Display
from '../core/display.js';
6 import { _forceCursorURIs
, browserSupportsCursorURIs
} from '../core/util/browsers.js';
8 import './assertions.js';
10 import sinonChai
from '../node_modules/sinon-chai/lib/sinon-chai.js'
13 describe('Display/Canvas Helper', function () {
15 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
16 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
17 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
18 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
20 checked_data
= new Uint8Array(checked_data
);
22 var basic_data
= [0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0xff, 0xff, 0xff, 255];
23 basic_data
= new Uint8Array(basic_data
);
25 function make_image_canvas (input_data
) {
26 var canvas
= document
.createElement('canvas');
29 var ctx
= canvas
.getContext('2d');
30 var data
= ctx
.createImageData(4, 4);
31 for (var i
= 0; i
< checked_data
.length
; i
++) { data
.data
[i
] = input_data
[i
]; }
32 ctx
.putImageData(data
, 0, 0);
36 function make_image_png (input_data
) {
37 var canvas
= make_image_canvas(input_data
);
38 var url
= canvas
.toDataURL();
39 var data
= url
.split(",")[1];
40 return Base64
.decode(data
);
43 describe('checking for cursor uri support', function () {
44 it('should disable cursor URIs if there is no support', function () {
45 _forceCursorURIs(false);
46 var display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: true, viewport
: false });
47 expect(display
._cursor_uri
).to
.be
.false;
50 it('should enable cursor URIs if there is support', function () {
51 _forceCursorURIs(true);
52 var display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: true, viewport
: false });
53 expect(display
._cursor_uri
).to
.be
.true;
56 it('respect the cursor_uri option if there is support', function () {
57 _forceCursorURIs(false);
58 var display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: true, viewport
: false, cursor_uri
: false });
59 expect(display
._cursor_uri
).to
.be
.false;
63 describe('viewport handling', function () {
65 beforeEach(function () {
66 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false, viewport
: true });
68 display
.viewportChangeSize(3, 3);
69 display
.viewportChangePos(1, 1);
72 it('should take viewport location into consideration when drawing images', function () {
74 display
.viewportChangeSize(2, 2);
75 display
.drawImage(make_image_canvas(basic_data
), 1, 1);
78 var expected
= new Uint8Array(16);
80 for (i
= 0; i
< 8; i
++) { expected
[i
] = basic_data
[i
]; }
81 for (i
= 8; i
< 16; i
++) { expected
[i
] = 0; }
82 expect(display
).to
.have
.displayed(expected
);
85 it('should resize the target canvas when resizing the viewport', function() {
86 display
.viewportChangeSize(2, 2);
87 expect(display
._target
.width
).to
.equal(2);
88 expect(display
._target
.height
).to
.equal(2);
91 it('should move the viewport if necessary', function() {
92 display
.viewportChangeSize(5, 5);
93 expect(display
.absX(0)).to
.equal(0);
94 expect(display
.absY(0)).to
.equal(0);
95 expect(display
._target
.width
).to
.equal(5);
96 expect(display
._target
.height
).to
.equal(5);
99 it('should limit the viewport to the framebuffer size', function() {
100 display
.viewportChangeSize(6, 6);
101 expect(display
._target
.width
).to
.equal(5);
102 expect(display
._target
.height
).to
.equal(5);
105 it('should redraw when moving the viewport', function () {
106 display
.flip
= sinon
.spy();
107 display
.viewportChangePos(-1, 1);
108 expect(display
.flip
).to
.have
.been
.calledOnce
;
111 it('should redraw when resizing the viewport', function () {
112 display
.flip
= sinon
.spy();
113 display
.viewportChangeSize(2, 2);
114 expect(display
.flip
).to
.have
.been
.calledOnce
;
117 it('should report clipping when framebuffer > viewport', function () {
118 var clipping
= display
.clippingDisplay();
119 expect(clipping
).to
.be
.true;
122 it('should report not clipping when framebuffer = viewport', function () {
123 display
.viewportChangeSize(5, 5);
124 var clipping
= display
.clippingDisplay();
125 expect(clipping
).to
.be
.false;
128 it('should show the entire framebuffer when disabling the viewport', function() {
129 display
.set_viewport(false);
130 expect(display
.absX(0)).to
.equal(0);
131 expect(display
.absY(0)).to
.equal(0);
132 expect(display
._target
.width
).to
.equal(5);
133 expect(display
._target
.height
).to
.equal(5);
136 it('should ignore viewport changes when the viewport is disabled', function() {
137 display
.set_viewport(false);
138 display
.viewportChangeSize(2, 2);
139 display
.viewportChangePos(1, 1);
140 expect(display
.absX(0)).to
.equal(0);
141 expect(display
.absY(0)).to
.equal(0);
142 expect(display
._target
.width
).to
.equal(5);
143 expect(display
._target
.height
).to
.equal(5);
146 it('should show the entire framebuffer just after enabling the viewport', function() {
147 display
.set_viewport(false);
148 display
.set_viewport(true);
149 expect(display
.absX(0)).to
.equal(0);
150 expect(display
.absY(0)).to
.equal(0);
151 expect(display
._target
.width
).to
.equal(5);
152 expect(display
._target
.height
).to
.equal(5);
156 describe('resizing', function () {
158 beforeEach(function () {
159 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false, viewport
: false });
160 display
.resize(4, 4);
163 it('should change the size of the logical canvas', function () {
164 display
.resize(5, 7);
165 expect(display
._fb_width
).to
.equal(5);
166 expect(display
._fb_height
).to
.equal(7);
169 it('should keep the framebuffer data', function () {
170 display
.fillRect(0, 0, 4, 4, [0, 0, 0xff]);
171 display
.resize(2, 2);
174 for (var i
= 0; i
< 4 * 2*2; i
+= 4) {
176 expected
[i
+1] = expected
[i
+2] = 0;
177 expected
[i
+3] = 0xff;
179 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
182 describe('viewport', function () {
183 beforeEach(function () {
184 display
.set_viewport(true);
185 display
.viewportChangeSize(3, 3);
186 display
.viewportChangePos(1, 1);
189 it('should keep the viewport position and size if possible', function () {
190 display
.resize(6, 6);
191 expect(display
.absX(0)).to
.equal(1);
192 expect(display
.absY(0)).to
.equal(1);
193 expect(display
._target
.width
).to
.equal(3);
194 expect(display
._target
.height
).to
.equal(3);
197 it('should move the viewport if necessary', function () {
198 display
.resize(3, 3);
199 expect(display
.absX(0)).to
.equal(0);
200 expect(display
.absY(0)).to
.equal(0);
201 expect(display
._target
.width
).to
.equal(3);
202 expect(display
._target
.height
).to
.equal(3);
205 it('should shrink the viewport if necessary', function () {
206 display
.resize(2, 2);
207 expect(display
.absX(0)).to
.equal(0);
208 expect(display
.absY(0)).to
.equal(0);
209 expect(display
._target
.width
).to
.equal(2);
210 expect(display
._target
.height
).to
.equal(2);
215 describe('rescaling', function () {
219 beforeEach(function () {
220 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false, viewport
: true });
221 display
.resize(4, 4);
222 display
.viewportChangeSize(3, 3);
223 display
.viewportChangePos(1, 1);
224 canvas
= display
.get_target();
225 document
.body
.appendChild(canvas
);
228 afterEach(function () {
229 document
.body
.removeChild(canvas
);
232 it('should not change the bitmap size of the canvas', function () {
233 display
.set_scale(2.0);
234 expect(canvas
.width
).to
.equal(3);
235 expect(canvas
.height
).to
.equal(3);
238 it('should change the effective rendered size of the canvas', function () {
239 display
.set_scale(2.0);
240 expect(canvas
.clientWidth
).to
.equal(6);
241 expect(canvas
.clientHeight
).to
.equal(6);
244 it('should not change when resizing', function () {
245 display
.set_scale(2.0);
246 display
.resize(5, 5);
247 expect(display
.get_scale()).to
.equal(2.0);
248 expect(canvas
.width
).to
.equal(3);
249 expect(canvas
.height
).to
.equal(3);
250 expect(canvas
.clientWidth
).to
.equal(6);
251 expect(canvas
.clientHeight
).to
.equal(6);
255 describe('autoscaling', function () {
259 beforeEach(function () {
260 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false, viewport
: true });
261 display
.resize(4, 3);
262 canvas
= display
.get_target();
263 document
.body
.appendChild(canvas
);
266 afterEach(function () {
267 document
.body
.removeChild(canvas
);
270 it('should preserve aspect ratio while autoscaling', function () {
271 display
.autoscale(16, 9);
272 expect(canvas
.clientWidth
/ canvas
.clientHeight
).to
.equal(4 / 3);
275 it('should use width to determine scale when the current aspect ratio is wider than the target', function () {
276 display
.autoscale(9, 16);
277 expect(display
.absX(9)).to
.equal(4);
278 expect(display
.absY(18)).to
.equal(8);
279 expect(canvas
.clientWidth
).to
.equal(9);
280 expect(canvas
.clientHeight
).to
.equal(7); // round 9 / (4 / 3)
283 it('should use height to determine scale when the current aspect ratio is taller than the target', function () {
284 display
.autoscale(16, 9);
285 expect(display
.absX(9)).to
.equal(3);
286 expect(display
.absY(18)).to
.equal(6);
287 expect(canvas
.clientWidth
).to
.equal(12); // 16 * (4 / 3)
288 expect(canvas
.clientHeight
).to
.equal(9);
292 it('should not change the bitmap size of the canvas', function () {
293 display
.autoscale(16, 9);
294 expect(canvas
.width
).to
.equal(4);
295 expect(canvas
.height
).to
.equal(3);
298 it('should not upscale when downscaleOnly is true', function () {
299 display
.autoscale(2, 2, true);
300 expect(display
.absX(9)).to
.equal(18);
301 expect(display
.absY(18)).to
.equal(36);
302 expect(canvas
.clientWidth
).to
.equal(2);
303 expect(canvas
.clientHeight
).to
.equal(2);
305 display
.autoscale(16, 9, true);
306 expect(display
.absX(9)).to
.equal(9);
307 expect(display
.absY(18)).to
.equal(18);
308 expect(canvas
.clientWidth
).to
.equal(4);
309 expect(canvas
.clientHeight
).to
.equal(3);
313 describe('drawing', function () {
315 // TODO(directxman12): improve the tests for each of the drawing functions to cover more than just the
317 function drawing_tests (pref_js
) {
319 beforeEach(function () {
320 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: pref_js
});
321 display
.resize(4, 4);
324 it('should clear the screen on #clear without a logo set', function () {
325 display
.fillRect(0, 0, 4, 4, [0x00, 0x00, 0xff]);
326 display
._logo
= null;
328 display
.resize(4, 4);
330 for (var i
= 0; i
< 4 * display
._fb_width
* display
._fb_height
; i
++) { empty
[i
] = 0; }
331 expect(display
).to
.have
.displayed(new Uint8Array(empty
));
334 it('should draw the logo on #clear with a logo set', function (done
) {
335 display
._logo
= { width
: 4, height
: 4, type
: "image/png", data
: make_image_png(checked_data
) };
337 display
.set_onFlush(function () {
338 expect(display
).to
.have
.displayed(checked_data
);
339 expect(display
._fb_width
).to
.equal(4);
340 expect(display
._fb_height
).to
.equal(4);
346 it('should not draw directly on the target canvas', function () {
347 display
.fillRect(0, 0, 4, 4, [0, 0, 0xff]);
349 display
.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
351 for (var i
= 0; i
< 4 * display
._fb_width
* display
._fb_height
; i
+= 4) {
353 expected
[i
+1] = expected
[i
+2] = 0;
354 expected
[i
+3] = 0xff;
356 expect(display
).to
.have
.displayed(new Uint8Array(expected
));
359 it('should support filling a rectangle with particular color via #fillRect', function () {
360 display
.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
361 display
.fillRect(0, 0, 2, 2, [0xff, 0, 0]);
362 display
.fillRect(2, 2, 2, 2, [0xff, 0, 0]);
364 expect(display
).to
.have
.displayed(checked_data
);
367 it('should support copying an portion of the canvas via #copyImage', function () {
368 display
.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
369 display
.fillRect(0, 0, 2, 2, [0xff, 0, 0x00]);
370 display
.copyImage(0, 0, 2, 2, 2, 2);
372 expect(display
).to
.have
.displayed(checked_data
);
375 it('should support drawing images via #imageRect', function (done
) {
376 display
.imageRect(0, 0, "image/png", make_image_png(checked_data
));
378 display
.set_onFlush(function () {
379 expect(display
).to
.have
.displayed(checked_data
);
385 it('should support drawing tile data with a background color and sub tiles', function () {
386 display
.startTile(0, 0, 4, 4, [0, 0xff, 0]);
387 display
.subTile(0, 0, 2, 2, [0xff, 0, 0]);
388 display
.subTile(2, 2, 2, 2, [0xff, 0, 0]);
389 display
.finishTile();
391 expect(display
).to
.have
.displayed(checked_data
);
394 it('should support drawing BGRX blit images with true color via #blitImage', function () {
396 for (var i
= 0; i
< 16; i
++) {
397 data
[i
* 4] = checked_data
[i
* 4 + 2];
398 data
[i
* 4 + 1] = checked_data
[i
* 4 + 1];
399 data
[i
* 4 + 2] = checked_data
[i
* 4];
400 data
[i
* 4 + 3] = checked_data
[i
* 4 + 3];
402 display
.blitImage(0, 0, 4, 4, data
, 0);
404 expect(display
).to
.have
.displayed(checked_data
);
407 it('should support drawing RGB blit images with true color via #blitRgbImage', function () {
409 for (var i
= 0; i
< 16; i
++) {
410 data
[i
* 3] = checked_data
[i
* 4];
411 data
[i
* 3 + 1] = checked_data
[i
* 4 + 1];
412 data
[i
* 3 + 2] = checked_data
[i
* 4 + 2];
414 display
.blitRgbImage(0, 0, 4, 4, data
, 0);
416 expect(display
).to
.have
.displayed(checked_data
);
419 it('should support drawing an image object via #drawImage', function () {
420 var img
= make_image_canvas(checked_data
);
421 display
.drawImage(img
, 0, 0);
423 expect(display
).to
.have
.displayed(checked_data
);
427 describe('(prefering native methods)', function () { drawing_tests
.call(this, false); });
428 describe('(prefering JavaScript)', function () { drawing_tests
.call(this, true); });
431 describe('the render queue processor', function () {
433 beforeEach(function () {
434 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false });
435 display
.resize(4, 4);
436 sinon
.spy(display
, '_scan_renderQ');
439 afterEach(function () {
440 window
.requestAnimationFrame
= this.old_requestAnimationFrame
;
443 it('should try to process an item when it is pushed on, if nothing else is on the queue', function () {
444 display
._renderQ_push({ type
: 'noop' }); // does nothing
445 expect(display
._scan_renderQ
).to
.have
.been
.calledOnce
;
448 it('should not try to process an item when it is pushed on if we are waiting for other items', function () {
449 display
._renderQ
.length
= 2;
450 display
._renderQ_push({ type
: 'noop' });
451 expect(display
._scan_renderQ
).to
.not
.have
.been
.called
;
454 it('should wait until an image is loaded to attempt to draw it and the rest of the queue', function () {
455 var img
= { complete
: false, addEventListener
: sinon
.spy() }
456 display
._renderQ
= [{ type
: 'img', x
: 3, y
: 4, img
: img
},
457 { type
: 'fill', x
: 1, y
: 2, width
: 3, height
: 4, color
: 5 }];
458 display
.drawImage
= sinon
.spy();
459 display
.fillRect
= sinon
.spy();
461 display
._scan_renderQ();
462 expect(display
.drawImage
).to
.not
.have
.been
.called
;
463 expect(display
.fillRect
).to
.not
.have
.been
.called
;
464 expect(img
.addEventListener
).to
.have
.been
.calledOnce
;
466 display
._renderQ
[0].img
.complete
= true;
467 display
._scan_renderQ();
468 expect(display
.drawImage
).to
.have
.been
.calledOnce
;
469 expect(display
.fillRect
).to
.have
.been
.calledOnce
;
470 expect(img
.addEventListener
).to
.have
.been
.calledOnce
;
473 it('should call callback when queue is flushed', function () {
474 display
.set_onFlush(sinon
.spy());
475 display
.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
476 expect(display
.get_onFlush()).to
.not
.have
.been
.called
;
478 expect(display
.get_onFlush()).to
.have
.been
.calledOnce
;
481 it('should draw a blit image on type "blit"', function () {
482 display
.blitImage
= sinon
.spy();
483 display
._renderQ_push({ type
: 'blit', x
: 3, y
: 4, width
: 5, height
: 6, data
: [7, 8, 9] });
484 expect(display
.blitImage
).to
.have
.been
.calledOnce
;
485 expect(display
.blitImage
).to
.have
.been
.calledWith(3, 4, 5, 6, [7, 8, 9], 0);
488 it('should draw a blit RGB image on type "blitRgb"', function () {
489 display
.blitRgbImage
= sinon
.spy();
490 display
._renderQ_push({ type
: 'blitRgb', x
: 3, y
: 4, width
: 5, height
: 6, data
: [7, 8, 9] });
491 expect(display
.blitRgbImage
).to
.have
.been
.calledOnce
;
492 expect(display
.blitRgbImage
).to
.have
.been
.calledWith(3, 4, 5, 6, [7, 8, 9], 0);
495 it('should copy a region on type "copy"', function () {
496 display
.copyImage
= sinon
.spy();
497 display
._renderQ_push({ type
: 'copy', x
: 3, y
: 4, width
: 5, height
: 6, old_x
: 7, old_y
: 8 });
498 expect(display
.copyImage
).to
.have
.been
.calledOnce
;
499 expect(display
.copyImage
).to
.have
.been
.calledWith(7, 8, 3, 4, 5, 6);
502 it('should fill a rect with a given color on type "fill"', function () {
503 display
.fillRect
= sinon
.spy();
504 display
._renderQ_push({ type
: 'fill', x
: 3, y
: 4, width
: 5, height
: 6, color
: [7, 8, 9]});
505 expect(display
.fillRect
).to
.have
.been
.calledOnce
;
506 expect(display
.fillRect
).to
.have
.been
.calledWith(3, 4, 5, 6, [7, 8, 9]);
509 it('should draw an image from an image object on type "img" (if complete)', function () {
510 display
.drawImage
= sinon
.spy();
511 display
._renderQ_push({ type
: 'img', x
: 3, y
: 4, img
: { complete
: true } });
512 expect(display
.drawImage
).to
.have
.been
.calledOnce
;
513 expect(display
.drawImage
).to
.have
.been
.calledWith({ complete
: true }, 3, 4);