Skip to content

Commit c779292

Browse files
Refactor Image(Display, int, int) in Tests (Automated Tests)
Replacing Image(Display, int, int) with Image(Display, ImageGcDrawer, int, int) in Tests. Automated Test Case.
1 parent cad131b commit c779292

File tree

4 files changed

+87
-92
lines changed

4 files changed

+87
-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
@@ -208,16 +208,17 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra
208208
data = new ImageData(10, 10, 8, new PaletteData(0x30, 0x0C, 0x03));
209209
// set red pixel at x=9, y=9
210210
data.setPixel(9, 9, 0x30);
211-
image = new Image(display, data);
212-
Image gcImage = new Image(display, 10, 10);
213-
GC gc = new GC(gcImage);
214-
gc.drawImage(image, 0, 0);
211+
final Image imageFromImageData = new Image(display, data);
212+
ImageGcDrawer gcDrawer = (gc, width, height) -> {
213+
gc.drawImage(imageFromImageData, 0, 0);
214+
};
215+
Image gcImage = new Image(display, gcDrawer, 10, 10);
215216
ImageData gcImageData = gcImage.getImageData();
216217
int redPixel = gcImageData.getPixel(9, 9);
217218
assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel));
218-
gc.dispose();
219219
gcImage.dispose();
220220
image.dispose();
221+
imageFromImageData.dispose();
221222
}
222223

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

271274
@Test
@@ -543,7 +546,7 @@ public void test_equalsLjava_lang_Object() {
543546

544547
@Test
545548
public void test_getBackground() {
546-
Image image = new Image(display, 10, 10);
549+
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
547550
image.dispose();
548551
SWTException e = assertThrows(SWTException.class, () -> image.getBackground());
549552
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
@@ -553,19 +556,19 @@ public void test_getBackground() {
553556
@Test
554557
public void test_getBounds() {
555558
Rectangle bounds = new Rectangle(0, 0, 10, 20);
556-
Image image1 = new Image(display, bounds.width, bounds.height);
559+
Image image1 = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
557560
image1.dispose();
558561
SWTException e = assertThrows(SWTException.class, () -> image1.getBounds());
559562
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
560563

561564
Image image;
562565
// creates bitmap image
563-
image = new Image(display, bounds.width, bounds.height);
566+
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
564567
Rectangle bounds1 = image.getBounds();
565568
image.dispose();
566569
assertEquals(bounds, bounds1);
567570

568-
image = new Image(display, bounds.width, bounds.height);
571+
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
569572
bounds1 = image.getBounds();
570573
image.dispose();
571574
assertEquals(bounds, bounds1);
@@ -721,6 +724,13 @@ void getImageData_int(int zoom) {
721724
image.dispose();
722725
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);
723726

727+
// creates bitmap image with GCDrawer and compare size of imageData
728+
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
729+
imageDataAtZoom = image.getImageData(zoom);
730+
image.dispose();
731+
boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height);
732+
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);
733+
724734
// create icon image and compare size of imageData
725735
ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)}));
726736
image = new Image(display, imageData);
@@ -770,7 +780,7 @@ public void test_hashCode() {
770780
Image image1 = null;
771781

772782
try {
773-
image = new Image(display, 10, 10);
783+
image = new Image(display, (gc, width, height) -> {}, 10, 10);
774784
image1 = image;
775785

776786
assertEquals(image1.hashCode(), image.hashCode());
@@ -831,14 +841,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
831841
"Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)",
832842
SwtTestUtil.isGTK);
833843
// TODO Fix GTK failure.
834-
Image image1 = new Image(display, 10, 10);
844+
Image image1 = new Image(display, (gc, width, height) -> {}, 10, 10);
835845
try {
836846
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> image1.setBackground(null));
837847
assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e);
838848
} finally {
839849
image1.dispose();
840850
}
841-
Image image2 = new Image(display, 10, 10);
851+
Image image2 = new Image(display, (gc, width, height) -> {}, 10, 10);
842852
Color color2 = new Color(255, 255, 255);
843853
color2.dispose();
844854
try {
@@ -847,14 +857,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
847857
} finally {
848858
image2.dispose();
849859
}
850-
Image image3 = new Image(display, 10, 10);
860+
Image image3 = new Image(display, (gc, width, height) -> {}, 10, 10);
851861
image3.dispose();
852862
Color color3 = new Color(255, 255, 255);
853863
SWTException e = assertThrows(SWTException.class, () -> image3.setBackground(color3));
854864
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
855865

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

874884
@Test
875885
public void test_toString() {
876-
Image image = new Image(display, 10, 10);
886+
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
877887
try {
878888
assertNotNull(image.toString());
879889
assertTrue(image.toString().length() > 0);
@@ -935,20 +945,16 @@ void getImageData2(int depth, PaletteData palette) {
935945
}
936946

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

@@ -976,15 +982,16 @@ public void test_bug566545_efficientGrayscaleImage() {
976982

977983
Image imageIndexed = new Image(display, imageDataIndexed);
978984
Image imageDirect = new Image(display, imageDataDirect);
979-
Image outImageIndexed = new Image(display, width, height);
980-
Image outImageDirect = new Image(display, width, height);
981985

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

989996
ImageTestUtil.assertImagesEqual(imageDataIndexed, imageDataDirect);
990997
ImageTestUtil.assertImagesEqual(imageIndexed.getImageData(), imageDirect.getImageData());
@@ -1001,7 +1008,7 @@ public void test_updateWidthHeightAfterDPIChange() {
10011008
int deviceZoom = DPIUtil.getDeviceZoom();
10021009
try {
10031010
Rectangle imageSize = new Rectangle(0, 0, 16, 16);
1004-
Image baseImage = new Image(display, imageSize.width, imageSize.height);
1011+
Image baseImage = new Image(display, (gc, width, height) -> {}, imageSize.width, imageSize.height);
10051012
GC gc = new GC(display);
10061013
gc.drawImage(baseImage, 10, 10);
10071014
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: 19 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,14 @@ 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);
979977

980978
Rectangle firstLineBounds = layout.getLineBounds(0);
981979
Rectangle searchRangeBorder = new Rectangle(0, 0, image.getBounds().width, offset + (int)(firstLineBounds.height * 0.3));
@@ -993,8 +991,6 @@ public void test_bug568740_multilineTextStyle() {
993991
} finally {
994992
if (layout != null)
995993
layout.dispose();
996-
if (gc != null)
997-
gc.dispose();
998994
if (image != null)
999995
image.dispose();
1000996
if (font != null)
@@ -1007,21 +1003,17 @@ public void test_bug568740_multilineTextStyle() {
10071003
* disposed by caller.
10081004
*/
10091005
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);
1006+
Rectangle rect = layout.getBounds();
1007+
final ImageGcDrawer gcDrawer = (gc, height, width) -> {
10151008
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
1016-
gc.fillRectangle(image.getBounds());
1009+
gc.fillRectangle(0, 0, width, height);
10171010
gc.setAntialias(antialias);
1018-
10191011
layout.draw(gc, 0, 0);
1020-
return image;
1021-
} finally {
1022-
if (gc != null)
1023-
gc.dispose();
1024-
}
1012+
};
1013+
Image image = new Image(display, gcDrawer, rect.width, rect.height);
1014+
image.getImageData(); //this will ensure that the gc calls inside gcDrawer are executed
1015+
1016+
return image;
10251017
}
10261018

10271019
/**

0 commit comments

Comments
 (0)