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.event.ActionEvent;
046: import java.beans.BeanInfo;
047: import java.io.IOException;
048: import javax.swing.AbstractAction;
049: import javax.swing.Action;
050: import javax.swing.JPanel;
051: import org.netbeans.modules.palette.Category;
052: import org.netbeans.modules.palette.Item;
053: import org.netbeans.modules.palette.Model;
054: import org.openide.nodes.Node;
055: import org.openide.util.Lookup;
056:
057: /**
058: *
059: * @author S. Aubrecht
060: */
061: public class ItemTest extends AbstractPaletteTestHid {
062:
063: public ItemTest(String testName) {
064: super (testName);
065: }
066:
067: /**
068: * Test of getName method, of class org.netbeans.modules.palette.Item.
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: for (int i = 0; i < itemNames.length; i++) {
077: Item[] items = categories[i].getItems();
078: assertEquals(itemNames[i].length, items.length);
079: for (int j = 0; j < items.length; j++) {
080: Node node = getItemNode(categoryNames[i],
081: itemNames[i][j]);
082: assertEquals(node.getName(), items[j].getName());
083: }
084: }
085: }
086:
087: /**
088: * Test of getDisplayName method, of class org.netbeans.modules.palette.Item.
089: */
090: public void testGetDisplayName() throws IOException {
091: PaletteController pc = PaletteFactory.createPalette(
092: getRootFolderName(), new DummyActions());
093: Model model = pc.getModel();
094: Category[] categories = model.getCategories();
095:
096: for (int i = 0; i < itemNames.length; i++) {
097: Item[] items = categories[i].getItems();
098: assertEquals(itemNames[i].length, items.length);
099: for (int j = 0; j < items.length; j++) {
100: Node node = getItemNode(categoryNames[i],
101: itemNames[i][j]);
102: assertEquals(node.getDisplayName(), items[j]
103: .getDisplayName());
104: }
105: }
106: }
107:
108: /**
109: * Test of getShortDescription method, of class org.netbeans.modules.palette.Item.
110: */
111: public void testGetShortDescription() throws IOException {
112: PaletteController pc = PaletteFactory.createPalette(
113: getRootFolderName(), new DummyActions());
114: Model model = pc.getModel();
115: Category[] categories = model.getCategories();
116:
117: for (int i = 0; i < itemNames.length; i++) {
118: Item[] items = categories[i].getItems();
119: assertEquals(itemNames[i].length, items.length);
120: for (int j = 0; j < items.length; j++) {
121: Node node = getItemNode(categoryNames[i],
122: itemNames[i][j]);
123: assertEquals(node.getShortDescription(), items[j]
124: .getShortDescription());
125: }
126: }
127: }
128:
129: /**
130: * Test of getIcon method, of class org.netbeans.modules.palette.Item.
131: */
132: public void testGetIcon() throws IOException {
133: PaletteController pc = PaletteFactory.createPalette(
134: getRootFolderName(), new DummyActions());
135: Model model = pc.getModel();
136: Category[] categories = model.getCategories();
137:
138: for (int i = 0; i < itemNames.length; i++) {
139: Item[] items = categories[i].getItems();
140: assertEquals(itemNames[i].length, items.length);
141: for (int j = 0; j < items.length; j++) {
142: Node node = getItemNode(categoryNames[i],
143: itemNames[i][j]);
144: assertEquals(node.getIcon(BeanInfo.ICON_COLOR_16x16),
145: items[i].getIcon(BeanInfo.ICON_COLOR_16x16));
146: assertEquals(node.getIcon(BeanInfo.ICON_COLOR_32x32),
147: items[i].getIcon(BeanInfo.ICON_COLOR_32x32));
148: }
149: }
150: }
151:
152: /**
153: * Test of getActions method, of class org.netbeans.modules.palette.Item.
154: */
155: public void testGetActions() throws IOException {
156: PaletteActions actions = new DummyActions();
157: PaletteController pc = PaletteFactory.createPalette(
158: getRootFolderName(), actions);
159: Model model = pc.getModel();
160:
161: Category[] categories = model.getCategories();
162:
163: for (int i = 0; i < categories.length; i++) {
164: Item[] items = categories[i].getItems();
165: for (int m = 0; m < items.length; m++) {
166: Action[] itemActions = items[m].getActions();
167:
168: Action[] providedActions = actions
169: .getCustomItemActions(items[m].getLookup());
170:
171: for (int k = 0; k < providedActions.length; k++) {
172: if (null == providedActions[k])
173: continue;
174: boolean found = false;
175: for (int j = 0; j < itemActions.length; j++) {
176: if (null == itemActions[j])
177: continue;
178: if (itemActions[j].equals(providedActions[k])) {
179: found = true;
180: break;
181: }
182: }
183: assertTrue("Action "
184: + providedActions[k].getValue(Action.NAME)
185: + " not found in palette actions.", found);
186: }
187: }
188: }
189: }
190:
191: /**
192: * Test of invokePreferredAction method, of class org.netbeans.modules.palette.Item.
193: */
194: public void testInvokePreferredAction() throws IOException {
195: DummyActions actions = new DummyActions();
196: PaletteController pc = PaletteFactory.createPalette(
197: getRootFolderName(), actions);
198: Model model = pc.getModel();
199: Category[] categories = model.getCategories();
200:
201: for (int i = 0; i < itemNames.length; i++) {
202: Item[] items = categories[i].getItems();
203: assertEquals(itemNames[i].length, items.length);
204: for (int j = 0; j < items.length; j++) {
205: MyPreferredAction a = new MyPreferredAction();
206: actions.setPreferredAction(a);
207: items[j].invokePreferredAction(new ActionEvent(
208: new JPanel(), 0, "junittest"));
209: assertEquals(1, a.getActionInvocations());
210: }
211: }
212: }
213:
214: private static class MyPreferredAction extends AbstractAction {
215: private int actionInvocations = 0;
216:
217: public MyPreferredAction() {
218: super ("JunitAction");
219: }
220:
221: public void actionPerformed(ActionEvent e) {
222: actionInvocations++;
223: }
224:
225: public int getActionInvocations() {
226: return actionInvocations;
227: }
228: }
229:
230: /**
231: * Test of getLookup method, of class org.netbeans.modules.palette.Item.
232: */
233: public void testGetLookup() throws IOException {
234: PaletteActions actions = new DummyActions();
235: PaletteController pc = PaletteFactory.createPalette(
236: getRootFolderName(), actions);
237: Model model = pc.getModel();
238:
239: Category[] categories = model.getCategories();
240:
241: for (int i = 0; i < categories.length; i++) {
242: Item[] items = categories[i].getItems();
243: for (int j = 0; j < items.length; j++) {
244: Lookup lkp = items[j].getLookup();
245: assertNotNull(lkp);
246: Node node = (Node) lkp.lookup(Node.class);
247: assertEquals(itemNames[i][j], node.getName());
248: }
249: }
250: }
251:
252: /**
253: * Test of drag method, of class org.netbeans.modules.palette.Item.
254: */
255: public void testDrag() throws Exception {
256: PaletteActions actions = new DummyActions();
257: PaletteController pc = PaletteFactory.createPalette(
258: getRootFolderName(), actions);
259: Model model = pc.getModel();
260:
261: Category[] categories = model.getCategories();
262:
263: for (int i = 0; i < categories.length; i++) {
264: Item[] items = categories[i].getItems();
265: for (int j = 0; j < items.length; j++) {
266: Transferable t = items[j].drag();
267: assertNotNull(t);
268: assertTrue(t
269: .isDataFlavorSupported(PaletteController.ITEM_DATA_FLAVOR));
270: Lookup lookup = (Lookup) t
271: .getTransferData(PaletteController.ITEM_DATA_FLAVOR);
272: assertNotNull(lookup);
273: Node node = (Node) lookup.lookup(Node.class);
274: assertEquals(itemNames[i][j], node.getName());
275: }
276: }
277: }
278:
279: /**
280: * Test of cut method, of class org.netbeans.modules.palette.Item.
281: */
282: public void testCut() throws Exception {
283: PaletteActions actions = new DummyActions();
284: PaletteController pc = PaletteFactory.createPalette(
285: getRootFolderName(), actions);
286: Model model = pc.getModel();
287:
288: Category[] categories = model.getCategories();
289:
290: for (int i = 0; i < categories.length; i++) {
291: Item[] items = categories[i].getItems();
292: for (int j = 0; j < items.length; j++) {
293: Transferable t = items[j].cut();
294: assertNotNull(t);
295: assertTrue(t
296: .isDataFlavorSupported(PaletteController.ITEM_DATA_FLAVOR));
297: Lookup lookup = (Lookup) t
298: .getTransferData(PaletteController.ITEM_DATA_FLAVOR);
299: assertNotNull(lookup);
300: Node node = (Node) lookup.lookup(Node.class);
301: assertEquals(itemNames[i][j], node.getName());
302: }
303: }
304: }
305: }
|