]>
Commit | Line | Data |
---|---|---|
13d9108f | 1 | // noVNC specific assertions |
2c9623b5 SR |
2 | chai.use(function (_chai, utils) { |
3 | _chai.Assertion.addMethod('displayed', function (target_data) { | |
2b5f94fa JD |
4 | const obj = this._obj; |
5 | const ctx = obj._target.getContext('2d'); | |
6 | const data_cl = ctx.getImageData(0, 0, obj._target.width, obj._target.height).data; | |
2c9623b5 | 7 | // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray, so work around that |
2b5f94fa JD |
8 | const data = new Uint8Array(data_cl); |
9 | const len = data_cl.length; | |
0880353b | 10 | new chai.Assertion(len).to.be.equal(target_data.length, "unexpected display size"); |
2b5f94fa JD |
11 | let same = true; |
12 | for (let i = 0; i < len; i++) { | |
0880353b PO |
13 | if (data[i] != target_data[i]) { |
14 | same = false; | |
15 | break; | |
38781d93 SR |
16 | } |
17 | } | |
18 | if (!same) { | |
8727f598 | 19 | // eslint-disable-next-line no-console |
38781d93 SR |
20 | console.log("expected data: %o, actual data: %o", target_data, data); |
21 | } | |
22 | this.assert(same, | |
7b536961 PO |
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 | target_data, | |
26 | data); | |
2c9623b5 SR |
27 | }); |
28 | ||
29 | _chai.Assertion.addMethod('sent', function (target_data) { | |
2b5f94fa | 30 | const obj = this._obj; |
651c23ec | 31 | obj.inspect = () => { |
2b5f94fa | 32 | const res = { _websocket: obj._websocket, rQi: obj._rQi, _rQ: new Uint8Array(obj._rQ.buffer, 0, obj._rQlen), |
7b536961 | 33 | _sQ: new Uint8Array(obj._sQ.buffer, 0, obj._sQlen) }; |
9ff86fb7 SR |
34 | res.prototype = obj; |
35 | return res; | |
36 | }; | |
2b5f94fa JD |
37 | const data = obj._websocket._get_sent_data(); |
38 | let same = true; | |
b1538a0f | 39 | if (data.length != target_data.length) { |
40 | same = false; | |
41 | } else { | |
2b5f94fa | 42 | for (let i = 0; i < data.length; i++) { |
b1538a0f | 43 | if (data[i] != target_data[i]) { |
44 | same = false; | |
45 | break; | |
46 | } | |
38781d93 SR |
47 | } |
48 | } | |
49 | if (!same) { | |
8727f598 | 50 | // eslint-disable-next-line no-console |
38781d93 SR |
51 | console.log("expected data: %o, actual data: %o", target_data, data); |
52 | } | |
53 | this.assert(same, | |
7b536961 PO |
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(target_data), | |
57 | Array.prototype.slice.call(data)); | |
2c9623b5 | 58 | }); |
38781d93 SR |
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')) { | |
2b5f94fa | 67 | const obj = this._obj; |
38781d93 | 68 | |
2b5f94fa | 69 | let same = true; |
38781d93 SR |
70 | |
71 | if (utils.flag(this, 'deep')) { | |
2b5f94fa | 72 | for (let i = 0; i < obj.length; i++) { |
38781d93 SR |
73 | if (!utils.eql(obj[i], target[i])) { |
74 | same = false; | |
75 | break; | |
76 | } | |
77 | } | |
78 | ||
79 | this.assert(same, | |
7b536961 PO |
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)); | |
38781d93 | 83 | } else { |
2b5f94fa | 84 | for (let i = 0; i < obj.length; i++) { |
38781d93 SR |
85 | if (obj[i] != target[i]) { |
86 | same = false; | |
87 | break; | |
88 | } | |
89 | } | |
90 | ||
91 | this.assert(same, | |
7b536961 PO |
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)); | |
38781d93 SR |
95 | } |
96 | } else { | |
97 | _super.apply(this, arguments); | |
98 | } | |
99 | }; | |
100 | }); | |
2c9623b5 | 101 | }); |