Skip to content

Commit 28a8e03

Browse files
committed
Adjust image test assertions for consistent device zoom handling
This makes the image tests also work properly on monitors with a different zoom than 100%.
1 parent a4a774c commit 28a8e03

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
214214
data.setPixel(9, 9, 0x30);
215215
final Image imageFromImageData = new Image(display, data);
216216
try {
217+
int originalDeviceZoom = DPIUtil.getDeviceZoom();
218+
DPIUtil.setDeviceZoom(100);
217219
ImageGcDrawer gcDrawer = (gc, width, height) -> {
218220
gc.drawImage(imageFromImageData, 0, 0);
219221
};
@@ -224,6 +226,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
224226
assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel));
225227
} finally {
226228
gcImage.dispose();
229+
DPIUtil.setDeviceZoom(originalDeviceZoom);
227230
}
228231
image.dispose();
229232
} finally {
@@ -269,6 +272,8 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
269272
gc.fillRectangle(0, 0, 10, 10);
270273
gc.drawImage(image2, 0, 0);
271274
};
275+
int originalDeviceZoom = DPIUtil.getDeviceZoom();
276+
DPIUtil.setDeviceZoom(100);
272277
Image gcImage = new Image(display, gcDrawer, 10, 10);
273278
try {
274279
ImageData gcImageData = gcImage.getImageData();
@@ -278,6 +283,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
278283
assertEquals(getRealRGB(backgroundColor), gcImageData.palette.getRGB(bluePixel));
279284
} finally {
280285
gcImage.dispose();
286+
DPIUtil.setDeviceZoom(originalDeviceZoom);
281287
}
282288
image.dispose();
283289
image2.dispose();
@@ -643,6 +649,7 @@ public void test_getBounds() {
643649
@Test
644650
public void test_getBoundsInPixels() {
645651
Rectangle initialBounds = new Rectangle(0, 0, 10, 20);
652+
Rectangle scaledInitialBounds = scaleBounds(initialBounds, DPIUtil.getDeviceZoom(), 100);
646653
Image image1 = new Image(display, initialBounds.width, initialBounds.height);
647654
image1.dispose();
648655
SWTException e = assertThrows(SWTException.class, () -> image1.getBoundsInPixels());
@@ -655,7 +662,7 @@ public void test_getBoundsInPixels() {
655662
Rectangle bounds = image.getBounds();
656663
image.dispose();
657664
assertEquals(initialBounds, bounds);
658-
assertEquals(initialBounds, boundsInPixels);
665+
assertEquals(scaledInitialBounds, boundsInPixels);
659666

660667
// create icon image
661668
ImageData imageData = new ImageData(initialBounds.width, initialBounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)}));
@@ -664,29 +671,29 @@ public void test_getBoundsInPixels() {
664671
bounds = image.getBounds();
665672
image.dispose();
666673
assertEquals(initialBounds, bounds);
667-
assertEquals(initialBounds, boundsInPixels);
674+
assertEquals(scaledInitialBounds, boundsInPixels);
668675

669676
// create image with FileNameProvider
670677
image = new Image(display, imageFileNameProvider);
671678
boundsInPixels = image.getBoundsInPixels();
672679
bounds = image.getBounds();
673680
image.dispose();
674-
assertEquals(bounds, boundsInPixels);
681+
assertEquals(scaleBounds(bounds, DPIUtil.getDeviceZoom(), 100), boundsInPixels);
675682

676683
// create image with ImageDataProvider
677684
image = new Image(display, imageDataProvider);
678685
boundsInPixels = image.getBoundsInPixels();
679686
bounds = image.getBounds();
680687
image.dispose();
681-
assertEquals(bounds, boundsInPixels);
688+
assertEquals(scaleBounds(bounds, DPIUtil.getDeviceZoom(), 100), boundsInPixels);
682689

683690
// create image with ImageGcDrawer
684691
image = new Image(display, imageGcDrawer, initialBounds.width, initialBounds.height);
685692
boundsInPixels = image.getBoundsInPixels();
686693
bounds = image.getBounds();
687694
image.dispose();
688695
assertEquals(initialBounds, bounds);
689-
assertEquals(initialBounds, boundsInPixels);
696+
assertEquals(scaledInitialBounds, boundsInPixels);
690697
}
691698

692699
@SuppressWarnings("removal")
@@ -704,31 +711,31 @@ public void test_getImageDataCurrentZoom() {
704711
ImageData imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
705712
image.dispose();
706713
Rectangle boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
707-
assertEquals(boundsAtCurrentZoom, bounds);
714+
assertEquals(boundsAtCurrentZoom, scaleBounds(bounds, DPIUtil.getDeviceZoom(), 100));
708715

709716
// create icon image and compare size of imageData
710717
ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)}));
711718
image = new Image(display, imageData);
712719
imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
713720
image.dispose();
714721
boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
715-
assertEquals(boundsAtCurrentZoom, bounds);
722+
assertEquals(boundsAtCurrentZoom, scaleBounds(bounds, DPIUtil.getDeviceZoom(), 100));
716723

717724
// create image with FileNameProvider
718725
image = new Image(display, imageFileNameProvider);
719726
imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
720727
boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
721728
bounds = image.getBounds();
722729
image.dispose();
723-
assertEquals(boundsAtCurrentZoom, bounds);
730+
assertEquals(boundsAtCurrentZoom, scaleBounds(bounds, DPIUtil.getDeviceZoom(), 100));
724731

725732
// create image with ImageDataProvider
726733
image = new Image(display, imageDataProvider);
727734
imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
728735
boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
729736
bounds = image.getBounds();
730737
image.dispose();
731-
assertEquals(boundsAtCurrentZoom, bounds);
738+
assertEquals(boundsAtCurrentZoom, scaleBounds(bounds, DPIUtil.getDeviceZoom(), 100));
732739
}
733740

734741
@Test
@@ -1128,8 +1135,8 @@ public void test_imageDataIsCached() {
11281135
};
11291136
Image fileNameProviderImage = new Image(display, imageFileNameProvider);
11301137
try {
1131-
callCount.set(0);
11321138
fileNameProviderImage.getImageData(100);
1139+
callCount.set(0);
11331140
fileNameProviderImage.getImageData(100);
11341141
fileNameProviderImage.getImageData(100);
11351142
assertEquals(0, callCount.get());

0 commit comments

Comments
 (0)