Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored CoordinateSystemMapper and added tests #1699

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*******************************************************************************
* Copyright (c) 2025 Yatta Solutions and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta Solutions - initial tests
*******************************************************************************/
package org.eclipse.swt.widgets;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.function.*;
import java.util.stream.*;

import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.*;
import org.junit.jupiter.params.provider.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CoordinateSystemMapperTests {

Supplier<Monitor[]> getMonitorConsumer;
Monitor[] monitors;

private Monitor createMonitor(CoordinateSystemMapper mapper, Rectangle boundsInPixels, int nativeZoom) {
Monitor monitor = new Monitor();
Rectangle bounds = mapper.mapMonitorBounds(boundsInPixels, DPIUtil.getZoomForAutoscaleProperty(nativeZoom));
monitor.setBounds(bounds);
monitor.setClientArea(bounds);
monitor.zoom = nativeZoom;
return monitor;
}

void setupMonitors(CoordinateSystemMapper mapper) {
Rectangle boundsInPixelsForLeftMonitor = new Rectangle(0, 0, 2000, 2000);
Rectangle boundsInPixelsForRightMonitor = new Rectangle(2000, 0, 2000, 2000);
monitors = new Monitor[] { createMonitor(mapper, boundsInPixelsForLeftMonitor, 200),
createMonitor(mapper, boundsInPixelsForRightMonitor, 100) };
}

Stream<CoordinateSystemMapper> provideCoordinateSystemMappers() {
return Stream.of(new MultiZoomCoordinateSystemMapper(null, () -> monitors),
new SingleZoomCoordinateSystemMapper(null));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Point pt = new Point(5000, -400);
Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom());
assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom()));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
void translatePointInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Point pt = new Point(1900, 400);
Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom());
assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom()));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPts = new Rectangle(5000, -400, 200, 200);
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPts = new Rectangle(750, 400, 100, 100);
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
}

@Test
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
CoordinateSystemMapper mapper = provideCoordinateSystemMappers().findFirst().get();
setupMonitors(mapper);
Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000);
Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, monitors[0].getZoom());
expectedSmallRectInPxs.x = rectInPxs.x + (rectInPxs.width / 2) - 200;
expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height / 2) - 200;
expectedSmallRectInPxs.width = 400;
expectedSmallRectInPxs.height = 400;
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
Rectangle smallRectInPts = new Rectangle(0, 0, 0, monitors[0].getZoom());
smallRectInPts.x = rectInPts.x + (rectInPts.width / 2) - 200;
smallRectInPts.y = rectInPts.y + (rectInPts.height / 2) - 200;
smallRectInPts.width = 400;
smallRectInPts.height = 400;
Rectangle smallRectInPxs = mapper.translateToDisplayCoordinates(smallRectInPts, monitors[0].getZoom());
assertEquals(expectedSmallRectInPxs, smallRectInPxs);
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPxs = new Rectangle(1500, 400, 502, 500);
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2025 Yatta Solutions and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta Solutions - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.widgets;

import org.eclipse.swt.graphics.*;

interface CoordinateSystemMapper {

Rectangle map(Control from, Control to, Rectangle rectangle);

Rectangle map(Control from, Control to, int x, int y, int width, int height);

Point map(Control from, Control to, Point point);

Point map(Control from, Control to, int x, int y);

Rectangle mapMonitorBounds(Rectangle rectangle, int zoom);

Point translateFromDisplayCoordinates(Point point, int zoom);

Point translateToDisplayCoordinates(Point point, int zoom);

Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom);

Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom);

void setCursorLocation(int x, int y);

Point getCursorLocation();
}
Loading
Loading