]> git.proxmox.com Git - mirror_novnc.git/blob - tests/assertions.js
noVNC 1.0.0
[mirror_novnc.git] / tests / assertions.js
1 // Assertions that make it easier to use sinon
2 import sinonChai from '../node_modules/sinon-chai/lib/sinon-chai.js';
3 chai.use(sinonChai);
4
5 // noVNC specific assertions
6 chai.use(function (_chai, utils) {
7 _chai.Assertion.addMethod('displayed', function (target_data) {
8 var obj = this._obj;
9 var ctx = obj._target.getContext('2d');
10 var data_cl = ctx.getImageData(0, 0, obj._target.width, obj._target.height).data;
11 // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray, so work around that
12 var data = new Uint8Array(data_cl);
13 var len = data_cl.length;
14 new chai.Assertion(len).to.be.equal(target_data.length, "unexpected display size");
15 var same = true;
16 for (var i = 0; i < len; i++) {
17 if (data[i] != target_data[i]) {
18 same = false;
19 break;
20 }
21 }
22 if (!same) {
23 console.log("expected data: %o, actual data: %o", target_data, data);
24 }
25 this.assert(same,
26 "expected #{this} to have displayed the image #{exp}, but instead it displayed #{act}",
27 "expected #{this} not to have displayed the image #{act}",
28 target_data,
29 data);
30 });
31
32 _chai.Assertion.addMethod('sent', function (target_data) {
33 var obj = this._obj;
34 obj.inspect = function () {
35 var res = { _websocket: obj._websocket, rQi: obj._rQi, _rQ: new Uint8Array(obj._rQ.buffer, 0, obj._rQlen),
36 _sQ: new Uint8Array(obj._sQ.buffer, 0, obj._sQlen) };
37 res.prototype = obj;
38 return res;
39 };
40 var data = obj._websocket._get_sent_data();
41 var same = true;
42 if (data.length != target_data.length) {
43 same = false;
44 } else {
45 for (var i = 0; i < data.length; i++) {
46 if (data[i] != target_data[i]) {
47 same = false;
48 break;
49 }
50 }
51 }
52 if (!same) {
53 console.log("expected data: %o, actual data: %o", target_data, data);
54 }
55 this.assert(same,
56 "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
57 "expected #{this} not to have sent the data #{act}",
58 Array.prototype.slice.call(target_data),
59 Array.prototype.slice.call(data));
60 });
61
62 _chai.Assertion.addProperty('array', function () {
63 utils.flag(this, 'array', true);
64 });
65
66 _chai.Assertion.overwriteMethod('equal', function (_super) {
67 return function assertArrayEqual(target) {
68 if (utils.flag(this, 'array')) {
69 var obj = this._obj;
70
71 var i;
72 var same = true;
73
74 if (utils.flag(this, 'deep')) {
75 for (i = 0; i < obj.length; i++) {
76 if (!utils.eql(obj[i], target[i])) {
77 same = false;
78 break;
79 }
80 }
81
82 this.assert(same,
83 "expected #{this} to have elements deeply equal to #{exp}",
84 "expected #{this} not to have elements deeply equal to #{exp}",
85 Array.prototype.slice.call(target));
86 } else {
87 for (i = 0; i < obj.length; i++) {
88 if (obj[i] != target[i]) {
89 same = false;
90 break;
91 }
92 }
93
94 this.assert(same,
95 "expected #{this} to have elements equal to #{exp}",
96 "expected #{this} not to have elements equal to #{exp}",
97 Array.prototype.slice.call(target));
98 }
99 } else {
100 _super.apply(this, arguments);
101 }
102 };
103 });
104 });