assertions.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. for (var i = 0; i < obj.length; i++) {
  10. if (data[i] != target_data[i]) {
  11. same = false;
  12. break;
  13. }
  14. }
  15. if (!same) {
  16. console.log("expected data: %o, actual data: %o", target_data, data);
  17. }
  18. this.assert(same,
  19. "expected #{this} to have displayed the image #{exp}, but instead it displayed #{act}",
  20. "expected #{this} not to have displayed the image #{act}",
  21. target_data,
  22. data);
  23. });
  24. _chai.Assertion.addMethod('sent', function (target_data) {
  25. var obj = this._obj;
  26. obj.inspect = function () {
  27. var res = { _websocket: obj._websocket, rQi: obj._rQi, _rQ: new Uint8Array(obj._rQ.buffer, 0, obj._rQlen),
  28. _sQ: new Uint8Array(obj._sQ.buffer, 0, obj._sQlen) };
  29. res.prototype = obj;
  30. return res;
  31. };
  32. var data = obj._websocket._get_sent_data();
  33. var same = true;
  34. for (var i = 0; i < obj.length; i++) {
  35. if (data[i] != target_data[i]) {
  36. same = false;
  37. break;
  38. }
  39. }
  40. if (!same) {
  41. console.log("expected data: %o, actual data: %o", target_data, data);
  42. }
  43. this.assert(same,
  44. "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
  45. "expected #{this} not to have sent the data #{act}",
  46. Array.prototype.slice.call(target_data),
  47. Array.prototype.slice.call(data));
  48. });
  49. _chai.Assertion.addProperty('array', function () {
  50. utils.flag(this, 'array', true);
  51. });
  52. _chai.Assertion.overwriteMethod('equal', function (_super) {
  53. return function assertArrayEqual(target) {
  54. if (utils.flag(this, 'array')) {
  55. var obj = this._obj;
  56. var i;
  57. var same = true;
  58. if (utils.flag(this, 'deep')) {
  59. for (i = 0; i < obj.length; i++) {
  60. if (!utils.eql(obj[i], target[i])) {
  61. same = false;
  62. break;
  63. }
  64. }
  65. this.assert(same,
  66. "expected #{this} to have elements deeply equal to #{exp}",
  67. "expected #{this} not to have elements deeply equal to #{exp}",
  68. Array.prototype.slice.call(target));
  69. } else {
  70. for (i = 0; i < obj.length; i++) {
  71. if (obj[i] != target[i]) {
  72. same = false;
  73. break;
  74. }
  75. }
  76. this.assert(same,
  77. "expected #{this} to have elements equal to #{exp}",
  78. "expected #{this} not to have elements equal to #{exp}",
  79. Array.prototype.slice.call(target));
  80. }
  81. } else {
  82. _super.apply(this, arguments);
  83. }
  84. };
  85. });
  86. });