context.js 3.0 KB

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