Skip to content

Commit b0bacb3

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Refactor Image(Display, int, int) in Tests (Manual Test)
Replacing Image(Display, int, int) with Image(Display, ImageGcDrawer, int, int) in Tests. Manual Tests.
1 parent cad131b commit b0bacb3

12 files changed

+166
-132
lines changed

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: 38 additions & 29 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,38 +80,40 @@ 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+
final StringBuilder[] reportHolder = new StringBuilder[] { new StringBuilder() };
84+
85+
ImageGcDrawer gcDrawer = (gc, width, height) -> {
86+
StringBuilder report = new StringBuilder();
87+
for (int isFinalCalc = 0; isFinalCalc < 2; isFinalCalc++) {
88+
for (int iTestString = 0; iTestString < testStrings.length; iTestString++) {
89+
for (int iTestFunction = 0; iTestFunction < testFunctions.length; iTestFunction++) {
90+
TestString testString = testStrings[iTestString];
91+
TestFunction testFunction = testFunctions[iTestFunction];
92+
93+
// Warm up before measuring
94+
final int iterations = (isFinalCalc != 0) ? finalIterations : 10;
95+
96+
final long time1 = System.nanoTime();
97+
for (int iIteration = 0; iIteration < iterations; iIteration++) {
98+
testFunction.function(gc, 0, 0, testString.string);
99+
}
100+
final long time2 = System.nanoTime();
101+
102+
if (isFinalCalc != 0) {
103+
final double elapsed = (time2 - time1) / 1000000000.0;
104+
report.append(String.format("%s, %s - %.3f sec\n", testString.caption, testFunction.caption, elapsed));
105+
}
98106
}
99107
}
100108
}
101-
}
102-
103-
gc.dispose();
109+
reportHolder[0] = report;
110+
};
111+
Image image = new Image(shell.getDisplay(), gcDrawer, 2000, 100);
112+
image.getImageData();
104113
image.dispose();
105114

106115
MessageBox messageBox = new MessageBox(shell);
107-
messageBox.setMessage(report);
116+
messageBox.setMessage(reportHolder[0].toString());
108117
messageBox.open();
109118
}
110119

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

Lines changed: 9 additions & 10 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,22 +52,21 @@ 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
}
6665

6766
static Image createItemImage(Display display) {
6867
Image image = createImage(display);
6968
ImageData imageData = image.getImageData();
70-
imageData.transparentPixel = imageData.getPixel(0, 0);
69+
imageData.transparentPixel = display.getSystemColor(SWT.COLOR_BLACK).getAlpha();
7170

7271
Image result = new Image(display, imageData);
7372
image.dispose();

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
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;
19+
import org.eclipse.swt.graphics.ImageGcDrawer;
2020
import org.eclipse.swt.layout.RowLayout;
2121
import org.eclipse.swt.widgets.Composite;
2222
import org.eclipse.swt.widgets.Display;
@@ -55,12 +55,12 @@ public static void main(String[] args) {
5555
Composite container = new Composite(shell, SWT.NONE);
5656
container.setLayout(new RowLayout(SWT.HORIZONTAL));
5757

58-
Image image = new Image(display, 32, 32);
59-
Color color = display.getSystemColor(SWT.COLOR_DARK_GREEN);
60-
GC gc = new GC(image);
61-
gc.setBackground(color);
62-
gc.fillRectangle(image.getBounds());
63-
gc.dispose();
58+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
59+
Color color = display.getSystemColor(SWT.COLOR_DARK_GREEN);
60+
gc.setBackground(color);
61+
gc.fillRectangle(0, 0, width, height);
62+
};
63+
Image image = new Image(display, imageGcDrawer, 32, 32);
6464

6565
Label labelText = new Label(container, SWT.NONE);
6666
Label labelImage = new Label(container, SWT.NONE);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import org.eclipse.swt.SWT;
1818
import org.eclipse.swt.custom.CTabFolder;
1919
import org.eclipse.swt.custom.CTabItem;
20-
import org.eclipse.swt.graphics.GC;
2120
import org.eclipse.swt.graphics.Image;
21+
import org.eclipse.swt.graphics.ImageGcDrawer;
2222
import org.eclipse.swt.graphics.Rectangle;
2323
import org.eclipse.swt.layout.FillLayout;
2424
import org.eclipse.swt.layout.RowLayout;
@@ -96,13 +96,13 @@ public static void main(String[] args) {
9696

9797
private static Image image(Display display, int shapeColor) {
9898
Rectangle bounds = new Rectangle(0, 0, 16, 16);
99-
Image image = new Image(display, bounds.width, bounds.height);
100-
GC gc = new GC(image);
101-
gc.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
102-
gc.fillRectangle(bounds);
103-
gc.setBackground(display.getSystemColor(shapeColor));
104-
gc.fillOval(bounds.x, bounds.y, bounds.width, bounds.height);
105-
gc.dispose();
99+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
100+
gc.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
101+
gc.fillRectangle(bounds);
102+
gc.setBackground(display.getSystemColor(shapeColor));
103+
gc.fillOval(bounds.x, bounds.y, bounds.width, bounds.height);
104+
};
105+
Image image = new Image(display, imageGcDrawer, bounds.width, bounds.height);
106106
return image;
107107
}
108108
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import org.eclipse.swt.SWT;
1818
import org.eclipse.swt.custom.StyledText;
19-
import org.eclipse.swt.graphics.GC;
2019
import org.eclipse.swt.graphics.Image;
20+
import org.eclipse.swt.graphics.ImageGcDrawer;
2121
import org.eclipse.swt.layout.GridData;
2222
import org.eclipse.swt.layout.GridLayout;
2323
import org.eclipse.swt.widgets.Button;
@@ -249,11 +249,11 @@ public static void main(String[] args) {
249249
);
250250

251251
// Make a small image
252-
Image image = new Image (display, 16, 16);
253-
GC gc = new GC(image);
254-
gc.setBackground (display.getSystemColor (SWT.COLOR_BLUE));
255-
gc.fillRectangle (0, 0, 16, 16);
256-
gc.dispose ();
252+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
253+
gc.setBackground (display.getSystemColor (SWT.COLOR_BLUE));
254+
gc.fillRectangle (0, 0, width, height);
255+
};
256+
Image image = new Image (display, imageGcDrawer, 16, 16);
257257

