001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.windows;
043:
044: import java.awt.event.ActionEvent;
045: import java.lang.ref.Reference;
046: import java.lang.ref.WeakReference;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.util.Set;
050: import javax.swing.AbstractAction;
051: import javax.swing.ActionMap;
052: import junit.framework.Test;
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.junit.NbTestSuite;
055: import org.openide.nodes.Node;
056: import org.openide.util.Lookup;
057: import org.openide.util.lookup.AbstractLookup;
058: import org.openide.util.lookup.InstanceContent;
059:
060: /** Tests behaviour of GlobalContextProviderImpl
061: * and its cooperation with activated and current nodes.
062: *
063: * @author Jaroslav Tulach
064: */
065: public class GlobalContextImplTest extends NbTestCase implements
066: org.openide.util.LookupListener {
067: private static Object KEY = new Object();
068:
069: private javax.swing.Action sampleAction = new AbstractActionImpl();
070: private TopComponent tc;
071: private Lookup lookup;
072: private Lookup.Result result;
073: private int cnt;
074:
075: public GlobalContextImplTest(java.lang.String testName) {
076: super (testName);
077: }
078:
079: public static Test suite() {
080: return new NbTestSuite(GlobalContextImplTest.class);
081: //return new GlobalContextImplTest("testRequestVisibleBlinksTheActionMapForAWhileWithOwnComponentAndAction");
082: }
083:
084: @Override
085: protected void setUp() throws Exception {
086: tc = new TopComponent();
087: tc.getActionMap().put(KEY, sampleAction);
088: tc.requestActive();
089:
090: Lookup global = Lookup.getDefault();
091:
092: Object p = global
093: .lookup(org.openide.util.ContextGlobalProvider.class);
094: assertNotNull("There is one", p);
095: assertEquals(
096: "Action context provider is our as well",
097: org.netbeans.modules.openide.windows.GlobalActionContextImpl.class,
098: p.getClass());
099:
100: lookup = org.openide.util.Utilities.actionsGlobalContext();
101: result = lookup.lookup(new Lookup.Template<Node>(Node.class));
102: result.addLookupListener(this );
103: result.allItems();
104: }
105:
106: private void assertActionMap() {
107: ActionMap map = lookup.lookup(ActionMap.class);
108: assertNotNull("Map has to be there", map);
109:
110: javax.swing.Action action = map.get(KEY);
111: assertEquals("It is really our action", sampleAction, action);
112: }
113:
114: public void testCurrentNodes() throws Exception {
115: tc.setActivatedNodes(new Node[] { Node.EMPTY });
116:
117: assertEquals("This fires change", 1, cnt);
118:
119: assertEquals("One item in result", 1, result.allItems().size());
120: Lookup.Item item = (Lookup.Item) result.allItems().iterator()
121: .next();
122: assertEquals("Item should return Node.EMPTY", Node.EMPTY, item
123: .getInstance());
124: assertActionMap();
125:
126: tc.setActivatedNodes(null);
127: assertEquals("One change", 2, cnt);
128:
129: assertEquals("One empty item in result", 1, result.allItems()
130: .size());
131: item = (Lookup.Item) result.allItems().iterator().next();
132: assertEquals("Item should return null", null, item
133: .getInstance());
134: assertEquals("Name is null", "none", item.getId());
135: assertActionMap();
136:
137: tc.setActivatedNodes(new Node[0]);
138: assertEquals("No change", 3, cnt);
139:
140: assertEquals("No items in lookup", 0, result.allItems().size());
141: assertActionMap();
142: }
143:
144: public void testRequestVisibleBlinksTheActionMapForAWhile()
145: throws Exception {
146: doRequestVisibleBlinksTheActionMapForAWhile(new TopComponent());
147: }
148:
149: private void doRequestVisibleBlinksTheActionMapForAWhile(
150: TopComponent my) throws Exception {
151: final org.openide.nodes.Node n = new org.openide.nodes.AbstractNode(
152: org.openide.nodes.Children.LEAF);
153: tc.setActivatedNodes(new Node[] { n });
154:
155: assertActionMap();
156:
157: class L implements org.openide.util.LookupListener {
158: Lookup.Result<ActionMap> res = lookup
159: .lookup(new Lookup.Template<ActionMap>(
160: ActionMap.class));
161: ArrayList<ActionMap> maps = new ArrayList<ActionMap>();
162:
163: public void resultChanged(org.openide.util.LookupEvent ev) {
164: assertEquals("Still only one", 1, res.allItems().size());
165: Lookup.Item<ActionMap> i = res.allItems().iterator()
166: .next();
167: assertNotNull(i);
168:
169: maps.add(i.getInstance());
170:
171: assertNode();
172: }
173:
174: public void assertNode() {
175: assertEquals("The node is available", n, lookup
176: .lookup(Node.class));
177: }
178: }
179: L myListener = new L();
180: assertEquals("One action map", 1, myListener.res.allItems()
181: .size());
182: myListener.assertNode();
183:
184: myListener.res.addLookupListener(myListener);
185:
186: my.requestVisible();
187:
188: if (myListener.maps.size() != 2) {
189: fail("Expected two changes in the ActionMaps: "
190: + myListener.maps);
191: }
192:
193: myListener.assertNode();
194:
195: ActionMap m1 = myListener.maps.get(0);
196: ActionMap m2 = myListener.maps.get(1);
197:
198: assertNull("Our action is not in first map", m1.get(KEY));
199: assertEquals("Our action is in second map", sampleAction, m2
200: .get(KEY));
201:
202: assertActionMap();
203:
204: myListener.res.removeLookupListener(myListener);
205:
206: my.close();
207:
208: // new TopComponent ().open();
209:
210: Reference<Object> ref = new WeakReference<Object>(my);
211: my = null;
212: m1 = null;
213: m2 = null;
214: myListener.maps.clear();
215:
216: //this.tc = null;
217: //this.lookup = null;
218: //this.result = null;
219: //myListener.res = null;
220: //myListener = null;
221: //this.sampleAction = null;
222:
223: assertGC("Can be GCed", ref);
224: }
225:
226: private static class OwnTopComponent extends TopComponent {
227: public OwnTopComponent() {
228: getActionMap().put("ahoj", new OwnAction());
229: }
230:
231: class OwnAction extends AbstractAction {
232:
233: public void actionPerformed(ActionEvent e) {
234: OwnTopComponent.this .open();
235: }
236:
237: }
238: }
239:
240: public void testRequestVisibleBlinksTheActionMapForAWhileWithOwnComponentAndAction()
241: throws Exception {
242: doRequestVisibleBlinksTheActionMapForAWhile(new OwnTopComponent());
243: }
244:
245: public void testComponentChangeActionMapIsPropagatedToGlobalLookup()
246: throws Exception {
247: assertEquals("test1", 0, cnt);
248:
249: InstanceContent ic = new InstanceContent();
250: AbstractLookup al = new AbstractLookup(ic);
251: tc = new TopComponent(al);
252:
253: assertEquals("test2", 0, cnt);
254:
255: ActionMap myMap = new ActionMap();
256: myMap.put(KEY, sampleAction);
257: assertEquals("test3", 0, cnt);
258:
259: tc.requestActive();
260:
261: assertEquals("test4", 1, cnt);
262:
263: result = lookup.lookup(new Lookup.Template<ActionMap>(
264: ActionMap.class));
265: result.addLookupListener(this );
266: result.allItems();
267:
268: assertEquals("test5", 1, cnt);
269:
270: ic.set(Collections.singleton(new ActionMap()), null);
271:
272: assertEquals("One change in ActiomMap delivered", 2, cnt);
273: }
274:
275: public void resultChanged(org.openide.util.LookupEvent ev) {
276: cnt++;
277: }
278:
279: private static class AbstractActionImpl extends AbstractAction {
280:
281: private AbstractActionImpl() {
282: }
283:
284: public void actionPerformed(java.awt.event.ActionEvent ev) {
285: }
286: }
287:
288: }
|