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.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.Collections;
048: import javax.swing.AbstractAction;
049: import javax.swing.ActionMap;
050: import org.netbeans.junit.NbTestCase;
051: import org.openide.nodes.Node;
052: import org.openide.util.Lookup;
053: import org.openide.util.lookup.AbstractLookup;
054: import org.openide.util.lookup.InstanceContent;
055:
056: /**
057: * Checks behaviour of a bridge that listens on a provided lookup
058: * and initializes activated nodes according to the nodes in the
059: * lookup.
060: *
061: * @author Jaroslav Tulach, Jesse Glick
062: */
063: public final class TopComponentLookupToNodesBridge extends NbTestCase {
064: /** own action map */
065: protected ActionMap map;
066: /** top component we work on */
067: protected TopComponent top;
068: /** instance in the lookup */
069: protected InstanceContent ic;
070: /** its lookup */
071: protected Lookup lookup;
072:
073: public TopComponentLookupToNodesBridge(String testName) {
074: super (testName);
075: }
076:
077: protected boolean runInEQ() {
078: return true;
079: }
080:
081: /** Setup component with lookup.
082: */
083: protected void setUp() {
084: System.setProperty("org.openide.util.Lookup", "-");
085:
086: map = new ActionMap();
087: ic = new InstanceContent();
088: ic.add(map);
089:
090: lookup = new AbstractLookup(ic);
091: top = new TopComponent(lookup);
092: }
093:
094: public void testTheLookupIsReturned() {
095: assertEquals(
096: "Lookup provided to TC in constructor is returned",
097: lookup, top.getLookup());
098: }
099:
100: public void testActionMapIsTakenFromTheLookupIfProvided() {
101: Action a1 = new Action();
102: map.put("key", a1);
103:
104: assertEquals("Action map is set", a1, top.getActionMap().get(
105: "key"));
106:
107: ActionMap another = new ActionMap();
108:
109: ic.set(Collections.singleton(another), null);
110: assertEquals(
111: "And is not changed (right now) if modified in list",
112: a1, top.getActionMap().get("key"));
113: }
114:
115: public void testEmptyLookupGeneratesZeroLengthArray() {
116: assertNotNull("Array is there", top.getActivatedNodes());
117: assertEquals("No nodes", 0, top.getActivatedNodes().length);
118: }
119:
120: public void testNodeIsThereIfInLookup() {
121: class Listener implements PropertyChangeListener {
122: public int cnt;
123: public String name;
124:
125: public void propertyChange(PropertyChangeEvent ev) {
126: cnt++;
127: name = ev.getPropertyName();
128: }
129: }
130:
131: Listener l = new Listener();
132: top.addPropertyChangeListener(l);
133:
134: ic.add(Node.EMPTY);
135:
136: assertNotNull("Array exists", top.getActivatedNodes());
137: assertEquals("One node", 1, top.getActivatedNodes().length);
138: assertEquals("The node", Node.EMPTY, top.getActivatedNodes()[0]);
139: assertEquals("One PCE", 1, l.cnt);
140: assertEquals("Name of property", "activatedNodes", l.name);
141:
142: ic.set(Collections.nCopies(2, Node.EMPTY), null);
143:
144: assertEquals("Two nodes", 2, top.getActivatedNodes().length);
145: assertEquals("The same", Node.EMPTY, top.getActivatedNodes()[0]);
146: assertEquals("The same", Node.EMPTY, top.getActivatedNodes()[1]);
147: assertEquals("second PCE change", 2, l.cnt);
148: assertEquals("Name of property", "activatedNodes", l.name);
149:
150: }
151:
152: private static final class Action extends AbstractAction {
153: public void actionPerformed(ActionEvent e) {
154: }
155: }
156: }
|