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