1 19 20 package org.netbeans.api.nodes2looks; 21 22 import junit.framework.*; 23 import junit.textui.TestRunner; 24 import java.util.*; 25 import org.netbeans.spi.looks.*; 26 import org.openide.nodes.*; 27 import org.openide.cookies.InstanceCookie; 28 29 import org.netbeans.junit.*; 30 import org.openide.util.Lookup; 31 32 import org.openide.util.LookupListener; 33 import org.openide.util.lookup.*; 34 35 38 public class LookNodeLookupTest extends NbTestCase { 39 public LookNodeLookupTest(String name) { 40 super(name); 41 } 42 43 public static void main(String [] args) { 44 TestRunner.run(new NbTestSuite(LookNodeLookupTest.class)); 45 } 46 47 protected void setUp() throws Exception { 48 super.setUp(); 49 TestUtil.setUpRegistryToDefault(); 50 } 51 52 public void testChangesAreFiredFromLookup () { 53 Collection ro = new ArrayList(); 54 TestLookupLook look = new TestLookupLook( "TLT" ); 55 56 Node node = Nodes.node (ro, look); 57 58 checkInstanceInGetCookie ( ro, new Node.Cookie () {}, look, node); 59 checkInstanceInGetLookup ( ro, new Node.Cookie () {}, look, node, true); 60 checkInstanceInGetLookup ( ro, new Node.Cookie () {}, look, node, true); 61 checkInstanceInGetLookup ( ro, "Some string", look, node, true); 62 63 } 64 65 public void testInstanceCookieOfInLookup () throws Exception { 66 67 Object rep = new AbstractNode (Children.LEAF); 68 Look look = Nodes.nodeLook (); 69 Node node = Nodes.node (rep, look); 70 71 InstanceCookie.Of icOf = (InstanceCookie.Of)node.getLookup ().lookup (InstanceCookie.Of.class); 72 73 assertNotNull ("Ic cookie should not be null.", icOf); 74 assertNotNull ("Ro from LookNode's lookup.", icOf.instanceCreate()); 75 assertEquals ("Sample look ro is same ro lookup.", icOf.instanceCreate(), rep); 76 77 } 78 79 public void testChangesAreFiredFromLookupThruFilterNode () { 80 Collection ro = new ArrayList(); 81 TestLookupLook look = new TestLookupLook( "TLT" ); 82 83 Node node = new FilterNode (Nodes.node (ro, look)); 84 85 checkInstanceInGetLookup ( ro, new Node.Cookie () {}, look, node, true); 86 checkInstanceInGetLookup ( ro, "Some string", look, node, true); 87 88 } 89 90 public void testChangesAreFiredFromLookupThruFilterNodeWithOverWrittenGetCookie () { 91 92 final Node.Cookie myInstance = new Node.Cookie () { }; 93 94 Collection ro = new ArrayList(); 95 TestLookupLook look = new TestLookupLook( "TLT" ); 96 97 Node node = new FilterNode (Nodes.node (ro, look)) { 98 public Node.Cookie getCookie (Class clazz) { 99 if (clazz == myInstance.getClass ()) { 100 return myInstance; 101 } 102 return super.getCookie (clazz); 103 } 104 }; 105 106 checkInstanceInGetCookie (ro, new Node.Cookie () {}, look, node); 107 checkInstanceInGetLookup (ro, new Node.Cookie () {}, look, node, true); 108 checkInstanceInGetLookup (ro, "Some string", look, node, false); 111 112 assertEquals ("It is possible to get myInstance from getCookie", myInstance, node.getCookie (myInstance.getClass ())); 113 assertEquals ("It also possible to get it from getLookup", myInstance, node.getLookup ().lookup (myInstance.getClass ())); 114 115 } 116 117 public void testOldEnvInLookup() { 118 119 final String oldCookie = "OLD"; 120 final String newCookie = "NEW"; 121 122 Collection ro = new ArrayList(); 123 ro.add( oldCookie ); 124 TestLookupLook look = new TestLookupLook( "OETLT" ); 125 126 Node n = Nodes.node( ro, look ); 127 n.getLookup().lookup( Object .class ); 129 assertEquals( "Old items should be empty.", 0, look.getValues().size() ); 130 131 ro.remove( oldCookie ); 132 ro.add( newCookie ); 133 134 look.lookupChange( ro ); 135 136 Lookup newLookup = n.getLookup(); 137 Collection oldValues = look.getValues(); 138 139 assertTrue( "New should conntain newCookie. ", newCookie == newLookup.lookup( String .class ) ); 140 assertEquals( "1 string in new Lookup ", 1, newLookup.lookup( new Lookup.Template( String .class ) ).allItems().size() ); 141 142 assertTrue( "Old should conntain oldCookie. ", oldValues.contains( oldCookie ) ); 143 assertEquals( "1 string in old Lookup ", 2, oldValues.size() ); 144 } 145 146 public void testLookupNoInit() { 147 148 Collection ro = new ArrayList(); 149 TestLookupLook look = new TestLookupLook( "OETLT" ); 150 Node n = Nodes.node( ro, look ); 151 152 n.getIcon( 0 ); 153 n.getDisplayName(); 154 n.getName(); 155 156 assertEquals( "getLookupItems should not be called", 0, look.getLookupItemsCallCount ); 157 158 } 159 160 161 private void checkInstanceInGetCookie (Collection ro, Object obj, TestLookupLook look, Node node) { 162 163 Listener listener = new Listener (); 164 node.addNodeListener(listener); 165 166 assertNull ("The object is not there yet", node.getCookie (obj.getClass ())); 167 168 ro.add (obj); 169 look.lookupChange( ro ); 170 listener.assertEvents ("One change in node", 1, -1); 171 172 if (obj instanceof Node.Cookie) { 173 assertEquals ("Can access cookie in the content", obj, node.getCookie (obj.getClass ())); 174 } else { 175 assertNull ("Cannot access noncookie in the content", node.getCookie (obj.getClass ())); 176 } 177 178 ro.remove (obj); 179 look.lookupChange( ro ); 180 listener.assertEvents ("One change in node", 1, -1); 181 182 } 183 184 private void checkInstanceInGetLookup (Collection ro, Object obj, TestLookupLook look, Node node, boolean shouldBeThere) { 185 186 Listener listener = new Listener (); 187 Lookup.Result res = node.getLookup ().lookup (new Lookup.Template (obj.getClass ())); 188 Collection ignore = res.allItems (); 189 res.addLookupListener(listener); 190 191 ro.add (obj); 192 look.lookupChange( ro ); 193 if (shouldBeThere) { 194 listener.assertEvents ("One change in node's lookup (add)", -1, 1); 195 assertEquals ("Can access object in content via lookup", obj, node.getLookup ().lookup (obj.getClass ())); 196 } else { 197 assertNull ("Cannot access object in content via lookup", node.getLookup ().lookup (obj.getClass ())); 198 } 199 200 201 ro.remove (obj); 202 look.lookupChange( ro ); 203 if (shouldBeThere) { 204 listener.assertEvents ("One change in node's lookup (remove)", -1, 1); 205 } 206 assertNull ("Cookie is removed", node.getLookup ().lookup (obj.getClass ())); 207 208 } 209 210 211 public void testNodeIsInItsLookup () { 215 Collection ro = new ArrayList(); 216 TestLookupLook look = new TestLookupLook( "TLT" ); 217 218 219 Node n = Nodes.node (ro, look); 220 ro.add (n); 221 look.lookupChange( ro ); 222 assertEquals ("Node is there", n, n.getLookup ().lookup (Node.class)); 223 224 } 225 226 227 private void checkInstanceInLookup (Node.Cookie obj, CookieSet ic, Lookup l) { 228 Listener listener = new Listener (); 229 Lookup.Result res = l.lookup (new Lookup.Template (Object .class)); 230 Collection justToEnsureChangesToListenerWillBeFired = res.allItems (); 231 res.addLookupListener(listener); 232 233 ic.add (obj); 234 listener.assertEvents ("One change in lookup", -1, 1); 235 236 assertEquals ("Can access cookie in the content", obj, l.lookup (obj.getClass ())); 237 238 ic.remove (obj); 239 listener.assertEvents ("One change in lookup", -1, 1); 240 241 ic.add (obj); 242 listener.assertEvents ("One change in lookup", -1, 1); 243 244 assertEquals ("Can access cookie in the content", obj, l.lookup (obj.getClass ())); 245 246 ic.remove (obj); 247 listener.assertEvents ("One change in lookup", -1, 1); 248 249 } 250 251 252 253 257 283 284 285 private static class Listener implements LookupListener, NodeListener { 286 private int cookies; 287 private int lookups; 288 289 public void assertEvents (String txt, int cookies, int lookups) { 290 if (cookies != -1) 292 assertEquals (txt + " cookies", cookies, this.cookies); 293 if (lookups != -1) 294 assertEquals (txt + " lookups", lookups, this.lookups); 295 296 this.cookies = 0; 297 this.lookups = 0; 298 } 299 300 public void childrenAdded(NodeMemberEvent ev) { 301 } 302 303 public void childrenRemoved(NodeMemberEvent ev) { 304 } 305 306 public void childrenReordered(NodeReorderEvent ev) { 307 } 308 309 public void nodeDestroyed(NodeEvent ev) { 310 } 311 312 public void propertyChange(java.beans.PropertyChangeEvent evt) { 313 if (Node.PROP_COOKIE == evt.getPropertyName()) { 314 cookies++; 315 } 316 } 317 318 public void resultChanged(org.openide.util.LookupEvent ev) { 319 lookups++; 320 } 321 322 } 324 325 private static class TestLookupLook extends Look { 326 327 private int getLookupItemsCallCount; 328 329 private Collection values; 330 331 public TestLookupLook( String name ) { 332 super( name ); 333 } 334 335 public void lookupChange( Object representedObject ) { 336 fireChange( representedObject, Look.GET_LOOKUP_ITEMS ); 337 } 338 339 public Collection getValues() { 340 return values; 341 } 342 343 public Collection getLookupItems(Object representedObject, Lookup oldEnv) { 344 345 getLookupItemsCallCount++; 346 347 this.values = oldEnv.lookup( new Lookup.Template( Object .class )).allInstances() ; 348 349 Collection items = new ArrayList(); 350 Collection c = (Collection)representedObject; 351 352 for( Iterator it = c.iterator(); it.hasNext(); ) { 353 items.add( new GoldenValue.TestLookupItem( it.next() ) ); 354 } 355 356 return items; 357 } 358 359 } 360 361 } | Popular Tags |