1 19 20 package org.openide.util.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.lang.ref.WeakReference ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.List ; 30 import javax.swing.Action ; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.cookies.OpenCookie; 33 import org.openide.nodes.AbstractNode; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.CookieSet; 36 import org.openide.nodes.Node; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.lookup.AbstractLookup; 39 40 45 public class CookieActionTest extends NbTestCase { 46 47 public CookieActionTest(String name) { 48 super(name); 49 } 50 51 private SystemAction a1; 52 private CookieNode n1, n2; 53 private Node n3; 54 55 protected void setUp() throws Exception { 56 a1 = SystemAction.get(SimpleCookieAction.class); 57 n1 = new CookieNode(); 58 n1.setName("n1"); 59 n2 = new CookieNode(); 60 n2.setName("n2"); 61 n3 = new AbstractNode(Children.LEAF); 62 n3.setName("n3"); 63 } 64 65 69 protected boolean runInEQ() { 70 return true; 71 } 72 73 74 public void testBasicUsage() throws Exception { 75 try { 76 ActionsInfraHid.WaitPCL l = new ActionsInfraHid.WaitPCL(NodeAction.PROP_ENABLED); 78 a1.addPropertyChangeListener(l); 79 assertFalse(a1.isEnabled()); 80 ActionsInfraHid.setCurrentNodes(new Node[] {n1}); 81 assertTrue(l.changed()); 82 l.gotit = 0; 83 assertTrue(a1.isEnabled()); 84 ActionsInfraHid.setCurrentNodes(new Node[] {n1, n2}); 85 assertTrue(l.changed()); 86 l.gotit = 0; 87 assertFalse(a1.isEnabled()); 88 ActionsInfraHid.setCurrentNodes(new Node[] {n2}); 89 assertTrue(l.changed()); 90 l.gotit = 0; 91 assertTrue(a1.isEnabled()); 92 ActionsInfraHid.setCurrentNodes(new Node[] {n3}); 93 assertTrue(l.changed()); 94 l.gotit = 0; 95 assertFalse(a1.isEnabled()); 96 ActionsInfraHid.setCurrentNodes(new Node[] {n3}); 97 if (!l.changed()) { 98 Thread.sleep(1000); 99 } 100 l.gotit = 0; 101 assertFalse(a1.isEnabled()); 102 ActionsInfraHid.setCurrentNodes(new Node[] {n1}); 103 assertTrue(l.changed()); 104 l.gotit = 0; 105 assertTrue(a1.isEnabled()); 106 ActionsInfraHid.setCurrentNodes(new Node[] {n1}); 107 if (!l.changed()) { 108 Thread.sleep(1000); 109 } 110 l.gotit = 0; 111 assertTrue(a1.isEnabled()); 112 ActionsInfraHid.setCurrentNodes(new Node[] {n1, n2}); 113 assertTrue(l.changed()); 114 l.gotit = 0; 115 assertFalse(a1.isEnabled()); 116 } finally { 117 ActionsInfraHid.setCurrentNodes(new Node[0]); 118 ActionsInfraHid.setCurrentNodes(null); 119 } 120 } 121 122 127 128 public void testChangeCookiesOnNodes() throws Exception { 129 ActionsInfraHid.WaitPCL l = new ActionsInfraHid.WaitPCL(NodeAction.PROP_ENABLED); 130 try { 131 assertFalse(a1.isEnabled()); 132 assertTrue(n1.getCookie(OpenCookie.class) != null); 133 a1.addPropertyChangeListener(l); 134 ActionsInfraHid.setCurrentNodes(new Node[] {n1}); 135 assertTrue("Received PROP_ENABLED on SimpleCookieAction after changing nodes", l.changed()); 136 l.gotit = 0; 137 assertTrue(a1.isEnabled()); 138 n1.setHasCookie(false); 139 assertTrue(l.changed()); 140 l.gotit = 0; 141 assertFalse(a1.isEnabled()); 142 ActionsInfraHid.setCurrentNodes(null); 143 if (!l.changed()) { 144 Thread.sleep(1000); 145 } 146 l.gotit = 0; 147 assertFalse(a1.isEnabled()); 148 n1.setHasCookie(true); 149 assertTrue(l.changed()); 150 l.gotit = 0; 151 assertTrue(a1.isEnabled()); 152 n2.setHasCookie(false); 153 ActionsInfraHid.setCurrentNodes(new Node[] {n2}); 154 assertTrue(l.changed()); 155 l.gotit = 0; 156 assertFalse(a1.isEnabled()); 157 n2.setHasCookie(true); 158 assertTrue(l.changed()); 159 l.gotit = 0; 160 assertTrue(a1.isEnabled()); 161 a1.removePropertyChangeListener(l); 162 assertTrue(a1.isEnabled()); 163 n2.setHasCookie(false); 164 assertFalse(a1.isEnabled()); 165 n2.setHasCookie(true); 166 assertTrue(a1.isEnabled()); 167 ActionsInfraHid.setCurrentNodes(new Node[] {n1}); 168 assertTrue(a1.isEnabled()); 169 Thread.sleep(1000); 170 assertTrue(a1.isEnabled()); 171 n1.setHasCookie(false); 172 Thread.sleep(1000); 173 assertFalse(a1.isEnabled()); 174 } finally { 175 a1.removePropertyChangeListener(l); 176 ActionsInfraHid.setCurrentNodes(new Node[0]); 177 ActionsInfraHid.setCurrentNodes(null); 178 n1.setHasCookie(true); 179 n2.setHasCookie(true); 180 } 181 } 182 183 187 public void testNodeActionIsCorrectlyClonned() throws Exception { 188 class Counter implements PropertyChangeListener { 189 int cnt; 190 191 public void propertyChange(PropertyChangeEvent ev) { 192 cnt++; 193 } 194 195 public void assertCnt(String txt, int cnt) { 196 assertEquals(txt, cnt, this.cnt); 197 this.cnt = 0; 198 } 199 } 200 201 202 SimpleCookieAction s = SimpleCookieAction.get(SimpleCookieAction.class); 203 Counter counter = new Counter(); 204 205 CookieNode node = new CookieNode(); 206 node.setHasCookie(false); 207 208 Action clone = s.createContextAwareInstance(node.getLookup()); 209 clone.addPropertyChangeListener(counter); 210 211 assertTrue("Not enabled", !clone.isEnabled()); 212 213 node.setHasCookie(true); 214 215 assertTrue("Enabled", clone.isEnabled()); 216 counter.assertCnt("Once change in enabled state", 1); 217 218 clone.actionPerformed(new ActionEvent (this, 0, "")); 219 220 assertEquals("Has been executed just once: ", 1, SimpleCookieAction.runOn.size()); 221 Collection c = (Collection )SimpleCookieAction.runOn.iterator().next(); 222 SimpleCookieAction.runOn.clear(); 223 assertTrue("Has been executed on mn1", c.contains(node)); 224 225 226 node.setHasCookie(false); 227 assertTrue("Not enabled", !clone.isEnabled()); 228 counter.assertCnt("One change", 1); 229 230 231 WeakReference w = new WeakReference (clone); 232 clone = null; 233 assertGC("Clone can disappear", w); 234 } 235 236 237 240 public void testCookiePrematureCreationInNodeWithNiceLookup() { 241 SimpleCookieAction2 action = SimpleCookieAction2.get(SimpleCookieAction2.class); 242 NodeWithNiceLookup node = new NodeWithNiceLookup(); 243 244 assertTrue("Node has to be enabled on OpenCookie", action.enable(new Node[] {node})); assertFalse("Node may not create OpenCookie instance, when tested on presence only", node.isCookieCreated()); } 247 248 251 public void testCookiePrematureCreationInNodeWithDefaultLookup() { 252 SimpleCookieAction2 action = SimpleCookieAction2.get(SimpleCookieAction2.class); 253 NodeWithDefaultLookup node = new NodeWithDefaultLookup(); 254 255 assertTrue("Node has to be enabled on OpenCookie", action.enable(new Node[] {node})); assertFalse("Node may not create OpenCookie instance, when tested on presence only", node.isCookieCreated()); } 258 259 public static class SimpleCookieAction extends CookieAction { 260 protected int mode() { 261 return MODE_EXACTLY_ONE; 262 } 263 protected Class [] cookieClasses() { 264 return new Class [] {OpenCookie.class}; 265 } 266 public static final List runOn = new ArrayList (); protected void performAction(Node[] activatedNodes) { 268 runOn.add(Arrays.asList(activatedNodes)); 269 } 270 public String getName() { 271 return "SimpleCookieAction"; 272 } 273 public HelpCtx getHelpCtx() { 274 return null; 275 } 276 protected boolean asynchronous() { 277 return false; 278 } 279 } 280 281 private static final class CookieNode extends AbstractNode { 282 private static final class Open implements OpenCookie { 283 public void open() { 284 } 286 } 287 public CookieNode() { 288 super(Children.LEAF); 289 getCookieSet().add(new Open()); 290 } 291 public void setHasCookie(boolean b) { 292 if (b && getCookie(OpenCookie.class) == null) { 293 getCookieSet().add(new Open()); 294 } else if (!b) { 295 OpenCookie o = getCookie(OpenCookie.class); 296 if (o != null) { 297 getCookieSet().remove(o); 298 } 299 } 300 } 301 } 302 303 304 public static class SimpleCookieAction2 extends CookieAction { 305 protected int mode() { 306 return MODE_EXACTLY_ONE; 307 } 308 protected Class [] cookieClasses() { 309 return new Class [] {OpenCookie.class}; 310 } 311 protected void performAction(Node[] activatedNodes) { 312 } 313 public String getName() { 314 return "SimpleCookieAction2"; 315 } 316 public HelpCtx getHelpCtx() { 317 return null; 318 } 319 protected boolean asynchronous() { 320 return false; 321 } 322 } 324 private static final class NodeWithDefaultLookup extends AbstractNode { 325 private static final class Open implements OpenCookie { 326 public void open() { 327 } 329 } 330 331 private boolean cookieCreated; 332 333 public NodeWithDefaultLookup() { 334 super(Children.LEAF); 335 getCookieSet().add(OpenCookie.class, new CookieSet.Factory() { 336 public Node.Cookie createCookie(Class clazz) { 337 if(clazz.isAssignableFrom(OpenCookie.class)) { 338 synchronized(NodeWithDefaultLookup.this) { 339 NodeWithDefaultLookup.this.cookieCreated = true; 340 } 341 return new Open(); 342 } 343 return null; 344 } 345 }); 346 } 347 348 public synchronized boolean isCookieCreated() { 349 return cookieCreated; 350 } 351 } 353 354 private static class NodeWithNiceLookup extends AbstractNode { 355 private static final class Open implements OpenCookie { 356 public void open() { 357 } 359 } 360 361 private boolean cookieCreated; 362 363 public NodeWithNiceLookup() { 364 super(Children.LEAF, new NiceLookup()); 365 } 366 367 public synchronized boolean isCookieCreated() { 368 return ((NiceLookup)getLookup()).isInstanceCreated(); 369 } 370 371 private static class NiceLookup extends AbstractLookup { 372 private boolean instanceCreated; 373 374 private NiceLookup() { 375 addPair(new AbstractLookup.Pair() { 376 private Object instance; 377 378 public boolean creatorOf(Object o) { 379 synchronized(NiceLookup.this) { 380 return o != null && o == instance; 381 } 382 } 383 384 public boolean instanceOf(Class c) { 385 return c.isAssignableFrom(OpenCookie.class); 386 } 387 388 public String getDisplayName() { 389 return "OpenCookie item"; } 391 392 public String getId() { 393 return toString(); } 395 396 public Class getType() { 397 return NodeWithNiceLookup.Open.class; 398 } 399 400 public Object getInstance() { 401 synchronized(NiceLookup.this) { 402 if(instance == null) { 403 instance = new Open(); 404 instanceCreated = true; 405 } 406 return instance; 407 } 408 } 409 }); 410 } 411 public synchronized boolean isInstanceCreated() { 412 return instanceCreated; 413 } 414 } 415 416 } 418 } 419 420 | Popular Tags |