Skip to content
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
Expand Up @@ -54,12 +54,11 @@ private void checkGcZoomLevelOnCanvas(int expectedZoom) {
@Test
public void drawnElementsShouldScaleUpToTheRightZoomLevel() {
Shell shell = new Shell(Display.getDefault());

int zoom = DPIUtil.getDeviceZoom();
int zoom = shell.nativeZoom;
int scalingFactor = 2;
GC gc = GC.win32_new(shell, new GCData());
gc.getGCData().nativeZoom = zoom * scalingFactor;
gc.getGCData().lineWidth = 10;
assertEquals("DPIUtil calls with getDeviceZoom should scale to the right value", gc.getGCData().lineWidth, gc.getLineWidth() * scalingFactor, 0);
assertEquals("Drawn elements should scale to the right value", gc.getGCData().lineWidth, gc.getLineWidth() * scalingFactor, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RegionWin32Tests {
public void testRegionMustBeScaledOnHandleOfScaledZoomLevel() {
Display display = Display.getDefault();

int zoom = DPIUtil.getDeviceZoom();
int zoom = 100;
int scalingFactor = 2;

Region region = new Region(display);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TransformWin32Tests {
@Test
public void testShouldHaveDifferentHandlesAtDifferentZoomLevels() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
int zoom = 100;
Transform transform = new Transform(display);
long scaledHandle = transform.getHandle(zoom * 2);
assertNotEquals("There should be different handles for different zoom levels", scaledHandle, transform.getHandle(zoom));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ record FontComparison(int originalFontHeight, int currentFontHeight) {
private FontComparison updateFont(int scalingFactor) {
Shell shell = new Shell(Display.getDefault());
Control control = new Composite(shell, SWT.NONE);
int zoom = DPIUtil.getDeviceZoom();
int zoom = shell.getNativeZoom();
int newZoom = zoom * scalingFactor;

Font oldFont = control.getFont();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class WidgetWin32Tests {
public void testWidgetZoomShouldChangeOnZoomLevelChange() {
Display display = Display.getDefault();
Shell shell = new Shell(display);
int zoom = DPIUtil.getDeviceZoom();
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;

Button button = new Button(shell, SWT.PUSH);
Expand All @@ -49,10 +49,11 @@ public void testWidgetZoomShouldChangeOnZoomLevelChange() {
@Test
public void testButtonPointsAfterZooming() throws NoSuchMethodException, IllegalAccessException {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();

int scaledZoom = zoom * 2;

Shell shell = new Shell(display);
shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand All @@ -73,7 +74,8 @@ public void testButtonPointsAfterZooming() throws NoSuchMethodException, Illegal
@Test
public void testImagePixelsWithDoubleZoomLevel() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;

InputStream inputStream = WidgetWin32Tests.class.getResourceAsStream("folder.png");
Expand All @@ -99,10 +101,12 @@ private Point getImageDimension(Image image, Integer zoomLevel) {
@Test
public void testButtonFontAfterZooming() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();

int scaledZoom = zoom * 2;

Shell shell = new Shell(display);

shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand All @@ -124,10 +128,11 @@ public void testButtonFontAfterZooming() {
@Test
public void testCoolItemAfterZooming() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;

Shell shell = new Shell(display);

shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand Down Expand Up @@ -163,10 +168,10 @@ public void testCoolItemAfterZooming() {
@Test
public void testExpandItemAfterZooming() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;

Shell shell = new Shell(display);
shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand All @@ -190,10 +195,10 @@ public void testExpandItemAfterZooming() {
@Test
public void testTabFolderSizeAfterZooming() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;

Shell shell = new Shell(display);
shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand All @@ -220,10 +225,10 @@ public void testTabFolderSizeAfterZooming() {
@Test
public void testTableAfterZooming() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
int scaledZoom = zoom * 2;

Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;
shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand Down Expand Up @@ -258,10 +263,10 @@ public void testTableAfterZooming() {
@Test
public void testTreeAfterZooming() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;

Shell shell = new Shell(display);
shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand Down Expand Up @@ -294,10 +299,10 @@ public void testTreeAfterZooming() {
@Test
public void testCaretInStyledTextAfterZooming() {
Display display = Display.getDefault();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scaledZoom = zoom * 2;

Shell shell = new Shell(display);
shell.setBounds(0, 0, 100, 160);
shell.setLayout(new FillLayout());
shell.pack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.internal.DPIUtil;

/*
* This Snippet tests a pattern at multiple zoom levels.
Expand All @@ -33,8 +32,8 @@ public class PatternWin32ManualTest {
private static Display display = Display.getDefault();

public static void main (String [] args) {
@SuppressWarnings("restriction")
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
int zoom = shell.getNativeZoom();
int scalingFactor = 3;
int scaledZoom = zoom * scalingFactor;
int width = 400;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
public class DPIUtilTests {

private int deviceZoom;
private static final int ZOOM_200 = 200;

@BeforeEach
public void setup() {
deviceZoom = DPIUtil.getDeviceZoom();
DPIUtil.setDeviceZoom(200);
DPIUtil.setDeviceZoom(ZOOM_200);
}

@AfterEach
Expand All @@ -57,10 +58,10 @@ public void scaleDownFloatArray() {

float[] scaledValue = DPIUtil.autoScaleDown(valueAt200);
assertArrayEquals(valueAt100, scaledValue, .001f, String.format("Scaling down float array from device zoom (%d) to 100 failed",
DPIUtil.getDeviceZoom()));
ZOOM_200));
scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200);
assertArrayEquals(valueAt100, scaledValue, .001f, String.format("Scaling down float array from device zoom (%d) to 100 with device failed",
DPIUtil.getDeviceZoom()));
ZOOM_200));

scaledValue = DPIUtil.scaleDown(valueAt200, 200);
assertArrayEquals(valueAt100, scaledValue, .001f, "Scaling down float array from 200 failed");
Expand All @@ -84,10 +85,10 @@ public void scaleDownInteger() {

int scaledValue = DPIUtil.autoScaleDown(valueAt200);
assertEquals(
valueAt100, scaledValue, String.format("Scaling down integer from device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
valueAt100, scaledValue, String.format("Scaling down integer from device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200);
assertEquals(
valueAt100, scaledValue, String.format("Scaling down integer from device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
valueAt100, scaledValue, String.format("Scaling down integer from device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleDown(valueAt200, 200);
assertEquals(valueAt100, scaledValue, "Scaling down integer from 200 failed");
Expand All @@ -111,10 +112,10 @@ public void scaleDownFloat() {

float scaledValue = DPIUtil.autoScaleDown(valueAt200);
assertEquals(valueAt100, scaledValue, .001f,
String.format("Scaling down float from device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling down float from device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200);
assertEquals(valueAt100, scaledValue, .001f, String
.format("Scaling down float from device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
.format("Scaling down float from device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleDown(valueAt200, 200);
assertEquals(valueAt100, scaledValue, .001f, "Scaling down float from 200 failed");
Expand All @@ -138,10 +139,10 @@ public void scaleDownPoint() {

Point scaledValue = DPIUtil.autoScaleDown(valueAt200);
assertEquals(valueAt100, scaledValue,
String.format("Scaling down Point from device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling down Point from device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200);
assertEquals(valueAt100, scaledValue, String
.format("Scaling down Point from device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
.format("Scaling down Point from device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleDown(valueAt200, 200);
assertEquals(valueAt100, scaledValue, "Scaling down Point from 200 failed");
Expand All @@ -165,10 +166,10 @@ public void scaleDownRectangle() {

Rectangle scaledValue = DPIUtil.autoScaleDown(valueAt200);
assertEquals(valueAt100, scaledValue,
String.format("Scaling down Rectangle from device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling down Rectangle from device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleDown((Device) null, valueAt200);
assertEquals(valueAt100, scaledValue, String.format(
"Scaling down Rectangle from device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
"Scaling down Rectangle from device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleDown(valueAt200, 200);
assertEquals(valueAt100, scaledValue, "Scaling down Rectangle from 200 failed");
Expand All @@ -192,10 +193,10 @@ public void scaleUpIntArray() {

int[] scaledValue = DPIUtil.autoScaleUp(valueAt100);
assertArrayEquals(valueAt200, scaledValue,
String.format("Scaling up int array to device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling up int array to device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100);
assertArrayEquals(valueAt200, scaledValue, String
.format("Scaling up int array to device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
.format("Scaling up int array to device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleUp(valueAt100, 200);
assertArrayEquals(valueAt200, scaledValue, "Scaling up int array to 200 failed");
Expand All @@ -219,10 +220,10 @@ public void scaleUpInteger() {

int scaledValue = DPIUtil.autoScaleUp(valueAt100);
assertEquals(valueAt200, scaledValue,
String.format("Scaling up integer to device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling up integer to device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100);
assertEquals(valueAt200, scaledValue, String
.format("Scaling up integer to device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
.format("Scaling up integer to device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleUp(valueAt100, 200);
assertEquals(valueAt200, scaledValue, "Scaling up integer to 200 failed");
Expand All @@ -246,10 +247,10 @@ public void scaleUpFloat() {

float scaledValue = DPIUtil.autoScaleUp(valueAt100);
assertEquals(valueAt200, scaledValue, 0.001f,
String.format("Scaling up integer to device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling up integer to device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100);
assertEquals(valueAt200, scaledValue, 0.001f, String
.format("Scaling up integer to device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
.format("Scaling up integer to device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleUp(valueAt100, 200);
assertEquals(valueAt200, scaledValue, 0.001f, "Scaling up integer to 200 failed");
Expand All @@ -273,10 +274,10 @@ public void scaleUpPoint() {

Point scaledValue = DPIUtil.autoScaleUp(valueAt100);
assertEquals(valueAt200, scaledValue,
String.format("Scaling up Point to device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling up Point to device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100);
assertEquals(valueAt200, scaledValue, String
.format("Scaling up Point to device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
.format("Scaling up Point to device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleUp(valueAt100, 200);
assertEquals(valueAt200, scaledValue, "Scaling up Point to 200 failed");
Expand All @@ -300,10 +301,10 @@ public void scaleUpRectangle() {

Rectangle scaledValue = DPIUtil.autoScaleUp(valueAt100);
assertEquals(valueAt200, scaledValue,
String.format("Scaling up Rectangle to device zoom (%d) to 100 failed", DPIUtil.getDeviceZoom()));
String.format("Scaling up Rectangle to device zoom (%d) to 100 failed", ZOOM_200));
scaledValue = DPIUtil.autoScaleUp((Device) null, valueAt100);
assertEquals(valueAt200, scaledValue, String
.format("Scaling up Rectangle to device zoom (%d) to 100 with device failed", DPIUtil.getDeviceZoom()));
.format("Scaling up Rectangle to device zoom (%d) to 100 with device failed", ZOOM_200));

scaledValue = DPIUtil.scaleUp(valueAt100, 200);
assertEquals(valueAt200, scaledValue, "Scaling up Rectangle to 200 failed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -154,13 +155,9 @@ public void test_copyAreaIIIIII() {
ImageData imageData = image.getImageData();
PaletteData palette = imageData.palette;

if (DPIUtil.getDeviceZoom() != 100) {
//TODO Fix non integer scaling factors.
if (SwtTestUtil.verbose) {
System.out.println("Excluded test_copyAreaIIIIII(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_GC)");
}
return;
}
Comment on lines -157 to -163
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that this can this safely be removed, i.e, will the test run properly on with deviceZoom != 100 on every OS? When I execute this test on Windows with 175% primary monitor zoom, it fails (probably because of fractional scaling rounding errors). So maybe we should rather replace this with an according JUnit assumption but in general keep the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this on Windows 11 with various zoom levels (100%, 125%, 150%, 175%, 200%, and 225%) and observed that the tests passed in all cases, which is why I removed the check.
Could you share the exact error you're encountering at 175%—is it an assertion failure?
Also, are you using any specific arguments in your run configuration?

If we replace the check with a JUnit assumption, don’t we still need to call DPIUtil.getDeviceZoom()? Or is there another way to get the primary monitor’s zoom level that works across platforms?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found that I cannot reproduce the issue with your exact branch, but when rebasing it on top of the lastest master branch state. I guess we made some adaptations since you last rebased this branch on master that affect these tests.

When executing the tests at 175% primary monitor zoom on that state, I get this error:

java.lang.AssertionError: :b: expected:<RGB {0, 0, 255}> but was:<RGB {255, 255, 255}>
	at org.junit.Assert.fail(Assert.java:89)
	at org.junit.Assert.failNotEquals(Assert.java:835)
	at org.junit.Assert.assertEquals(Assert.java:120)
	at org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_GC.test_copyAreaIIIIII(Test_org_eclipse_swt_graphics_GC.java:160)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)

If we replace the check with a JUnit assumption, don’t we still need to call DPIUtil.getDeviceZoom()? Or is there another way to get the primary monitor’s zoom level that works across platforms?

Yes, we would then still need to use DPIUtil.getDeviceZoom(). But our primary goal it to reevaluate all existing usages whether they can be replaced or whether they are fine and we may consider it fine to be used here as we are not in the situation that the returned value is useless because of potentially having multiple shells placed at different monitors with different zoom values on Windows (which is actually the use case in which the usage of get[Native]DeviceZoom() fails).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may consider it fine to be used here as we are not in the situation that the returned value is useless because of potentially having multiple shells placed at different monitors with different zoom values on Windows (which is actually the use case in which the usage of get[Native]DeviceZoom() fails).

Yeah makes sense, I will modify this to junit assumptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the checks with junit assumption

// This test verifies pixel-level color values after a copyArea() operation.
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");

int pixel = imageData.getPixel(destX + 4, destY);
assertEquals(":a:", whiteRGB, palette.getRGB(pixel));
Expand Down Expand Up @@ -199,13 +196,9 @@ public void test_copyAreaIIIIII_overlapingSourceTarget() {
imageData = image.getImageData();
palette = imageData.palette;

if (DPIUtil.getDeviceZoom() != 100) {
//TODO Fix non integer scaling factors.
if (SwtTestUtil.verbose) {
System.out.println("Excluded test_copyAreaIIIIII(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_GC)");
}
return;
}
// This test verifies pixel-level color values after a copyArea() operation.
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");

pixel = imageData.getPixel(0, 105);
assertEquals(redRGB, palette.getRGB(pixel));
Expand Down Expand Up @@ -234,14 +227,9 @@ public void test_copyAreaLorg_eclipse_swt_graphics_ImageII() {
ImageData imageData = image.getImageData();
PaletteData palette = imageData.palette;

if (DPIUtil.getDeviceZoom() != 100) {
//TODO Fix non integer scaling factors.
if (SwtTestUtil.verbose) {
System.out.println("Excluded test_copyAreaLorg_eclipse_swt_graphics_ImageII(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_GC)");
}
image.dispose();
return;
}
// This test verifies pixel-level color values after a copyArea() operation.
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");

int pixel = imageData.getPixel(4, 0);
assertEquals(":a:", whiteRGB, palette.getRGB(pixel));
Expand Down
Loading
Loading