|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Yatta Solutions and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Yatta Solutions - initial tests |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.swt.widgets; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +import java.util.function.*; |
| 19 | +import java.util.stream.*; |
| 20 | + |
| 21 | +import org.eclipse.swt.graphics.*; |
| 22 | +import org.eclipse.swt.internal.*; |
| 23 | +import org.junit.jupiter.api.*; |
| 24 | +import org.junit.jupiter.params.*; |
| 25 | +import org.junit.jupiter.params.provider.*; |
| 26 | + |
| 27 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 28 | +public class CoordinateSystemMapperTests { |
| 29 | + |
| 30 | + Supplier<Monitor[]> getMonitorConsumer; |
| 31 | + Monitor[] monitors; |
| 32 | + |
| 33 | + private Monitor createMonitor(CoordinateSystemMapper mapper, Rectangle boundsInPixels, int nativeZoom) { |
| 34 | + Monitor monitor = new Monitor(); |
| 35 | + Rectangle bounds = mapper.mapMonitorBounds(boundsInPixels, DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); |
| 36 | + monitor.setBounds(bounds); |
| 37 | + monitor.setClientArea(bounds); |
| 38 | + monitor.zoom = nativeZoom; |
| 39 | + return monitor; |
| 40 | + } |
| 41 | + |
| 42 | + void setupMonitors(CoordinateSystemMapper mapper) { |
| 43 | + Rectangle boundsInPixelsForLeftMonitor = new Rectangle(0, 0, 2000, 2000); |
| 44 | + Rectangle boundsInPixelsForRightMonitor = new Rectangle(2000, 0, 2000, 2000); |
| 45 | + monitors = new Monitor[] { createMonitor(mapper, boundsInPixelsForLeftMonitor, 200), |
| 46 | + createMonitor(mapper, boundsInPixelsForRightMonitor, 100) }; |
| 47 | + } |
| 48 | + |
| 49 | + Stream<CoordinateSystemMapper> provideCoordinateSystemMappers() { |
| 50 | + return Stream.of(new MultiZoomCoordinateSystemMapper(null, () -> monitors), |
| 51 | + new SingleZoomCoordinateSystemMapper(null)); |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @MethodSource("provideCoordinateSystemMappers") |
| 56 | + void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 57 | + setupMonitors(mapper); |
| 58 | + Point pt = new Point(5000, -400); |
| 59 | + Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom()); |
| 60 | + assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom())); |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest |
| 64 | + @MethodSource("provideCoordinateSystemMappers") |
| 65 | + @Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper") |
| 66 | + void translatePointInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 67 | + setupMonitors(mapper); |
| 68 | + Point pt = new Point(1900, 400); |
| 69 | + Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom()); |
| 70 | + assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom())); |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @MethodSource("provideCoordinateSystemMappers") |
| 75 | + void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 76 | + setupMonitors(mapper); |
| 77 | + Rectangle rectInPts = new Rectangle(5000, -400, 200, 200); |
| 78 | + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); |
| 79 | + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @MethodSource("provideCoordinateSystemMappers") |
| 84 | + @Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper") |
| 85 | + void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 86 | + setupMonitors(mapper); |
| 87 | + Rectangle rectInPts = new Rectangle(1800, 400, 100, 100); |
| 88 | + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); |
| 89 | + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); |
| 90 | + } |
| 91 | + |
| 92 | + @ParameterizedTest |
| 93 | + @MethodSource("provideCoordinateSystemMappers") |
| 94 | + @Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper") |
| 95 | + void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 96 | + setupMonitors(mapper); |
| 97 | + Rectangle rectInPts = new Rectangle(1950, 400, 100, 100); |
| 98 | + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); |
| 99 | + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); |
| 100 | + } |
| 101 | + |
| 102 | + @ParameterizedTest |
| 103 | + @MethodSource("provideCoordinateSystemMappers") |
| 104 | + void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 105 | + setupMonitors(mapper); |
| 106 | + Rectangle rectInPts = new Rectangle(750, 400, 100, 100); |
| 107 | + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); |
| 108 | + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); |
| 109 | + } |
| 110 | + |
| 111 | + @ParameterizedTest |
| 112 | + @MethodSource("provideCoordinateSystemMappers") |
| 113 | + void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 114 | + setupMonitors(mapper); |
| 115 | + Rectangle rectInPts = new Rectangle(950, 400, 1500, 100); |
| 116 | + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); |
| 117 | + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper") |
| 122 | + void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() { |
| 123 | + CoordinateSystemMapper mapper = provideCoordinateSystemMappers().findFirst().get(); |
| 124 | + setupMonitors(mapper); |
| 125 | + Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000); |
| 126 | + Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, monitors[0].getZoom()); |
| 127 | + expectedSmallRectInPxs.x = rectInPxs.x + (rectInPxs.width / 2) - 200; |
| 128 | + expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height / 2) - 200; |
| 129 | + expectedSmallRectInPxs.width = 400; |
| 130 | + expectedSmallRectInPxs.height = 400; |
| 131 | + Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); |
| 132 | + Rectangle smallRectInPts = new Rectangle(0, 0, 0, monitors[0].getZoom()); |
| 133 | + smallRectInPts.x = rectInPts.x + (rectInPts.width / 2) - 200; |
| 134 | + smallRectInPts.y = rectInPts.y + (rectInPts.height / 2) - 200; |
| 135 | + smallRectInPts.width = 400; |
| 136 | + smallRectInPts.height = 400; |
| 137 | + Rectangle smallRectInPxs = mapper.translateToDisplayCoordinates(smallRectInPts, monitors[0].getZoom()); |
| 138 | + assertEquals(expectedSmallRectInPxs, smallRectInPxs); |
| 139 | + } |
| 140 | + |
| 141 | + @ParameterizedTest |
| 142 | + @MethodSource("provideCoordinateSystemMappers") |
| 143 | + @Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper") |
| 144 | + void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 145 | + setupMonitors(mapper); |
| 146 | + Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000); |
| 147 | + Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); |
| 148 | + assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom())); |
| 149 | + } |
| 150 | + |
| 151 | + @ParameterizedTest |
| 152 | + @MethodSource("provideCoordinateSystemMappers") |
| 153 | + void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { |
| 154 | + setupMonitors(mapper); |
| 155 | + Rectangle rectInPxs = new Rectangle(1500, 400, 502, 500); |
| 156 | + Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); |
| 157 | + assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom())); |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments