Files
noVNC/tests/assertions.js
T

97 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-09-28 14:10:19 +02:00
// noVNC specific assertions
2014-09-19 12:16:06 -04:00
chai.use(function (_chai, utils) {
2020-09-04 16:48:44 +02:00
function _equal(a, b) {
return a === b;
}
_chai.Assertion.addMethod('displayed', function (targetData, cmp=_equal) {
2018-05-24 00:27:09 +03:00
const obj = this._obj;
const ctx = obj._target.getContext('2d');
const data = ctx.getImageData(0, 0, obj._target.width, obj._target.height).data;
const len = data.length;
2020-05-31 21:03:38 +02:00
new chai.Assertion(len).to.be.equal(targetData.length, "unexpected display size");
2018-05-24 00:27:09 +03:00
let same = true;
for (let i = 0; i < len; i++) {
2020-09-04 16:48:44 +02:00
if (!cmp(data[i], targetData[i])) {
same = false;
break;
}
}
if (!same) {
2018-05-24 00:25:44 +03:00
// eslint-disable-next-line no-console
2020-05-31 21:03:38 +02:00
console.log("expected data: %o, actual data: %o", targetData, data);
}
this.assert(same,
2018-09-06 16:17:43 +02:00
"expected #{this} to have displayed the image #{exp}, but instead it displayed #{act}",
"expected #{this} not to have displayed the image #{act}",
2020-05-31 21:03:38 +02:00
targetData,
2018-09-06 16:17:43 +02:00
data);
2014-09-19 12:16:06 -04:00
});
2020-05-31 21:03:38 +02:00
_chai.Assertion.addMethod('sent', function (targetData) {
2018-05-24 00:27:09 +03:00
const obj = this._obj;
2020-05-31 21:03:38 +02:00
const data = obj._websocket._getSentData();
2018-05-24 00:27:09 +03:00
let same = true;
2020-05-31 21:03:38 +02:00
if (data.length != targetData.length) {
2016-06-03 14:13:35 +02:00
same = false;
} else {
2018-05-24 00:27:09 +03:00
for (let i = 0; i < data.length; i++) {
2020-05-31 21:03:38 +02:00
if (data[i] != targetData[i]) {
2016-06-03 14:13:35 +02:00
same = false;
break;
}
}
}
if (!same) {
2018-05-24 00:25:44 +03:00
// eslint-disable-next-line no-console
2020-05-31 21:03:38 +02:00
console.log("expected data: %o, actual data: %o", targetData, data);
}
this.assert(same,
2018-09-06 16:17:43 +02:00
"expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
"expected #{this} not to have sent the data #{act}",
2020-05-31 21:03:38 +02:00
Array.prototype.slice.call(targetData),
2018-09-06 16:17:43 +02:00
Array.prototype.slice.call(data));
2014-09-19 12:16:06 -04:00
});
_chai.Assertion.addProperty('array', function () {
utils.flag(this, 'array', true);
});
_chai.Assertion.overwriteMethod('equal', function (_super) {
return function assertArrayEqual(target) {
if (utils.flag(this, 'array')) {
2018-05-24 00:27:09 +03:00
const obj = this._obj;
2018-05-24 00:27:09 +03:00
let same = true;
if (utils.flag(this, 'deep')) {
2018-05-24 00:27:09 +03:00
for (let i = 0; i < obj.length; i++) {
if (!utils.eql(obj[i], target[i])) {
same = false;
break;
}
}
this.assert(same,
2018-09-06 16:17:43 +02:00
"expected #{this} to have elements deeply equal to #{exp}",
"expected #{this} not to have elements deeply equal to #{exp}",
Array.prototype.slice.call(target));
} else {
2018-05-24 00:27:09 +03:00
for (let i = 0; i < obj.length; i++) {
if (obj[i] != target[i]) {
same = false;
break;
}
}
this.assert(same,
2018-09-06 16:17:43 +02:00
"expected #{this} to have elements equal to #{exp}",
"expected #{this} not to have elements equal to #{exp}",
Array.prototype.slice.call(target));
}
} else {
_super.apply(this, arguments);
}
};
});
2014-09-19 12:16:06 -04:00
});