assertions.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. _chai.Assertion.addMethod('sent', function (target_data) {
  30. var obj = this._obj;
  31. obj.inspect = function () {
  32. var 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. var data = obj._websocket._get_sent_data();
  38. var same = true;
  39. for (var i = 0; i < obj.length; i++) {
  40. if (data[i] != target_data[i]) {
  41. same = false;
  42. break;
  43. }
  44. }
  45. if (!same) {
  46. console.log("expected data: %o, actual data: %o", target_data, data);
  47. }
  48. this.assert(same,
  49. "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
  50. "expected #{this} not to have sent the data #{act}",
  51. Array.prototype.slice.call(target_data),
  52. Array.prototype.slice.call(data));
  53. });
  54. _chai.Assertion.addProperty('array', function () {
  55. utils.flag(this, 'array', true);
  56. });
  57. _chai.Assertion.overwriteMethod('equal', function (_super) {
  58. return function assertArrayEqual(target) {
  59. if (utils.flag(this, 'array')) {
  60. var obj = this._obj;
  61. var i;
  62. var same = true;
  63. if (utils.flag(this, 'deep')) {
  64. for (i = 0; i < obj.length; i++) {
  65. if (!utils.eql(obj[i], target[i])) {
  66. same = false;
  67. break;
  68. }
  69. }
  70. this.assert(same,
  71. "expected #{this} to have elements deeply equal to #{exp}",
  72. "expected #{this} not to have elements deeply equal to #{exp}",
  73. Array.prototype.slice.call(target));
  74. } else {
  75. for (i = 0; i < obj.length; i++) {
  76. if (obj[i] != target[i]) {
  77. same = false;
  78. break;
  79. }
  80. }
  81. this.assert(same,
  82. "expected #{this} to have elements equal to #{exp}",
  83. "expected #{this} not to have elements equal to #{exp}",
  84. Array.prototype.slice.call(target));
  85. }
  86. } else {
  87. _super.apply(this, arguments);
  88. }
  89. };
  90. });
  91. });