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