1 19 20 package org.openide.windows; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyVetoException ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import javax.swing.SwingUtilities ; 28 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 import junit.textui.TestRunner; 32 33 import org.netbeans.junit.NbTestCase; 34 import org.netbeans.junit.NbTestSuite; 35 36 import org.openide.explorer.ExplorerManager; 37 import org.openide.explorer.ExplorerPanel; 38 import org.openide.explorer.view.BeanTreeView; 39 import org.openide.explorer.view.TreeView; 40 import org.openide.nodes.AbstractNode; 41 import org.openide.nodes.Children; 42 import org.openide.nodes.Node; 43 import org.openide.util.Lookup; 44 45 46 47 48 53 public class TopComponentActivatedNodesTest extends NbTestCase { 54 55 private TopComponent top; 56 57 public TopComponentActivatedNodesTest(java.lang.String testName) { 58 super(testName); 59 } 60 61 public static void main(java.lang.String [] args) { 62 TestRunner.run(suite()); 63 } 64 65 public static Test suite() { 66 TestSuite suite = new NbTestSuite(TopComponentActivatedNodesTest.class); 67 68 return suite; 69 } 70 71 protected boolean runInEQ() { 72 return true; 73 } 74 75 private ExplorerPanel p; 76 private ExplorerManager em; 77 private Node[] nodes; 78 private PropertyChangeListener listenerEM, listenerTC; 79 80 protected void setUp () { 81 System.setProperty("org.openide.util.Lookup", "-"); 83 84 p = new ExplorerPanel (); 85 em = p.getExplorerManager (); 86 87 TreeView tv = new BeanTreeView (); 88 p.add (tv); 89 Children ch = new Children.Array (); 90 nodes = new Node[10]; 91 for (int i = 0; i < 10; i++) { 92 nodes[i] = new AbstractNode (Children.LEAF); 93 nodes[i].setName ("Node" + i); 94 } 95 ch.add (nodes); 96 Node root = new AbstractNode (ch); 97 em.setRootContext (root); 98 99 assertArrays ("INIT: getSelectedNodes equals getActivatedNodes.", 101 em.getSelectedNodes (), p.getActivatedNodes ()); 102 } 103 104 private void initListeners () { 105 listenerTC = new PropertyChangeListener () { 106 public void propertyChange (PropertyChangeEvent ev) { 107 System.out.println("TC: PROP_ACTIVATED_NODES change!"); 108 assertArrays ("FIRED TC CHANGE: getSelectedNodes equals PROP_ACTIVATED_NODES", 113 em.getSelectedNodes (), p.getActivatedNodes ()); 114 } 115 }; 116 117 p.addPropertyChangeListener (TopComponent.Registry.PROP_ACTIVATED_NODES, listenerTC); 118 119 listenerEM = new PropertyChangeListener () { 120 public void propertyChange (PropertyChangeEvent ev) { 121 if (ExplorerManager.PROP_SELECTED_NODES.equals (ev.getPropertyName ())) { 122 System.out.println("EM: PROP_SELECTED_NODES change!"); 123 assertArrays ("FIRED EM CHANGE: PROP_SELECTED_NODES equals getActivatedNodes", 124 ((Object [])ev.getNewValue ()), p.getActivatedNodes ()); 125 } 126 } 127 }; 128 129 em.addPropertyChangeListener (listenerEM); 130 131 } 132 133 private void removeListeners () { 134 em.removePropertyChangeListener (listenerEM); 135 p.removePropertyChangeListener (TopComponent.Registry.PROP_ACTIVATED_NODES, listenerTC); 136 } 137 138 public void testOnceChange () { 139 SwingUtilities.invokeLater (new Runnable () { 140 public void run () { 141 142 initListeners (); 143 144 try { 146 em.setSelectedNodes (new Node[] { nodes[3], nodes[5] }); 147 148 Node[] activatedNodes = p.getActivatedNodes (); 149 150 assertArrays ("ONCE CHANGE: getSelectedNodes equals getActivatedNodes.", 152 em.getSelectedNodes (), p.getActivatedNodes ()); 153 154 Lookup.Result result = p.getLookup ().lookup (new Lookup.Template (Node.class)); 156 Collection col = result.allInstances (); 157 Iterator it = col.iterator (); 158 Node[] lookupNodes = new Node[col.size ()]; 159 int i = 0; 160 while (it.hasNext ()) { 161 lookupNodes[i] = (Node)it.next (); 162 i++; 163 } 164 165 assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes", 167 lookupNodes, activatedNodes); 168 169 } catch (PropertyVetoException pve) { 170 fail ("Caught PropertyVetoException. msg:" + pve.getMessage ()); 171 } finally { 172 removeListeners (); 173 } 174 175 } 176 }); 177 } 178 179 public void testIntensiveChange () { 180 SwingUtilities.invokeLater (new Runnable () { 181 public void run () { 182 183 initListeners (); 184 185 try { 187 for (int i = 3; i < 8; i++) 188 em.setSelectedNodes (new Node[] { nodes[i] }); 189 190 Node[] activatedNodes = p.getActivatedNodes (); 191 192 assertArrays ("INTENSIVE CHANGES: getSelectedNodes equals getActivatedNodes.", 194 em.getSelectedNodes (), activatedNodes); 195 196 Lookup.Result result = p.getLookup ().lookup (new Lookup.Template (Node.class)); 198 Collection col = result.allInstances (); 199 Iterator it = col.iterator (); 200 Node[] lookupNodes = new Node[col.size ()]; 201 int i = 0; 202 while (it.hasNext ()) { 203 lookupNodes[i] = (Node)it.next (); 204 i++; 205 } 206 207 assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes", 209 lookupNodes, em.getSelectedNodes ()); 210 211 } catch (PropertyVetoException pve) { 212 fail ("Caught PropertyVetoException. msg:" + pve.getMessage ()); 213 } finally { 214 removeListeners (); 215 } 216 } 217 }); 218 } 219 220 public void testIntensiveChangeWithLookup () { 221 SwingUtilities.invokeLater (new Runnable () { 222 public void run () { 223 224 initListeners (); 225 try { 227 for (int i = 3; i < 8; i++) 228 em.setSelectedNodes (new Node[] { nodes[i] }); 229 } catch (PropertyVetoException pve) { 230 fail ("Caught PropertyVetoException. msg:" + pve.getMessage ()); 231 } finally { 232 removeListeners (); 233 } 234 235 Lookup.Result result = p.getLookup ().lookup (new Lookup.Template (Node.class)); 237 Collection col = result.allInstances (); 238 Iterator it = col.iterator (); 239 Node[] lookupNodes = new Node[col.size ()]; 240 int i = 0; 241 while (it.hasNext ()) { 242 lookupNodes[i] = (Node)it.next (); 243 i++; 244 } 245 246 assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes", 248 lookupNodes, p.getActivatedNodes ()); 249 250 assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes", 252 lookupNodes, em.getSelectedNodes ()); 253 } 254 }); 255 } 256 257 258 public void testInteroperabilityWithTopComponentRegistry () throws Exception { 259 final TopComponent tc = new TopComponent (); 260 final Lookup.Result res = tc.getLookup ().lookup (new Lookup.Template (Node.class)); 261 262 assertNull ("Empty arrays", tc.getActivatedNodes()); 263 assertEquals ("Empty list of nodes", 0, res.allInstances().size ()); 264 265 class L implements PropertyChangeListener , org.openide.util.LookupListener { 266 public Object [] expectedArray; 267 public java.util.ArrayList events = new java.util.ArrayList (); 268 269 public void resultChanged (org.openide.util.LookupEvent ev) { 270 events.add (ev); 271 } 272 273 public void propertyChange (PropertyChangeEvent ev) { 274 if (TopComponent.Registry.PROP_CURRENT_NODES.equals (ev.getPropertyName ())) { 275 assertArrays ("Should be the same", tc.getActivatedNodes(), expectedArray); 276 assertArrays ( 277 "Also in lookup. ", 278 res.allInstances ().toArray (), 279 expectedArray 280 ); 281 } 282 events.add (ev); 283 } 284 } 285 L l = new L (); 286 res.addLookupListener(l); 287 288 tc.requestActive (); 289 assertEquals ("Really activated", tc, TopComponent.getRegistry ().getActivated ()); 290 try { 291 TopComponent.getRegistry ().addPropertyChangeListener (l); 292 293 Node[] arr = { Node.EMPTY }; 294 l.expectedArray = arr; 295 tc.setActivatedNodes (arr); 296 297 Object [] ev = l.events.toArray (); 298 assertEquals ("Three events", 3, ev.length); 299 assertEquals ("First is lookup change", org.openide.util.LookupEvent.class, ev[0].getClass ()); 300 assertEquals ("Second is prop change", PropertyChangeEvent .class, ev[1].getClass ()); 301 assertEquals ("Third is prop change", PropertyChangeEvent .class, ev[2].getClass ()); 302 303 assertEquals (TopComponent.Registry.PROP_ACTIVATED_NODES, ((PropertyChangeEvent )ev[1]).getPropertyName()); 304 assertEquals (TopComponent.Registry.PROP_CURRENT_NODES, ((PropertyChangeEvent )ev[2]).getPropertyName()); 305 } finally { 306 TopComponent.getRegistry ().removePropertyChangeListener (l); 307 } 308 } 309 310 private void assertArrays (String msg, Object [] arr1, Object [] arr2) { 311 if (arr1 == null && arr2 == null) return ; 318 if (arr1 == null) { 319 if (arr2.length == 0) { 320 return ; 321 } else { 322 fail (msg + " BUT: Array1 was null Array2 was " + java.util.Arrays.asList (arr2)); 323 } 324 } 325 if (arr2 == null) { 326 if (arr1.length == 0) { 327 return ; 328 } else { 329 fail (msg + " BUT: Array2 was null Array1 was " + java.util.Arrays.asList (arr1)); 330 } 331 } 332 if (arr1.length != arr2.length) fail (msg + "Arrays have a diferent size. First: " + java.util.Arrays.asList (arr1) + " second: " + java.util.Arrays.asList (arr2)); 333 for (int i = 0; i < arr1.length; i++) { 336 if (! arr1[i].equals (arr2[i]) ) { 337 fail (msg + " BUT: excepted: " + arr1[i] + ", was: " + arr2[i]); 338 } 339 } 340 } 341 342 } 343 | Popular Tags |