diff --git a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java index 53d23f7e14a..7337fea3f87 100644 --- a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java +++ b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java @@ -36,9 +36,9 @@ import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.dnd.URLTransfer; import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.RowLayout; @@ -239,18 +239,17 @@ public void testUrlTransfer() throws InterruptedException { * Creates a DDB test image with a uniform color applied to all pixels. */ private Image createTestImage() { - final Image image = new Image(shell.getDisplay(), 16, 16); try { Color color = shell.getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE); - GC gc = new GC(image); - gc.setBackground(color); - gc.fillRectangle(image.getBounds()); - gc.dispose(); + final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { + gc.setBackground(color); + gc.fillRectangle(0, 0, width, height); + }; + return new Image(shell.getDisplay(), imageGcDrawer, 16, 16); } catch (Exception e) { - image.dispose(); fail("test image could not be initialized: " + e); } - return image; + return null; } /** diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java index 46b97d49c05..7becd12d71a 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java @@ -37,6 +37,7 @@ import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.LineAttributes; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.Point; @@ -837,20 +838,16 @@ public void test_bug1288_createGCFromImageFromNonDisplayThread() throws Interrup * (16bpp or less). */ RGB getRealRGB(Color color) { - Image colorImage = new Image(display, 10, 10); - GC imageGc = new GC(colorImage); - ImageData imageData; - PaletteData palette; - int pixel; - - imageGc.setBackground(color); - imageGc.setForeground(color); - imageGc.fillRectangle(0, 0, 10, 10); - imageData = colorImage.getImageData(); - palette = imageData.palette; - imageGc.dispose(); + ImageGcDrawer gcDrawer = (imageGc, width, height) -> { + imageGc.setBackground(color); + imageGc.setForeground(color); + imageGc.fillRectangle(0, 0, width, height); + }; + Image colorImage = new Image(display, gcDrawer, 10, 10); + ImageData imageData = colorImage.getImageData(); + PaletteData palette = imageData.palette; colorImage.dispose(); - pixel = imageData.getPixel(0, 0); + int pixel = imageData.getPixel(0, 0); return palette.getRGB(pixel); } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java index b4ff1bc0780..eceef1c9596 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java @@ -209,16 +209,17 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra data = new ImageData(10, 10, 8, new PaletteData(0x30, 0x0C, 0x03)); // set red pixel at x=9, y=9 data.setPixel(9, 9, 0x30); - image = new Image(display, data); - Image gcImage = new Image(display, 10, 10); - GC gc = new GC(gcImage); - gc.drawImage(image, 0, 0); + final Image imageFromImageData = new Image(display, data); + ImageGcDrawer gcDrawer = (gc, width, height) -> { + gc.drawImage(imageFromImageData, 0, 0); + }; + Image gcImage = new Image(display, gcDrawer, 10, 10); ImageData gcImageData = gcImage.getImageData(); int redPixel = gcImageData.getPixel(9, 9); assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel)); - gc.dispose(); gcImage.dispose(); image.dispose(); + imageFromImageData.dispose(); } @Test @@ -252,21 +253,23 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra data6.setPixel(9, 9, 0x30); data7 = new ImageData(10, 10, 1, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255))); data7.setPixel(9, 9, 1); - image = new Image(display, data6, data7); - Image gcImage = new Image(display, 10, 10); - GC gc = new GC(gcImage); + Image image2 = new Image(display, data6, data7); Color backgroundColor = display.getSystemColor(SWT.COLOR_BLUE); - gc.setBackground(backgroundColor); - gc.fillRectangle(0, 0, 10, 10); - gc.drawImage(image, 0, 0); + final ImageGcDrawer gcDrawer = (gc, width, height) -> { + gc.setBackground(backgroundColor); + gc.fillRectangle(0, 0, 10, 10); + gc.drawImage(image2, 0, 0); + }; + Image gcImage = new Image(display, gcDrawer, 10, 10); + ImageData gcImageData = gcImage.getImageData(); int redPixel = gcImageData.getPixel(9, 9); assertEquals(getRealRGB(display.getSystemColor(SWT.COLOR_RED)), gcImageData.palette.getRGB(redPixel)); int bluePixel = gcImageData.getPixel(0, 0); assertEquals(getRealRGB(backgroundColor), gcImageData.palette.getRGB(bluePixel)); - gc.dispose(); gcImage.dispose(); image.dispose(); + image2.dispose(); } @Test @@ -544,7 +547,7 @@ public void test_equalsLjava_lang_Object() { @Test public void test_getBackground() { - Image image = new Image(display, 10, 10); + Image image = new Image(display, (gc, width, height) -> {}, 10, 10); image.dispose(); SWTException e = assertThrows(SWTException.class, () -> image.getBackground()); assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e); @@ -554,19 +557,19 @@ public void test_getBackground() { @Test public void test_getBounds() { Rectangle bounds = new Rectangle(0, 0, 10, 20); - Image image1 = new Image(display, bounds.width, bounds.height); + Image image1 = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); image1.dispose(); SWTException e = assertThrows(SWTException.class, () -> image1.getBounds()); assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e); Image image; // creates bitmap image - image = new Image(display, bounds.width, bounds.height); + image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); Rectangle bounds1 = image.getBounds(); image.dispose(); assertEquals(bounds, bounds1); - image = new Image(display, bounds.width, bounds.height); + image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); bounds1 = image.getBounds(); image.dispose(); assertEquals(bounds, bounds1); @@ -722,6 +725,13 @@ void getImageData_int(int zoom) { image.dispose(); 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); + // creates bitmap image with GCDrawer and compare size of imageData + image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height); + imageDataAtZoom = image.getImageData(zoom); + image.dispose(); + boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height); + 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); + // create icon image and compare size of imageData ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB[] {new RGB(0, 0, 0)})); image = new Image(display, imageData); @@ -771,7 +781,7 @@ public void test_hashCode() { Image image1 = null; try { - image = new Image(display, 10, 10); + image = new Image(display, (gc, width, height) -> {}, 10, 10); image1 = image; assertEquals(image1.hashCode(), image.hashCode()); @@ -832,14 +842,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() { "Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)", SwtTestUtil.isGTK); // TODO Fix GTK failure. - Image image1 = new Image(display, 10, 10); + Image image1 = new Image(display, (gc, width, height) -> {}, 10, 10); try { IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> image1.setBackground(null)); assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e); } finally { image1.dispose(); } - Image image2 = new Image(display, 10, 10); + Image image2 = new Image(display, (gc, width, height) -> {}, 10, 10); Color color2 = new Color(255, 255, 255); color2.dispose(); try { @@ -848,14 +858,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() { } finally { image2.dispose(); } - Image image3 = new Image(display, 10, 10); + Image image3 = new Image(display, (gc, width, height) -> {}, 10, 10); image3.dispose(); Color color3 = new Color(255, 255, 255); SWTException e = assertThrows(SWTException.class, () -> image3.setBackground(color3)); assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e); // this image does not have a transparent pixel by default so setBackground has no effect - Image image4 = new Image(display, 10, 10); + Image image4 = new Image(display, (gc, width, height) -> {}, 10, 10); image4.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); Color color4 = image4.getBackground(); assertNull("background color should be null for non-transparent image", color4); @@ -874,7 +884,7 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() { @Test public void test_toString() { - Image image = new Image(display, 10, 10); + Image image = new Image(display, (gc, width, height) -> {}, 10, 10); try { assertNotNull(image.toString()); assertTrue(image.toString().length() > 0); @@ -936,20 +946,16 @@ void getImageData2(int depth, PaletteData palette) { } RGB getRealRGB(Color color) { - Image colorImage = new Image(display, 10, 10); - GC imageGc = new GC(colorImage); - ImageData imageData; - PaletteData palette; - int pixel; - - imageGc.setBackground(color); - imageGc.setForeground(color); - imageGc.fillRectangle(0, 0, 10, 10); - imageData = colorImage.getImageData(); - palette = imageData.palette; - imageGc.dispose(); + ImageGcDrawer gcDrawer = (imageGc, width, height) -> { + imageGc.setBackground(color); + imageGc.setForeground(color); + imageGc.fillRectangle(0, 0, width, height); + }; + Image colorImage = new Image(display, gcDrawer, 10, 10); + ImageData imageData = colorImage.getImageData(); + PaletteData palette = imageData.palette; colorImage.dispose(); - pixel = imageData.getPixel(0, 0); + int pixel = imageData.getPixel(0, 0); return palette.getRGB(pixel); } @@ -977,15 +983,16 @@ public void test_bug566545_efficientGrayscaleImage() { Image imageIndexed = new Image(display, imageDataIndexed); Image imageDirect = new Image(display, imageDataDirect); - Image outImageIndexed = new Image(display, width, height); - Image outImageDirect = new Image(display, width, height); - GC gc = new GC(outImageIndexed); - gc.drawImage(imageIndexed, 0, 0); - gc.dispose(); - gc = new GC(outImageDirect); - gc.drawImage(imageDirect, 0, 0); - gc.dispose(); + ImageGcDrawer gcDrawer1 = (gc, iWidth, iHeight) -> { + gc.drawImage(imageIndexed, 0, 0); + }; + Image outImageIndexed = new Image(display, gcDrawer1, width, height); + + ImageGcDrawer gcDrawer2 = (gc, iWidth, iHeight) -> { + gc.drawImage(imageDirect, 0, 0); + }; + Image outImageDirect = new Image(display, gcDrawer2, width, height); ImageTestUtil.assertImagesEqual(imageDataIndexed, imageDataDirect); ImageTestUtil.assertImagesEqual(imageIndexed.getImageData(), imageDirect.getImageData()); @@ -1002,7 +1009,7 @@ public void test_updateWidthHeightAfterDPIChange() { int deviceZoom = DPIUtil.getDeviceZoom(); try { Rectangle imageSize = new Rectangle(0, 0, 16, 16); - Image baseImage = new Image(display, imageSize.width, imageSize.height); + Image baseImage = new Image(display, (gc, width, height) -> {}, imageSize.width, imageSize.height); GC gc = new GC(display); gc.drawImage(baseImage, 10, 10); assertEquals("Base image size differs unexpectedly", imageSize, baseImage.getBounds()); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java index 3bf72505b9d..e8dcc71d28f 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_TextLayout.java @@ -26,8 +26,8 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageGcDrawer; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.TextLayout; @@ -938,17 +938,9 @@ public void test_getTextDirection() { public void test_bug568740_multilineTextStyle() { Font font = null; Image image = null; - GC gc = null; - TextLayout layout = null; + final TextLayout layout = new TextLayout(display); + int offset = 10; try { - font = new Font(display, SwtTestUtil.testFontName, 16, SWT.NORMAL); - image = new Image(display, 200, 100); - gc = new GC(image); - gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); - gc.fillRectangle(image.getBounds()); - gc.setAntialias(SWT.OFF); // aa can change colors and break the test in worst case - - layout = new TextLayout(display); layout.setFont(font); layout.setText("first line\nsecond line"); @@ -974,8 +966,15 @@ public void test_bug568740_multilineTextStyle() { controlStyle.strikeoutColor = display.getSystemColor(SWT.COLOR_DARK_RED); layout.setStyle(controlStyle, 15, 23); - int offset = 10; - layout.draw(gc, offset, offset); + font = new Font(display, SwtTestUtil.testFontName, 16, SWT.NORMAL); + final ImageGcDrawer gcDrawer = (gc, width, height) -> { + gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); + gc.fillRectangle(0, 0, width, height); + gc.setAntialias(SWT.OFF); // aa can change colors and break the test in worst case + layout.draw(gc, offset, offset); + }; + image = new Image(display, gcDrawer, 200, 100); + image.getImageData(); // ensures that the ImageGcDrawer is executed immediately Rectangle firstLineBounds = layout.getLineBounds(0); Rectangle searchRangeBorder = new Rectangle(0, 0, image.getBounds().width, offset + (int)(firstLineBounds.height * 0.3)); @@ -993,8 +992,6 @@ public void test_bug568740_multilineTextStyle() { } finally { if (layout != null) layout.dispose(); - if (gc != null) - gc.dispose(); if (image != null) image.dispose(); if (font != null) @@ -1007,21 +1004,17 @@ public void test_bug568740_multilineTextStyle() { * disposed by caller. */ private Image draw(TextLayout layout, int antialias) { - GC gc = null; - try { - Rectangle rect = layout.getBounds(); - Image image = new Image(display, rect.width, rect.height); - gc = new GC(image); + Rectangle rect = layout.getBounds(); + final ImageGcDrawer gcDrawer = (gc, height, width) -> { gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); - gc.fillRectangle(image.getBounds()); + gc.fillRectangle(0, 0, width, height); gc.setAntialias(antialias); - layout.draw(gc, 0, 0); - return image; - } finally { - if (gc != null) - gc.dispose(); - } + }; + Image image = new Image(display, gcDrawer, rect.width, rect.height); + image.getImageData(); // ensures that the ImageGcDrawer is executed immediately + + return image; } /**