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: /*
043: *
044: */
045: package org.openide.explorer.view;
046:
047: import java.awt.BorderLayout;
048: import java.awt.event.ActionEvent;
049: import java.beans.PropertyVetoException;
050: import java.util.ArrayList;
051: import java.util.List;
052: import javax.swing.Action;
053: import javax.swing.JScrollPane;
054:
055: import junit.textui.TestRunner;
056:
057: import org.netbeans.junit.NbTestCase;
058: import org.netbeans.junit.NbTestSuite;
059:
060: import org.openide.explorer.ExplorerPanel;
061: import org.openide.nodes.AbstractNode;
062: import org.openide.nodes.Children;
063: import org.openide.nodes.Node;
064: import org.openide.util.HelpCtx;
065: import org.openide.util.Lookup;
066: import org.openide.util.actions.NodeAction;
067: import org.openide.windows.TopComponent;
068:
069: /**
070: * Test DefaulAction of node selected in a view.
071: * @author Jiri Rechtacek
072: */
073: public class DefaultActionTest extends NbTestCase {
074:
075: public DefaultActionTest(String name) {
076: super (name);
077: }
078:
079: public static void main(String args[]) {
080: TestRunner.run(new NbTestSuite(DefaultActionTest.class));
081: }
082:
083: /** Run all tests in AWT thread */
084: public final void run(final junit.framework.TestResult result) {
085: try {
086: // XXX ExplorerManager when updating selected nodes
087: // replanes all firing into AWT thread, therefore the test
088: // has to run in AWT.
089: javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
090: public void run() {
091: DefaultActionTest.super .run(result);
092: }
093: });
094: } catch (Exception ex) {
095: ex.printStackTrace();
096: throw new IllegalStateException();
097: }
098: }
099:
100: private boolean performed;
101: private Node root, investigatedNode;
102: private List fails = new ArrayList();
103:
104: protected void setUp() {
105: final Children children = new Children.Array();
106: root = new AbstractNode(children);
107: final NodeAction a = new NodeAction() {
108: protected void performAction(Node[] activatedNodes) {
109: assertActionPerformed(activatedNodes);
110: }
111:
112: public boolean asynchronous() {
113: return false;
114: }
115:
116: protected boolean enable(Node[] activatedNodes) {
117: return true;
118: }
119:
120: public boolean isEnabled() {
121: return true;
122: }
123:
124: public HelpCtx getHelpCtx() {
125: return null;
126: }
127:
128: public String getName() {
129: return "Test default action";
130: }
131: };
132: investigatedNode = new AbstractNode(Children.LEAF, Lookup.EMPTY) {
133: public String getName() {
134: return "Node with default action";
135: }
136:
137: public Action getPreferredAction() {
138: return a;
139: }
140: };
141: children.add(new Node[] { investigatedNode });
142: }
143:
144: private TopComponent prepareExplorerPanel(JScrollPane view) {
145: final ExplorerPanel p = new ExplorerPanel();
146: p.setSize(200, 200);
147: p.add(view, BorderLayout.CENTER);
148: p.getExplorerManager().setRootContext(root);
149:
150: try {
151: p.getExplorerManager().setSelectedNodes(
152: root.getChildren().getNodes());
153: } catch (PropertyVetoException pve) {
154: fail(pve.getMessage());
155: }
156:
157: return p;
158: }
159:
160: private void invokeDefaultAction(final TopComponent tc) {
161: performed = false;
162: try {
163: Node[] nodes = tc.getActivatedNodes();
164: assertNotNull("View has the active nodes.", nodes);
165: Node n = nodes.length > 0 ? nodes[0] : null;
166: assertNotNull("View has a active node.", n);
167:
168: final Action action = n.getPreferredAction();
169: action.actionPerformed(new ActionEvent(n,
170: ActionEvent.ACTION_PERFORMED, ""));
171:
172: // wait to invoke action is propagated
173: Thread.sleep(300);
174: } catch (Exception x) {
175: fail(x.getMessage());
176: }
177: }
178:
179: public void testNodeInLoopup() throws Exception {
180: TreeView tv = new BeanTreeView();
181: TopComponent tc = prepareExplorerPanel(tv);
182: TreeView.PopupSupport supp = tv.defaultActionListener;
183: tv.manager = ((ExplorerPanel) tc).getExplorerManager();
184: supp.actionPerformed(null);
185: assertDefaultActionWasPerformed("BeanTreeView");
186: }
187:
188: public void testListView() {
189: TopComponent tc = prepareExplorerPanel(new ListView());
190: invokeDefaultAction(tc);
191: assertDefaultActionWasPerformed("ListView");
192: tc.close();
193: }
194:
195: public void testBeanTreeView() {
196: TopComponent tc = prepareExplorerPanel(new BeanTreeView());
197: invokeDefaultAction(tc);
198: assertDefaultActionWasPerformed("BeanTreeView");
199: }
200:
201: public void testTreeTableView() {
202: TopComponent tc = prepareExplorerPanel(new TreeTableView());
203: invokeDefaultAction(tc);
204: assertDefaultActionWasPerformed("TreeTableView");
205: }
206:
207: public void testTreeContextView() {
208: TopComponent tc = prepareExplorerPanel(new ContextTreeView());
209: invokeDefaultAction(tc);
210: assertDefaultActionWasPerformed("ContextTreeView");
211: }
212:
213: void assertDefaultActionWasPerformed(String nameOfView) {
214: assertTrue("[" + nameOfView + "] DefaultAction was preformed.",
215: performed);
216: }
217:
218: void assertActionPerformed(Node[] nodes) {
219: log("Action performed.");
220: assertNotNull("Activated nodes exist.", nodes);
221: assertEquals("Only one node is activated.", 1, nodes.length);
222: assertEquals("It's the testedNode.", investigatedNode, nodes[0]);
223: performed = true;
224: }
225:
226: }
|