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.io.IOException;
046: import javax.swing.AbstractAction;
047: import javax.swing.Action;
048: import org.netbeans.modules.palette.Category;
049: import org.netbeans.modules.palette.Item;
050: import org.netbeans.modules.palette.Model;
051: import org.openide.filesystems.FileObject;
052: import org.openide.loaders.DataFolder;
053: import org.openide.util.Lookup;
054:
055: /**
056: *
057: * @author Stanislav Aubrecht
058: */
059: public class ModelTest extends AbstractPaletteTestHid {
060:
061: public ModelTest(String testName) {
062: super (testName);
063: }
064:
065: /**
066: * Test of getName method, of class org.netbeans.modules.palette.Model.
067: */
068: public void testGetName() throws IOException {
069: PaletteController pc = PaletteFactory.createPalette(
070: getRootFolderName(), new DummyActions());
071: Model model = pc.getModel();
072: assertEquals(getRootFolderName(), model.getName());
073: }
074:
075: /**
076: * Test of getCategories method, of class org.netbeans.modules.palette.Model.
077: */
078: public void testGetCategories() throws IOException {
079: PaletteController pc = PaletteFactory.createPalette(
080: getRootFolderName(), new DummyActions());
081: Model model = pc.getModel();
082: Category[] categories = model.getCategories();
083: assertEquals(categoryNames.length, categories.length);
084: for (int i = 0; i < categories.length; i++) {
085: assertEquals(categoryNames[i], categories[i].getName());
086: }
087: }
088:
089: /**
090: * Test of getActions method, of class org.netbeans.modules.palette.Model.
091: */
092: public void testGetActions() throws IOException {
093: PaletteActions actions = new DummyActions();
094: PaletteController pc = PaletteFactory.createPalette(
095: getRootFolderName(), actions);
096: Model model = pc.getModel();
097: Action[] modelActions = model.getActions();
098: Action[] rootActions = actions.getCustomPaletteActions();
099: for (int i = 0; i < rootActions.length; i++) {
100: if (null == rootActions[i])
101: continue;
102: boolean found = false;
103: for (int j = 0; j < modelActions.length; j++) {
104: if (null == modelActions[j])
105: continue;
106: if (modelActions[j].equals(rootActions[i])) {
107: found = true;
108: break;
109: }
110: }
111: assertTrue("Action " + rootActions[i].getValue(Action.NAME)
112: + " not found in palette actions.", found);
113: }
114: }
115:
116: /**
117: * Test of getSelectedItem method, of class org.netbeans.modules.palette.Model.
118: */
119: public void testSelectedItemAndCategory() throws IOException {
120: PaletteController pc = PaletteFactory.createPalette(
121: getRootFolderName(), new DummyActions());
122: Model model = pc.getModel();
123:
124: assertNull("No item is selected by default", model
125: .getSelectedItem());
126: assertNull("No category is selected by default", model
127: .getSelectedCategory());
128:
129: Category[] categories = model.getCategories();
130: Category catToSelect = categories[3];
131: Item itemToSelect = catToSelect.getItems()[4];
132:
133: model.setSelectedItem(catToSelect.getLookup(), itemToSelect
134: .getLookup());
135:
136: assertEquals(catToSelect, model.getSelectedCategory());
137: assertEquals(itemToSelect, model.getSelectedItem());
138:
139: model.clearSelection();
140:
141: assertNull("No item is selected after clearSelection()", model
142: .getSelectedItem());
143: assertNull("No category is selected after clearSelection()",
144: model.getSelectedCategory());
145: }
146:
147: /**
148: * Test of getRoot method, of class org.netbeans.modules.palette.Model.
149: */
150: public void testGetRoot() throws IOException {
151: PaletteController pc = PaletteFactory.createPalette(
152: getRootFolderName(), new DummyActions());
153: Model model = pc.getModel();
154: Lookup rootLookup = model.getRoot();
155:
156: DataFolder df = (DataFolder) rootLookup
157: .lookup(DataFolder.class);
158: assertNotNull(df);
159:
160: FileObject fo = df.getPrimaryFile();
161: assertNotNull(fo);
162:
163: assertEquals(getRootFolderName(), fo.getName());
164: }
165:
166: /**
167: * Test of moveCategory method, of class org.netbeans.modules.palette.Model.
168: */
169: public void testMoveCategoryBefore() throws IOException {
170: PaletteController pc = PaletteFactory.createPalette(
171: getRootFolderName(), new DummyActions());
172: Model model = pc.getModel();
173:
174: Category[] categories = model.getCategories();
175: Category source = categories[0];
176: Category target = categories[categories.length - 1];
177:
178: model.moveCategory(source, target, true);
179:
180: pc.refresh();
181:
182: Category[] movedCategories = model.getCategories();
183: assertEquals(categories.length, movedCategories.length);
184: assertEquals(target.getName(),
185: movedCategories[movedCategories.length - 1].getName());
186: assertEquals(source.getName(),
187: movedCategories[movedCategories.length - 1 - 1]
188: .getName());
189: }
190:
191: /**
192: * Test of moveCategory method, of class org.netbeans.modules.palette.Model.
193: */
194: public void testMoveCategoryAfter() throws IOException {
195: PaletteController pc = PaletteFactory.createPalette(
196: getRootFolderName(), new DummyActions());
197: Model model = pc.getModel();
198:
199: Category[] categories = model.getCategories();
200: Category source = categories[0];
201: Category target = categories[categories.length - 1];
202:
203: model.moveCategory(source, target, false);
204:
205: pc.refresh();
206:
207: Category[] movedCategories = model.getCategories();
208: assertEquals(categories.length, movedCategories.length);
209: assertEquals(target.getName(),
210: movedCategories[movedCategories.length - 1 - 1]
211: .getName());
212: assertEquals(source.getName(),
213: movedCategories[movedCategories.length - 1].getName());
214: }
215:
216: /**
217: * Test of moveCategory method, of class org.netbeans.modules.palette.Model.
218: */
219: public void testMoveCategorySamePosition() throws IOException {
220: PaletteController pc = PaletteFactory.createPalette(
221: getRootFolderName(), new DummyActions());
222: Model model = pc.getModel();
223:
224: Category[] categories = model.getCategories();
225: Category source = categories[0];
226: Category target = categories[0];
227:
228: model.moveCategory(source, target, false);
229:
230: Category[] movedCategories = model.getCategories();
231: assertEquals(categories.length, movedCategories.length);
232: assertEquals(target, movedCategories[0]);
233: assertEquals(source, movedCategories[0]);
234: }
235:
236: /**
237: * Test of getName method, of class org.netbeans.modules.palette.Model.
238: */
239: public void testCustomRefresh() throws IOException {
240: CustomRefresh refresh = new CustomRefresh();
241: PaletteController pc = PaletteFactory.createPalette(
242: getRootFolderName(), new CustomRefreshActions(refresh));
243: pc.refresh();
244: assertTrue(refresh.actionInvoked);
245: }
246:
247: private static class CustomRefreshActions extends DummyActions {
248: private Action refresh;
249:
250: public CustomRefreshActions(Action refresh) {
251: this .refresh = refresh;
252: }
253:
254: @Override
255: public Action getRefreshAction() {
256: return refresh;
257: }
258: }
259:
260: private static class CustomRefresh extends AbstractAction {
261: private boolean actionInvoked = false;
262:
263: public CustomRefresh() {
264: }
265:
266: public void actionPerformed(ActionEvent arg0) {
267: actionInvoked = true;
268: }
269: }
270: }
|