helpers.canvas.tests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. describe('Chart.helpers.canvas', function() {
  3. describe('auto', jasmine.fixture.specs('helpers.canvas'));
  4. var helpers = Chart.helpers;
  5. describe('clear', function() {
  6. it('should clear the chart canvas', function() {
  7. var chart = acquireChart({}, {
  8. canvas: {
  9. style: 'width: 150px; height: 245px'
  10. }
  11. });
  12. spyOn(chart.ctx, 'clearRect');
  13. helpers.canvas.clear(chart);
  14. expect(chart.ctx.clearRect.calls.count()).toBe(1);
  15. expect(chart.ctx.clearRect.calls.first().object).toBe(chart.ctx);
  16. expect(chart.ctx.clearRect.calls.first().args).toEqual([0, 0, 150, 245]);
  17. });
  18. });
  19. describe('roundedRect', function() {
  20. it('should create a rounded rectangle path', function() {
  21. var context = window.createMockContext();
  22. helpers.canvas.roundedRect(context, 10, 20, 30, 40, 5);
  23. expect(context.getCalls()).toEqual([
  24. {name: 'moveTo', args: [10, 25]},
  25. {name: 'arc', args: [15, 25, 5, -Math.PI, -Math.PI / 2]},
  26. {name: 'arc', args: [35, 25, 5, -Math.PI / 2, 0]},
  27. {name: 'arc', args: [35, 55, 5, 0, Math.PI / 2]},
  28. {name: 'arc', args: [15, 55, 5, Math.PI / 2, Math.PI]},
  29. {name: 'closePath', args: []},
  30. {name: 'moveTo', args: [10, 20]}
  31. ]);
  32. });
  33. it('should optimize path if radius is exactly half of height', function() {
  34. var context = window.createMockContext();
  35. helpers.canvas.roundedRect(context, 10, 20, 40, 30, 15);
  36. expect(context.getCalls()).toEqual([
  37. {name: 'moveTo', args: [10, 35]},
  38. {name: 'moveTo', args: [25, 20]},
  39. {name: 'arc', args: [35, 35, 15, -Math.PI / 2, Math.PI / 2]},
  40. {name: 'arc', args: [25, 35, 15, Math.PI / 2, Math.PI * 3 / 2]},
  41. {name: 'closePath', args: []},
  42. {name: 'moveTo', args: [10, 20]}
  43. ]);
  44. });
  45. it('should optimize path if radius is exactly half of width', function() {
  46. var context = window.createMockContext();
  47. helpers.canvas.roundedRect(context, 10, 20, 30, 40, 15);
  48. expect(context.getCalls()).toEqual([
  49. {name: 'moveTo', args: [10, 35]},
  50. {name: 'arc', args: [25, 35, 15, -Math.PI, 0]},
  51. {name: 'arc', args: [25, 45, 15, 0, Math.PI]},
  52. {name: 'closePath', args: []},
  53. {name: 'moveTo', args: [10, 20]}
  54. ]);
  55. });
  56. it('should optimize path if radius is exactly half of width and height', function() {
  57. var context = window.createMockContext();
  58. helpers.canvas.roundedRect(context, 10, 20, 30, 30, 15);
  59. expect(context.getCalls()).toEqual([
  60. {name: 'moveTo', args: [10, 35]},
  61. {name: 'arc', args: [25, 35, 15, -Math.PI, Math.PI]},
  62. {name: 'closePath', args: []},
  63. {name: 'moveTo', args: [10, 20]}
  64. ]);
  65. });
  66. it('should optimize path if radius is 0', function() {
  67. var context = window.createMockContext();
  68. helpers.canvas.roundedRect(context, 10, 20, 30, 40, 0);
  69. expect(context.getCalls()).toEqual([{name: 'rect', args: [10, 20, 30, 40]}]);
  70. });
  71. });
  72. describe('isPointInArea', function() {
  73. it('should determine if a point is in the area', function() {
  74. var isPointInArea = helpers.canvas._isPointInArea;
  75. var area = {left: 0, top: 0, right: 512, bottom: 256};
  76. expect(isPointInArea({x: 0, y: 0}, area)).toBe(true);
  77. expect(isPointInArea({x: -1e-12, y: -1e-12}, area)).toBe(true);
  78. expect(isPointInArea({x: 512, y: 256}, area)).toBe(true);
  79. expect(isPointInArea({x: 512 + 1e-12, y: 256 + 1e-12}, area)).toBe(true);
  80. expect(isPointInArea({x: -1e-3, y: 0}, area)).toBe(false);
  81. expect(isPointInArea({x: 0, y: 256 + 1e-3}, area)).toBe(false);
  82. });
  83. });
  84. });