KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import junit.framework.TestCase;
26 import org.netbeans.modules.palette.Category;
27 import org.netbeans.modules.palette.Item;
28 import org.netbeans.modules.palette.Model;
29 import org.netbeans.modules.palette.Settings;
30 import org.openide.nodes.Node;
31 import org.openide.util.Lookup;
32
33 /**
34  *
35  * @author Stanislav Aubrecht
36  */

37 public class PaletteControllerTest extends TestCase {
38
39     private PaletteController controller;
40     private Node rootNode;
41     private DummyActions actions;
42     private Model model;
43     private Settings settings;
44     
45     public PaletteControllerTest(String JavaDoc testName) {
46         super(testName);
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         actions = new DummyActions();
51         rootNode = DummyPalette.createPaletteRoot();
52         controller = PaletteFactory.createPalette( rootNode, actions );
53         model = controller.getModel();
54         settings = controller.getSettings();
55     }
56
57     /**
58      * Test of addPropertyChangeListener method, of class org.netbeans.modules.palette.api.PaletteController.
59      */

60     public void testAddPropertyChangeListener() {
61         //make sure nothing is selected by default
62
model.clearSelection();
63         
64         MyPropertyChangeListener myListener = new MyPropertyChangeListener();
65         controller.addPropertyChangeListener( myListener );
66         
67         Category cat = model.getCategories()[0];
68         Item item = cat.getItems()[0];
69         
70         model.setSelectedItem( cat.getLookup(), item.getLookup() );
71         
72         assertEquals( PaletteController.PROP_SELECTED_ITEM, myListener.getPropertyName() );
73         assertEquals( item.getLookup(), myListener.getValue() );
74         
75         myListener.clear();
76         model.clearSelection();
77
78         assertEquals( PaletteController.PROP_SELECTED_ITEM, myListener.getPropertyName() );
79         assertEquals( Lookup.EMPTY, myListener.getValue() );
80     }
81
82     /**
83      * Test of removePropertyChangeListener method, of class org.netbeans.modules.palette.api.PaletteController.
84      */

85     public void testRemovePropertyChangeListener() {
86         //make sure nothing is selected by default
87
model.clearSelection();
88         
89         MyPropertyChangeListener myListener = new MyPropertyChangeListener();
90         controller.addPropertyChangeListener( myListener );
91         
92         Category cat = model.getCategories()[0];
93         Item item = cat.getItems()[0];
94         
95         model.setSelectedItem( cat.getLookup(), item.getLookup() );
96         
97         assertEquals( PaletteController.PROP_SELECTED_ITEM, myListener.getPropertyName() );
98         assertEquals( item.getLookup(), myListener.getValue() );
99         
100         controller.removePropertyChangeListener( myListener );
101         myListener.clear();
102         model.clearSelection();
103
104         assertEquals( null, myListener.getPropertyName() );
105         assertEquals( null, myListener.getValue() );
106     }
107
108     /**
109      * Test of getSelectedItem method, of class org.netbeans.modules.palette.api.PaletteController.
110      */

111     public void testGetSelectedItem() {
112         //make sure nothing is selected by default
113
model.clearSelection();
114         
115         assertEquals( Lookup.EMPTY, controller.getSelectedItem() );
116         
117         Category cat = model.getCategories()[0];
118         Item item = cat.getItems()[0];
119         model.setSelectedItem( cat.getLookup(), item.getLookup() );
120         
121         assertEquals( item.getLookup(), controller.getSelectedItem() );
122         
123         cat = model.getCategories()[3];
124         item = cat.getItems()[5];
125         model.setSelectedItem( cat.getLookup(), item.getLookup() );
126
127         assertEquals( item.getLookup(), controller.getSelectedItem() );
128
129         model.clearSelection();
130
131         assertEquals( Lookup.EMPTY, controller.getSelectedItem() );
132     }
133
134     /**
135      * Test of getSelectedCategory method, of class org.netbeans.modules.palette.api.PaletteController.
136      */

137     public void testGetSelectedCategory() {
138         //make sure nothing is selected by default
139
model.clearSelection();
140         
141         assertEquals( Lookup.EMPTY, controller.getSelectedItem() );
142         
143         Category cat = model.getCategories()[0];
144         Item item = cat.getItems()[0];
145         model.setSelectedItem( cat.getLookup(), item.getLookup() );
146         
147         assertEquals( cat.getLookup(), controller.getSelectedCategory() );
148         
149         cat = model.getCategories()[0];
150         item = cat.getItems()[5];
151         model.setSelectedItem( cat.getLookup(), item.getLookup() );
152
153         assertEquals( cat.getLookup(), controller.getSelectedCategory() );
154
155         cat = model.getCategories()[4];
156         item = cat.getItems()[6];
157         model.setSelectedItem( cat.getLookup(), item.getLookup() );
158
159         assertEquals( cat.getLookup(), controller.getSelectedCategory() );
160
161         model.clearSelection();
162
163         assertEquals( Lookup.EMPTY, controller.getSelectedCategory() );
164     }
165
166     /**
167      * Test of clearSelection method, of class org.netbeans.modules.palette.api.PaletteController.
168      */

169     public void testClearSelection() {
170         //make sure nothing is selected by default
171
model.clearSelection();
172         
173         assertEquals( Lookup.EMPTY, controller.getSelectedItem() );
174         
175         Category cat = model.getCategories()[0];
176         Item item = cat.getItems()[0];
177         model.setSelectedItem( cat.getLookup(), item.getLookup() );
178         
179         assertEquals( cat.getLookup(), controller.getSelectedCategory() );
180         
181         controller.clearSelection();
182         assertEquals( Lookup.EMPTY, controller.getSelectedCategory() );
183         
184         controller.clearSelection();
185         assertEquals( Lookup.EMPTY, controller.getSelectedCategory() );
186     }
187
188     /**
189      * Test of resetPalette method, of class org.netbeans.modules.palette.api.PaletteController.
190      */

191     public void testResetPalette() {
192 // System.out.println("testResetPalette");
193
//
194
// PaletteController instance = null;
195
//
196
// instance.resetPalette();
197
//
198
// // TODO add your test code below by replacing the default call to fail.
199
// fail("The test case is empty.");
200
}
201
202     /**
203      * Test of setPaletteFilter method, of class org.netbeans.modules.palette.api.PaletteController.
204      */

205     public void testRefresh() throws IOException JavaDoc {
206         MyPaletteFilter filter = new MyPaletteFilter( false );
207         
208         PaletteController myController = PaletteFactory.createPalette( DummyPalette.createPaletteRoot(), new DummyActions(), filter, null );
209         
210         Model myModel = myController.getModel();
211         
212         
213         Category[] categories = myModel.getCategories();
214         assertEquals( 9, categories.length );
215         for( int i=0; i<categories.length; i++ ) {
216             assertEquals( 9, categories[i].getItems().length );
217         }
218         
219         filter.setEnabled( true );
220         myController.refresh();
221         
222         categories = myModel.getCategories();
223         for( int i=0; i<categories.length; i++ ) {
224             //System.out.println( categories[i].getName() );
225
assertTrue( filter.isValidName( categories[i].getName() ) );
226             
227             Item[] items = categories[i].getItems();
228             for( int j=0; j<items.length; j++ ) {
229                 //System.out.println( items[j].getName() );
230
assertTrue( filter.isValidName( items[j].getName() ) );
231             }
232         }
233         
234         filter.setEnabled( false );
235         myController.refresh();
236         
237         categories = myModel.getCategories();
238         assertEquals( 9, categories.length );
239         for( int i=0; i<categories.length; i++ ) {
240             assertEquals( 9, categories[i].getItems().length );
241         }
242     }
243
244     /**
245      * Test of showCustomizer method, of class org.netbeans.modules.palette.api.PaletteController.
246      */

247     public void testShowCustomizer() {
248         ProxyModel myModel = new ProxyModel( model );
249         controller.setModel( myModel );
250         
251         controller.showCustomizer();
252         
253         assertTrue( myModel.showCustomizerCalled );
254     }
255
256     /**
257      * Test of getRoot method, of class org.netbeans.modules.palette.api.PaletteController.
258      */

259     public void testGetRoot() {
260         assertEquals( rootNode.getName(), controller.getRoot().lookup( Node.class ).getName() );
261     }
262
263     private static class MyPropertyChangeListener implements PropertyChangeListener JavaDoc {
264         private String JavaDoc propertyName;
265         private Object JavaDoc newValue;
266         
267         public void propertyChange( PropertyChangeEvent JavaDoc evt ) {
268             propertyName = evt.getPropertyName();
269             newValue = evt.getNewValue();
270         }
271         
272         public String JavaDoc getPropertyName() {
273             return propertyName;
274         }
275         
276         public Object JavaDoc getValue() {
277             return newValue;
278         }
279         
280         public void clear() {
281             propertyName = null;
282             newValue = null;
283         }
284     }
285     
286     private static class MyPaletteFilter extends PaletteFilter {
287         
288         private boolean isEnabled;
289         
290         public MyPaletteFilter( boolean enabled ) {
291             this.isEnabled = enabled;
292         }
293         
294         public boolean isValidItem(Lookup lkp) {
295             if( !isEnabled )
296                 return true;
297             
298             Node node = (Node)lkp.lookup( Node.class );
299             
300             return nodeNameEndsWith1or2or3( node );
301         }
302
303         public boolean isValidCategory(Lookup lkp) {
304             if( !isEnabled )
305                 return true;
306             
307             Node node = (Node)lkp.lookup( Node.class );
308             
309             return nodeNameEndsWith1or2or3( node );
310         }
311         
312         private boolean nodeNameEndsWith1or2or3( Node node ) {
313             if( null == node )
314                 return false;
315             
316             return isValidName( node.getName() );
317         }
318         
319         public boolean isValidName( String JavaDoc name ) {
320             if( null == name )
321                 return false;
322             
323             return name.endsWith( "1" ) || name.endsWith( "2" ) || name.endsWith( "3" );
324         }
325         
326         public void setEnabled( boolean enable ) {
327             this.isEnabled = enable;
328         }
329     }
330 }
331
Popular Tags