Skip to content

Commit 40c087f

Browse files
Refactor Image(Display, int, int) in Tests
Replacing Image(Display, int, int) with Image(Display, ImageGcDrawer, int, int) in Tests
1 parent d2cb26d commit 40c087f

24 files changed

+307
-275
lines changed

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

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

4040
@Test
4141
public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() {
42-
Image image = new Image(display, 10, 10);
42+
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
4343
int zoom1 = 125;
4444
int zoom2 = 150;
4545
ImageData imageDataAtZoom1 = image.getImageData(zoom1);
@@ -54,7 +54,7 @@ public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() {
5454
public void testImageShouldHaveDimesionAsPerZoomLevel() {
5555
int zoom = DPIUtil.getDeviceZoom();
5656
int scalingFactor = 2;
57-
Image image = new Image(display, 10, 10);
57+
Image image = new Image(display, (gc, width, height) -> {}, 10, 10);
5858
try {
5959
ImageData baseImageData = image.getImageData(zoom);
6060
assertEquals("Width should equal the initial width on the same zoom", 10, baseImageData.width);

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.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug121220_AlphaTransparencyWithPatterns.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import org.eclipse.swt.SWT;
44
import org.eclipse.swt.graphics.Color;
5-
import org.eclipse.swt.graphics.GC;
65
import org.eclipse.swt.graphics.Image;
6+
import org.eclipse.swt.graphics.ImageGcDrawer;
77
import org.eclipse.swt.graphics.Pattern;
88
import org.eclipse.swt.widgets.Display;
99
import org.eclipse.swt.widgets.Shell;
@@ -16,16 +16,15 @@ public static void main(String[] args) {
1616
Shell shell = new Shell(disp);
1717
shell.setSize(400, 300);
1818
shell.setText("Pattern with alpha");
19-
Image image = new Image(disp, 50, 50);
20-
{
21-
GC gc = new GC(image);
19+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
2220
gc.setForeground(new Color(null, 255, 0, 0));
2321
gc.drawLine(0, 0, 49, 49);
2422
gc.drawLine(0, 49, 49, 0);
2523
gc.setForeground(new Color(null, 0, 0, 200));
2624
gc.drawString("Pat", 5, 5);
27-
gc.dispose();
28-
}
25+
};
26+
Image image = new Image(disp, imageGcDrawer, 50, 50);
27+
2928
final Pattern pat = new Pattern(disp, image);
3029
shell.addPaintListener(e -> {
3130

tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug536008_DarkDisabledLabel.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,24 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.tests.win32.snippets;
1515

16+
import java.util.function.Consumer;
17+
1618
import org.eclipse.swt.SWT;
17-
import org.eclipse.swt.graphics.*;
19+
import org.eclipse.swt.graphics.Color;
20+
import org.eclipse.swt.graphics.Image;
21+
import org.eclipse.swt.graphics.ImageGcDrawer;
22+
import org.eclipse.swt.graphics.Point;
1823
import org.eclipse.swt.layout.FillLayout;
1924
import org.eclipse.swt.layout.FormData;
2025
import org.eclipse.swt.layout.FormLayout;
2126
import org.eclipse.swt.layout.RowLayout;
22-
import org.eclipse.swt.widgets.*;
23-
24-
import java.util.function.Consumer;
27+
import org.eclipse.swt.widgets.Button;
28+
import org.eclipse.swt.widgets.Composite;
29+
import org.eclipse.swt.widgets.Control;
30+
import org.eclipse.swt.widgets.Display;
31+
import org.eclipse.swt.widgets.Group;
32+
import org.eclipse.swt.widgets.Label;
33+
import org.eclipse.swt.widgets.Shell;
2534

2635
public class Bug536008_DarkDisabledLabel {
2736
static void moveControl(Control control, Point point) {
@@ -73,17 +82,13 @@ public static void main (String [] args) {
7382
int cx = 60;
7483
int cy = 60;
7584
int nRow = 0;
76-
77-
Image image = new Image(display, cx / 2, cy / 2);
78-
{
79-
GC gc = new GC(image);
85+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
8086
gc.setBackground(backColor);
8187
gc.setForeground(foreColor);
82-
83-
Rectangle imageBounds = image.getBounds();
84-
gc.fillRectangle(imageBounds);
85-
gc.drawOval(imageBounds.x, imageBounds.y, imageBounds.width - 1, imageBounds.height - 1);
86-
}
88+
gc.fillRectangle(0, 0, width, height);
89+
gc.drawOval(0, 0, width - 1, height - 1);
90+
};
91+
Image image = new Image(display, imageGcDrawer, cx / 2, cy / 2);
8792

8893
FillLayout fillLayout = new FillLayout();
8994
fillLayout.marginWidth = 10;

tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560358_DarkMenuBar.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,28 @@
1414
package org.eclipse.swt.tests.win32.snippets;
1515

1616
import org.eclipse.swt.SWT;
17-
import org.eclipse.swt.graphics.*;
17+
import org.eclipse.swt.graphics.Color;
18+
import org.eclipse.swt.graphics.Device;
19+
import org.eclipse.swt.graphics.Image;
20+
import org.eclipse.swt.graphics.ImageGcDrawer;
1821
import org.eclipse.swt.layout.RowLayout;
19-
import org.eclipse.swt.widgets.*;
22+
import org.eclipse.swt.widgets.Display;
23+
import org.eclipse.swt.widgets.Menu;
24+
import org.eclipse.swt.widgets.MenuItem;
25+
import org.eclipse.swt.widgets.Shell;
26+
import org.eclipse.swt.widgets.Text;
2027

2128
public class Bug560358_DarkMenuBar {
2229
static Image createMenuImage(Device a_Device) {
23-
Image result = new Image(a_Device, 16, 16);
24-
GC gc = new GC(result);
30+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
31+
gc.setBackground(a_Device.getSystemColor(SWT.COLOR_BLUE));
32+
gc.fillRectangle(0, 0, width, height);
2533

26-
gc.setBackground(a_Device.getSystemColor(SWT.COLOR_BLUE));
27-
gc.fillRectangle(0, 0, 16, 16);
34+
gc.setForeground(a_Device.getSystemColor(SWT.COLOR_RED));
35+
gc.drawOval(4, 4, 8, 8);
36+
};
37+
Image result = new Image(a_Device, imageGcDrawer, 16, 16);
2838

29-
gc.setForeground(a_Device.getSystemColor(SWT.COLOR_RED));
30-
gc.drawOval(4, 4, 8, 8);
31-
32-
gc.dispose();
3339
return result;
3440
}
3541

tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug560546_GC_drawString_vs_GC_drawText.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.tests.win32.snippets;
1515

16-
import org.eclipse.swt.*;
17-
import org.eclipse.swt.graphics.*;
18-
import org.eclipse.swt.widgets.*;
16+
import org.eclipse.swt.SWT;
17+
import org.eclipse.swt.graphics.Font;
18+
import org.eclipse.swt.graphics.GC;
19+
import org.eclipse.swt.graphics.Image;
20+
import org.eclipse.swt.graphics.ImageGcDrawer;
21+
import org.eclipse.swt.widgets.Button;
22+
import org.eclipse.swt.widgets.Canvas;
23+
import org.eclipse.swt.widgets.Display;
24+
import org.eclipse.swt.widgets.MessageBox;
25+
import org.eclipse.swt.widgets.Shell;
1926

2027
public class Bug560546_GC_drawString_vs_GC_drawText {
2128
static Font fontSegoeUI;
@@ -73,39 +80,37 @@ void function(GC gc, int x, int y, String string) {
7380

7481
static void testSpeeds(Shell shell) {
7582
final int finalIterations = 20 * 1000;
76-
String report = "";
77-
78-
Image image = new Image(shell.getDisplay(), 2000, 100);
79-
GC gc = new GC(image);
80-
for (int isFinalCalc = 0; isFinalCalc < 2; isFinalCalc++) {
81-
for (int iTestString = 0; iTestString < testStrings.length; iTestString++) {
82-
for (int iTestFunction = 0; iTestFunction < testFunctions.length; iTestFunction++) {
83-
TestString testString = testStrings[iTestString];
84-
TestFunction testFunction = testFunctions[iTestFunction];
85-
86-
// Warm up before measuring
87-
final int iterations = (isFinalCalc != 0) ? finalIterations : 10;
88-
89-
final long time1 = System.nanoTime();
90-
for (int iIteration = 0; iIteration < iterations; iIteration++) {
91-
testFunction.function(gc, 0, 0, testString.string);
92-
}
93-
final long time2 = System.nanoTime();
94-
95-
if (isFinalCalc != 0) {
96-
final double elapsed = (time2 - time1) / 1000000000.0;
97-
report += String.format("%s, %s - %.3f sec\n", testString.caption, testFunction.caption, elapsed);
83+
ImageGcDrawer gcDrawer = (gc, width, height) -> {
84+
String report = "";
85+
for (int isFinalCalc = 0; isFinalCalc < 2; isFinalCalc++) {
86+
for (int iTestString = 0; iTestString < testStrings.length; iTestString++) {
87+
for (int iTestFunction = 0; iTestFunction < testFunctions.length; iTestFunction++) {
88+
TestString testString = testStrings[iTestString];
89+
TestFunction testFunction = testFunctions[iTestFunction];
90+
91+
// Warm up before measuring
92+
final int iterations = (isFinalCalc != 0) ? finalIterations : 10;
93+
94+
final long time1 = System.nanoTime();
95+
for (int iIteration = 0; iIteration < iterations; iIteration++) {
96+
testFunction.function(gc, 0, 0, testString.string);
97+
}
98+
final long time2 = System.nanoTime();
99+
100+
if (isFinalCalc != 0) {
101+
final double elapsed = (time2 - time1) / 1000000000.0;
102+
report += String.format("%s, %s - %.3f sec\n", testString.caption, testFunction.caption, elapsed);
103+
}
98104
}
99105
}
100106
}
101-
}
102-
103-
gc.dispose();
107+
MessageBox messageBox = new MessageBox(shell);
108+
messageBox.setMessage(report);
109+
messageBox.open();
110+
};
111+
Image image = new Image(shell.getDisplay(), gcDrawer, 2000, 100);
112+
image.getImageData();
104113
image.dispose();
105-
106-
MessageBox messageBox = new MessageBox(shell);
107-
messageBox.setMessage(report);
108-
messageBox.open();
109114
}
110115

111116
public static void main(String[] args) {

tests/org.eclipse.swt.tests.win32/ManualTests/org/eclipse/swt/tests/win32/snippets/Bug563475_DarkDisabledTable.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
import org.eclipse.swt.SWT;
1717
import org.eclipse.swt.graphics.Color;
18-
import org.eclipse.swt.graphics.GC;
1918
import org.eclipse.swt.graphics.Image;
2019
import org.eclipse.swt.graphics.ImageData;
20+
import org.eclipse.swt.graphics.ImageGcDrawer;
2121
import org.eclipse.swt.internal.win32.OS;
2222
import org.eclipse.swt.layout.GridData;
2323
import org.eclipse.swt.layout.GridLayout;
@@ -52,14 +52,13 @@ static void setColors(Control control, Color backColor, Color foreColor) {
5252
static Image createImage(Display display) {
5353
Color transparentColor = display.getSystemColor(SWT.COLOR_BLACK);
5454

55-
Image image = new Image(display, 16, 16);
56-
57-
GC gc = new GC(image);
58-
gc.setBackground(transparentColor);
59-
gc.fillRectangle(image.getBounds());
60-
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
61-
gc.fillRectangle(6, 6, 4, 4);
62-
gc.dispose();
55+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
56+
gc.setBackground(transparentColor);
57+
gc.fillRectangle(0, 0, width, height);
58+
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
59+
gc.fillRectangle(6, 6, 4, 4);
60+
};
61+
Image image = new Image(display, imageGcDrawer, 16, 16);
6362

6463
return image;
6564
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public static boolean hasPixel(Control control, Color expectedColor) {
504504
*/
505505
public static boolean hasPixel(Control control, Color expectedColor, Rectangle rect) {
506506
GC gc = new GC(control);
507-
final Image image = new Image(control.getDisplay(), control.getSize().x, control.getSize().y);
507+
final Image image = new Image(control.getDisplay(), (iGc, width, height) -> {}, control.getSize().x, control.getSize().y);
508508
gc.copyArea(image, 0, 0);
509509
gc.dispose();
510510
boolean result = hasPixel(image, expectedColor, rect);

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

Lines changed: 11 additions & 16 deletions
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.LineAttributes;
4041
import org.eclipse.swt.graphics.PaletteData;
4142
import org.eclipse.swt.graphics.Point;
@@ -824,9 +825,7 @@ public void test_bug1288_createGCFromImageFromNonDisplayThread() throws Interrup
824825
AtomicReference<Exception> exceptionReference = new AtomicReference<>();
825826
Thread thread = new Thread(() -> {
826827
try {
827-
Image image = new Image(null, 100, 100);
828-
GC gc = new GC(image);
829-
gc.dispose();
828+
Image image = new Image(null, (gc, width, height) -> {}, 100, 100);
830829
image.dispose();
831830
} catch(Exception e) {
832831
exceptionReference.set(e);
@@ -849,20 +848,16 @@ public void test_bug1288_createGCFromImageFromNonDisplayThread() throws Interrup
849848
* (16bpp or less).
850849
*/
851850
RGB getRealRGB(Color color) {
852-
Image colorImage = new Image(display, 10, 10);
853-
GC imageGc = new GC(colorImage);
854-
ImageData imageData;
855-
PaletteData palette;
856-
int pixel;
857-
858-
imageGc.setBackground(color);
859-
imageGc.setForeground(color);
860-
imageGc.fillRectangle(0, 0, 10, 10);
861-
imageData = colorImage.getImageData();
862-
palette = imageData.palette;
863-
imageGc.dispose();
851+
ImageGcDrawer gcDrawer = (imageGc, width, height) -> {
852+
imageGc.setBackground(color);
853+
imageGc.setForeground(color);
854+
imageGc.fillRectangle(0, 0, width, height);
855+
};
856+
Image colorImage = new Image(display, gcDrawer, 10, 10);
857+
ImageData imageData = colorImage.getImageData();
858+
PaletteData palette = imageData.palette;
864859
colorImage.dispose();
865-
pixel = imageData.getPixel(0, 0);
860+
int pixel = imageData.getPixel(0, 0);
866861
return palette.getRGB(pixel);
867862
}
868863

0 commit comments

Comments
 (0)