KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.swing.AbstractAction JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import org.netbeans.modules.palette.Category;
27 import org.netbeans.modules.palette.Item;
28 import org.netbeans.modules.palette.Model;
29 import org.openide.filesystems.FileObject;
30 import org.openide.loaders.DataFolder;
31 import org.openide.util.Lookup;
32
33 /**
34  *
35  * @author Stanislav Aubrecht
36  */

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

46     public void testGetName() throws IOException JavaDoc {
47         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
48         Model model = pc.getModel();
49         assertEquals( getRootFolderName(), model.getName() );
50     }
51
52     /**
53      * Test of getCategories method, of class org.netbeans.modules.palette.Model.
54      */

55     public void testGetCategories() throws IOException JavaDoc {
56         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
57         Model model = pc.getModel();
58         Category[] categories = model.getCategories();
59         assertEquals( categoryNames.length, categories.length );
60         for( int i=0; i<categories.length; i++ ) {
61             assertEquals( categoryNames[i], categories[i].getName() );
62         }
63     }
64
65     /**
66      * Test of getActions method, of class org.netbeans.modules.palette.Model.
67      */

68     public void testGetActions() throws IOException JavaDoc {
69         PaletteActions actions = new DummyActions();
70         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions );
71         Model model = pc.getModel();
72         Action JavaDoc[] modelActions = model.getActions();
73         Action JavaDoc[] rootActions = actions.getCustomPaletteActions();
74         for( int i=0; i<rootActions.length; i++ ) {
75             if( null == rootActions[i] )
76                 continue;
77             boolean found = false;
78             for( int j=0; j<modelActions.length; j++ ) {
79                 if( null == modelActions[j] )
80                     continue;
81                 if( modelActions[j].equals( rootActions[i] ) ) {
82                     found = true;
83                     break;
84                 }
85             }
86             assertTrue( "Action " + rootActions[i].getValue( Action.NAME ) + " not found in palette actions.", found );
87         }
88     }
89
90     /**
91      * Test of getSelectedItem method, of class org.netbeans.modules.palette.Model.
92      */

93     public void testSelectedItemAndCategory() throws IOException JavaDoc {
94         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
95         Model model = pc.getModel();
96
97         assertNull( "No item is selected by default", model.getSelectedItem() );
98         assertNull( "No category is selected by default", model.getSelectedCategory() );
99         
100         Category[] categories = model.getCategories();
101         Category catToSelect = categories[3];
102         Item itemToSelect = catToSelect.getItems()[4];
103         
104         model.setSelectedItem( catToSelect.getLookup(), itemToSelect.getLookup() );
105         
106         assertEquals( catToSelect, model.getSelectedCategory() );
107         assertEquals( itemToSelect, model.getSelectedItem() );
108         
109         model.clearSelection();
110         
111         assertNull( "No item is selected after clearSelection()", model.getSelectedItem() );
112         assertNull( "No category is selected after clearSelection()", model.getSelectedCategory() );
113     }
114
115     /**
116      * Test of getRoot method, of class org.netbeans.modules.palette.Model.
117      */

118     public void testGetRoot() throws IOException JavaDoc {
119         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
120         Model model = pc.getModel();
121         Lookup rootLookup = model.getRoot();
122         
123         DataFolder df = (DataFolder)rootLookup.lookup( DataFolder.class );
124         assertNotNull( df );
125         
126         FileObject fo = df.getPrimaryFile();
127         assertNotNull( fo );
128         
129         assertEquals( getRootFolderName(), fo.getName() );
130     }
131
132     /**
133      * Test of moveCategory method, of class org.netbeans.modules.palette.Model.
134      */

135     public void testMoveCategoryBefore() throws IOException JavaDoc {
136         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
137         Model model = pc.getModel();
138
139         Category[] categories = model.getCategories();
140         Category source = categories[0];
141         Category target = categories[categories.length-1];
142         
143         model.moveCategory( source, target, true );
144         
145         pc.refresh();
146         
147         Category[] movedCategories = model.getCategories();
148         assertEquals( categories.length, movedCategories.length );
149         assertEquals( target.getName(), movedCategories[movedCategories.length-1].getName() );
150         assertEquals( source.getName(), movedCategories[movedCategories.length-1-1].getName() );
151     }
152
153     /**
154      * Test of moveCategory method, of class org.netbeans.modules.palette.Model.
155      */

156     public void testMoveCategoryAfter() throws IOException JavaDoc {
157         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
158         Model model = pc.getModel();
159
160         Category[] categories = model.getCategories();
161         Category source = categories[0];
162         Category target = categories[categories.length-1];
163         
164         model.moveCategory( source, target, false );
165         
166         pc.refresh();
167         
168         Category[] movedCategories = model.getCategories();
169         assertEquals( categories.length, movedCategories.length );
170         assertEquals( target.getName(), movedCategories[movedCategories.length-1-1].getName() );
171         assertEquals( source.getName(), movedCategories[movedCategories.length-1].getName() );
172     }
173
174     /**
175      * Test of moveCategory method, of class org.netbeans.modules.palette.Model.
176      */

177     public void testMoveCategorySamePosition() throws IOException JavaDoc {
178         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() );
179         Model model = pc.getModel();
180
181         Category[] categories = model.getCategories();
182         Category source = categories[0];
183         Category target = categories[0];
184         
185         model.moveCategory( source, target, false );
186         
187         Category[] movedCategories = model.getCategories();
188         assertEquals( categories.length, movedCategories.length );
189         assertEquals( target, movedCategories[0] );
190         assertEquals( source, movedCategories[0] );
191     }
192
193     /**
194      * Test of getName method, of class org.netbeans.modules.palette.Model.
195      */

196     public void testCustomRefresh() throws IOException JavaDoc {
197         CustomRefresh refresh = new CustomRefresh();
198         PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new CustomRefreshActions( refresh ) );
199         pc.refresh();
200         assertTrue( refresh.actionInvoked );
201 }
202     
203     private static class CustomRefreshActions extends DummyActions {
204         private Action JavaDoc refresh;
205         public CustomRefreshActions( Action JavaDoc refresh ) {
206             this.refresh = refresh;
207         }
208     
209         @Override JavaDoc
210         public Action JavaDoc getRefreshAction() {
211             return refresh;
212         }
213     }
214
215     private static class CustomRefresh extends AbstractAction JavaDoc {
216         private boolean actionInvoked = false;
217         
218         public CustomRefresh() {
219         }
220     
221         public void actionPerformed(ActionEvent JavaDoc arg0) {
222             actionInvoked = true;
223         }
224     }
225 }
226
Popular Tags