jasmine.context.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Code from http://stackoverflow.com/questions/4406864/html-canvas-unit-testing
  2. var Context = function() {
  3. this._calls = []; // names/args of recorded calls
  4. this._initMethods();
  5. this._fillStyle = null;
  6. this._lineCap = null;
  7. this._lineDashOffset = null;
  8. this._lineJoin = null;
  9. this._lineWidth = null;
  10. this._strokeStyle = null;
  11. // Define properties here so that we can record each time they are set
  12. Object.defineProperties(this, {
  13. fillStyle: {
  14. get: function() {
  15. return this._fillStyle;
  16. },
  17. set: function(style) {
  18. this._fillStyle = style;
  19. this.record('setFillStyle', [style]);
  20. }
  21. },
  22. lineCap: {
  23. get: function() {
  24. return this._lineCap;
  25. },
  26. set: function(cap) {
  27. this._lineCap = cap;
  28. this.record('setLineCap', [cap]);
  29. }
  30. },
  31. lineDashOffset: {
  32. get: function() {
  33. return this._lineDashOffset;
  34. },
  35. set: function(offset) {
  36. this._lineDashOffset = offset;
  37. this.record('setLineDashOffset', [offset]);
  38. }
  39. },
  40. lineJoin: {
  41. get: function() {
  42. return this._lineJoin;
  43. },
  44. set: function(join) {
  45. this._lineJoin = join;
  46. this.record('setLineJoin', [join]);
  47. }
  48. },
  49. lineWidth: {
  50. get: function() {
  51. return this._lineWidth;
  52. },
  53. set: function(width) {
  54. this._lineWidth = width;
  55. this.record('setLineWidth', [width]);
  56. }
  57. },
  58. strokeStyle: {
  59. get: function() {
  60. return this._strokeStyle;
  61. },
  62. set: function(style) {
  63. this._strokeStyle = style;
  64. this.record('setStrokeStyle', [style]);
  65. }
  66. },
  67. });
  68. };
  69. Context.prototype._initMethods = function() {
  70. // define methods to test here
  71. // no way to introspect so we have to do some extra work :(
  72. var me = this;
  73. var methods = {
  74. arc: function() {},
  75. beginPath: function() {},
  76. bezierCurveTo: function() {},
  77. clearRect: function() {},
  78. closePath: function() {},
  79. fill: function() {},
  80. fillRect: function() {},
  81. fillText: function() {},
  82. lineTo: function() {},
  83. measureText: function(text) {
  84. // return the number of characters * fixed size
  85. return text ? {width: text.length * 10} : {width: 0};
  86. },
  87. moveTo: function() {},
  88. quadraticCurveTo: function() {},
  89. rect: function() {},
  90. restore: function() {},
  91. rotate: function() {},
  92. save: function() {},
  93. setLineDash: function() {},
  94. stroke: function() {},
  95. strokeRect: function() {},
  96. setTransform: function() {},
  97. translate: function() {},
  98. };
  99. Object.keys(methods).forEach(function(name) {
  100. me[name] = function() {
  101. me.record(name, arguments);
  102. return methods[name].apply(me, arguments);
  103. };
  104. });
  105. };
  106. Context.prototype.record = function(methodName, args) {
  107. this._calls.push({
  108. name: methodName,
  109. args: Array.prototype.slice.call(args)
  110. });
  111. };
  112. Context.prototype.getCalls = function() {
  113. return this._calls;
  114. };
  115. Context.prototype.resetCalls = function() {
  116. this._calls = [];
  117. };
  118. module.exports = Context;