KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > DefaultActionTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  *
22  */

23 package org.openide.explorer.view;
24
25 import java.awt.BorderLayout JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.beans.PropertyVetoException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.JScrollPane JavaDoc;
32
33
34 import junit.textui.TestRunner;
35
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.junit.NbTestSuite;
38
39 import org.openide.explorer.ExplorerPanel;
40 import org.openide.nodes.AbstractNode;
41 import org.openide.nodes.Children;
42 import org.openide.nodes.Node;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.Lookup;
45 import org.openide.util.actions.NodeAction;
46 import org.openide.windows.TopComponent;
47
48
49 /**
50  * Test DefaulAction of node selected in a view.
51  * @author Jiri Rechtacek
52  */

53 public class DefaultActionTest extends NbTestCase {
54     
55     
56     public DefaultActionTest (String JavaDoc name) {
57         super(name);
58     }
59    
60     public static void main (String JavaDoc args[]) {
61          TestRunner.run (new NbTestSuite (DefaultActionTest.class));
62     }
63     
64     /** Run all tests in AWT thread */
65     public final void run (final junit.framework.TestResult result) {
66         try {
67             // XXX ExplorerManager when updating selected nodes
68
// replanes all firing into AWT thread, therefore the test
69
// has to run in AWT.
70
javax.swing.SwingUtilities.invokeAndWait (new Runnable JavaDoc () {
71                 public void run () {
72                     DefaultActionTest.super.run (result);
73                 }
74             });
75         } catch (Exception JavaDoc ex) {
76             ex.printStackTrace ();
77             throw new IllegalStateException JavaDoc ();
78         }
79     }
80     
81     private boolean performed;
82     private Node root, investigatedNode;
83     private List JavaDoc fails = new ArrayList JavaDoc ();
84     
85     protected void setUp () {
86         final Children children = new Children.Array ();
87         root = new AbstractNode (children);
88         final NodeAction a = new NodeAction () {
89             protected void performAction (Node[] activatedNodes) {
90                 assertActionPerformed (activatedNodes);
91             }
92             
93             public boolean asynchronous () {
94                 return false;
95             }
96
97             protected boolean enable (Node[] activatedNodes) {
98                 return true;
99             }
100             
101             public boolean isEnabled () {
102                 return true;
103             }
104             
105             public HelpCtx getHelpCtx () {
106                 return null;
107             }
108             
109             public String JavaDoc getName () {
110                 return "Test default action";
111             }
112         };
113         investigatedNode = new AbstractNode (Children.LEAF, Lookup.EMPTY) {
114             public String JavaDoc getName () {
115                 return "Node with default action";
116             }
117             public Action JavaDoc getPreferredAction () {
118                 return a;
119             }
120         };
121         children.add (new Node[] { investigatedNode });
122     }
123     
124     private TopComponent prepareExplorerPanel (JScrollPane JavaDoc view) {
125         final ExplorerPanel p = new ExplorerPanel ();
126         p.setSize (200, 200);
127         p.add (view, BorderLayout.CENTER);
128         p.getExplorerManager ().setRootContext (root);
129
130         try {
131             p.getExplorerManager ().setSelectedNodes (root.getChildren().getNodes ());
132         } catch (PropertyVetoException JavaDoc pve) {
133             fail (pve.getMessage ());
134         }
135         
136         return p;
137     }
138
139     private void invokeDefaultAction (final TopComponent tc) {
140         performed = false;
141         try {
142             Node[] nodes = tc.getActivatedNodes ();
143             assertNotNull ("View has the active nodes.", nodes);
144             Node n = nodes.length > 0 ? nodes[0] : null;
145             assertNotNull ("View has a active node.", n);
146             
147             final Action JavaDoc action = n.getPreferredAction ();
148             action.actionPerformed (new ActionEvent JavaDoc (n, ActionEvent.ACTION_PERFORMED, ""));
149             
150             // wait to invoke action is propagated
151
Thread.sleep (300);
152         } catch (Exception JavaDoc x) {
153             fail (x.getMessage ());
154         }
155     }
156     
157     public void testNodeInLoopup () throws Exception JavaDoc {
158         TreeView tv = new BeanTreeView ();
159         TopComponent tc = prepareExplorerPanel (tv);
160         TreeView.PopupSupport supp = tv.defaultActionListener;
161         tv.manager = ((ExplorerPanel)tc).getExplorerManager ();
162         supp.actionPerformed (null);
163         assertDefaultActionWasPerformed ("BeanTreeView");
164     }
165     
166     public void testListView () {
167         TopComponent tc = prepareExplorerPanel (new ListView ());
168         invokeDefaultAction (tc);
169         assertDefaultActionWasPerformed ("ListView");
170         tc.close ();
171     }
172     
173     public void testBeanTreeView () {
174         TopComponent tc = prepareExplorerPanel (new BeanTreeView ());
175         invokeDefaultAction (tc);
176         assertDefaultActionWasPerformed ("BeanTreeView");
177     }
178     
179     public void testTreeTableView () {
180         TopComponent tc = prepareExplorerPanel (new TreeTableView ());
181         invokeDefaultAction (tc);
182         assertDefaultActionWasPerformed ("TreeTableView");
183     }
184     
185     public void testTreeContextView () {
186         TopComponent tc = prepareExplorerPanel (new ContextTreeView ());
187         invokeDefaultAction (tc);
188         assertDefaultActionWasPerformed ("ContextTreeView");
189     }
190     
191     void assertDefaultActionWasPerformed (String JavaDoc nameOfView) {
192         assertTrue ("[" + nameOfView + "] DefaultAction was preformed.", performed);
193     }
194     
195     void assertActionPerformed (Node[] nodes) {
196         log ("Action performed.");
197         assertNotNull ("Activated nodes exist.", nodes);
198         assertEquals ("Only one node is activated.", 1, nodes.length);
199         assertEquals ("It's the testedNode.", investigatedNode, nodes[0]);
200         performed = true;
201     }
202     
203 }
204
Popular Tags