flamegraphCanvas.spec.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {mat3, vec2} from 'gl-matrix';
  2. import {makeCanvasMock, makeContextMock} from 'sentry-test/profiling/utils';
  3. import {FlamegraphCanvas} from 'sentry/utils/profiling/flamegraphCanvas';
  4. import {Rect} from 'sentry/utils/profiling/gl/utils';
  5. describe('flamegraphCanvas', () => {
  6. beforeEach(() => {
  7. // We simulate regular screens unless differently specified
  8. window.devicePixelRatio = 1;
  9. });
  10. it('initializes physical space', () => {
  11. const canvas = makeCanvasMock();
  12. const flamegraphCanvas = new FlamegraphCanvas(canvas, vec2.fromValues(0, 0));
  13. expect(flamegraphCanvas.physicalSpace).toEqual(new Rect(0, 0, 1000, 1000));
  14. });
  15. it('initializes physical space with origin', () => {
  16. const canvas = makeCanvasMock();
  17. const flamegraphCanvas = new FlamegraphCanvas(canvas, vec2.fromValues(10, 10));
  18. expect(flamegraphCanvas.physicalSpace).toEqual(new Rect(10, 10, 990, 990));
  19. });
  20. it('initializes logical space', () => {
  21. window.devicePixelRatio = 2;
  22. const canvas = makeCanvasMock();
  23. const flamegraphCanvas = new FlamegraphCanvas(canvas, vec2.fromValues(0, 0));
  24. expect(flamegraphCanvas.logicalSpace).toEqual(new Rect(0, 0, 500, 500));
  25. });
  26. it('initializes logical space with origin', () => {
  27. window.devicePixelRatio = 2;
  28. const canvas = makeCanvasMock();
  29. const flamegraphCanvas = new FlamegraphCanvas(canvas, vec2.fromValues(10, 10));
  30. expect(flamegraphCanvas.logicalSpace).toEqual(new Rect(5, 5, 495, 495));
  31. });
  32. it('initializes logicalToPhysicalSpace', () => {
  33. window.devicePixelRatio = 2;
  34. // @ts-ignore partial mock
  35. const context = makeContextMock({canvas: {width: 100, height: 100}});
  36. const canvas = makeCanvasMock({
  37. getContext: jest.fn().mockReturnValue(context),
  38. });
  39. const flamegraphCanvas = new FlamegraphCanvas(canvas, vec2.fromValues(10, 10));
  40. expect(flamegraphCanvas.logicalToPhysicalSpace).toEqual(
  41. mat3.fromScaling(mat3.create(), vec2.fromValues(2, 2))
  42. );
  43. });
  44. it('initializes physicalToLogicalSpace', () => {
  45. window.devicePixelRatio = 2;
  46. // @ts-ignore partial mock
  47. const context = makeContextMock({canvas: {width: 100, height: 100}});
  48. const canvas = makeCanvasMock({
  49. getContext: jest.fn().mockReturnValue(context),
  50. });
  51. const flamegraphCanvas = new FlamegraphCanvas(canvas, vec2.fromValues(10, 10));
  52. expect(flamegraphCanvas.physicalToLogicalSpace).toEqual(
  53. mat3.fromScaling(mat3.create(), vec2.fromValues(0.5, 0.5))
  54. );
  55. });
  56. it('handles resize events by updating space', () => {
  57. // @ts-ignore partial canvas mock
  58. const canvas = makeCanvasMock({
  59. width: 100,
  60. height: 100,
  61. });
  62. const flamegraphCanvas = new FlamegraphCanvas(canvas, vec2.fromValues(0, 0));
  63. expect(flamegraphCanvas.physicalSpace).toEqual(new Rect(0, 0, 100, 100));
  64. canvas.width = 200;
  65. canvas.height = 200;
  66. flamegraphCanvas.initPhysicalSpace();
  67. expect(flamegraphCanvas.physicalSpace).toEqual(new Rect(0, 0, 200, 200));
  68. });
  69. });