Skip to content

Commit 80df3da

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 51fed8e commit 80df3da

File tree

8 files changed

+43
-22
lines changed

8 files changed

+43
-22
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: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ public void test_equalsLjava_lang_Object() {
547547

548548
@Test
549549
public void test_getBackground() {
550-
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
550+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
551+
Image image = new Image(display, noOpGcDrawer, 10, 10);
551552
image.dispose();
552553
SWTException e = assertThrows(SWTException.class, () -> image.getBackground());
553554
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
@@ -556,20 +557,21 @@ public void test_getBackground() {
556557

557558
@Test
558559
public void test_getBounds() {
560+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
559561
Rectangle bounds = new Rectangle(0, 0, 10, 20);
560-
Image image1 = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
562+
Image image1 = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
561563
image1.dispose();
562564
SWTException e = assertThrows(SWTException.class, () -> image1.getBounds());
563565
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
564566

565567
Image image;
566568
// creates bitmap image
567-
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
569+
image = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
568570
Rectangle bounds1 = image.getBounds();
569571
image.dispose();
570572
assertEquals(bounds, bounds1);
571573

572-
image = new Image(display, (gc, width, height) -> {}, bounds.width, bounds.height);
574+
image = new Image(display, noOpGcDrawer, bounds.width, bounds.height);
573575
bounds1 = image.getBounds();
574576
image.dispose();
575577
assertEquals(bounds, bounds1);
@@ -779,9 +781,9 @@ public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int current
779781
public void test_hashCode() {
780782
Image image = null;
781783
Image image1 = null;
782-
784+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
783785
try {
784-
image = new Image(display, (gc, width, height) -> {}, 10, 10);
786+
image = new Image(display, noOpGcDrawer, 10, 10);
785787
image1 = image;
786788

787789
assertEquals(image1.hashCode(), image.hashCode());
@@ -842,14 +844,15 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
842844
"Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)",
843845
SwtTestUtil.isGTK);
844846
// TODO Fix GTK failure.
845-
Image image1 = new Image(display, (gc, width, height) -> {}, 10, 10);
847+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
848+
Image image1 = new Image(display, noOpGcDrawer, 10, 10);
846849
try {
847850
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> image1.setBackground(null));
848851
assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e);
849852
} finally {
850853
image1.dispose();
851854
}
852-
Image image2 = new Image(display, (gc, width, height) -> {}, 10, 10);
855+
Image image2 = new Image(display, noOpGcDrawer, 10, 10);
853856
Color color2 = new Color(255, 255, 255);
854857
color2.dispose();
855858
try {
@@ -858,14 +861,14 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
858861
} finally {
859862
image2.dispose();
860863
}
861-
Image image3 = new Image(display, (gc, width, height) -> {}, 10, 10);
864+
Image image3 = new Image(display, noOpGcDrawer, 10, 10);
862865
image3.dispose();
863866
Color color3 = new Color(255, 255, 255);
864867
SWTException e = assertThrows(SWTException.class, () -> image3.setBackground(color3));
865868
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
866869

867870
// this image does not have a transparent pixel by default so setBackground has no effect
868-
Image image4 = new Image(display, (gc, width, height) -> {}, 10, 10);
871+
Image image4 = new Image(display, noOpGcDrawer, 10, 10);
869872
image4.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
870873
Color color4 = image4.getBackground();
871874
assertNull("background color should be null for non-transparent image", color4);
@@ -884,7 +887,8 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
884887

885888
@Test
886889
public void test_toString() {
887-
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
890+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
891+
Image image = new Image(display, noOpGcDrawer, 10, 10);
888892
try {
889893
assertNotNull(image.toString());
890894
assertTrue(image.toString().length() > 0);
@@ -983,7 +987,6 @@ public void test_bug566545_efficientGrayscaleImage() {
983987

984988
Image imageIndexed = new Image(display, imageDataIndexed);
985989
Image imageDirect = new Image(display, imageDataDirect);
986-
987990
ImageGcDrawer gcDrawer1 = (gc, iWidth, iHeight) -> {
988991
gc.drawImage(imageIndexed, 0, 0);
989992
};
@@ -1006,10 +1009,11 @@ public void test_bug566545_efficientGrayscaleImage() {
10061009

10071010
@Test
10081011
public void test_updateWidthHeightAfterDPIChange() {
1012+
ImageGcDrawer noOpGcDrawer = (gc, width, height) -> {};
10091013
int deviceZoom = DPIUtil.getDeviceZoom();
10101014
try {
10111015
Rectangle imageSize = new Rectangle(0, 0, 16, 16);
1012-
Image baseImage = new Image(display, (gc, width, height) -> {}, imageSize.width, imageSize.height);
1016+
Image baseImage = new Image(display, noOpGcDrawer, imageSize.width, imageSize.height);
10131017
GC gc = new GC(display);
10141018
gc.drawImage(baseImage, 10, 10);
10151019
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)