Skip to content

Commit eac27cd

Browse files
committed
Simplify and modernize ImageLoader and other graphic related tests
1 parent a977a6e commit eac27cd

File tree

5 files changed

+65
-104
lines changed

5 files changed

+65
-104
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,8 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceI() {
137137
@Test
138138
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_ImageDataLorg_eclipse_swt_graphics_ImageDataII() {
139139
// Test new Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int hotspotY)
140-
int numFormats = SwtTestUtil.imageFormats.length;
141140
String fileName = SwtTestUtil.imageFilenames[0];
142-
for (int i=0; i<numFormats; i++) {
143-
String format = SwtTestUtil.imageFormats[i];
141+
for (String format : SwtTestUtil.imageFormats) {
144142
ImageLoader loader = new ImageLoader();
145143
try (InputStream stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) {
146144
ImageData source = loader.load(stream)[0];

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLjava_io_InputStream
256256

257257
String firstFile = SwtTestUtil.invalidImageFilenames[0];
258258
Display[] displays = { display, null };
259-
for (int j = 0; j < displays.length; j++) {
259+
for (Display display : displays) {
260260
for (String format : SwtTestUtil.imageFormats) {
261261
try (InputStream stream = SwtTestUtil.class.getResourceAsStream(firstFile + "." + format)) {
262262
e = assertThrows(SWTException.class, () -> new Image(display, stream));
@@ -316,8 +316,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLjava_lang_String()
316316
// create valid images
317317
for (Display display : displays) {
318318
for (String fileName : SwtTestUtil.imageFilenames) {
319-
for (int i = 0; i < SwtTestUtil.imageFormats.length; i++) {
320-
String format = SwtTestUtil.imageFormats[i];
319+
for (String format : SwtTestUtil.imageFormats) {
321320
String pathName = getPath(fileName + "." + format).toString();
322321
Image image = new Image(display, pathName);
323322
image.dispose();
@@ -868,10 +867,8 @@ public void test_toString() {
868867
/** Test implementation **/
869868

870869
void getImageData1() {
871-
int numFormats = SwtTestUtil.imageFormats.length;
872870
String fileName = SwtTestUtil.imageFilenames[0];
873-
for (int i=0; i<numFormats; i++) {
874-
String format = SwtTestUtil.imageFormats[i];
871+
for (String format : SwtTestUtil.imageFormats) {
875872
try (InputStream stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) {
876873
ImageData data1 = new ImageData(stream);
877874
Image image = new Image(display, data1);

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.io.IOException;
2929
import java.io.InputStream;
30-
import java.lang.reflect.InvocationTargetException;
3130
import java.util.ArrayList;
3231
import java.util.List;
3332

@@ -60,9 +59,10 @@ public void setUp() {
6059
/**
6160
* Tests {@link ImageData#blit}:
6261
* creates a random image and tests over all combinations of depth,format,scale
62+
* @throws Exception
6363
*/
6464
@Test
65-
public void test_blit() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
65+
public void test_blit() throws Exception {
6666
List<BlitTestInfo> tests = new ArrayList<>();
6767

6868
// Compose a list of all supported formats
@@ -117,9 +117,10 @@ public void test_blit() throws NoSuchMethodException, SecurityException, Illegal
117117
/**
118118
* Tests {@link ImageData#blit}:
119119
* Ensures that (MSB_FIRST, LSB_FIRST) round trip produces original.
120+
* @throws Exception
120121
*/
121122
@Test
122-
public void test_blit_MsbLsb() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
123+
public void test_blit_MsbLsb() throws Exception {
123124
List<BlitTestInfo> tests = new ArrayList<>();
124125
{
125126
for (int depth : indexedDepths) {
@@ -218,10 +219,8 @@ public void test_ConstructorLjava_io_InputStream() throws IOException {
218219
assertThrows("No exception thrown for invalid InputStream", SWTException.class, () ->new ImageData(stream1));
219220
}
220221

221-
int numFormats = SwtTestUtil.imageFormats.length;
222222
String fileName = SwtTestUtil.imageFilenames[0];
223-
for (int i=0; i<numFormats; i++) {
224-
String format = SwtTestUtil.imageFormats[i];
223+
for (String format : SwtTestUtil.imageFormats) {
225224
try (InputStream stream2 = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) {
226225
new ImageData(stream2);
227226
}

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

Lines changed: 47 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertTrue;
22-
import static org.junit.Assert.fail;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2323

2424
import java.io.ByteArrayInputStream;
2525
import java.io.ByteArrayOutputStream;
2626
import java.io.IOException;
2727
import java.io.InputStream;
28+
import java.io.OutputStream;
29+
import java.util.Arrays;
2830

2931
import org.eclipse.swt.SWT;
3032
import org.eclipse.swt.SWTException;
@@ -49,30 +51,26 @@ public void test_Constructor() {
4951
}
5052

5153
@Test
52-
public void test_addImageLoaderListenerLorg_eclipse_swt_graphics_ImageLoaderListener() {
54+
public void test_addImageLoaderListenerLorg_eclipse_swt_graphics_ImageLoaderListener() throws IOException {
5355
ImageLoader loader = new ImageLoader();
5456
ImageLoaderListener loaderListener = e -> loaderListenerCalled = true;
5557

56-
try {
57-
loader.addImageLoaderListener(null);
58-
fail("No exception thrown for addImageLoaderListener with null argument");
59-
} catch (IllegalArgumentException e) {
60-
}
61-
58+
assertThrows(IllegalArgumentException.class, () -> loader.addImageLoaderListener(null),
59+
"No exception thrown for addImageLoaderListener with null argument");
6260
assertFalse(":a:", loader.hasListeners());
6361
loader.addImageLoaderListener(loaderListener);
6462
assertTrue(":b:", loader.hasListeners());
6563

6664
loaderListenerCalled = false;
6765
try (InputStream stream = SwtTestUtil.class.getResourceAsStream("interlaced_target.png")) {
6866
loader.load(stream);
69-
} catch (IOException e) {}
67+
}
7068
assertTrue(":c:", loaderListenerCalled);
7169

7270
loaderListenerCalled = false;
7371
try (InputStream stream = SwtTestUtil.class.getResourceAsStream("target.png")) {
7472
loader.load(stream);
75-
} catch (IOException e) {}
73+
}
7674
assertFalse(":d:", loaderListenerCalled);
7775

7876
loaderListenerCalled = false;
@@ -84,98 +82,70 @@ public void test_addImageLoaderListenerLorg_eclipse_swt_graphics_ImageLoaderList
8482
}
8583

8684
@Test
87-
public void test_loadLjava_io_InputStream() {
88-
ImageLoader loader = new ImageLoader();
89-
try (InputStream stream = null) {
90-
loader.load(stream);
91-
fail("No exception thrown for load inputStream == null");
92-
} catch (IllegalArgumentException | IOException e) {
93-
}
85+
public void test_loadLjava_io_InputStream() throws IOException {
86+
ImageLoader loader = new ImageLoader();
87+
assertThrows(IllegalArgumentException.class, () -> loader.load((InputStream) null),
88+
"No exception thrown for load inputStream == null");
9489

95-
try (InputStream stream = SwtTestUtil.class.getResourceAsStream("empty.txt")) {
96-
loader.load(stream);
97-
fail("No exception thrown for load from invalid inputStream");
98-
} catch (IOException|SWTException e) {
99-
}
90+
try (InputStream stream = SwtTestUtil.class.getResourceAsStream("empty.txt")) {
91+
assertThrows(SWTException.class, () -> loader.load(stream),
92+
"No exception thrown for load from invalid inputStream");
93+
}
10094

101-
int numFormats = SwtTestUtil.imageFormats.length;
102-
String fileName = SwtTestUtil.imageFilenames[0];
103-
for (int i = 0; i < numFormats; i++) {
104-
String format = SwtTestUtil.imageFormats[i];
105-
try (InputStream stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) {
106-
loader.load(stream);
107-
} catch (IOException e) {
108-
}
95+
String fileName = SwtTestUtil.imageFilenames[0];
96+
for (String format : SwtTestUtil.imageFormats) {
97+
try (InputStream stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) {
98+
loader.load(stream);
10999
}
100+
}
110101
}
111102

112103
@Test
113104
public void test_loadLjava_lang_String() {
114105
ImageLoader loader = new ImageLoader();
115106
String filename = null;
116-
try {
117-
loader.load(filename);
118-
fail("No exception thrown for load filename == null");
119-
} catch (IllegalArgumentException e) {
120-
}
107+
assertThrows(IllegalArgumentException.class, () -> loader.load(filename),
108+
"No exception thrown for load filename == null");
121109
}
122110

123111
@Test
124-
public void test_saveLjava_io_OutputStreamI() {
112+
public void test_saveLjava_io_OutputStreamI() throws IOException {
125113
ImageLoader loader = new ImageLoader();
126-
ByteArrayOutputStream outStream = null;
127-
try {
128-
try {
129-
loader.save(outStream, 0);
130-
fail("No exception thrown for save outputStream == null");
131-
} catch (IllegalArgumentException e) {
132-
}
133114

134-
outStream = new ByteArrayOutputStream();
135-
try {
136-
loader.save(outStream, -1);
137-
fail("No exception thrown for save to invalid outputStream format");
138-
} catch (SWTException e) {
115+
OutputStream outStream1 = null;
116+
assertThrows(IllegalArgumentException.class, () -> loader.save(outStream1, 0),
117+
"No exception thrown for save outputStream == null");
118+
119+
OutputStream outStream2 = new ByteArrayOutputStream();
120+
assertThrows(SWTException.class, () -> loader.save(outStream2, -1),
121+
"No exception thrown for save to invalid outputStream format");
122+
123+
boolean jpgSupported = Arrays.asList(SwtTestUtil.imageFormats).contains("jpg");
124+
if (jpgSupported) {
125+
String filename = SwtTestUtil.imageFilenames[0];
126+
// must use jpg since save is not implemented yet in png format
127+
String filetype = "jpg";
128+
try (InputStream inStream = SwtTestUtil.class.getResourceAsStream(filename + "." + filetype)) {
129+
loader.load(inStream);
139130
}
140-
boolean jpgSupported = false;
141-
for (String imageFormat : SwtTestUtil.imageFormats) {
142-
if (imageFormat.equals("jpg")) {
143-
jpgSupported = true;
131+
OutputStream outStream = new ByteArrayOutputStream();
132+
int i = 0;
133+
for (String format : SwtTestUtil.imageFormats) {
134+
if (format.equals(filetype)) {
135+
// save using the appropriate format
136+
loader.save(outStream, i++);
144137
break;
145138
}
146139
}
147-
if (jpgSupported) {
148-
String filename = SwtTestUtil.imageFilenames[0];
149-
// must use jpg since save is not implemented yet in png format
150-
String filetype = "jpg";
151-
try (InputStream inStream = SwtTestUtil.class.getResourceAsStream(filename + "." + filetype)) {
152-
loader.load(inStream);
153-
} catch (IOException e) {}
154-
for (int i = 0; i < SwtTestUtil.imageFormats.length; i++) {
155-
if (SwtTestUtil.imageFormats[i].equals(filetype)) {
156-
// save using the appropriate format
157-
loader.save(outStream, i);
158-
break;
159-
}
160-
}
161-
}
162-
} finally {
163-
try {
164-
outStream.close();
165-
} catch (Exception e) {
166-
}
167140
}
168141
}
169142

170143
@Test
171144
public void test_saveLjava_lang_StringI() {
172145
ImageLoader loader = new ImageLoader();
173146
String filename = null;
174-
try {
175-
loader.save(filename, 0);
176-
fail("No exception thrown for save filename == null");
177-
} catch (IllegalArgumentException e) {
178-
}
147+
assertThrows(IllegalArgumentException.class, () -> loader.save(filename, 0),
148+
"No exception thrown for save filename == null");
179149
}
180150

181151
/**

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.io.IOException;
2323
import java.io.InputStream;
24+
import java.util.ArrayList;
25+
import java.util.List;
2426

2527
import org.eclipse.swt.SWT;
2628
import org.eclipse.swt.graphics.Image;
@@ -123,9 +125,9 @@ public void test_setDefaultButtonLorg_eclipse_swt_widgets_Button() {
123125
public void test_setImageLorg_eclipse_swt_graphics_Image() {
124126
assertNull(":a:", decorations.getImage());
125127
loadImages();
126-
decorations.setImage(images[0]);
127-
assertTrue(":b:", images[0] == decorations.getImage());
128-
assertTrue(":c:", images[1] != decorations.getImage());
128+
decorations.setImage(images.get(0));
129+
assertTrue(":b:", images.get(0) == decorations.getImage());
130+
assertTrue(":c:", images.get(1) != decorations.getImage());
129131
decorations.setImage(null);
130132
assertNull(":d:", decorations.getImage());
131133
freeImages();
@@ -189,7 +191,7 @@ public void test_setVisibleZ() {
189191

190192
/* custom */
191193
Decorations decorations;
192-
Image[] images = new Image [SwtTestUtil.imageFormats.length*SwtTestUtil.imageFilenames.length];
194+
private List<Image> images = new ArrayList<>();
193195

194196
@Override
195197
protected void setWidget(Widget w) {
@@ -201,15 +203,10 @@ protected void setWidget(Widget w) {
201203

202204
// this method must be private or protected so the auto-gen tool keeps it
203205
private void loadImages() {
204-
int numFormats = SwtTestUtil.imageFormats.length;
205-
int numFiles = SwtTestUtil.imageFilenames.length;
206-
for (int i=0; i<numFormats; i++) {
207-
String format = SwtTestUtil.imageFormats[i];
208-
int index = i*numFiles;
209-
for (int j=0; j<numFiles; j++){
210-
String fileName = SwtTestUtil.imageFilenames[j];
206+
for (String format : SwtTestUtil.imageFormats) {
207+
for (String fileName : SwtTestUtil.imageFilenames) {
211208
try (InputStream resource = this.getClass().getResourceAsStream(fileName + "." + format)) {
212-
images [index+j] = new Image (shell.getDisplay(), resource);
209+
images.add(new Image(shell.getDisplay(), resource));
213210
} catch (IOException e) {
214211
// continue;
215212
}

0 commit comments

Comments
 (0)