assertions.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // noVNC specific assertions
  2. chai.use(function (_chai, utils) {
  3. function _equal(a, b) {
  4. return a === b;
  5. }
  6. _chai.Assertion.addMethod('displayed', function (targetData, cmp=_equal) {
  7. const obj = this._obj;
  8. const ctx = obj._target.getContext('2d');
  9. const data = ctx.getImageData(0, 0, obj._target.width, obj._target.height).data;
  10. const len = data.length;
  11. new chai.Assertion(len).to.be.equal(targetData.length, "unexpected display size");
  12. let same = true;
  13. for (let i = 0; i < len; i++) {
  14. if (!cmp(data[i], targetData[i])) {
  15. same = false;
  16. break;
  17. }
  18. }
  19. if (!same) {
  20. // eslint-disable-next-line no-console
  21. console.log("expected data: %o, actual data: %o", targetData, 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. targetData,
  27. data);
  28. });
  29. _chai.Assertion.addMethod('sent', function (targetData) {
  30. const obj = this._obj;
  31. obj.inspect = () => {
  32. const res = { _websocket: obj._websocket, rQi: obj._rQi, _rQ: new Uint8Array(obj._rQ.buffer, 0, obj._rQlen),
  33. _sQ: new Uint8Array(obj._sQ.buffer, 0, obj._sQlen) };
  34. res.prototype = obj;
  35. return res;
  36. };
  37. const data = obj._websocket._getSentData();
  38. let same = true;
  39. if (data.length != targetData.length) {
  40. same = false;
  41. } else {
  42. for (let i = 0; i < data.length; i++) {
  43. if (data[i] != targetData[i]) {
  44. same = false;
  45. break;
  46. }
  47. }
  48. }
  49. if (!same) {
  50. // eslint-disable-next-line no-console
  51. console.log("expected data: %o, actual data: %o", targetData, data);
  52. }
  53. this.assert(same,
  54. "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
  55. "expected #{this} not to have sent the data #{act}",
  56. Array.prototype.slice.call(targetData),
  57. Array.prototype.slice.call(data));
  58. });
  59. _chai.Assertion.addProperty('array', function () {
  60. utils.flag(this, 'array', true);
  61. });
  62. _chai.Assertion.overwriteMethod('equal', function (_super) {
  63. return function assertArrayEqual(target) {
  64. if (utils.flag(this, 'array')) {
  65. const obj = this._obj;
  66. let same = true;
  67. if (utils.flag(this, 'deep')) {
  68. for (let i = 0; i < obj.length; i++) {
  69. if (!utils.eql(obj[i], target[i])) {
  70. same = false;
  71. break;
  72. }
  73. }
  74. this.assert(same,
  75. "expected #{this} to have elements deeply equal to #{exp}",
  76. "expected #{this} not to have elements deeply equal to #{exp}",
  77. Array.prototype.slice.call(target));
  78. } else {
  79. for (let i = 0; i < obj.length; i++) {
  80. if (obj[i] != target[i]) {
  81. same = false;
  82. break;
  83. }
  84. }
  85. this.assert(same,
  86. "expected #{this} to have elements equal to #{exp}",
  87. "expected #{this} not to have elements equal to #{exp}",
  88. Array.prototype.slice.call(target));
  89. }
  90. } else {
  91. _super.apply(this, arguments);
  92. }
  93. };
  94. });
  95. });