KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.Reference JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.event.EventListenerList JavaDoc;
30 import org.netbeans.junit.MemoryFilter;
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.Node;
36 import org.openide.nodes.NodeListener;
37 import org.openide.util.ContextGlobalProvider;
38 import org.openide.util.HelpCtx;
39 import org.openide.util.Lookup;
40 import org.openide.util.Utilities;
41 import org.openide.util.lookup.Lookups;
42 import org.openide.util.lookup.ProxyLookup;
43
44 /** Test CookieAction functionality.
45  * @author Jesse Glick
46  */

47 public class CookieAction2Test extends NbTestCase {
48     
49     public CookieAction2Test(String JavaDoc name) {
50         super(name);
51     }
52     
53     protected void setUp() {
54         System.setProperty("org.openide.util.Lookup", "org.openide.util.actions.CookieAction2Test$Lkp");
55         
56         assertTrue(Utilities.actionsGlobalContext() instanceof Lkp);
57     }
58     
59     public void testDirectCallToEnabled() throws Exception JavaDoc {
60         SimpleCookieAction sca = SystemAction.get(SimpleCookieAction.class);
61         assertTrue(sca.enable(new Node[] {new CookieNode()}));
62         assertTrue(!sca.enable(new Node[] {}));
63         sca.getName();
64         assertTrue(sca.enable(new Node[] {new CookieNode()}));
65         assertTrue(!sca.enable(new Node[] {}));
66     }
67     
68     public void testChangesOfCookiesPossibleFromNonAWTThreadIssue40937() throws Exception JavaDoc {
69         doAWT(true);
70     }
71     public void testChangesOfCookiesPossibleFromNonAWTThreadWithGlobalActionIssue40937() throws Exception JavaDoc {
72         doAWT(false);
73     }
74     
75     public void testNodeListenersDetachedAtFinalizeIssue58065() throws Exception JavaDoc {
76         CookieNode node = new CookieNode();
77         SimpleCookieAction2 sca = new SimpleCookieAction2();
78         Action JavaDoc action = sca.createContextAwareInstance(node.getLookup());
79         
80         class NodeListenerMemoryFilter implements MemoryFilter {
81             public int numofnodelisteners = 0;
82             public boolean reject(Object JavaDoc obj) {
83                 numofnodelisteners += (obj instanceof NodeListener)?1:0;
84                 return !((obj instanceof EventListenerList JavaDoc) | (obj instanceof Object JavaDoc[]));
85             }
86         }
87         NodeListenerMemoryFilter filter = new NodeListenerMemoryFilter();
88         assertSize("",Arrays.asList( new Object JavaDoc[] {node} ),1000000,filter);
89         assertTrue("Node is expected to have a NodeListener attached", filter.numofnodelisteners > 0);
90         
91         Reference JavaDoc actionref = new WeakReference JavaDoc(sca);
92         sca = null;
93         action = null;
94         assertGC("CookieAction is supposed to be GCed", actionref);
95         
96         NodeListenerMemoryFilter filter2 = new NodeListenerMemoryFilter();
97         assertSize("",Arrays.asList( new Object JavaDoc[] {node} ),1000000,filter2);
98         assertEquals("Node is expected to have no NodeListener attached", 0, filter2.numofnodelisteners);
99     }
100     public static class SimpleCookieAction2 extends CookieAction {
101         protected int mode() {
102             return MODE_EXACTLY_ONE;
103         }
104         protected Class JavaDoc[] cookieClasses() {
105             return new Class JavaDoc[] {OpenCookie.class};
106         }
107         protected void performAction(Node[] activatedNodes) {
108             // do nothing
109
}
110         public String JavaDoc getName() {
111             return "SimpleCookieAction2";
112         }
113         public HelpCtx getHelpCtx() {
114             return null;
115         }
116     }
117     
118     private void doAWT(boolean clone) throws Exception JavaDoc {
119         assertFalse("We should not run in AWT thread", SwingUtilities.isEventDispatchThread());
120         
121         SimpleCookieAction sca = SystemAction.get(SimpleCookieAction.class);
122         
123         CookieNode node = new CookieNode();
124         
125         
126         
127         Action JavaDoc action;
128         if (clone) {
129             action = sca.createContextAwareInstance(node.getLookup());
130             Lkp l = (Lkp)Lkp.getDefault();
131             l.set(Lookup.EMPTY);
132         } else {
133             action = sca;
134             Lkp l = (Lkp)Lkp.getDefault();
135             l.set(node.getLookup());
136         }
137         
138         
139         class L implements PropertyChangeListener JavaDoc, Runnable JavaDoc {
140             public int cnt;
141             public void propertyChange(PropertyChangeEvent JavaDoc ev) {
142                 cnt++;
143                 assertTrue("Change delivered in AWT thread", SwingUtilities.isEventDispatchThread());
144             }
145             public void run() {
146                 
147             }
148         }
149         L l = new L();
150         action.addPropertyChangeListener(l);
151         
152         assertTrue("Is enabled", action.isEnabled());
153         
154         
155         node.enable(false);
156         // just wait for all changes in AWT to be processed
157
SwingUtilities.invokeAndWait(l);
158         
159         assertFalse("Not enabled", action.isEnabled());
160         assertEquals("One change", 1, l.cnt);
161     }
162     public static class SimpleCookieAction extends CookieAction {
163         protected int mode() {
164             return MODE_EXACTLY_ONE;
165         }
166         protected Class JavaDoc[] cookieClasses() {
167             return new Class JavaDoc[] {OpenCookie.class};
168         }
169         protected void performAction(Node[] activatedNodes) {
170             // do nothing
171
}
172         public String JavaDoc getName() {
173             return "SimpleCookieAction";
174         }
175         public HelpCtx getHelpCtx() {
176             return null;
177         }
178     }
179     
180     private static final class CookieNode extends AbstractNode {
181         private Open open;
182         
183         private static final class Open implements OpenCookie {
184             public void open() {
185                 // do nothing
186
}
187         }
188         public CookieNode() {
189             super(Children.LEAF);
190             open = new Open();
191             getCookieSet().add(open);
192         }
193         
194         public void enable(boolean t) {
195             if (t) {
196                 getCookieSet().add(open);
197             } else {
198                 getCookieSet().remove(open);
199             }
200         }
201         
202     }
203     
204     public static final class Lkp extends ProxyLookup
205             implements ContextGlobalProvider {
206         public Lkp() {
207             super(new Lookup[0]);
208             set(Lookup.EMPTY);
209         }
210         
211         public void set(Lookup lkp) {
212             setLookups(new Lookup[] {
213                 lkp,
214                 Lookups.singleton(this)
215             });
216         }
217         
218         public Lookup createGlobalContext() {
219             return this;
220         }
221     }
222 }
223
Popular Tags