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.datatransfer.Transferable;
045: import java.awt.datatransfer.UnsupportedFlavorException;
046: import java.awt.dnd.DnDConstants;
047: import java.beans.BeanInfo;
048: import java.io.IOException;
049: import javax.swing.Action;
050: import org.netbeans.modules.palette.Category;
051: import org.netbeans.modules.palette.Item;
052: import org.netbeans.modules.palette.Model;
053: import org.openide.nodes.Node;
054: import org.openide.util.Lookup;
055: import org.openide.util.datatransfer.ExTransferable;
056:
057: /**
058: *
059: * @author S. Aubrecht
060: */
061: public class CategoryTest extends AbstractPaletteTestHid {
062:
063: public CategoryTest(String testName) {
064: super (testName);
065: }
066:
067: /**
068: * Test of getName method, of class org.netbeans.modules.palette.Category.
069: */
070: public void testGetName() throws IOException {
071: PaletteController pc = PaletteFactory.createPalette(
072: getRootFolderName(), new DummyActions());
073: Model model = pc.getModel();
074: Category[] categories = model.getCategories();
075:
076: assertEquals(categoryNames.length, categories.length);
077:
078: for (int i = 0; i < categoryNames.length; i++) {
079: Node catNode = getCategoryNode(categoryNames[i]);
080: assertEquals(catNode.getName(), categories[i].getName());
081: }
082: }
083:
084: /**
085: * Test of getDisplayName method, of class org.netbeans.modules.palette.Category.
086: */
087: public void testGetDisplayName() throws IOException {
088: PaletteController pc = PaletteFactory.createPalette(
089: getRootFolderName(), new DummyActions());
090: Model model = pc.getModel();
091: Category[] categories = model.getCategories();
092:
093: assertEquals(categoryNames.length, categories.length);
094:
095: for (int i = 0; i < categoryNames.length; i++) {
096: Node catNode = getCategoryNode(categoryNames[i]);
097: assertEquals(catNode.getDisplayName(), categories[i]
098: .getDisplayName());
099: }
100: }
101:
102: /**
103: * Test of getShortDescription method, of class org.netbeans.modules.palette.Category.
104: */
105: public void testGetShortDescription() throws IOException {
106: PaletteController pc = PaletteFactory.createPalette(
107: getRootFolderName(), new DummyActions());
108: Model model = pc.getModel();
109: Category[] categories = model.getCategories();
110:
111: assertEquals(categoryNames.length, categories.length);
112:
113: for (int i = 0; i < categoryNames.length; i++) {
114: Node catNode = getCategoryNode(categoryNames[i]);
115: assertEquals(catNode.getShortDescription(), categories[i]
116: .getShortDescription());
117: }
118: }
119:
120: /**
121: * Test of getIcon method, of class org.netbeans.modules.palette.Category.
122: */
123: public void testGetIcon() throws IOException {
124: PaletteController pc = PaletteFactory.createPalette(
125: getRootFolderName(), new DummyActions());
126: Model model = pc.getModel();
127: Category[] categories = model.getCategories();
128:
129: assertEquals(categoryNames.length, categories.length);
130:
131: for (int i = 0; i < categoryNames.length; i++) {
132: Node catNode = getCategoryNode(categoryNames[i]);
133: assertEquals(catNode.getIcon(BeanInfo.ICON_COLOR_16x16),
134: categories[i].getIcon(BeanInfo.ICON_COLOR_16x16));
135: assertEquals(catNode.getIcon(BeanInfo.ICON_COLOR_32x32),
136: categories[i].getIcon(BeanInfo.ICON_COLOR_32x32));
137: }
138: }
139:
140: /**
141: * Test of getActions method, of class org.netbeans.modules.palette.Category.
142: */
143: public void testGetActions() throws IOException {
144: PaletteActions actions = new DummyActions();
145: PaletteController pc = PaletteFactory.createPalette(
146: getRootFolderName(), actions);
147: Model model = pc.getModel();
148:
149: Category[] categories = model.getCategories();
150:
151: assertEquals(categoryNames.length, categories.length);
152:
153: for (int i = 0; i < categoryNames.length; i++) {
154: Action[] catActions = categories[i].getActions();
155:
156: Action[] providedActions = actions
157: .getCustomCategoryActions(categories[i].getLookup());
158:
159: for (int k = 0; k < providedActions.length; k++) {
160: if (null == providedActions[k])
161: continue;
162: boolean found = false;
163: for (int j = 0; j < catActions.length; j++) {
164: if (null == catActions[j])
165: continue;
166: if (catActions[j].equals(providedActions[k])) {
167: found = true;
168: break;
169: }
170: }
171: assertTrue("Action "
172: + providedActions[k].getValue(Action.NAME)
173: + " not found in palette actions.", found);
174: }
175: }
176: }
177:
178: /**
179: * Test of getItems method, of class org.netbeans.modules.palette.Category.
180: */
181: public void testGetItems() throws IOException {
182: PaletteActions actions = new DummyActions();
183: PaletteController pc = PaletteFactory.createPalette(
184: getRootFolderName(), actions);
185: Model model = pc.getModel();
186:
187: Category[] categories = model.getCategories();
188:
189: assertEquals(categoryNames.length, categories.length);
190:
191: for (int i = 0; i < categories.length; i++) {
192: Item[] items = categories[i].getItems();
193: assertEquals(itemNames[i].length, items.length);
194: for (int j = 0; j < items.length; j++) {
195: assertEquals(itemNames[i][j], items[j].getName());
196: }
197: }
198: }
199:
200: /**
201: * Test of getTransferable method, of class org.netbeans.modules.palette.Category.
202: */
203: public void testGetTransferable() throws IOException {
204: PaletteActions actions = new DummyActions();
205: PaletteController pc = PaletteFactory.createPalette(
206: getRootFolderName(), actions);
207: Model model = pc.getModel();
208:
209: Category[] categories = model.getCategories();
210:
211: assertEquals(categoryNames.length, categories.length);
212:
213: for (int i = 0; i < categories.length; i++) {
214: Transferable t = categories[i].getTransferable();
215: assertNotNull(t);
216: }
217: }
218:
219: /**
220: * Test of getLookup method, of class org.netbeans.modules.palette.Category.
221: */
222: public void testGetLookup() throws IOException {
223: PaletteActions actions = new DummyActions();
224: PaletteController pc = PaletteFactory.createPalette(
225: getRootFolderName(), actions);
226: Model model = pc.getModel();
227:
228: Category[] categories = model.getCategories();
229:
230: assertEquals(categoryNames.length, categories.length);
231:
232: for (int i = 0; i < categories.length; i++) {
233: Lookup lkp = categories[i].getLookup();
234: assertNotNull(lkp);
235: Node node = (Node) lkp.lookup(Node.class);
236: assertEquals(categoryNames[i], node.getName());
237: }
238: }
239:
240: /**
241: * Test of moveItem method, of class org.netbeans.modules.palette.Category.
242: */
243: public void testMoveItemBefore() throws IOException {
244: PaletteActions actions = new DummyActions();
245: PaletteController pc = PaletteFactory.createPalette(
246: getRootFolderName(), actions);
247: Model model = pc.getModel();
248:
249: Category[] categories = model.getCategories();
250:
251: Category cat = categories[0];
252: Item[] itemsBeforeMove = cat.getItems();
253:
254: Item source = itemsBeforeMove[0];
255: Item target = itemsBeforeMove[itemsBeforeMove.length - 1];
256:
257: cat.dropItem(createTransferable(source),
258: DnDConstants.ACTION_COPY_OR_MOVE, target, true);
259:
260: Item[] itemsAfterMove = cat.getItems();
261:
262: assertEquals(itemsBeforeMove.length, itemsAfterMove.length);
263: assertEquals(source.getName(),
264: itemsAfterMove[itemsAfterMove.length - 1 - 1].getName());
265: assertEquals(itemsBeforeMove[1].getName(), itemsAfterMove[0]
266: .getName());
267: assertEquals(target.getName(),
268: itemsAfterMove[itemsAfterMove.length - 1].getName());
269: }
270:
271: /**
272: * Test of moveItem method, of class org.netbeans.modules.palette.Category.
273: */
274: public void testMoveItemAfter() throws IOException {
275: PaletteActions actions = new DummyActions();
276: PaletteController pc = PaletteFactory.createPalette(
277: getRootFolderName(), actions);
278: Model model = pc.getModel();
279:
280: Category[] categories = model.getCategories();
281:
282: Category cat = categories[0];
283: Item[] itemsBeforeMove = cat.getItems();
284:
285: Item source = itemsBeforeMove[0];
286: Item target = itemsBeforeMove[itemsBeforeMove.length - 1];
287:
288: cat.dropItem(createTransferable(source),
289: DnDConstants.ACTION_COPY_OR_MOVE, target, false);
290:
291: Item[] itemsAfterMove = cat.getItems();
292:
293: assertEquals(itemsBeforeMove.length, itemsAfterMove.length);
294: assertEquals(source.getName(),
295: itemsAfterMove[itemsAfterMove.length - 1].getName());
296: assertEquals(itemsBeforeMove[1].getName(), itemsAfterMove[0]
297: .getName());
298: assertEquals(target.getName(),
299: itemsAfterMove[itemsAfterMove.length - 1 - 1].getName());
300: }
301:
302: public void testDropItemBefore() throws IOException {
303: PaletteActions actions = new DummyActions();
304: PaletteController pc = PaletteFactory.createPalette(
305: getRootFolderName(), actions);
306: Model model = pc.getModel();
307:
308: Category[] categories = model.getCategories();
309:
310: Category srcCat = categories[0];
311: Item[] srcItemsBefore = srcCat.getItems();
312: Item dropItem = srcItemsBefore[0];
313:
314: Category tgtCat = categories[1];
315: Item[] tgtItemsBefore = tgtCat.getItems();
316: Item target = tgtItemsBefore[5];
317:
318: tgtCat.dropItem(dropItem.cut(),
319: DnDConstants.ACTION_COPY_OR_MOVE, target, true);
320:
321: //force all nodes in the palette to update their children
322: pc.refresh();
323: categories = model.getCategories();
324: srcCat = categories[0];
325: tgtCat = categories[1];
326:
327: Item[] srcItemsAfter = srcCat.getItems();
328: Item[] tgtItemsAfter = tgtCat.getItems();
329:
330: assertEquals(srcItemsBefore.length, srcItemsAfter.length + 1);
331: for (int i = 0; i < srcItemsAfter.length; i++) {
332: assertEquals(srcItemsBefore[i + 1].getName(),
333: srcItemsAfter[i].getName());
334: }
335:
336: assertEquals(tgtItemsBefore.length, tgtItemsAfter.length - 1);
337: assertEquals(target.getName(), tgtItemsAfter[5 + 1].getName());
338: assertEquals(dropItem.getName(), tgtItemsAfter[5].getName());
339: }
340:
341: public void testDropItemAfter() throws IOException {
342: PaletteActions actions = new DummyActions();
343: PaletteController pc = PaletteFactory.createPalette(
344: getRootFolderName(), actions);
345: Model model = pc.getModel();
346:
347: Category[] categories = model.getCategories();
348:
349: Category srcCat = categories[0];
350: Item[] srcItemsBefore = srcCat.getItems();
351: Item dropItem = srcItemsBefore[0];
352:
353: Category tgtCat = categories[1];
354: Item[] tgtItemsBefore = tgtCat.getItems();
355: Item target = tgtItemsBefore[5];
356:
357: tgtCat.dropItem(dropItem.cut(),
358: DnDConstants.ACTION_COPY_OR_MOVE, target, false);
359:
360: //force all nodes in the palette to update their children
361: pc.refresh();
362: categories = model.getCategories();
363: srcCat = categories[0];
364: tgtCat = categories[1];
365:
366: Item[] srcItemsAfter = srcCat.getItems();
367: Item[] tgtItemsAfter = tgtCat.getItems();
368:
369: assertEquals(srcItemsBefore.length, srcItemsAfter.length + 1);
370: for (int i = 0; i < srcItemsAfter.length; i++) {
371: assertEquals(srcItemsBefore[i + 1].getName(),
372: srcItemsAfter[i].getName());
373: }
374:
375: assertEquals(tgtItemsBefore.length, tgtItemsAfter.length - 1);
376: assertEquals(target.getName(), tgtItemsAfter[5].getName());
377: assertEquals(dropItem.getName(), tgtItemsAfter[5 + 1].getName());
378: }
379:
380: private Transferable createTransferable(final Item item) {
381: return new ExTransferable.Single(
382: PaletteController.ITEM_DATA_FLAVOR) {
383: protected Object getData() throws IOException,
384: UnsupportedFlavorException {
385: return item.getLookup();
386: }
387: };
388: }
389: }
|