KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > AbstractNodeTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.nodes;
21
22 import java.beans.BeanInfo JavaDoc;
23 import org.openide.util.Utilities;
24 import java.awt.Image JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import junit.framework.TestCase;
27
28 /**
29  * A test for icon loading only so far.
30  *
31  * @author Nenik
32  */

33 public class AbstractNodeTest extends TestCase {
34
35     public AbstractNodeTest(String JavaDoc testName) {
36         super(testName);
37     }
38
39     /**
40      * Test of setIconBaseWithExtension method, of class org.openide.nodes.AbstractNode.
41      */

42     public void testSetIconBaseWithExtension() {
43         AbstractNode an = new AbstractNode(Children.LEAF);
44         Listener lst = new Listener();
45         an.addNodeListener(lst);
46         
47         // default icons
48
Image JavaDoc def = getImage("org/openide/nodes/defaultNode.gif");
49         Image JavaDoc def32 = getImage("org/openide/nodes/defaultNode32.gif");
50         
51         // PNGs, Open32 is missing
52
Image JavaDoc aPng = getImage("org/openide/nodes/data/a.png");
53         Image JavaDoc aPngOpen = getImage("org/openide/nodes/data/aOpen.png");
54         Image JavaDoc a32Png = getImage("org/openide/nodes/data/a32.png");
55         
56         // GIFs, 32 is missing
57
Image JavaDoc aGif = getImage("org/openide/nodes/data/a.gif");
58         Image JavaDoc a32Gif = getImage("org/openide/nodes/data/a32.gif");
59         Image JavaDoc a32GifOpen = getImage("org/openide/nodes/data/aOpen32.gif");
60         
61         // extension-less icons
62
Image JavaDoc b = getImage("org/openide/nodes/data/b");
63         Image JavaDoc b32 = getImage("org/openide/nodes/data/b32");
64
65         // ugly one, no extension, dot in path
66
Image JavaDoc b2 = getImage("org/openide/nodes/data/res.t2/b");
67
68         // check the default icon first
69
checkIcons(an, def, def32, def, def32);
70         
71         // verify the original method behaviour
72
an.setIconBase("org/openide/nodes/data/a");
73         lst.assertEvents(2); // icon and opened icon
74
checkIcons(an, aGif, a32Gif, aGif, a32GifOpen);
75         
76         // Check the preferred method
77
an.setIconBaseWithExtension("org/openide/nodes/data/a.png");
78         lst.assertEvents(2); // icon and opened icon
79
checkIcons(an, aPng, a32Png, aPngOpen, aPngOpen);
80         
81         // also for gifs
82
an.setIconBaseWithExtension("org/openide/nodes/data/a.gif");
83         lst.assertEvents(2); // icon and opened icon
84
checkIcons(an, aGif, a32Gif, aGif, a32GifOpen);
85         
86         // What if there is no extension?
87
an.setIconBaseWithExtension("org/openide/nodes/data/b");
88         lst.assertEvents(2); // icon and opened icon
89
checkIcons(an, b, b32, b, b32);
90
91         // Do we support such insane resources too?
92
an.setIconBaseWithExtension("org/openide/nodes/data/res.t2/b");
93         lst.assertEvents(2); // icon and opened icon
94
checkIcons(an, b2, b2, b2, b2);
95
96     }
97
98
99     private static Image JavaDoc getImage(String JavaDoc path) {
100         Image JavaDoc ret = Utilities.loadImage(path, true);
101         assertNotNull("Icon loaded", ret);
102         return ret;
103     }
104     
105     private static void checkIcons(Node n, Image JavaDoc base, Image JavaDoc base32, Image JavaDoc open, Image JavaDoc open32) {
106         assertSame("Base icon", base, n.getIcon(BeanInfo.ICON_COLOR_16x16));
107         assertSame("Base icon (mono)", base, n.getIcon(BeanInfo.ICON_MONO_16x16));
108         assertSame("Base icon32", base32, n.getIcon(BeanInfo.ICON_COLOR_32x32));
109         assertSame("Base icon32 (mono)", base32, n.getIcon(BeanInfo.ICON_MONO_32x32));
110         assertSame("Open icon", open, n.getOpenedIcon(BeanInfo.ICON_COLOR_16x16));
111         assertSame("Open icon (mono)", open, n.getOpenedIcon(BeanInfo.ICON_MONO_16x16));
112         assertSame("Open icon32", open32, n.getOpenedIcon(BeanInfo.ICON_COLOR_32x32));
113         assertSame("Open icon32 (mono)", open32, n.getOpenedIcon(BeanInfo.ICON_MONO_32x32));
114         
115     }
116     
117     static class Listener extends NodeAdapter {
118         int cnt;
119         
120         public void propertyChange(PropertyChangeEvent JavaDoc propertyChangeEvent) {
121             cnt++;
122         }
123         
124         public void assertEvents(int count) {
125             assertEquals("Number of events", count, cnt);
126             cnt = 0;
127         }
128     }
129 }
130
Popular Tags