Skip to content

Commit b339485

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Refactor Image(Display, int, int) in Tests (Automated Tests)
Replacing Image(Display, int, int) with Image(Display, ImageGcDrawer, int, int) in automated test cases.
1 parent a0e1809 commit b339485

File tree

4 files changed

+88
-92
lines changed

4 files changed

+88
-92
lines changed

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
import org.eclipse.swt.dnd.Transfer;
3737
import org.eclipse.swt.dnd.URLTransfer;
3838
import org.eclipse.swt.graphics.Color;
39-
import org.eclipse.swt.graphics.GC;
4039
import org.eclipse.swt.graphics.Image;
4140
import org.eclipse.swt.graphics.ImageData;
41+
import org.eclipse.swt.graphics.ImageGcDrawer;
4242
import org.eclipse.swt.graphics.PaletteData;
4343
import org.eclipse.swt.graphics.Point;
4444
import org.eclipse.swt.layout.RowLayout;
@@ -239,18 +239,17 @@ public void testUrlTransfer() throws InterruptedException {
239239
* Creates a DDB test image with a uniform color applied to all pixels.
240240
*/
241241
private Image createTestImage() {
242-
final Image image = new Image(shell.getDisplay(), 16, 16);
243242
try {
244243
Color color = shell.getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
245-
GC gc = new GC(image);
246-
gc.setBackground(color);
247-
gc.fillRectangle(image.getBounds());
248-
gc.dispose();
244+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
245+
gc.setBackground(color);
246+
gc.fillRectangle(0, 0, width, height);
247+
};
248+
return new Image(shell.getDisplay(), imageGcDrawer, 16, 16);
249249
} catch (Exception e) {
250-
image.dispose();
251250
fail("test image could not be initialized: " + e);
252251
}
253-
return image;
252+
return null;
254253
}
255254

256255
/**

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.eclipse.swt.graphics.GC;
3838
import org.eclipse.swt.graphics.Image;
3939
import org.eclipse.swt.graphics.ImageData;
40+
import org.eclipse.swt.graphics.ImageGcDrawer;
4041
import org.eclipse.swt.graphics.LineAttributes;
4142
import org.eclipse.swt.graphics.PaletteData;
4243
import org.eclipse.swt.graphics.Point;
@@ -837,20 +838,16 @@ public void test_bug1288_createGCFromImageFromNonDisplayThread() throws Interrup
837838
* (16bpp or less).
838839
*/
839840
RGB getRealRGB(Color color) {
840-
Image colorImage = new Image(display, 10, 10);
841-
GC imageGc = new GC(colorImage);
842-
ImageData imageData;
843-
PaletteData palette;
844-
int pixel;
845-
846-
imageGc.setBackground(color);
847-
imageGc.setForeground(color);
848-
imageGc.fillRectangle(0, 0, 10, 10);
849-
imageData = colorImage.getImageData();
850-
palette = imageData.palette;
851-
imageGc.dispose();
841+
ImageGcDrawer gcDrawer = (imageGc, width, height) -> {
842+
imageGc.setBackground(color);
843+
imageGc.setForeground(color);
844+
imageGc.fillRectangle(0, 0, width, height);
845+
};
846+
Image colorImage = new Image(display, gcDrawer, 10, 10);
847+
ImageData imageData = colorImage.getImageData();
848+
PaletteData palette = imageData.palette;
852849
colorImage.dispose();
853-
pixel = imageData.getPixel(0, 0);
850+
int pixel = imageData.getPixel(0, 0);
854851
return palette.getRGB(pixel);
855852
}
856853

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

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,17 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
209209
data = new ImageData(10, 10, 8, new PaletteData(0x30, 0x0C, 0x03));
210210
// set red pixel at x=9, y=9
211211
data.setPixel(9, 9, 0x30);
212-
image = new Image(display, data);
213-
Image gcImage = new Image(display, 10, 10);
214-
GC gc = new GC(gcImage);
215-
gc.drawImage(image, 0, 0);
212+
final Image imageFromImageData = new Image(display, data);
213+
ImageGcDrawer gcDrawer = (gc, width, height) -> {
214+
gc.drawImage(imageFromImageData, 0, 0);
215+
};
216+
Image gcImage = new Image(display, gcDrawer, 10, 10);
216217
ImageData gcImageData = gcImage.getImageData();
217218
int redPixel = gcImageData.getPixel(9, 9);
218219
assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel));
219-
gc.dispose();
220220
gcImage.dispose();
221221
image.dispose();
222+
imageFromImageData.dispose();
222223
}
223224

