KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
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 /** Simulation for bug 40734.
36  *
37  * @author Jaroslav Tulach
38  */

39 public class CookieActionIsTooSlowTest extends NbTestCase implements PropertyChangeListener JavaDoc {
40     
41     public CookieActionIsTooSlowTest(String JavaDoc 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 JavaDoc {
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 JavaDoc {
60         a1.removePropertyChangeListener(this);
61     }
62     
63     /**
64      * in order to run in awt event queue
65      * fix for #39789
66      */

67     protected boolean runInEQ() {
68         return true;
69     }
70     
71     public void propertyChange(PropertyChangeEvent JavaDoc ev) {
72         propertyChange++;
73     }
74     
75     public void testSelectionOfMoreNodesMakesToManyCallsToActionEnableMethodIssue40734() throws Exception JavaDoc {
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 JavaDoc[] cookieClasses() {
99             return new Class JavaDoc[] {OpenCookie.class};
100         }
101         public static final List JavaDoc runOn = new ArrayList JavaDoc(); // List<List<Node>>
102
protected void performAction(Node[] activatedNodes) {
103             runOn.add(Arrays.asList(activatedNodes));
104         }
105         public String JavaDoc 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                 // do nothing
128
}
129         }
130         public CookieNode(String JavaDoc 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