001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 05.04.2005
021:
022: */package javax.swing;
023:
024: import java.awt.Component;
025: import java.awt.Image;
026: import java.awt.MediaTracker;
027: import java.awt.image.BufferedImage;
028: import java.awt.image.ImageObserver;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.net.URL;
032: import javax.accessibility.AccessibleContext;
033: import javax.accessibility.AccessibleIcon;
034:
035: public class ImageIconTest extends SwingTestCase {
036: private static final String FILE_NAME_1 = "images/Error.gif";
037:
038: private static final int FILE_SIZE_1 = 923;
039:
040: private static final int ICON_SIZE_1 = 32;
041:
042: private static final String FILE_NAME_2 = "ImageIconTest.class";
043:
044: class MyImageIcon extends ImageIcon {
045: private static final long serialVersionUID = 1L;
046:
047: public Component getComponent() {
048: return component;
049: }
050:
051: public MediaTracker getTracker() {
052: return tracker;
053: }
054:
055: @Override
056: public void loadImage(final Image image) {
057: super .loadImage(image);
058: }
059: };
060:
061: protected ImageIcon icon = null;
062:
063: /*
064: * @see TestCase#setUp()
065: */
066: @Override
067: protected void setUp() throws Exception {
068: super .setUp();
069: }
070:
071: /*
072: * @see TestCase#tearDown()
073: */
074: @Override
075: protected void tearDown() throws Exception {
076: icon = null;
077: super .tearDown();
078: }
079:
080: /*
081: * Class under test for void ImageIcon(String)
082: */
083: public void testImageIconString() {
084: URL url1 = getClass().getResource(FILE_NAME_1);
085: URL url2 = getClass().getResource(FILE_NAME_2);
086: assertNotNull("file is found", url1);
087: String filePath1 = url1.getPath();
088: // preventing jar failures
089: if (filePath1.indexOf(".jar!") != -1) {
090: return;
091: }
092: icon = new ImageIcon(filePath1);
093: assertEquals("loaded", MediaTracker.COMPLETE, icon
094: .getImageLoadStatus());
095: assertEquals("description", filePath1, icon.getDescription());
096: assertEquals("width", ICON_SIZE_1, icon.getIconWidth());
097: assertNotNull("file is found", url2);
098: String filePath2 = url2.getPath();
099: icon = new ImageIcon(filePath2);
100: assertEquals("loaded", MediaTracker.ERRORED, icon
101: .getImageLoadStatus());
102: assertEquals("description", filePath2, icon.getDescription());
103: assertEquals("width", -1, icon.getIconWidth());
104: }
105:
106: /*
107: * Class under test for void ImageIcon(String, String)
108: */
109: public void testImageIconStringString() {
110: final String description1 = "bullet in your head";
111: final String description2 = null;
112: URL url1 = getClass().getResource(FILE_NAME_1);
113: URL url2 = getClass().getResource(FILE_NAME_2);
114: assertNotNull("file is found", url1);
115: String filePath1 = url1.getPath();
116: // preventing jar failures
117: if (filePath1.indexOf(".jar!") != -1) {
118: return;
119: }
120: icon = new ImageIcon(filePath1, description1);
121: assertEquals("loaded", MediaTracker.COMPLETE, icon
122: .getImageLoadStatus());
123: assertEquals("description", description1, icon.getDescription());
124: assertEquals("width", ICON_SIZE_1, icon.getIconWidth());
125: assertNotNull("file is found", url2);
126: String filePath2 = url2.getPath();
127: icon = new ImageIcon(filePath2, description2);
128: assertEquals("loaded", MediaTracker.ERRORED, icon
129: .getImageLoadStatus());
130: assertEquals("description", description2, icon.getDescription());
131: assertEquals("width", -1, icon.getIconWidth());
132: }
133:
134: /*
135: * Class under test for void ImageIcon(URL)
136: */
137: public void testImageIconURL() {
138: URL url1 = getClass().getResource(FILE_NAME_1);
139: URL url2 = getClass().getResource(FILE_NAME_2);
140: assertNotNull("file is found", url1);
141: icon = new ImageIcon(url1);
142: assertEquals("loaded", MediaTracker.COMPLETE, icon
143: .getImageLoadStatus());
144: assertEquals("description", url1.toString(), icon
145: .getDescription());
146: assertEquals("width", ICON_SIZE_1, icon.getIconWidth());
147: assertNotNull("file is found", url2);
148: icon = new ImageIcon(url2);
149: assertEquals("loaded", MediaTracker.ERRORED, icon
150: .getImageLoadStatus());
151: assertEquals("description", url2.toString(), icon
152: .getDescription());
153: assertEquals("width", -1, icon.getIconWidth());
154: }
155:
156: /*
157: * Class under test for void ImageIcon(URL, String)
158: */
159: public void testImageIconURLString() {
160: final String description1 = "bullet in your head";
161: final String description2 = null;
162: URL url1 = getClass().getResource(FILE_NAME_1);
163: URL url2 = getClass().getResource(FILE_NAME_2);
164: assertNotNull("file is found", url1);
165: icon = new ImageIcon(url1, description1);
166: assertEquals("loaded", MediaTracker.COMPLETE, icon
167: .getImageLoadStatus());
168: assertEquals("description", description1, icon.getDescription());
169: assertEquals("width", ICON_SIZE_1, icon.getIconWidth());
170: assertNotNull("file is found", url2);
171: icon = new ImageIcon(url2, description2);
172: assertEquals("loaded", MediaTracker.ERRORED, icon
173: .getImageLoadStatus());
174: assertEquals("description", description2, icon.getDescription());
175: assertEquals("width", -1, icon.getIconWidth());
176: }
177:
178: /*
179: * Class under test for void ImageIcon(Image, String)
180: */
181: public void testImageIconImageString() {
182: final String description1 = "bullet in your head";
183: final String description2 = "born without a face";
184: Image image1 = new BufferedImage(100, 100,
185: BufferedImage.TYPE_INT_RGB) {
186: @Override
187: public Object getProperty(final String name,
188: final ImageObserver observer) {
189: return description1;
190: }
191: };
192: icon = new ImageIcon(image1, description2);
193: assertEquals("description", description2, icon.getDescription());
194: icon = new ImageIcon(image1, null);
195: assertNull("description", icon.getDescription());
196: assertEquals("image", image1, icon.getImage());
197: }
198:
199: /*
200: * Class under test for void ImageIcon(byte[])
201: */
202: public void testImageIconbyteArray() throws IOException {
203: InputStream stream1 = getClass().getResourceAsStream(
204: FILE_NAME_1);
205: assertTrue("file is found", stream1 != null);
206: byte[] array1 = new byte[10000];
207: icon = new ImageIcon(array1);
208: assertEquals("loaded", MediaTracker.ERRORED, icon
209: .getImageLoadStatus());
210: assertNull("description", icon.getDescription());
211: assertEquals("width", -1, icon.getIconWidth());
212: int bytesRead = stream1.read(array1);
213: assertEquals("array size", FILE_SIZE_1, bytesRead);
214: icon = new ImageIcon(array1);
215: assertEquals("loaded", MediaTracker.COMPLETE, icon
216: .getImageLoadStatus());
217: if (isHarmony()) {
218: assertNotNull("description", icon.getDescription());
219: }
220: assertEquals("width", ICON_SIZE_1, icon.getIconWidth());
221: }
222:
223: /*
224: * Class under test for void ImageIcon(byte[], String)
225: */
226: public void testImageIconbyteArrayString() throws IOException {
227: final String description1 = "bullet in your head";
228: final String description2 = "born without a face";
229: InputStream stream1 = getClass().getResourceAsStream(
230: FILE_NAME_1);
231: assertTrue("file is found", stream1 != null);
232: byte[] array1 = new byte[10000];
233: icon = new ImageIcon(array1, description1);
234: assertEquals("loaded", MediaTracker.ERRORED, icon
235: .getImageLoadStatus());
236: assertEquals("description", description1, icon.getDescription());
237: assertEquals("width", -1, icon.getIconWidth());
238: int bytesRead = stream1.read(array1);
239: assertEquals("array size", FILE_SIZE_1, bytesRead);
240: icon = new ImageIcon(array1, description2);
241: assertEquals("loaded", MediaTracker.COMPLETE, icon
242: .getImageLoadStatus());
243: assertEquals("description", description2, icon.getDescription());
244: assertEquals("width", ICON_SIZE_1, icon.getIconWidth());
245: }
246:
247: /*
248: * Class under test for void ImageIcon(Image)
249: */
250: public void testImageIconImage() {
251: final String description1 = "bullet in your head";
252: Image image1 = new BufferedImage(100, 100,
253: BufferedImage.TYPE_INT_RGB) {
254: @Override
255: public Object getProperty(final String name,
256: final ImageObserver observer) {
257: return description1;
258: }
259: };
260: Image image2 = new BufferedImage(100, 100,
261: BufferedImage.TYPE_INT_RGB);
262: icon = new ImageIcon(image1);
263: assertEquals("description", description1, icon.getDescription());
264: assertEquals("loaded", MediaTracker.COMPLETE, icon
265: .getImageLoadStatus());
266: assertEquals("image", image1, icon.getImage());
267: icon = new ImageIcon(image2);
268: assertNull("description", icon.getDescription());
269: assertEquals("loaded", MediaTracker.COMPLETE, icon
270: .getImageLoadStatus());
271: assertEquals("image", image2, icon.getImage());
272: }
273:
274: /*
275: * Class under test for void ImageIcon()
276: */
277: public void testImageIcon() {
278: icon = new ImageIcon();
279: assertNull("image", icon.getImage());
280: assertEquals("height", -1, icon.getIconHeight());
281: assertEquals("width", -1, icon.getIconWidth());
282: assertNull("description", icon.getDescription());
283: assertEquals("loaded", 0, icon.getImageLoadStatus());
284: }
285:
286: public void testGetAccessibleContext() {
287: int width = 11;
288: int height = 32;
289: ImageIcon icon1 = new ImageIcon(new BufferedImage(width,
290: height, BufferedImage.TYPE_INT_RGB));
291: ImageIcon icon2 = new ImageIcon(new BufferedImage(width,
292: height, BufferedImage.TYPE_INT_RGB));
293: AccessibleContext accessible1 = icon1.getAccessibleContext();
294: AccessibleContext accessible2 = icon1.getAccessibleContext();
295: AccessibleContext accessible3 = icon2.getAccessibleContext();
296: assertTrue("accessible",
297: accessible1 instanceof ImageIcon.AccessibleImageIcon);
298: assertSame("accessibles ain't unique", accessible1, accessible2);
299: assertTrue("accessibles are unique", accessible1 != accessible3);
300: }
301:
302: /**
303: * this method is being tested by testGetDescription();
304: */
305: public void testSetDescription() {
306: }
307:
308: /*
309: * Class under test for String toString()
310: */
311: public void testToString() {
312: final String description = "bullet in your head";
313: Image image1 = new BufferedImage(100, 100,
314: BufferedImage.TYPE_INT_RGB);
315: icon = new ImageIcon(image1, description);
316: assertEquals("string is not empty", description, icon
317: .toString());
318: icon = new ImageIcon(image1);
319: assertTrue("string is not empty", icon.toString() != null
320: && !icon.toString().equals(""));
321: }
322:
323: public void testGetDescription() {
324: final String description1 = "bullet in your head";
325: final String description2 = "born without a face";
326: final String description3 = "snakecharmer";
327: Image image1 = new BufferedImage(100, 100,
328: BufferedImage.TYPE_INT_RGB) {
329: @Override
330: public Object getProperty(final String name,
331: final ImageObserver observer) {
332: return description1;
333: }
334: };
335: Image image2 = new BufferedImage(100, 100,
336: BufferedImage.TYPE_INT_RGB) {
337: @Override
338: public Object getProperty(final String name,
339: final ImageObserver observer) {
340: return description3;
341: }
342: };
343: icon = new ImageIcon(image1);
344: assertEquals("description", description1, icon.getDescription());
345: icon.setDescription(description2);
346: assertEquals("description", description2, icon.getDescription());
347: icon.setDescription(null);
348: assertNull("description", icon.getDescription());
349: icon.setDescription(description2);
350: assertEquals("description", description2, icon.getDescription());
351: icon.setImage(image2);
352: assertEquals("description", description2, icon.getDescription());
353: icon = new ImageIcon(image2, description1);
354: assertEquals("description", description1, icon.getDescription());
355: icon = new ImageIcon(image2, null);
356: assertNull("description", icon.getDescription());
357: }
358:
359: public void testGetImageObserver() {
360: ImageObserver observer1 = new JPanel();
361: ImageObserver observer2 = new JPanel();
362: int width = 111;
363: int height = 235;
364: icon = new ImageIcon(new BufferedImage(width, height,
365: BufferedImage.TYPE_INT_RGB));
366: assertNull(icon.getImageObserver());
367: icon.setImageObserver(observer1);
368: assertEquals("observer", observer1, icon.getImageObserver());
369: icon.setImageObserver(observer2);
370: assertEquals("observer", observer2, icon.getImageObserver());
371: }
372:
373: public void testSetImage() {
374: int width = 111;
375: int height = 235;
376: Image image1 = new BufferedImage(width, height,
377: BufferedImage.TYPE_INT_RGB);
378: Image image2 = new BufferedImage(width, height,
379: BufferedImage.TYPE_INT_RGB);
380: icon = new ImageIcon(image1);
381: assertEquals("image", image1, icon.getImage());
382: assertEquals("loaded", MediaTracker.COMPLETE, icon
383: .getImageLoadStatus());
384: icon.setImage(image2);
385: assertEquals("image", image2, icon.getImage());
386: assertEquals("loaded", MediaTracker.COMPLETE, icon
387: .getImageLoadStatus());
388: icon.setImage(image1);
389: assertEquals("image", image1, icon.getImage());
390: assertEquals("loaded", MediaTracker.COMPLETE, icon
391: .getImageLoadStatus());
392: }
393:
394: public void testLoadImage() {
395: int width = 111;
396: int height = 235;
397: Image image1 = new BufferedImage(width, height,
398: BufferedImage.TYPE_INT_RGB);
399: MyImageIcon icon = new MyImageIcon();
400: assertEquals("load status", 0, icon.getImageLoadStatus());
401: assertNull("image", icon.getImage());
402: icon.loadImage(image1);
403: assertEquals("load status", MediaTracker.COMPLETE, icon
404: .getImageLoadStatus());
405: assertNull("image", icon.getImage());
406: }
407:
408: /**
409: * this method is being tested by testSetImage();
410: */
411: public void testGetImage() {
412: }
413:
414: /**
415: * this method is being tested by constructors testcases
416: */
417: public void testGetImageLoadStatus() {
418: }
419:
420: public void testGetIconWidth() {
421: int width = 111;
422: int height = 235;
423: icon = new ImageIcon(new BufferedImage(width, height,
424: BufferedImage.TYPE_INT_RGB));
425: assertEquals("width", width, icon.getIconWidth());
426: icon.setImage(new BufferedImage(7 * width, 4 * height,
427: BufferedImage.TYPE_INT_RGB));
428: assertEquals("width", 7 * width, icon.getIconWidth());
429: }
430:
431: public void testGetIconHeight() {
432: int width = 111;
433: int height = 235;
434: icon = new ImageIcon(new BufferedImage(width, height,
435: BufferedImage.TYPE_INT_RGB));
436: assertEquals("height", height, icon.getIconHeight());
437: icon.setImage(new BufferedImage(7 * width, 4 * height,
438: BufferedImage.TYPE_INT_RGB));
439: assertEquals("height", 4 * height, icon.getIconHeight());
440: icon = new ImageIcon("");
441: assertEquals("height", -1, icon.getIconHeight());
442: }
443:
444: public void testStaticFields() {
445: MyImageIcon myIcon = new MyImageIcon();
446: assertNotNull(myIcon.getTracker());
447: assertNotNull(myIcon.getComponent());
448: }
449:
450: public void testAccessibleImageIcon() {
451: int width = 111;
452: int height = 235;
453: icon = new ImageIcon(new BufferedImage(width, height,
454: BufferedImage.TYPE_INT_RGB));
455:
456: AccessibleContext ac = icon.getAccessibleContext();
457: AccessibleIcon ai = (AccessibleIcon) ac;
458:
459: assertEquals(ac.getAccessibleChildrenCount(), 0);
460: assertNull(ac.getAccessibleChild(0));
461: assertNull(ac.getAccessibleChild(10));
462: assertNull(ac.getAccessibleChild(-1));
463:
464: assertNull(ac.getAccessibleParent());
465:
466: ac.setAccessibleParent(new ImageIcon());
467: assertNull(ac.getAccessibleParent());
468: assertNull(ac.getAccessibleStateSet());
469:
470: assertEquals("width", width, ai.getAccessibleIconWidth());
471: assertEquals("height", height, ai.getAccessibleIconHeight());
472: }
473: }
|