224225
@Test
@@ -252,21 +253,23 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
252253
data6.setPixel(9, 9, 0x30);
253254
data7 = new ImageData(10, 10, 1, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
254255
data7.setPixel(9, 9, 1);
255-
image = new Image(display, data6, data7);
256-
Image gcImage = new Image(display, 10, 10);
257-
GC gc = new GC(gcImage);
256+
Image image2 = new Image(display, data6, data7);
258257
Color backgroundColor = display.getSystemColor(SWT.COLOR_BLUE);
259-
gc.setBackground(backgroundColor);
260-
gc.fillRectangle(0, 0, 10, 10);
261-
gc.drawImage(image, 0, 0);
258+
final ImageGcDrawer gcDrawer = (gc, width, height) -> {
259+
gc.setBackground(backgroundColor);
260+
gc.fillRectangle(0, 0, 10, 10);
261+
gc.drawImage(image2, 0, 0);
262+
};
263+
Image gcImage = new Image(display, gcDrawer, 10, 10);
264+
262265
ImageData gcImageData = gcImage.getImageData();
263266
int redPixel = gcImageData.getPixel(9, 9);
264267
assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel));
265268
int bluePixel = gcImageData.getPixel(0, 0);
266269
assertEquals(getRealRGB(backgroundColor), gcImageData.palette.getRGB(bluePixel));
267-
gc.dispose();
268270
gcImage.dispose();
269271
image.dispose();
272+
image2.dispose();
270273
}
271274

272275
@Test
@@ -544,7 +547,7 @@ public void test_equalsLjava_lang_Object() {
544547

545548
@Test
546549
public void test_getBackground() {
547-
Image image = new Image(display, 10, 10);
550+
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
548551
image.dispose();
549552
SWTException e = assertThrows(SWTException.class, () -> image.getBackground());
550553
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
@@ -554,19 +557,19 @@ public void test_getBackground() {
554557
@Test
555558
public void test_getBounds() {
556559
Rectangle bounds = new Rectangle(0, 0, 10, 20);
557-
Image image1 = new Image(display, bounds.width, bounds.height);
560+
Image image1 = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
558561
image1.dispose();
559562
SWTException e = assertThrows(SWTException.class, () -> image1.getBounds());
560563
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
561564

562565
Image image;
563566
// creates bitmap image
564-
image = new Image(display, bounds.width, bounds.height);
567+
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
565568
Rectangle bounds1 = image.getBounds();
566569
image.dispose();
567570
assertEquals(bounds, bounds1);
568571

569-
image = new Image(display, bounds.width, bounds.height);
572+
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
570573
bounds1 = image.getBounds();
571574
image.dispose();
572575
assertEquals(bounds, bounds1);
@@ -722,6 +725,13 @@ void getImageData_int(int zoom) {
722725
image.dispose();
723726
assertEquals(":a: Size of ImageData returned from Image.getImageData(int) method doesn't return matches with bounds in Pixel values.", scaleBounds(bounds, zoom, 100), boundsAtZoom);
724727

728+
// creates bitmap image with GCDrawer and compare size of imageData
729+
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
730+
imageDataAtZoom = image.getImageData(zoom);
731+
image.dispose();
732+
boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height);
733+
assertEquals(":a: Size of ImageData returned from Image.getImageData(int) method doesn't return matches with bounds in Pixel values.", scaleBounds(bounds, zoom, 100), boundsAtZoom);
734+
725735
// create icon image and compare size of imageData
726736
ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)}));
727737
image = new Image(display, imageData);
@@ -771,7 +781,7 @@ public void test_hashCode() {
771781
Image image1 = null;
772782

773783
try {
774-
image = new Image(display, 10, 10);
784+
image = new Image(display, (gc, width, height) -> {}, 10, 10);
775785
image1 = image;
776786

777787
assertEquals(image1.hashCode(), image.hashCode());
@@ -832,14 +842,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
832842
"Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)",
833843
SwtTestUtil.isGTK);
834844
// TODO Fix GTK failure.
835-
Image image1 = new Image(display, 10, 10);
845+
Image image1 = new Image(display, (gc, width, height) -> {}, 10, 10);
836846
try {
837847
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> image1.setBackground(null));
838848
assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e);
839849
} finally {
840850
image1.dispose();
841851
}
842-
Image image2 = new Image(display, 10, 10);
852+
Image image2 = new Image(display, (gc, width, height) -> {}, 10, 10);
843853
Color color2 = new Color(255, 255, 255);
844854
color2.dispose();
845855
try {
@@ -848,14 +858,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
848858
} finally {
849859
image2.dispose();
850860
}
851-
Image image3 = new Image(display, 10, 10);
861+
Image image3 = new Image(display, (gc, width, height) -> {}, 10, 10);
852862
image3.dispose();
853863
Color color3 = new Color(255, 255, 255);
854864
SWTException e = assertThrows(SWTException.class, () -> image3.setBackground(color3));
855865
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
856866

