]> git.proxmox.com Git - mirror_novnc.git/blob - tests/assertions.js
Tests: Fixed bug in displayed assertion
[mirror_novnc.git] / tests / assertions.js
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);
8 var same = true;
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 }
18 }
19 }
20 if (!same) {
21 console.log("expected data: %o, actual data: %o", target_data, data);
22 }
23 this.assert(same,
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;
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 };
38 var data = obj._websocket._get_sent_data();
39 var same = true;
40 for (var i = 0; i < obj.length; i++) {
41 if (data[i] != target_data[i]) {
42 same = false;
43 break;
44 }
45 }
46 if (!same) {
47 console.log("expected data: %o, actual data: %o", target_data, data);
48 }
49 this.assert(same,
50 "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
51 "expected #{this} not to have sent the data #{act}",
52 Array.prototype.slice.call(target_data),
53 Array.prototype.slice.call(data));
54 });
55
56 _chai.Assertion.addProperty('array', function () {
57 utils.flag(this, 'array', true);
58 });
59
60 _chai.Assertion.overwriteMethod('equal', function (_super) {
61 return function assertArrayEqual(target) {
62 if (utils.flag(this, 'array')) {
63 var obj = this._obj;
64
65 var i;
66 var same = true;
67
68 if (utils.flag(this, 'deep')) {
69 for (i = 0; i < obj.length; i++) {
70 if (!utils.eql(obj[i], target[i])) {
71 same = false;
72 break;
73 }
74 }
75
76 this.assert(same,
77 "expected #{this} to have elements deeply equal to #{exp}",
78 "expected #{this} not to have elements deeply equal to #{exp}",
79 Array.prototype.slice.call(target));
80 } else {
81 for (i = 0; i < obj.length; i++) {
82 if (obj[i] != target[i]) {
83 same = false;
84 break;
85 }
86 }
87
88 this.assert(same,
89 "expected #{this} to have elements equal to #{exp}",
90 "expected #{this} not to have elements equal to #{exp}",
91 Array.prototype.slice.call(target));
92 }
93 } else {
94 _super.apply(this, arguments);
95 }
96 };
97 });
98 });