]>
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; | |
5 | var data_cl = obj._drawCtx.getImageData(0, 0, obj._viewportLoc.w, obj._viewportLoc.h).data; | |
6 | // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray, so work around that | |
7 | var data = new Uint8Array(data_cl); | |
38781d93 | 8 | var same = true; |
bb180145 SR |
9 | var len = data_cl.length; |
10 | if (len != target_data.length) { | |
11 | same = false; | |
12 | } else { | |
13 | for (var i = 0; i < len; i++) { | |
14 | if (data[i] != target_data[i]) { | |
15 | same = false; | |
16 | break; | |
17 | } | |
38781d93 SR |
18 | } |
19 | } | |
20 | if (!same) { | |
21 | console.log("expected data: %o, actual data: %o", target_data, data); | |
22 | } | |
23 | this.assert(same, | |
2c9623b5 SR |
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 | target_data, | |
27 | data); | |
28 | }); | |
29 | ||
30 | _chai.Assertion.addMethod('sent', function (target_data) { | |
31 | var obj = this._obj; | |
9ff86fb7 SR |
32 | obj.inspect = function () { |
33 | var 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 | }; | |
2c9623b5 | 38 | var data = obj._websocket._get_sent_data(); |
38781d93 | 39 | var same = true; |
b1538a0f | 40 | if (data.length != target_data.length) { |
41 | same = false; | |
42 | } else { | |
43 | for (var i = 0; i < data.length; i++) { | |
44 | if (data[i] != target_data[i]) { | |
45 | same = false; | |
46 | break; | |
47 | } | |
38781d93 SR |
48 | } |
49 | } | |
50 | if (!same) { | |
51 | console.log("expected data: %o, actual data: %o", target_data, data); | |
52 | } | |
53 | this.assert(same, | |
2c9623b5 SR |
54 | "expected #{this} to have sent the data #{exp}, but it actually sent #{act}", |
55 | "expected #{this} not to have sent the data #{act}", | |
9ff86fb7 SR |
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')) { | |
67 | var obj = this._obj; | |
68 | ||
69 | var i; | |
70 | var same = true; | |
71 | ||
72 | if (utils.flag(this, 'deep')) { | |
73 | for (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 (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 | }); | |
2c9623b5 | 102 | }); |