857867
// this image does not have a transparent pixel by default so setBackground has no effect
858-
Image image4 = new Image(display, 10, 10);
868+
Image image4 = new Image(display, (gc, width, height) -> {}, 10, 10);
859869
image4.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
860870
Color color4 = image4.getBackground();
861871
assertNull("background color should be null for non-transparent image", color4);
@@ -874,7 +884,7 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
874884

875885
@Test
876886
public void test_toString() {
877-
Image image = new Image(display, 10, 10);
887+
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
878888
try {
879889
assertNotNull(image.toString());
880890
assertTrue(image.toString().length() > 0);
@@ -936,20 +946,16 @@ void getImageData2(int depth, PaletteData palette) {
936946
}
937947

938948
RGB getRealRGB(Color color) {
939-
Image colorImage = new Image(display, 10, 10);
940-
GC imageGc = new GC(colorImage);
941-
ImageData imageData;
942-
PaletteData palette;
943-
int pixel;
944-
945-
imageGc.setBackground(color);
946-
imageGc.setForeground(color);
947-
imageGc.fillRectangle(0, 0, 10, 10);
948-
imageData = colorImage.getImageData();
949-
palette = imageData.palette;
950-
imageGc.dispose();
949+
ImageGcDrawer gcDrawer = (imageGc, width, height) -> {
950+
imageGc.setBackground(color);
951+
imageGc.setForeground(color);
952+
imageGc.fillRectangle(0, 0, width, height);
953+
};
954+
Image colorImage = new Image(display, gcDrawer, 10, 10);
955+
ImageData imageData = colorImage.getImageData();
956+
PaletteData palette = imageData.palette;
951957
colorImage.dispose();
952-
pixel = imageData.getPixel(0, 0);
958+
int pixel = imageData.getPixel(0, 0);
953959
return palette.getRGB(pixel);
954960
}
955961

@@ -977,15 +983,16 @@ public void test_bug566545_efficientGrayscaleImage() {
977983

978984
Image imageIndexed = new Image(display, imageDataIndexed);
979985
Image imageDirect = new Image(display, imageDataDirect);
980-
Image outImageIndexed = new Image(display, width, height);
981-
Image outImageDirect = new Image(display, width, height);
982986

983-
GC gc = new GC(outImageIndexed);
984-
gc.drawImage(imageIndexed, 0, 0);
985-
gc.dispose();
986-
gc = new GC(outImageDirect);
987-
gc.drawImage(imageDirect, 0, 0);
988-
gc.dispose();
987+
ImageGcDrawer gcDrawer1 = (gc, iWidth, iHeight) -> {
988+
gc.drawImage(imageIndexed, 0, 0);
989+
};
990+
Image outImageIndexed = new Image(display, gcDrawer1, width, height);
991+
992+
ImageGcDrawer gcDrawer2 = (gc, iWidth, iHeight) -> {
993+
gc.drawImage(imageDirect, 0, 0);
994+
};
995+
Image outImageDirect = new Image(display, gcDrawer2, width, height);
989996

990997
ImageTestUtil.assertImagesEqual(imageDataIndexed, imageDataDirect);
991998
ImageTestUtil.assertImagesEqual(imageIndexed.getImageData(), imageDirect.getImageData());
@@ -1002,7 +1009,7 @@ public void test_updateWidthHeightAfterDPIChange() {
10021009
int deviceZoom = DPIUtil.getDeviceZoom();
10031010
try {
10041011
Rectangle imageSize = new Rectangle(0, 0, 16, 16);
1005-
Image baseImage = new Image(display, imageSize.width, imageSize.height);
1012+
Image baseImage = new Image(display, (gc, width, height) -> {}, imageSize.width, imageSize.height);
10061013
GC gc = new GC(display);
10071014
gc.drawImage(baseImage, 10, 10);
10081015
assertEquals("Base image size differs unexpectedly", imageSize, baseImage.getBounds());

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.eclipse.swt.SWT;
2727
import org.eclipse.swt.graphics.Color;
2828
import org.eclipse.swt.graphics.Font;
29-
import org.eclipse.swt.graphics.GC;
3029
import org.eclipse.swt.graphics.Image;
30+
import org.eclipse.swt.graphics.ImageGcDrawer;
3131
import org.eclipse.swt.graphics.Point;
3232
import org.eclipse.swt.graphics.Rectangle;
3333
import org.eclipse.swt.graphics.TextLayout;
@@ -938,17 +938,9 @@ public void test_getTextDirection() {
938938
public void test_bug568740_multilineTextStyle() {
939939
Font font = null;
940940
Image image = null;
941-
GC gc = null;
942-
TextLayout layout = null;
941+
final TextLayout layout = new TextLayout(display);
942+
int offset = 10;
943943
try {
944-
font = new Font(display, SwtTestUtil.testFontName, 16, SWT.NORMAL);
945-
image = new Image(display, 200, 100);
946-
gc = new GC(image);
947-
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
948-
gc.fillRectangle(image.getBounds());
949-
gc.setAntialias(SWT.OFF); // aa can change colors and break the test in worst case
950-
951-
layout = new TextLayout(display);
952944
layout.setFont(font);
953945
layout.setText("first line\nsecond line");
954946

@@ -974,8 +966,15 @@ public void test_bug568740_multilineTextStyle() {
974966
controlStyle.strikeoutColor = display.getSystemColor(SWT.COLOR_DARK_RED);
975967
layout.setStyle(controlStyle, 15, 23);
976968

977-
int offset = 10;
978-
layout.draw(gc, offset, offset);
969+
font = new Font(display, SwtTestUtil.testFontName, 16, SWT.NORMAL);
970+
final ImageGcDrawer gcDrawer = (gc, width, height) -> {
971+
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
972+
gc.fillRectangle(0, 0, width, height);
973+
gc.setAntialias(SWT.OFF); // aa can change colors and break the test in worst case
974+
layout.draw(gc, offset, offset);
975+
};
976+
image = new Image(display, gcDrawer, 200, 100);
977+
image.getImageData(); // ensures that the ImageGcDrawer is executed immediately
979978

980979
Rectangle firstLineBounds = layout.getLineBounds(0);
981980
Rectangle searchRangeBorder = new Rectangle(0, 0, image.getBounds().width, offset + (int)(firstLineBounds.height * 0.3));
@@ -993,8 +992,6 @@ public void test_bug568740_multilineTextStyle() {
993992
} finally {
994993
if (layout != null)
995994
layout.dispose();
996-
if (gc != null)
997-
gc.dispose();
998995
if (image != null)
999996
image.dispose();
1000997
if (font != null)
@@ -1007,21 +1004,17 @@ public void test_bug568740_multilineTextStyle() {
10071004
* disposed by caller.
10081005
*/
10091006
private Image draw(TextLayout layout, int antialias) {
1010-
GC gc = null;
1011-
try {
1012-
Rectangle rect = layout.getBounds();
1013-
Image image = new Image(display, rect.width, rect.height);
1014-
gc = new GC(image);
1007+
Rectangle rect = layout.getBounds();
1008+
final ImageGcDrawer gcDrawer = (gc, height, width) -> {
10151009
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
1016-
gc.fillRectangle(image.getBounds());
1010+
gc.fillRectangle(0, 0, width, height);
10171011
gc.setAntialias(antialias);
1018-
10191012
layout.draw(gc, 0, 0);
1020-
return image;
1021-
} finally {
1022-
if (gc != null)
1023-
gc.dispose();
1024-
}
1013+
};
1014+
Image image = new Image(display, gcDrawer, rect.width, rect.height);
1015+
image.getImageData(); // ensures that the ImageGcDrawer is executed immediately
1016+
1017+
return image;
10251018
}
10261019

10271020
/**

0 commit comments

Comments
 (0)