1 19 20 package org.openide.util.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.cookies.OpenCookie; 29 import org.openide.nodes.AbstractNode; 30 import org.openide.nodes.Children; 31 import org.openide.nodes.FilterNode; 32 import org.openide.nodes.Node; 33 import org.openide.util.HelpCtx; 34 35 39 public class CookieActionIsTooSlowTest extends NbTestCase implements PropertyChangeListener { 40 41 public CookieActionIsTooSlowTest(String name) { 42 super(name); 43 } 44 45 private SimpleCookieAction a1; 46 private Node[] arr; 47 private int propertyChange; 48 49 protected void setUp() throws Exception { 50 a1 = SystemAction.get(SimpleCookieAction.class); 51 a1.addPropertyChangeListener(this); 52 int count = 10; 53 arr = new Node[count]; 54 for (int i = 0; i < count; i++) { 55 arr[i] = new FilterNode(new CookieNode("n" + i)); 56 } 57 } 58 59 protected void tearDown() throws Exception { 60 a1.removePropertyChangeListener(this); 61 } 62 63 67 protected boolean runInEQ() { 68 return true; 69 } 70 71 public void propertyChange(PropertyChangeEvent ev) { 72 propertyChange++; 73 } 74 75 public void testSelectionOfMoreNodesMakesToManyCallsToActionEnableMethodIssue40734() throws Exception { 76 77 assertFalse("No nodes are enabled", a1.isEnabled()); 78 assertEquals("One call to enabled method", 1, a1.queried); 79 a1.queried = 0; 80 81 ActionsInfraHid.setCurrentNodes(arr); 82 83 assertTrue("All nodes have open cookie", a1.isEnabled()); 84 assertEquals("The enable method has been called once", 1, a1.queried); 85 86 assertEquals("Listener changed once", 1, propertyChange); 87 } 88 89 90 public static class SimpleCookieAction extends CookieAction { 91 private int queried; 92 93 protected int mode() { 94 return MODE_ALL; 95 } 96 97 98 protected Class [] cookieClasses() { 99 return new Class [] {OpenCookie.class}; 100 } 101 public static final List runOn = new ArrayList (); protected void performAction(Node[] activatedNodes) { 103 runOn.add(Arrays.asList(activatedNodes)); 104 } 105 public String getName() { 106 return "SimpleCookieAction"; 107 } 108 public HelpCtx getHelpCtx() { 109 return null; 110 } 111 protected boolean asynchronous() { 112 return false; 113 } 114 115 protected boolean enable(Node[] activatedNodes) { 116 queried++; 117 118 boolean retValue = super.enable(activatedNodes); 119 return retValue; 120 } 121 122 } 123 124 private static final class CookieNode extends AbstractNode { 125 private static final class Open implements OpenCookie { 126 public void open() { 127 } 129 } 130 public CookieNode(String name) { 131 super(Children.LEAF); 132 getCookieSet().add(new Open()); 133 setName(name); 134 } 135 public void setHasCookie(boolean b) { 136 if (b && getCookie(OpenCookie.class) == null) { 137 getCookieSet().add(new Open()); 138 } else if (!b) { 139 OpenCookie o = getCookie(OpenCookie.class); 140 if (o != null) { 141 getCookieSet().remove(o); 142 } 143 } 144 } 145 } 146 147 148 } 149 150 | Popular Tags |