|
| 1 | +// Copyright (C) 2022-2025 Intel Corporation |
| 2 | +// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE |
| 3 | + |
| 4 | +import { getBoundingBoxInRoi, getBoundingBoxResizePoints, getClampedBoundingBox } from './tool-utils'; |
| 5 | + |
| 6 | +const mockedRoi = { |
| 7 | + x: 0, |
| 8 | + y: 0, |
| 9 | + width: 100, |
| 10 | + height: 100, |
| 11 | +}; |
| 12 | + |
| 13 | +describe('edit-tool utils', () => { |
| 14 | + describe('getClampedBoundingBox', () => { |
| 15 | + const mockedBoundingBox = { x: 0, y: 0, width: 20, height: 20 }; |
| 16 | + test.each([ |
| 17 | + [{ x: 0, y: 0 }, mockedBoundingBox, { ...mockedBoundingBox, x: mockedRoi.x, y: mockedRoi.y }], // No clamping needed |
| 18 | + [{ x: mockedRoi.width, y: mockedRoi.height }, mockedBoundingBox, { ...mockedBoundingBox, x: 80, y: 80 }], // Clamped outside bottom-right |
| 19 | + [{ x: -10, y: -10 }, mockedBoundingBox, { ...mockedBoundingBox }], // Clamped outside top-left |
| 20 | + ])('given point %o move the boundingBox %o to %o', (point, boundingBox, expectedResult) => { |
| 21 | + const result = getClampedBoundingBox(point, boundingBox, mockedRoi); |
| 22 | + expect(result).toEqual(expectedResult); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('getBoundingBoxInRoi', () => { |
| 27 | + const mockedBoundingBox = { x: 0, y: 0, width: 10, height: 10 }; |
| 28 | + |
| 29 | + test.each([ |
| 30 | + [mockedBoundingBox, mockedBoundingBox], // Completely inside ROI |
| 31 | + [{ ...mockedBoundingBox, x: -10 }, mockedBoundingBox], // Partially outside left |
| 32 | + [ |
| 33 | + { ...mockedBoundingBox, x: 95 }, |
| 34 | + { ...mockedBoundingBox, x: 95, width: 5 }, // Partially outside right |
| 35 | + ], |
| 36 | + [{ ...mockedBoundingBox, y: -10 }, mockedBoundingBox], // Partially outside top |
| 37 | + [ |
| 38 | + { ...mockedBoundingBox, y: 95 }, |
| 39 | + { ...mockedBoundingBox, y: 95, height: 5 }, // Partially outside bottom |
| 40 | + ], |
| 41 | + ])('given boundingBox %o, returns the portion inside the roi %o', (boundingBox, expectedResult) => { |
| 42 | + const result = getBoundingBoxInRoi(boundingBox, mockedRoi); |
| 43 | + expect(result).toEqual(expectedResult); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('getBoundingBoxResizePoints', () => { |
| 48 | + it('should create resize points with correct properties', () => { |
| 49 | + const gap = 0; |
| 50 | + const boundingBox = { x: 50, y: 50, width: 50, height: 50 }; |
| 51 | + const resizePoints = getBoundingBoxResizePoints({ boundingBox, gap, onResized: jest.fn() }); |
| 52 | + |
| 53 | + const expectedAnchors = [ |
| 54 | + { x: 50, y: 50, cursor: 'nw-resize', label: 'North west resize anchor' }, |
| 55 | + { x: 75, y: 50, cursor: 'n-resize', label: 'North resize anchor' }, |
| 56 | + { x: 100, y: 50, cursor: 'ne-resize', label: 'North east resize anchor' }, |
| 57 | + { x: 100, y: 75, cursor: 'e-resize', label: 'East resize anchor' }, |
| 58 | + { x: 100, y: 100, cursor: 'se-resize', label: 'South east resize anchor' }, |
| 59 | + { x: 75, y: 100, cursor: 's-resize', label: 'South resize anchor' }, |
| 60 | + { x: 50, y: 100, cursor: 'sw-resize', label: 'South west resize anchor' }, |
| 61 | + { x: 50, y: 75, cursor: 'w-resize', label: 'West resize anchor' }, |
| 62 | + ]; |
| 63 | + |
| 64 | + expect(resizePoints).toHaveLength(8); |
| 65 | + expectedAnchors.forEach((expected, index) => { |
| 66 | + expect(resizePoints[index]).toMatchObject(expected); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('getBoundingBoxResizePoints', () => { |
| 71 | + const gap = 0; |
| 72 | + const onResized = jest.fn(); |
| 73 | + const boundingBox = { x: 50, y: 50, width: 50, height: 50 }; |
| 74 | + const resizePoints = getBoundingBoxResizePoints({ boundingBox, gap, onResized }); |
| 75 | + const [ |
| 76 | + topLeftPoint, |
| 77 | + topCenterPoint, |
| 78 | + topRightPoint, |
| 79 | + rightCenterPoint, |
| 80 | + bottomRightPoint, |
| 81 | + bottomCenterPoint, |
| 82 | + bottomLeftPoint, |
| 83 | + centerLeftPoint, |
| 84 | + ] = resizePoints; |
| 85 | + |
| 86 | + const testCases = [ |
| 87 | + { |
| 88 | + index: 0, |
| 89 | + moveTo: [topLeftPoint.x - 50, 0], // Move NW anchor 50 units left and up |
| 90 | + expected: { x: 0, y: 0, width: 100, height: 100 }, |
| 91 | + description: 'topLeftPoint', |
| 92 | + }, |
| 93 | + { |
| 94 | + index: 1, |
| 95 | + moveTo: [topCenterPoint.x - 50, 0], // Move N anchor 50 units up |
| 96 | + expected: { x: 50, y: 0, width: 50, height: 100 }, |
| 97 | + description: 'topCenterPoint', |
| 98 | + }, |
| 99 | + { |
| 100 | + index: 2, |
| 101 | + moveTo: [topRightPoint.x + 50, 0], // Move NE anchor 50 units up |
| 102 | + expected: { x: 50, y: 0, width: 100, height: 100 }, |
| 103 | + description: 'topRightPoint', |
| 104 | + }, |
| 105 | + { |
| 106 | + index: 3, |
| 107 | + moveTo: [rightCenterPoint.x + 50, 0], // Move E anchor 50 units right |
| 108 | + expected: { x: 50, y: 50, width: 100, height: 50 }, |
| 109 | + description: 'rightCenterPoint', |
| 110 | + }, |
| 111 | + { |
| 112 | + index: 4, |
| 113 | + moveTo: [bottomRightPoint.x + 50, bottomRightPoint.y + 50], // Move SE anchor 50 units down and right |
| 114 | + expected: { x: 50, y: 50, width: 100, height: 100 }, |
| 115 | + description: 'bottomRightPoint', |
| 116 | + }, |
| 117 | + { |
| 118 | + index: 5, |
| 119 | + moveTo: [0, bottomCenterPoint.y + 50], // Move S anchor 50 units down |
| 120 | + expected: { x: 50, y: 50, width: 50, height: 100 }, |
| 121 | + description: 'bottomCenterPoint', |
| 122 | + }, |
| 123 | + { |
| 124 | + index: 6, |
| 125 | + moveTo: [0, bottomLeftPoint.y + 50], // Move SW anchor 50 units down and left |
| 126 | + expected: { x: 0, y: 50, width: 100, height: 100 }, |
| 127 | + description: 'bottomLeftPoint', |
| 128 | + }, |
| 129 | + |
| 130 | + { |
| 131 | + index: 7, |
| 132 | + moveTo: [centerLeftPoint.x - 50, 0], // Move W anchor 50 units left |
| 133 | + expected: { x: 0, y: 50, width: 100, height: 50 }, |
| 134 | + description: 'centerLeftPoint', |
| 135 | + }, |
| 136 | + ]; |
| 137 | + |
| 138 | + test.each(testCases)('$description moves and resizes correctly', ({ index, moveTo, expected }) => { |
| 139 | + const [x, y] = moveTo; |
| 140 | + resizePoints[index].moveAnchorTo(x, y); |
| 141 | + expect(onResized).toHaveBeenCalledWith(expected); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should not go below gap', () => { |
| 146 | + const gap = 20; |
| 147 | + const onResized = jest.fn(); |
| 148 | + const boundingBox = { x: 50, y: 50, width: 50, height: 50 }; |
| 149 | + const resizePoints = getBoundingBoxResizePoints({ boundingBox, gap, onResized }); |
| 150 | + |
| 151 | + resizePoints[3].moveAnchorTo(-100, 0); |
| 152 | + |
| 153 | + expect(onResized).toHaveBeenCalledWith({ ...boundingBox, width: gap }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('clamp to minimum 0 for coordinates', () => { |
| 157 | + const gap = 20; |
| 158 | + const onResized = jest.fn(); |
| 159 | + const boundingBox = { x: 50, y: 50, width: 50, height: 50 }; |
| 160 | + const resizePoints = getBoundingBoxResizePoints({ boundingBox, gap, onResized }); |
| 161 | + |
| 162 | + resizePoints[6].moveAnchorTo(-10, -10); |
| 163 | + |
| 164 | + expect(onResized).toHaveBeenCalledWith({ x: 0, width: 100, y: 50, height: gap }); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments