001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.palette;
043:
044: import java.awt.event.ActionEvent;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.io.IOException;
048: import javax.swing.AbstractAction;
049: import javax.swing.Action;
050: import junit.framework.TestCase;
051: import org.netbeans.modules.palette.Category;
052: import org.netbeans.modules.palette.Item;
053: import org.netbeans.modules.palette.Model;
054: import org.netbeans.modules.palette.Settings;
055: import org.netbeans.modules.palette.Utils;
056: import org.openide.nodes.Node;
057: import org.openide.util.Lookup;
058:
059: /**
060: *
061: * @author Stanislav Aubrecht
062: */
063: public class PaletteControllerTest extends TestCase {
064:
065: private PaletteController controller;
066: private Node rootNode;
067: private DummyActions actions;
068: private Model model;
069: private Settings settings;
070:
071: public PaletteControllerTest(String testName) {
072: super (testName);
073: }
074:
075: protected void setUp() throws Exception {
076: actions = new DummyActions();
077: rootNode = DummyPalette.createPaletteRoot();
078: controller = PaletteFactory.createPalette(rootNode, actions);
079: model = controller.getModel();
080: settings = controller.getSettings();
081: }
082:
083: /**
084: * Test of addPropertyChangeListener method, of class org.netbeans.modules.palette.api.PaletteController.
085: */
086: public void testAddPropertyChangeListener() {
087: //make sure nothing is selected by default
088: model.clearSelection();
089:
090: MyPropertyChangeListener myListener = new MyPropertyChangeListener();
091: controller.addPropertyChangeListener(myListener);
092:
093: Category cat = model.getCategories()[0];
094: Item item = cat.getItems()[0];
095:
096: model.setSelectedItem(cat.getLookup(), item.getLookup());
097:
098: assertEquals(PaletteController.PROP_SELECTED_ITEM, myListener
099: .getPropertyName());
100: assertEquals(item.getLookup(), myListener.getValue());
101:
102: myListener.clear();
103: model.clearSelection();
104:
105: assertEquals(PaletteController.PROP_SELECTED_ITEM, myListener
106: .getPropertyName());
107: assertEquals(Lookup.EMPTY, myListener.getValue());
108: }
109:
110: /**
111: * Test of removePropertyChangeListener method, of class org.netbeans.modules.palette.api.PaletteController.
112: */
113: public void testRemovePropertyChangeListener() {
114: //make sure nothing is selected by default
115: model.clearSelection();
116:
117: MyPropertyChangeListener myListener = new MyPropertyChangeListener();
118: controller.addPropertyChangeListener(myListener);
119:
120: Category cat = model.getCategories()[0];
121: Item item = cat.getItems()[0];
122:
123: model.setSelectedItem(cat.getLookup(), item.getLookup());
124:
125: assertEquals(PaletteController.PROP_SELECTED_ITEM, myListener
126: .getPropertyName());
127: assertEquals(item.getLookup(), myListener.getValue());
128:
129: controller.removePropertyChangeListener(myListener);
130: myListener.clear();
131: model.clearSelection();
132:
133: assertEquals(null, myListener.getPropertyName());
134: assertEquals(null, myListener.getValue());
135: }
136:
137: /**
138: * Test of getSelectedItem method, of class org.netbeans.modules.palette.api.PaletteController.
139: */
140: public void testGetSelectedItem() {
141: //make sure nothing is selected by default
142: model.clearSelection();
143:
144: assertEquals(Lookup.EMPTY, controller.getSelectedItem());
145:
146: Category cat = model.getCategories()[0];
147: Item item = cat.getItems()[0];
148: model.setSelectedItem(cat.getLookup(), item.getLookup());
149:
150: assertEquals(item.getLookup(), controller.getSelectedItem());
151:
152: cat = model.getCategories()[3];
153: item = cat.getItems()[5];
154: model.setSelectedItem(cat.getLookup(), item.getLookup());
155:
156: assertEquals(item.getLookup(), controller.getSelectedItem());
157:
158: model.clearSelection();
159:
160: assertEquals(Lookup.EMPTY, controller.getSelectedItem());
161: }
162:
163: /**
164: * Test of getSelectedCategory method, of class org.netbeans.modules.palette.api.PaletteController.
165: */
166: public void testGetSelectedCategory() {
167: //make sure nothing is selected by default
168: model.clearSelection();
169:
170: assertEquals(Lookup.EMPTY, controller.getSelectedItem());
171:
172: Category cat = model.getCategories()[0];
173: Item item = cat.getItems()[0];
174: model.setSelectedItem(cat.getLookup(), item.getLookup());
175:
176: assertEquals(cat.getLookup(), controller.getSelectedCategory());
177:
178: cat = model.getCategories()[0];
179: item = cat.getItems()[5];
180: model.setSelectedItem(cat.getLookup(), item.getLookup());
181:
182: assertEquals(cat.getLookup(), controller.getSelectedCategory());
183:
184: cat = model.getCategories()[4];
185: item = cat.getItems()[6];
186: model.setSelectedItem(cat.getLookup(), item.getLookup());
187:
188: assertEquals(cat.getLookup(), controller.getSelectedCategory());
189:
190: model.clearSelection();
191:
192: assertEquals(Lookup.EMPTY, controller.getSelectedCategory());
193: }
194:
195: /**
196: * Test of clearSelection method, of class org.netbeans.modules.palette.api.PaletteController.
197: */
198: public void testClearSelection() {
199: //make sure nothing is selected by default
200: model.clearSelection();
201:
202: assertEquals(Lookup.EMPTY, controller.getSelectedItem());
203:
204: Category cat = model.getCategories()[0];
205: Item item = cat.getItems()[0];
206: model.setSelectedItem(cat.getLookup(), item.getLookup());
207:
208: assertEquals(cat.getLookup(), controller.getSelectedCategory());
209:
210: controller.clearSelection();
211: assertEquals(Lookup.EMPTY, controller.getSelectedCategory());
212:
213: controller.clearSelection();
214: assertEquals(Lookup.EMPTY, controller.getSelectedCategory());
215: }
216:
217: /**
218: * Test of resetPalette method, of class org.netbeans.modules.palette.api.PaletteController.
219: */
220: public void testCustomResetPalette() {
221: MyActions actions = new MyActions();
222: PaletteController myController = PaletteFactory.createPalette(
223: DummyPalette.createPaletteRoot(), actions, null, null);
224:
225: Utils.resetPalette(myController, settings);
226: assertTrue(actions.customResetInvoked);
227: }
228:
229: /**
230: * Test of setPaletteFilter method, of class org.netbeans.modules.palette.api.PaletteController.
231: */
232: public void testRefresh() throws IOException {
233: MyPaletteFilter filter = new MyPaletteFilter(false);
234:
235: PaletteController myController = PaletteFactory.createPalette(
236: DummyPalette.createPaletteRoot(), new DummyActions(),
237: filter, null);
238:
239: Model myModel = myController.getModel();
240:
241: Category[] categories = myModel.getCategories();
242: assertEquals(9, categories.length);
243: for (int i = 0; i < categories.length; i++) {
244: assertEquals(9, categories[i].getItems().length);
245: }
246:
247: filter.setEnabled(true);
248: myController.refresh();
249:
250: categories = myModel.getCategories();
251: for (int i = 0; i < categories.length; i++) {
252: //System.out.println( categories[i].getName() );
253: assertTrue(filter.isValidName(categories[i].getName()));
254:
255: Item[] items = categories[i].getItems();
256: for (int j = 0; j < items.length; j++) {
257: //System.out.println( items[j].getName() );
258: assertTrue(filter.isValidName(items[j].getName()));
259: }
260: }
261:
262: filter.setEnabled(false);
263: myController.refresh();
264:
265: categories = myModel.getCategories();
266: assertEquals(9, categories.length);
267: for (int i = 0; i < categories.length; i++) {
268: assertEquals(9, categories[i].getItems().length);
269: }
270: }
271:
272: /**
273: * Test of showCustomizer method, of class org.netbeans.modules.palette.api.PaletteController.
274: */
275: public void testShowCustomizer() {
276: ProxyModel myModel = new ProxyModel(model);
277: controller.setModel(myModel);
278:
279: controller.showCustomizer();
280:
281: assertTrue(myModel.showCustomizerCalled);
282: }
283:
284: /**
285: * Test of getRoot method, of class org.netbeans.modules.palette.api.PaletteController.
286: */
287: public void testGetRoot() {
288: assertEquals(rootNode.getName(), controller.getRoot().lookup(
289: Node.class).getName());
290: }
291:
292: private static class MyPropertyChangeListener implements
293: PropertyChangeListener {
294: private String propertyName;
295: private Object newValue;
296:
297: public void propertyChange(PropertyChangeEvent evt) {
298: propertyName = evt.getPropertyName();
299: newValue = evt.getNewValue();
300: }
301:
302: public String getPropertyName() {
303: return propertyName;
304: }
305:
306: public Object getValue() {
307: return newValue;
308: }
309:
310: public void clear() {
311: propertyName = null;
312: newValue = null;
313: }
314: }
315:
316: private static class MyPaletteFilter extends PaletteFilter {
317:
318: private boolean isEnabled;
319:
320: public MyPaletteFilter(boolean enabled) {
321: this .isEnabled = enabled;
322: }
323:
324: public boolean isValidItem(Lookup lkp) {
325: if (!isEnabled)
326: return true;
327:
328: Node node = (Node) lkp.lookup(Node.class);
329:
330: return nodeNameEndsWith1or2or3(node);
331: }
332:
333: public boolean isValidCategory(Lookup lkp) {
334: if (!isEnabled)
335: return true;
336:
337: Node node = (Node) lkp.lookup(Node.class);
338:
339: return nodeNameEndsWith1or2or3(node);
340: }
341:
342: private boolean nodeNameEndsWith1or2or3(Node node) {
343: if (null == node)
344: return false;
345:
346: return isValidName(node.getName());
347: }
348:
349: public boolean isValidName(String name) {
350: if (null == name)
351: return false;
352:
353: return name.endsWith("1") || name.endsWith("2")
354: || name.endsWith("3");
355: }
356:
357: public void setEnabled(boolean enable) {
358: this .isEnabled = enable;
359: }
360: }
361:
362: private static class MyActions extends PaletteActions {
363:
364: boolean customResetInvoked = false;
365:
366: public Action[] getImportActions() {
367: return null;
368: }
369:
370: public Action[] getCustomPaletteActions() {
371: return null;
372: }
373:
374: public Action[] getCustomCategoryActions(Lookup category) {
375: return null;
376: }
377:
378: public Action[] getCustomItemActions(Lookup item) {
379: return null;
380: }
381:
382: public Action getPreferredAction(Lookup item) {
383: return null;
384: }
385:
386: @Override
387: public Action getResetAction() {
388: return new AbstractAction() {
389: public void actionPerformed(ActionEvent arg0) {
390: customResetInvoked = true;
391: }
392: };
393: }
394: }
395: }
|