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