KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > actions > CookieActionTest


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 package org.openide.util.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.Action JavaDoc;
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 /** Test that cookie actions are in fact sensitive to the correct cookies in the
41  * correct numbers, and that changes to either node selection or cookies on the
42  * selected nodes trigger a change in the selected state.
43  * @author Jesse Glick
44  */

45 public class CookieActionTest extends NbTestCase {
46     
47     public CookieActionTest(String JavaDoc 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 JavaDoc {
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     /**
66      * in order to run in awt event queue
67      * fix for #39789
68      */

69     protected boolean runInEQ() {
70         return true;
71     }
72     
73     /** Similar to NodeActionTest. */
74     public void testBasicUsage() throws Exception JavaDoc {
75         try {
76             // Check enablement logic.
77
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     // XXX test advanced cookie modes, multiple cookies, etc.:
123
// all combinations of one cookie class vs. two, and any
124
// disjunctions of MODE_* constants, against any combination
125
// of nodes {n1, n2, n3} (first add a different cookie to n3 and also to n2)
126

127     /** Make sure it works to change the cookies on a selected node. */
128     public void testChangeCookiesOnNodes() throws Exception JavaDoc {
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     //
184
// cloneAction support
185
//
186

187     public void testNodeActionIsCorrectlyClonned() throws Exception JavaDoc {
188         class Counter implements PropertyChangeListener JavaDoc {
189             int cnt;
190             
191             public void propertyChange(PropertyChangeEvent JavaDoc ev) {
192                 cnt++;
193             }
194             
195             public void assertCnt(String JavaDoc 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 JavaDoc 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 JavaDoc(this, 0, ""));
219         
220         assertEquals("Has been executed just once: ", 1, SimpleCookieAction.runOn.size());
221         Collection JavaDoc c = (Collection JavaDoc)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 JavaDoc w = new WeakReference JavaDoc(clone);
232         clone = null;
233         assertGC("Clone can disappear", w);
234     }
235     
236     
237     // #35834
238
/** Test of enablement of CookieAction caused creation of that cookie instance in node, which has implemented lookup in 'nice' way.
239      * @see #testCookiePrematureCreationInNodeWithDefaultLookup */

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})); // NOI18N
245
assertFalse("Node may not create OpenCookie instance, when tested on presence only", node.isCookieCreated()); // NOI18N
246
}
247     
248     // #35856
249
/** Test of enablement of CookieAction causes creation of that cookie instance in node, which has default lookup.
250      * @see #testCookiePrematureCreationInNodeWithNiceLookup */

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})); // NOI18N
256
assertFalse("Node may not create OpenCookie instance, when tested on presence only", node.isCookieCreated()); // NOI18N
257
}
258     
259     public static class SimpleCookieAction extends CookieAction {
260         protected int mode() {
261             return MODE_EXACTLY_ONE;
262         }
263         protected Class JavaDoc[] cookieClasses() {
264             return new Class JavaDoc[] {OpenCookie.class};
265         }
266         public static final List JavaDoc runOn = new ArrayList JavaDoc(); // List<List<Node>>
267
protected void performAction(Node[] activatedNodes) {
268             runOn.add(Arrays.asList(activatedNodes));
269         }
270         public String JavaDoc 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                 // do nothing
285
}
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 JavaDoc[] cookieClasses() {
309             return new Class JavaDoc[] {OpenCookie.class};
310         }
311         protected void performAction(Node[] activatedNodes) {
312         }
313         public String JavaDoc getName() {
314             return "SimpleCookieAction2";
315         }
316         public HelpCtx getHelpCtx() {
317             return null;
318         }
319         protected boolean asynchronous() {
320             return false;
321         }
322     } // End of SimpleCookieAction2.
323

324     private static final class NodeWithDefaultLookup extends AbstractNode {
325         private static final class Open implements OpenCookie {
326             public void open() {
327                 // Do nothing.
328
}
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 JavaDoc 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     } // End of class NodeWithDefaultLookup.
352

353     
354     private static class NodeWithNiceLookup extends AbstractNode {
355         private static final class Open implements OpenCookie {
356             public void open() {
357                 // Do nothing.
358
}
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 JavaDoc instance;
377                     
378                     public boolean creatorOf(Object JavaDoc o) {
379                         synchronized(NiceLookup.this) {
380                             return o != null && o == instance;
381                         }
382                     }
383                     
384                     public boolean instanceOf(Class JavaDoc c) {
385                         return c.isAssignableFrom(OpenCookie.class);
386                     }
387                     
388                     public String JavaDoc getDisplayName() {
389                         return "OpenCookie item"; // NOI18N XXX
390
}
391                     
392                     public String JavaDoc getId() {
393                         return toString(); // XXX
394
}
395                     
396                     public Class JavaDoc getType() {
397                         return NodeWithNiceLookup.Open.class;
398                     }
399                     
400                     public Object JavaDoc 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     } // End of class NodeWithNiceLookup.
417

418 }
419
420
Popular Tags