258258
// And a Table to use it
259259
Table table = new Table(shell, 0);

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,29 @@
1414

1515
package org.eclipse.swt.tests.manual;
1616

17-
import org.eclipse.swt.*;
18-
import org.eclipse.swt.dnd.*;
19-
import org.eclipse.swt.graphics.*;
20-
import org.eclipse.swt.layout.*;
21-
import org.eclipse.swt.widgets.*;
17+
import org.eclipse.swt.SWT;
18+
import org.eclipse.swt.dnd.DND;
19+
import org.eclipse.swt.dnd.DragSource;
20+
import org.eclipse.swt.dnd.DragSourceAdapter;
21+
import org.eclipse.swt.dnd.TextTransfer;
22+
import org.eclipse.swt.graphics.Color;
23+
import org.eclipse.swt.graphics.Device;
24+
import org.eclipse.swt.graphics.Image;
25+
import org.eclipse.swt.graphics.ImageGcDrawer;
26+
import org.eclipse.swt.graphics.Point;
27+
import org.eclipse.swt.layout.GridData;
28+
import org.eclipse.swt.layout.GridLayout;
29+
import org.eclipse.swt.widgets.Composite;
30+
import org.eclipse.swt.widgets.Display;
31+
import org.eclipse.swt.widgets.Event;
32+
import org.eclipse.swt.widgets.Label;
33+
import org.eclipse.swt.widgets.Shell;
34+
import org.eclipse.swt.widgets.Table;
35+
import org.eclipse.swt.widgets.TableColumn;
36+
import org.eclipse.swt.widgets.TableItem;
37+
import org.eclipse.swt.widgets.Tree;
38+
import org.eclipse.swt.widgets.TreeColumn;
39+
import org.eclipse.swt.widgets.TreeItem;
2240

2341
public final class Bug577878_GTK_TreeTable_NoDragImageWithPaintItem {
2442
static final int NUM_ROWS = 100;
@@ -31,17 +49,15 @@ static String makeItemText(int iRow, int iCol) {
3149
}
3250

3351
static Image makeItemImage(Device device, int iRow) {
34-
Image image = new Image(device, IMG_CX, IMG_CY);
35-
GC gc = new GC(image);
36-
gc.setBackground(new Color(100,255,100));
37-
gc.fillRectangle(0, 0, IMG_CX-1, IMG_CY-1);
38-
gc.drawRectangle(0, 0, IMG_CX-1, IMG_CY-1);
39-
4052
String text = Integer.toString(iRow);
41-
Point textSize = gc.stringExtent(text);
42-
gc.drawText(text, (IMG_CX - textSize.x) / 2, (IMG_CY - textSize.y) / 2);
43-
44-
gc.dispose();
53+
final ImageGcDrawer imageGcDrawer = (gc, width, height) -> {
54+
gc.setBackground(new Color(100,255,100));
55+
gc.fillRectangle(0, 0, width-1, height-1);
56+
gc.drawRectangle(0, 0, width-1, height-1);
57+
Point textSize = gc.stringExtent(text);
58+
gc.drawText(text, (IMG_CX - textSize.x) / 2, (IMG_CY - textSize.y) / 2);
59+
};
60+
Image image = new Image(device, imageGcDrawer, IMG_CX, IMG_CY);
4561
return image;
4662
}
4763

0 commit comments

Comments
 (0)