Skip to content

Commit 026ee02

Browse files
Refactor Image(Display, int, int) in Tests (No Ops)
Replacing Image(Display, int, int) with Image(Display, ImageGcDrawer, int, int) in Tests. Basic Case
1 parent 4862ef8 commit 026ee02

File tree

8 files changed

+47
-24
lines changed

8 files changed

+47
-24
lines changed

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics/ImageWin32Tests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public void setUp() {
3939

4040
@Test
4141
public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() {
42-
Image image = new Image(display, 10, 10);
42+
ImageGcDrawer noOpGcDrawer = (gc, height, width) -> {};
43+
Image image = new Image(display, noOpGcDrawer, 10, 10);
4344
int zoom1 = 125;
4445
int zoom2 = 150;
4546
ImageData imageDataAtZoom1 = image.getImageData(zoom1);
@@ -52,9 +53,10 @@ public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() {
5253

5354
@Test
5455
public void testImageShouldHaveDimesionAsPerZoomLevel() {
56+
ImageGcDrawer noOpGcDrawer = (gc, height, width) -> {};
5557
int zoom = DPIUtil.getDeviceZoom();
5658
int scalingFactor = 2;
57-
Image image = new Image(display, 10, 10);
59+
Image image = new Image(display, noOpGcDrawer, 10, 10);
5860
try {
5961
ImageData baseImageData = image.getImageData(zoom);
6062
assertEquals("Width should equal the initial width on the same zoom", 10, baseImageData.width);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.eclipse.swt.graphics.GC;
3737
import org.eclipse.swt.graphics.Image;
3838
import org.eclipse.swt.graphics.ImageData;
39+
import org.eclipse.swt.graphics.ImageGcDrawer;
3940
import org.eclipse.swt.graphics.PaletteData;
4041
import org.eclipse.swt.graphics.RGB;
4142
import org.eclipse.swt.graphics.Rectangle;
@@ -503,8 +504,9 @@ public static boolean hasPixel(Control control, Color expectedColor) {
503504
* widget
504505
*/
505506
public static boolean hasPixel(Control control, Color expectedColor, Rectangle rect) {
507+
ImageGcDrawer noOpGcDrawer = (gc, height, width) -> {};
506508
GC gc = new GC(control);
507-
final Image image = new Image(control.getDisplay(), control.getSize().x, control.getSize().y);
509+
final Image image = new Image(control.getDisplay(), noOpGcDrawer, control.getSize().x, control.getSize().y);
508510
gc.copyArea(image, 0, 0);
509511
gc.dispose();
510512
boolean result = hasPixel(image, expectedColor, rect);

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ public void test_equalsLjava_lang_Object() {
543543

544544
@Test
545545
public void test_getBackground() {
546-
Image image = new Image(display, 10, 10);
546+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
547+
Image image = new Image(display, noOpGcDrawer, 10, 10);
547548
image.dispose();
548549
SWTException e = assertThrows(SWTException.class, () -> image.getBackground());
549550
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
@@ -552,20 +553,21 @@ public void test_getBackground() {
552553

553554
@Test
554555
public void test_getBounds() {
556+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
555557
Rectangle bounds = new Rectangle(0, 0, 10, 20);
556-
Image image1 = new Image(display, bounds.width, bounds.height);
558+
Image image1 = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
557559
image1.dispose();
558560
SWTException e = assertThrows(SWTException.class, () -> image1.getBounds());
559561
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
560562

561563
Image image;
562564
// creates bitmap image
563-
image = new Image(display, bounds.width, bounds.height);
565+
image = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
564566
Rectangle bounds1 = image.getBounds();
565567
image.dispose();
566568
assertEquals(bounds, bounds1);
567569

568-
image = new Image(display, bounds.width, bounds.height);
570+
image = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
569571
bounds1 = image.getBounds();
570572
image.dispose();
571573
assertEquals(bounds, bounds1);
@@ -699,22 +701,23 @@ public void test_getImageData_200() {
699701

700702

701703
void getImageData_int(int zoom) {
704+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
702705
Rectangle bounds = new Rectangle(0, 0, 10, 20);
703-
Image image1 = new Image(display, bounds.width, bounds.height);
706+
Image image1 = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
704707
image1.dispose();
705708
SWTException e = assertThrows(SWTException.class, () -> image1.getImageData(zoom));
706709
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
707710

708711
Image image;
709712
// creates bitmap image and compare size of imageData
710-
image = new Image(display, bounds.width, bounds.height);
713+
image = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
711714
ImageData imageDataAtZoom = image.getImageData(zoom);
712715
image.dispose();
713716
Rectangle boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height);
714717
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);
715718

716719
// creates second bitmap image and compare size of imageData
717-
image = new Image(display, bounds.width, bounds.height);
720+
image = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
718721
imageDataAtZoom = image.getImageData(zoom);
719722
boundsAtZoom = new Rectangle(0, 0, imageDataAtZoom.width, imageDataAtZoom.height);
720723
bounds = image.getBounds();
@@ -768,9 +771,9 @@ public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int current
768771
public void test_hashCode() {
769772
Image image = null;
770773
Image image1 = null;
771-
774+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
772775
try {
773-
image = new Image(display, 10, 10);
776+
image = new Image(display, noOpGcDrawer, 10, 10);
774777
image1 = image;
775778

776779
assertEquals(image1.hashCode(), image.hashCode());
@@ -831,14 +834,15 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
831834
"Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)",
832835
SwtTestUtil.isGTK);
833836
// TODO Fix GTK failure.
834-
Image image1 = new Image(display, 10, 10);
837+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
838+
Image image1 = new Image(display, noOpGcDrawer, 10, 10);
835839
try {
836840
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> image1.setBackground(null));
837841
assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e);
838842
} finally {
839843
image1.dispose();
840844
}
841-
Image image2 = new Image(display, 10, 10);
845+
Image image2 = new Image(display, noOpGcDrawer, 10, 10);
842846
Color color2 = new Color(255, 255, 255);
843847
color2.dispose();
844848
try {
@@ -847,14 +851,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
847851
} finally {
848852
image2.dispose();
849853
}
850-
Image image3 = new Image(display, 10, 10);
854+
Image image3 = new Image(display, noOpGcDrawer, 10, 10);
851855
image3.dispose();
852856
Color color3 = new Color(255, 255, 255);
853857
SWTException e = assertThrows(SWTException.class, () -> image3.setBackground(color3));
854858
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
855859

856860
// this image does not have a transparent pixel by default so setBackground has no effect
857-
Image image4 = new Image(display, 10, 10);
861+
Image image4 = new Image(display, noOpGcDrawer, 10, 10);
858862
image4.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
859863
Color color4 = image4.getBackground();
860864
assertNull("background color should be null for non-transparent image", color4);
@@ -873,7 +877,8 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
873877

874878
@Test
875879
public void test_toString() {
876-
Image image = new Image(display, 10, 10);
880+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
881+
Image image = new Image(display, noOpGcDrawer, 10, 10);
877882
try {
878883
assertNotNull(image.toString());
879884
assertTrue(image.toString().length() > 0);
@@ -998,10 +1003,11 @@ public void test_bug566545_efficientGrayscaleImage() {
9981003

9991004
@Test
10001005
public void test_updateWidthHeightAfterDPIChange() {
1006+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
10011007
int deviceZoom = DPIUtil.getDeviceZoom();
10021008
try {
10031009
Rectangle imageSize = new Rectangle(0, 0, 16, 16);
1004-
Image baseImage = new Image(display, imageSize.width, imageSize.height);
1010+
Image baseImage = new Image(display, noOpGcDrawer, imageSize.width, imageSize.height);
10051011
GC gc = new GC(display);
10061012
gc.drawImage(baseImage, 10, 10);
10071013
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_widgets_Button.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.eclipse.swt.events.SelectionListener;
3030
import org.eclipse.swt.graphics.Color;
3131
import org.eclipse.swt.graphics.Image;
32+
import org.eclipse.swt.graphics.ImageGcDrawer;
3233
import org.eclipse.swt.layout.FillLayout;
3334
import org.eclipse.swt.layout.GridLayout;
3435
import org.eclipse.swt.widgets.Button;
@@ -384,7 +385,8 @@ public void test_setImageLorg_eclipse_swt_graphics_Image() {
384385
button.setImage(null);
385386
assertNull(button.getImage());
386387

387-
image = new Image(shell.getDisplay(), 10, 10);
388+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
389+
image = new Image(shell.getDisplay(), noOpGcDrawer, 10, 10);
388390
button.setImage(image);
389391
assertEquals(image, button.getImage());
390392

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.swt.SWT;
2525
import org.eclipse.swt.graphics.Font;
2626
import org.eclipse.swt.graphics.Image;
27+
import org.eclipse.swt.graphics.ImageGcDrawer;
2728
import org.eclipse.swt.graphics.Rectangle;
2829
import org.eclipse.swt.widgets.Canvas;
2930
import org.eclipse.swt.widgets.Caret;
@@ -147,7 +148,8 @@ public void test_setImageLorg_eclipse_swt_graphics_Image() {
147148
caret.setImage(null);
148149
assertNull(caret.getImage());
149150

150-
image = new Image(shell.getDisplay(), 10, 10);
151+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
152+
image = new Image(shell.getDisplay(), noOpGcDrawer, 10, 10);
151153
caret.setImage(image);
152154
assertEquals(image, caret.getImage());
153155

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.eclipse.swt.SWT;
2828
import org.eclipse.swt.graphics.Image;
29+
import org.eclipse.swt.graphics.ImageGcDrawer;
2930
import org.eclipse.swt.graphics.Rectangle;
3031
import org.eclipse.swt.widgets.Button;
3132
import org.eclipse.swt.widgets.Decorations;
@@ -71,7 +72,8 @@ public void test_getDefaultButton() {
7172

7273
@Test
7374
public void test_getImage() {
74-
Image[] cases = {null, new Image(null, 100, 100)};
75+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
76+
Image[] cases = {null, new Image(null, noOpGcDrawer, 100, 100)};
7577
for (Image image : cases) {
7678
decorations.setImage(image);
7779
assertEquals(decorations.getImage(), image);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.eclipse.swt.SWT;
2020
import org.eclipse.swt.graphics.Image;
21+
import org.eclipse.swt.graphics.ImageGcDrawer;
2122
import org.eclipse.swt.widgets.Label;
2223
import org.junit.Before;
2324
import org.junit.Test;
@@ -71,7 +72,8 @@ public void test_getAlignment(){
7172

7273
@Test
7374
public void test_getImage(){
74-
Image[] cases = {null, new Image(null, 100, 100)};
75+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
76+
Image[] cases = {null, new Image(null, noOpGcDrawer, 100, 100)};
7577
for (Image image : cases) {
7678
label.setImage(image);
7779
assertEquals(label.getImage(), image);

tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug569752_DetectNonDisposedOsResources.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515

1616
import org.eclipse.swt.SWT;
1717
import org.eclipse.swt.graphics.Image;
18+
import org.eclipse.swt.graphics.ImageGcDrawer;
1819
import org.eclipse.swt.graphics.Resource;
1920
import org.eclipse.swt.layout.GridData;
2021
import org.eclipse.swt.layout.GridLayout;
21-
import org.eclipse.swt.widgets.*;
22+
import org.eclipse.swt.widgets.Button;
23+
import org.eclipse.swt.widgets.Display;
24+
import org.eclipse.swt.widgets.Label;
25+
import org.eclipse.swt.widgets.Shell;
2226

2327
public class Bug569752_DetectNonDisposedOsResources {
2428
public static void main(String[] args) {
@@ -41,7 +45,8 @@ public static void main(String[] args) {
4145
Button btnTest = new Button(shell, SWT.PUSH);
4246
btnTest.setText("Leak Image");
4347
btnTest.addListener(SWT.Selection, event -> {
44-
Image image = new Image(display, 10, 10);
48+
ImageGcDrawer noOpGcDrawer = (gc, height, width) -> {};
49+
Image image = new Image(display, noOpGcDrawer, 10, 10);
4550

4651
if (chkDispose.getSelection())
4752
image.dispose();

0 commit comments

Comments
 (0)