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