1 19 20 package org.openide.util.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.Reference ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Arrays ; 27 import javax.swing.Action ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.event.EventListenerList ; 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 47 public class CookieAction2Test extends NbTestCase { 48 49 public CookieAction2Test(String 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 { 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 { 69 doAWT(true); 70 } 71 public void testChangesOfCookiesPossibleFromNonAWTThreadWithGlobalActionIssue40937() throws Exception { 72 doAWT(false); 73 } 74 75 public void testNodeListenersDetachedAtFinalizeIssue58065() throws Exception { 76 CookieNode node = new CookieNode(); 77 SimpleCookieAction2 sca = new SimpleCookieAction2(); 78 Action action = sca.createContextAwareInstance(node.getLookup()); 79 80 class NodeListenerMemoryFilter implements MemoryFilter { 81 public int numofnodelisteners = 0; 82 public boolean reject(Object obj) { 83 numofnodelisteners += (obj instanceof NodeListener)?1:0; 84 return !((obj instanceof EventListenerList ) | (obj instanceof Object [])); 85 } 86 } 87 NodeListenerMemoryFilter filter = new NodeListenerMemoryFilter(); 88 assertSize("",Arrays.asList( new Object [] {node} ),1000000,filter); 89 assertTrue("Node is expected to have a NodeListener attached", filter.numofnodelisteners > 0); 90 91 Reference actionref = new WeakReference (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 [] {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 [] cookieClasses() { 105 return new Class [] {OpenCookie.class}; 106 } 107 protected void performAction(Node[] activatedNodes) { 108 } 110 public String getName() { 111 return "SimpleCookieAction2"; 112 } 113 public HelpCtx getHelpCtx() { 114 return null; 115 } 116 } 117 118 private void doAWT(boolean clone) throws Exception { 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 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 , Runnable { 140 public int cnt; 141 public void propertyChange(PropertyChangeEvent 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 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 [] cookieClasses() { 167 return new Class [] {OpenCookie.class}; 168 } 169 protected void performAction(Node[] activatedNodes) { 170 } 172 public String 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 } 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 |