KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

62     public void testGetDisplayName() throws IOException JavaDoc {
63         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
64         Model model = pc.getModel();
65         Category[] categories = model.getCategories();
66         
67         assertEquals( categoryNames.length, categories.length );
68         
69         for( int i=0; i<categoryNames.length; i++ ) {
70             Node catNode = getCategoryNode( categoryNames[i] );
71             assertEquals( catNode.getDisplayName(), categories[i].getDisplayName() );
72         }
73     }
74
75     /**
76      * Test of getShortDescription method, of class org.netbeans.modules.palette.Category.
77      */

78     public void testGetShortDescription() throws IOException JavaDoc {
79         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
80         Model model = pc.getModel();
81         Category[] categories = model.getCategories();
82         
83         assertEquals( categoryNames.length, categories.length );
84         
85         for( int i=0; i<categoryNames.length; i++ ) {
86             Node catNode = getCategoryNode( categoryNames[i] );
87             assertEquals( catNode.getShortDescription(), categories[i].getShortDescription() );
88         }
89     }
90
91     /**
92      * Test of getIcon method, of class org.netbeans.modules.palette.Category.
93      */

94     public void testGetIcon() throws IOException JavaDoc {
95         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
96         Model model = pc.getModel();
97         Category[] categories = model.getCategories();
98         
99         assertEquals( categoryNames.length, categories.length );
100         
101         for( int i=0; i<categoryNames.length; i++ ) {
102             Node catNode = getCategoryNode( categoryNames[i] );
103             assertEquals( catNode.getIcon( BeanInfo.ICON_COLOR_16x16 ), categories[i].getIcon( BeanInfo.ICON_COLOR_16x16 ) );
104             assertEquals( catNode.getIcon( BeanInfo.ICON_COLOR_32x32 ), categories[i].getIcon( BeanInfo.ICON_COLOR_32x32 ) );
105         }
106     }
107
108     /**
109      * Test of getActions method, of class org.netbeans.modules.palette.Category.
110      */

111     public void testGetActions() throws IOException JavaDoc {
112         PaletteActions actions = new DummyActions();
113         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
114         Model model = pc.getModel();
115
116         Category[] categories = model.getCategories();
117         
118         assertEquals( categoryNames.length, categories.length );
119         
120         for( int i=0; i<categoryNames.length; i++ ) {
121             Action JavaDoc[] catActions = categories[i].getActions();
122             
123             Action JavaDoc[] providedActions = actions.getCustomCategoryActions( categories[i].getLookup() );
124             
125             for( int k=0; k<providedActions.length; k++ ) {
126                 if( null == providedActions[k] )
127                     continue;
128                 boolean found = false;
129                 for( int j=0; j<catActions.length; j++ ) {
130                     if( null == catActions[j] )
131                         continue;
132                     if( catActions[j].equals( providedActions[k] ) ) {
133                         found = true;
134                         break;
135                     }
136                 }
137                 assertTrue( "Action " + providedActions[k].getValue( Action.NAME ) + " not found in palette actions.", found );
138             }
139         }
140     }
141
142     /**
143      * Test of getItems method, of class org.netbeans.modules.palette.Category.
144      */

145     public void testGetItems() throws IOException JavaDoc {
146         PaletteActions actions = new DummyActions();
147         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
148         Model model = pc.getModel();
149
150         Category[] categories = model.getCategories();
151         
152         assertEquals( categoryNames.length, categories.length );
153         
154         for( int i=0; i<categories.length; i++ ) {
155             Item[] items = categories[i].getItems();
156             assertEquals( itemNames[i].length, items.length );
157             for( int j=0; j<items.length; j++ ) {
158                 assertEquals( itemNames[i][j], items[j].getName() );
159             }
160         }
161     }
162
163     /**
164      * Test of getTransferable method, of class org.netbeans.modules.palette.Category.
165      */

166     public void testGetTransferable() throws IOException JavaDoc {
167         PaletteActions actions = new DummyActions();
168         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
169         Model model = pc.getModel();
170
171         Category[] categories = model.getCategories();
172         
173         assertEquals( categoryNames.length, categories.length );
174         
175         for( int i=0; i<categories.length; i++ ) {
176             Transferable JavaDoc t = categories[i].getTransferable();
177             assertNotNull( t );
178         }
179     }
180
181     /**
182      * Test of getLookup method, of class org.netbeans.modules.palette.Category.
183      */

184     public void testGetLookup() throws IOException JavaDoc {
185         PaletteActions actions = new DummyActions();
186         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
187         Model model = pc.getModel();
188
189         Category[] categories = model.getCategories();
190         
191         assertEquals( categoryNames.length, categories.length );
192         
193         for( int i=0; i<categories.length; i++ ) {
194             Lookup lkp = categories[i].getLookup();
195             assertNotNull( lkp );
196             Node node = (Node)lkp.lookup( Node.class );
197             assertEquals( categoryNames[i], node.getName() );
198         }
199     }
200
201     /**
202      * Test of moveItem method, of class org.netbeans.modules.palette.Category.
203      */

