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