KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > palette > ItemTest


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.netbeans.spi.palette;
21
22 import java.awt.datatransfer.Transferable JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import java.io.IOException JavaDoc;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import org.netbeans.modules.palette.Category;
30 import org.netbeans.modules.palette.Item;
31 import org.netbeans.modules.palette.Model;
32 import org.openide.nodes.Node;
33 import org.openide.util.Lookup;
34
35 /**
36  *
37  * @author S. Aubrecht
38  */

39 public class ItemTest extends AbstractPaletteTestHid {
40
41     public ItemTest(String JavaDoc testName) {
42         super(testName);
43     }
44
45     /**
46      * Test of getName method, of class org.netbeans.modules.palette.Item.
47      */

48     public void testGetName() throws IOException JavaDoc {
49         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
50         Model model = pc.getModel();
51         Category[] categories = model.getCategories();
52         
53         for( int i=0; i<itemNames.length; i++ ) {
54             Item[] items = categories[i].getItems();
55             assertEquals( itemNames[i].length, items.length );
56             for( int j=0; j<items.length; j++ ) {
57                 Node node = getItemNode( categoryNames[i], itemNames[i][j] );
58                 assertEquals( node.getName(), items[j].getName() );
59             }
60         }
61     }
62
63     /**
64      * Test of getDisplayName method, of class org.netbeans.modules.palette.Item.
65      */

66     public void testGetDisplayName() throws IOException JavaDoc {
67         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
68         Model model = pc.getModel();
69         Category[] categories = model.getCategories();
70         
71         for( int i=0; i<itemNames.length; i++ ) {
72             Item[] items = categories[i].getItems();
73             assertEquals( itemNames[i].length, items.length );
74             for( int j=0; j<items.length; j++ ) {
75                 Node node = getItemNode( categoryNames[i], itemNames[i][j] );
76                 assertEquals( node.getDisplayName(), items[j].getDisplayName() );
77             }
78         }
79     }
80
81     /**
82      * Test of getShortDescription method, of class org.netbeans.modules.palette.Item.
83      */

84     public void testGetShortDescription() throws IOException JavaDoc {
85         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
86         Model model = pc.getModel();
87         Category[] categories = model.getCategories();
88         
89         for( int i=0; i<itemNames.length; i++ ) {
90             Item[] items = categories[i].getItems();
91             assertEquals( itemNames[i].length, items.length );
92             for( int j=0; j<items.length; j++ ) {
93                 Node node = getItemNode( categoryNames[i], itemNames[i][j] );
94                 assertEquals( node.getShortDescription(), items[j].getShortDescription() );
95             }
96         }
97     }
98
99     /**
100      * Test of getIcon method, of class org.netbeans.modules.palette.Item.
101      */

102     public void testGetIcon() throws IOException JavaDoc {
103         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
104         Model model = pc.getModel();
105         Category[] categories = model.getCategories();
106         
107         for( int i=0; i<itemNames.length; i++ ) {
108             Item[] items = categories[i].getItems();
109             assertEquals( itemNames[i].length, items.length );
110             for( int j=0; j<items.length; j++ ) {
111                 Node node = getItemNode( categoryNames[i], itemNames[i][j] );
112                 assertEquals( node.getIcon( BeanInfo.ICON_COLOR_16x16 ), items[i].getIcon( BeanInfo.ICON_COLOR_16x16 ) );
113                 assertEquals( node.getIcon( BeanInfo.ICON_COLOR_32x32 ), items[i].getIcon( BeanInfo.ICON_COLOR_32x32 ) );
114             }
115         }
116     }
117
118     /**
119      * Test of getActions method, of class org.netbeans.modules.palette.Item.
120      */

121     public void testGetActions() throws IOException JavaDoc {
122         PaletteActions actions = new DummyActions();
123         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
124         Model model = pc.getModel();
125
126         Category[] categories = model.getCategories();
127         
128         for( int i=0; i<categories.length; i++ ) {
129             Item[] items = categories[i].getItems();
130             for( int m=0; m<items.length; m++ ) {
131                 Action JavaDoc[] itemActions = items[m].getActions();
132
133                 Action JavaDoc[] providedActions = actions.getCustomItemActions( items[m].getLookup() );
134
135                 for( int k=0; k<providedActions.length; k++ ) {
136                     if( null == providedActions[k] )
137                         continue;
138                     boolean found = false;
139                     for( int j=0; j<itemActions.length; j++ ) {
140                         if( null == itemActions[j] )
141                             continue;
142                         if( itemActions[j].equals( providedActions[k] ) ) {
143                             found = true;
144                             break;
145                         }
146                     }
147                     assertTrue( "Action " + providedActions[k].getValue( Action.NAME ) + " not found in palette actions.", found );
148                 }
149             }
150         }
151     }
152
153     /**
154      * Test of invokePreferredAction method, of class org.netbeans.modules.palette.Item.
155      */

156     public void testInvokePreferredAction() throws IOException JavaDoc {
157         DummyActions actions = new DummyActions();
158         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
159         Model model = pc.getModel();
160         Category[] categories = model.getCategories();
161         
162         for( int i=0; i<itemNames.length; i++ ) {
163             Item[] items = categories[i].getItems();
164             assertEquals( itemNames[i].length, items.length );
165             for( int j=0; j<items.length; j++ ) {
166                 MyPreferredAction a = new MyPreferredAction();
167                 actions.setPreferredAction( a );
168                 items[j].invokePreferredAction( new ActionEvent JavaDoc( new JPanel JavaDoc(), 0, "junittest") );
169                 assertEquals( 1, a.getActionInvocations() );
170             }
171         }
172     }
173     
174     private static class MyPreferredAction extends AbstractAction JavaDoc {
175         private int actionInvocations = 0;
176         
177         public MyPreferredAction() {
178             super( "JunitAction" );
179         }
180
181         public void actionPerformed(ActionEvent JavaDoc e) {
182             actionInvocations++;
183         }
184         
185         public int getActionInvocations() {
186             return actionInvocations;
187         }
188     }
189
190     /**
191      * Test of getLookup method, of class org.netbeans.modules.palette.Item.
192      */

193     public void testGetLookup() throws IOException JavaDoc {
194         PaletteActions actions = new DummyActions();
195         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
196         Model model = pc.getModel();
197
198         Category[] categories = model.getCategories();
199         
200         for( int i=0; i<categories.length; i++ ) {
201             Item[] items = categories[i].getItems();
202             for( int j=0; j<items.length; j++ ) {
203                 Lookup lkp = items[j].getLookup();
204                 assertNotNull( lkp );
205                 Node node = (Node)lkp.lookup( Node.class );
206                 assertEquals( itemNames[i][j], node.getName() );
207             }
208         }
209     }
210
211     /**
212      * Test of drag method, of class org.netbeans.modules.palette.Item.
213      */

214     public void testDrag() throws Exception JavaDoc {
215         PaletteActions actions = new DummyActions();
216         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
217         Model model = pc.getModel();
218
219         Category[] categories = model.getCategories();
220         
221         for( int i=0; i<categories.length; i++ ) {
222             Item[] items = categories[i].getItems();
223             for( int j=0; j<items.length; j++ ) {
224                 Transferable JavaDoc t = items[j].drag();
225                 assertNotNull( t );
226                 assertTrue( t.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) );
227                 Lookup lookup = (Lookup)t.getTransferData( PaletteController.ITEM_DATA_FLAVOR );
228                 assertNotNull( lookup );
229                 Node node = (Node)lookup.lookup( Node.class );
230                 assertEquals( itemNames[i][j], node.getName() );
231             }
232         }
233     }
234
235     /**
236      * Test of cut method, of class org.netbeans.modules.palette.Item.
237      */

238     public void testCut() throws Exception JavaDoc {
239         PaletteActions actions = new DummyActions();
240         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
241         Model model = pc.getModel();
242
243         Category[] categories = model.getCategories();
244         
245         for( int i=0; i<categories.length; i++ ) {
246             Item[] items = categories[i].getItems();
247             for( int j=0; j<items.length; j++ ) {
248                 Transferable JavaDoc t = items[j].cut();
249                 assertNotNull( t );
250                 assertTrue( t.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) );
251                 Lookup lookup = (Lookup)t.getTransferData( PaletteController.ITEM_DATA_FLAVOR );
252                 assertNotNull( lookup );
253                 Node node = (Node)lookup.lookup( Node.class );
254                 assertEquals( itemNames[i][j], node.getName() );
255             }
256         }
257     }
258 }
259
Popular Tags