]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.display.js
1 // requires local modules: util, base64, display
2 // requires test modules: assertions
3 /* jshint expr: true */
4 var expect
= chai
.expect
;
6 describe('Display/Canvas Helper', function () {
8 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
9 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
10 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
11 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
13 checked_data
= new Uint8Array(checked_data
);
15 var basic_data
= [0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0xff, 0xff, 0xff, 255];
16 basic_data
= new Uint8Array(basic_data
);
18 function make_image_canvas (input_data
) {
19 var canvas
= document
.createElement('canvas');
22 var ctx
= canvas
.getContext('2d');
23 var data
= ctx
.createImageData(4, 4);
24 for (var i
= 0; i
< checked_data
.length
; i
++) { data
.data
[i
] = input_data
[i
]; }
25 ctx
.putImageData(data
, 0, 0);
29 describe('viewport handling', function () {
31 beforeEach(function () {
32 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false, viewport
: true });
34 display
.viewportChange(1, 1, 3, 3);
35 display
.getCleanDirtyReset();
38 it('should take viewport location into consideration when drawing images', function () {
40 display
.viewportChange(0, 0, 2, 2);
41 display
.drawImage(make_image_canvas(basic_data
), 1, 1);
43 var expected
= new Uint8Array(16);
45 for (i
= 0; i
< 8; i
++) { expected
[i
] = basic_data
[i
]; }
46 for (i
= 8; i
< 16; i
++) { expected
[i
] = 0; }
47 expect(display
).to
.have
.displayed(expected
);
50 it('should redraw the left side when shifted left', function () {
51 display
.viewportChange(-1, 0, 3, 3);
52 var cdr
= display
.getCleanDirtyReset();
53 expect(cdr
.cleanBox
).to
.deep
.equal({ x
: 1, y
: 1, w
: 2, h
: 3 });
54 expect(cdr
.dirtyBoxes
).to
.have
.length(1);
55 expect(cdr
.dirtyBoxes
[0]).to
.deep
.equal({ x
: 0, y
: 1, w
: 2, h
: 3 });
58 it('should redraw the right side when shifted right', function () {
59 display
.viewportChange(1, 0, 3, 3);
60 var cdr
= display
.getCleanDirtyReset();
61 expect(cdr
.cleanBox
).to
.deep
.equal({ x
: 2, y
: 1, w
: 2, h
: 3 });
62 expect(cdr
.dirtyBoxes
).to
.have
.length(1);
63 expect(cdr
.dirtyBoxes
[0]).to
.deep
.equal({ x
: 4, y
: 1, w
: 1, h
: 3 });
66 it('should redraw the top part when shifted up', function () {
67 display
.viewportChange(0, -1, 3, 3);
68 var cdr
= display
.getCleanDirtyReset();
69 expect(cdr
.cleanBox
).to
.deep
.equal({ x
: 1, y
: 1, w
: 3, h
: 2 });
70 expect(cdr
.dirtyBoxes
).to
.have
.length(1);
71 expect(cdr
.dirtyBoxes
[0]).to
.deep
.equal({ x
: 1, y
: 0, w
: 3, h
: 1 });
74 it('should redraw the bottom part when shifted down', function () {
75 display
.viewportChange(0, 1, 3, 3);
76 var cdr
= display
.getCleanDirtyReset();
77 expect(cdr
.cleanBox
).to
.deep
.equal({ x
: 1, y
: 2, w
: 3, h
: 2 });
78 expect(cdr
.dirtyBoxes
).to
.have
.length(1);
79 expect(cdr
.dirtyBoxes
[0]).to
.deep
.equal({ x
: 1, y
: 4, w
: 3, h
: 1 });
82 it('should reset the entire viewport to being clean after calculating the clean/dirty boxes', function () {
83 display
.viewportChange(0, 1, 3, 3);
84 var cdr1
= display
.getCleanDirtyReset();
85 var cdr2
= display
.getCleanDirtyReset();
86 expect(cdr1
).to
.not
.deep
.equal(cdr2
);
87 expect(cdr2
.cleanBox
).to
.deep
.equal({ x
: 1, y
: 2, w
: 3, h
: 3 });
88 expect(cdr2
.dirtyBoxes
).to
.be
.empty
;
91 it('should simply mark the whole display area as dirty if not using viewports', function () {
92 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false, viewport
: false });
94 var cdr
= display
.getCleanDirtyReset();
95 expect(cdr
.cleanBox
).to
.deep
.equal({ x
: 0, y
: 0, w
: 0, h
: 0 });
96 expect(cdr
.dirtyBoxes
).to
.have
.length(1);
97 expect(cdr
.dirtyBoxes
[0]).to
.deep
.equal({ x
: 0, y
: 0, w
: 5, h
: 5 });
101 describe('resizing', function () {
103 beforeEach(function () {
104 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false, viewport
: true });
105 display
.resize(4, 3);
108 it('should change the size of the logical canvas', function () {
109 display
.resize(5, 7);
110 expect(display
._fb_width
).to
.equal(5);
111 expect(display
._fb_height
).to
.equal(7);
114 it('should update the viewport dimensions', function () {
115 sinon
.spy(display
, 'viewportChange');
116 display
.resize(2, 2);
117 expect(display
.viewportChange
).to
.have
.been
.calledOnce
;
121 describe('drawing', function () {
123 // TODO(directxman12): improve the tests for each of the drawing functions to cover more than just the
125 function drawing_tests (pref_js
) {
127 beforeEach(function () {
128 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: pref_js
});
129 display
.resize(4, 4);
132 it('should clear the screen on #clear without a logo set', function () {
133 display
.fillRect(0, 0, 4, 4, [0x00, 0x00, 0xff]);
134 display
._logo
= null;
136 display
.resize(4, 4);
138 for (var i
= 0; i
< 4 * display
._fb_width
* display
._fb_height
; i
++) { empty
[i
] = 0; }
139 expect(display
).to
.have
.displayed(new Uint8Array(empty
));
142 it('should draw the logo on #clear with a logo set', function (done
) {
143 display
._logo
= { width
: 4, height
: 4, data
: make_image_canvas(checked_data
).toDataURL() };
144 display
._drawCtx
._act_drawImg
= display
._drawCtx
.drawImage
;
145 display
._drawCtx
.drawImage = function (img
, x
, y
) {
146 this._act_drawImg(img
, x
, y
);
147 expect(display
).to
.have
.displayed(checked_data
);
151 expect(display
._fb_width
).to
.equal(4);
152 expect(display
._fb_height
).to
.equal(4);
155 it('should support filling a rectangle with particular color via #fillRect', function () {
156 display
.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
157 display
.fillRect(0, 0, 2, 2, [0xff, 0, 0]);
158 display
.fillRect(2, 2, 2, 2, [0xff, 0, 0]);
159 expect(display
).to
.have
.displayed(checked_data
);
162 it('should support copying an portion of the canvas via #copyImage', function () {
163 display
.fillRect(0, 0, 4, 4, [0, 0xff, 0]);
164 display
.fillRect(0, 0, 2, 2, [0xff, 0, 0x00]);
165 display
.copyImage(0, 0, 2, 2, 2, 2);
166 expect(display
).to
.have
.displayed(checked_data
);
169 it('should support drawing tile data with a background color and sub tiles', function () {
170 display
.startTile(0, 0, 4, 4, [0, 0xff, 0]);
171 display
.subTile(0, 0, 2, 2, [0xff, 0, 0]);
172 display
.subTile(2, 2, 2, 2, [0xff, 0, 0]);
173 display
.finishTile();
174 expect(display
).to
.have
.displayed(checked_data
);
177 it('should support drawing BGRX blit images with true color via #blitImage', function () {
179 for (var i
= 0; i
< 16; i
++) {
180 data
[i
* 4] = checked_data
[i
* 4 + 2];
181 data
[i
* 4 + 1] = checked_data
[i
* 4 + 1];
182 data
[i
* 4 + 2] = checked_data
[i
* 4];
183 data
[i
* 4 + 3] = checked_data
[i
* 4 + 3];
185 display
.blitImage(0, 0, 4, 4, data
, 0);
186 expect(display
).to
.have
.displayed(checked_data
);
189 it('should support drawing RGB blit images with true color via #blitRgbImage', function () {
191 for (var i
= 0; i
< 16; i
++) {
192 data
[i
* 3] = checked_data
[i
* 4];
193 data
[i
* 3 + 1] = checked_data
[i
* 4 + 1];
194 data
[i
* 3 + 2] = checked_data
[i
* 4 + 2];
196 display
.blitRgbImage(0, 0, 4, 4, data
, 0);
197 expect(display
).to
.have
.displayed(checked_data
);
200 it('should support drawing blit images from a data URL via #blitStringImage', function (done
) {
201 var img_url
= make_image_canvas(checked_data
).toDataURL();
202 display
._drawCtx
._act_drawImg
= display
._drawCtx
.drawImage
;
203 display
._drawCtx
.drawImage = function (img
, x
, y
) {
204 this._act_drawImg(img
, x
, y
);
205 expect(display
).to
.have
.displayed(checked_data
);
208 display
.blitStringImage(img_url
, 0, 0);
211 it('should support drawing solid colors with color maps', function () {
212 display
._true_color
= false;
213 display
.set_colourMap({ 0: [0xff, 0, 0], 1: [0, 0xff, 0] });
214 display
.fillRect(0, 0, 4, 4, [1]);
215 display
.fillRect(0, 0, 2, 2, [0]);
216 display
.fillRect(2, 2, 2, 2, [0]);
217 expect(display
).to
.have
.displayed(checked_data
);
220 it('should support drawing blit images with color maps', function () {
221 display
._true_color
= false;
222 display
.set_colourMap({ 1: [0xff, 0, 0], 0: [0, 0xff, 0] });
223 var data
= [1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1].map(function (elem
) { return [elem
]; });
224 display
.blitImage(0, 0, 4, 4, data
, 0);
225 expect(display
).to
.have
.displayed(checked_data
);
228 it('should support drawing an image object via #drawImage', function () {
229 var img
= make_image_canvas(checked_data
);
230 display
.drawImage(img
, 0, 0);
231 expect(display
).to
.have
.displayed(checked_data
);
235 describe('(prefering native methods)', function () { drawing_tests
.call(this, false); });
236 describe('(prefering JavaScript)', function () { drawing_tests
.call(this, true); });
239 describe('the render queue processor', function () {
241 beforeEach(function () {
242 display
= new Display({ target
: document
.createElement('canvas'), prefer_js
: false });
243 display
.resize(4, 4);
244 sinon
.spy(display
, '_scan_renderQ');
245 this.old_requestAnimFrame
= window
.requestAnimFrame
;
246 window
.requestAnimFrame = function (cb
) {
247 this.next_frame_cb
= cb
;
249 this.next_frame = function () { this.next_frame_cb(); };
252 afterEach(function () {
253 window
.requestAnimFrame
= this.old_requestAnimFrame
;
256 it('should try to process an item when it is pushed on, if nothing else is on the queue', function () {
257 display
.renderQ_push({ type
: 'noop' }); // does nothing
258 expect(display
._scan_renderQ
).to
.have
.been
.calledOnce
;
261 it('should not try to process an item when it is pushed on if we are waiting for other items', function () {
262 display
._renderQ
.length
= 2;
263 display
.renderQ_push({ type
: 'noop' });
264 expect(display
._scan_renderQ
).to
.not
.have
.been
.called
;
267 it('should wait until an image is loaded to attempt to draw it and the rest of the queue', function () {
268 var img
= { complete
: false };
269 display
._renderQ
= [{ type
: 'img', x
: 3, y
: 4, img
: img
},
270 { type
: 'fill', x
: 1, y
: 2, width
: 3, height
: 4, color
: 5 }];
271 display
.drawImage
= sinon
.spy();
272 display
.fillRect
= sinon
.spy();
274 display
._scan_renderQ();
275 expect(display
.drawImage
).to
.not
.have
.been
.called
;
276 expect(display
.fillRect
).to
.not
.have
.been
.called
;
278 display
._renderQ
[0].img
.complete
= true;
280 expect(display
.drawImage
).to
.have
.been
.calledOnce
;
281 expect(display
.fillRect
).to
.have
.been
.calledOnce
;
284 it('should draw a blit image on type "blit"', function () {
285 display
.blitImage
= sinon
.spy();
286 display
.renderQ_push({ type
: 'blit', x
: 3, y
: 4, width
: 5, height
: 6, data
: [7, 8, 9] });
287 expect(display
.blitImage
).to
.have
.been
.calledOnce
;
288 expect(display
.blitImage
).to
.have
.been
.calledWith(3, 4, 5, 6, [7, 8, 9], 0);
291 it('should draw a blit RGB image on type "blitRgb"', function () {
292 display
.blitRgbImage
= sinon
.spy();
293 display
.renderQ_push({ type
: 'blitRgb', x
: 3, y
: 4, width
: 5, height
: 6, data
: [7, 8, 9] });
294 expect(display
.blitRgbImage
).to
.have
.been
.calledOnce
;
295 expect(display
.blitRgbImage
).to
.have
.been
.calledWith(3, 4, 5, 6, [7, 8, 9], 0);
298 it('should copy a region on type "copy"', function () {
299 display
.copyImage
= sinon
.spy();
300 display
.renderQ_push({ type
: 'copy', x
: 3, y
: 4, width
: 5, height
: 6, old_x
: 7, old_y
: 8 });
301 expect(display
.copyImage
).to
.have
.been
.calledOnce
;
302 expect(display
.copyImage
).to
.have
.been
.calledWith(7, 8, 3, 4, 5, 6);
305 it('should fill a rect with a given color on type "fill"', function () {
306 display
.fillRect
= sinon
.spy();
307 display
.renderQ_push({ type
: 'fill', x
: 3, y
: 4, width
: 5, height
: 6, color
: [7, 8, 9]});
308 expect(display
.fillRect
).to
.have
.been
.calledOnce
;
309 expect(display
.fillRect
).to
.have
.been
.calledWith(3, 4, 5, 6, [7, 8, 9]);
312 it('should draw an image from an image object on type "img" (if complete)', function () {
313 display
.drawImage
= sinon
.spy();
314 display
.renderQ_push({ type
: 'img', x
: 3, y
: 4, img
: { complete
: true } });
315 expect(display
.drawImage
).to
.have
.been
.calledOnce
;
316 expect(display
.drawImage
).to
.have
.been
.calledWith({ complete
: true }, 3, 4);