204     public void testMoveItemBefore() throws IOException JavaDoc {
205         PaletteActions actions = new DummyActions();
206         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
207         Model model = pc.getModel();
208
209         Category[] categories = model.getCategories();
210         
211         Category cat = categories[0];
212         Item[] itemsBeforeMove = cat.getItems();
213         
214         Item source = itemsBeforeMove[0];
215         Item target = itemsBeforeMove[itemsBeforeMove.length-1];
216         
217         cat.dropItem( createTransferable( source ), DnDConstants.ACTION_COPY_OR_MOVE, target, true );
218         
219         Item[] itemsAfterMove = cat.getItems();
220         
221         assertEquals( itemsBeforeMove.length, itemsAfterMove.length );
222         assertEquals( source.getName(), itemsAfterMove[itemsAfterMove.length-1-1].getName() );
223         assertEquals( itemsBeforeMove[1].getName(), itemsAfterMove[0].getName() );
224         assertEquals( target.getName(), itemsAfterMove[itemsAfterMove.length-1].getName() );
225     }
226
227     /**
228      * Test of moveItem method, of class org.netbeans.modules.palette.Category.
229      */

230     public void testMoveItemAfter() throws IOException JavaDoc {
231         PaletteActions actions = new DummyActions();
232         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
233         Model model = pc.getModel();
234
235         Category[] categories = model.getCategories();
236         
237         Category cat = categories[0];
238         Item[] itemsBeforeMove = cat.getItems();
239         
240         Item source = itemsBeforeMove[0];
241         Item target = itemsBeforeMove[itemsBeforeMove.length-1];
242         
243         cat.dropItem( createTransferable( source ), DnDConstants.ACTION_COPY_OR_MOVE, target, false );
244         
245         Item[] itemsAfterMove = cat.getItems();
246         
247         assertEquals( itemsBeforeMove.length, itemsAfterMove.length );
248         assertEquals( source.getName(), itemsAfterMove[itemsAfterMove.length-1].getName() );
249         assertEquals( itemsBeforeMove[1].getName(), itemsAfterMove[0].getName() );
250         assertEquals( target.getName(), itemsAfterMove[itemsAfterMove.length-1-1].getName() );
251     }
252
253     public void testDropItemBefore() throws IOException JavaDoc {
254         PaletteActions actions = new DummyActions();
255         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
256         Model model = pc.getModel();
257
258         Category[] categories = model.getCategories();
259         
260         Category srcCat = categories[0];
261         Item[] srcItemsBefore = srcCat.getItems();
262         Item dropItem = srcItemsBefore[0];
263
264         Category tgtCat = categories[1];
265         Item[] tgtItemsBefore = tgtCat.getItems();
266         Item target = tgtItemsBefore[5];
267         
268         tgtCat.dropItem( dropItem.cut(), DnDConstants.ACTION_COPY_OR_MOVE, target, true );
269         
270         //force all nodes in the palette to update their children
271
pc.refresh();
272         categories = model.getCategories();
273         srcCat = categories[0];
274         tgtCat = categories[1];
275         
276         Item[] srcItemsAfter = srcCat.getItems();
277         Item[] tgtItemsAfter = tgtCat.getItems();
278         
279         assertEquals( srcItemsBefore.length, srcItemsAfter.length+1 );
280         for( int i=0; i<srcItemsAfter.length; i++ ) {
281             assertEquals( srcItemsBefore[i+1].getName(), srcItemsAfter[i].getName() );
282         }
283         
284         assertEquals( tgtItemsBefore.length, tgtItemsAfter.length-1 );
285         assertEquals( target.getName(), tgtItemsAfter[5+1].getName() );
286         assertEquals( dropItem.getName(), tgtItemsAfter[5].getName() );
287     }
288
289     public void testDropItemAfter() throws IOException JavaDoc {
290         PaletteActions actions = new DummyActions();
291         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
292         Model model = pc.getModel();
293
294         Category[] categories = model.getCategories();
295         
296         Category srcCat = categories[0];
297         Item[] srcItemsBefore = srcCat.getItems();
298         Item dropItem = srcItemsBefore[0];
299
300         Category tgtCat = categories[1];
301         Item[] tgtItemsBefore = tgtCat.getItems();
302         Item target = tgtItemsBefore[5];
303         
304         tgtCat.dropItem( dropItem.cut(), DnDConstants.ACTION_COPY_OR_MOVE, target, false );
305         
306         //force all nodes in the palette to update their children
307
pc.refresh();
308         categories = model.getCategories();
309         srcCat = categories[0];
310         tgtCat = categories[1];
311         
312         Item[] srcItemsAfter = srcCat.getItems();
313         Item[] tgtItemsAfter = tgtCat.getItems();
314         
315         assertEquals( srcItemsBefore.length, srcItemsAfter.length+1 );
316         for( int i=0; i<srcItemsAfter.length; i++ ) {
317             assertEquals( srcItemsBefore[i+1].getName(), srcItemsAfter[i].getName() );
318         }
319         
320         assertEquals( tgtItemsBefore.length, tgtItemsAfter.length-1 );
321         assertEquals( target.getName(), tgtItemsAfter[5].getName() );
322         assertEquals( dropItem.getName(), tgtItemsAfter[5+1].getName() );
323     }
324     
325     private Transferable JavaDoc createTransferable( final Item item ) {
326         return new ExTransferable.Single( PaletteController.ITEM_DATA_FLAVOR ) {
327             protected Object JavaDoc getData() throws IOException JavaDoc, UnsupportedFlavorException JavaDoc {
328                 return item.getLookup();
329             }
330         };
331     }
332 }
333
Popular Tags