1 19 20 23 package org.openide.explorer.view; 24 25 import java.awt.BorderLayout ; 26 import java.awt.event.ActionEvent ; 27 import java.beans.PropertyVetoException ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import javax.swing.Action ; 31 import javax.swing.JScrollPane ; 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 53 public class DefaultActionTest extends NbTestCase { 54 55 56 public DefaultActionTest (String name) { 57 super(name); 58 } 59 60 public static void main (String args[]) { 61 TestRunner.run (new NbTestSuite (DefaultActionTest.class)); 62 } 63 64 65 public final void run (final junit.framework.TestResult result) { 66 try { 67 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { 71 public void run () { 72 DefaultActionTest.super.run (result); 73 } 74 }); 75 } catch (Exception ex) { 76 ex.printStackTrace (); 77 throw new IllegalStateException (); 78 } 79 } 80 81 private boolean performed; 82 private Node root, investigatedNode; 83 private List fails = new ArrayList (); 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 getName () { 110 return "Test default action"; 111 } 112 }; 113 investigatedNode = new AbstractNode (Children.LEAF, Lookup.EMPTY) { 114 public String getName () { 115 return "Node with default action"; 116 } 117 public Action getPreferredAction () { 118 return a; 119 } 120 }; 121 children.add (new Node[] { investigatedNode }); 122 } 123 124 private TopComponent prepareExplorerPanel (JScrollPane 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 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 action = n.getPreferredAction (); 148 action.actionPerformed (new ActionEvent (n, ActionEvent.ACTION_PERFORMED, "")); 149 150 Thread.sleep (300); 152 } catch (Exception x) { 153 fail (x.getMessage ()); 154 } 155 } 156 157 public void testNodeInLoopup () throws Exception { 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 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 |