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.util;
043:
044: import java.lang.ref.*;
045: import javax.swing.Action;
046: import javax.swing.JMenuItem;
047:
048: import junit.framework.*;
049:
050: import org.netbeans.junit.*;
051:
052: /** Tests of actions related methods in Utilities class.
053: */
054: public class UtilitiesActionsTest extends NbTestCase {
055:
056: public UtilitiesActionsTest(java.lang.String testName) {
057: super (testName);
058: }
059:
060: public static void main(java.lang.String[] args) {
061: junit.textui.TestRunner.run(new NbTestSuite(
062: UtilitiesActionsTest.class));
063: }
064:
065: public void testActionClone() throws Exception {
066: CloneAction original = new CloneAction();
067:
068: javax.swing.Action clone = original
069: .createContextAwareInstance(Lookup.EMPTY);
070:
071: assertTrue("Clone is instance of desired class",
072: clone instanceof CloneAction);
073: assertNull("Original has empty lookup", original.lookup);
074: assertEquals("Clone has lookup assigned", Lookup.EMPTY,
075: ((CloneAction) clone).lookup);
076: }
077:
078: public void testActionToPopupForLookup() throws Exception {
079: class MyAction extends CloneAction implements
080: org.openide.util.actions.Presenter.Popup {
081: public JMenuItem item = new JMenuItem("Ahoj");
082: public int called;
083:
084: protected CloneAction cloneAction() {
085: return new MyAction();
086: }
087:
088: public JMenuItem getPopupPresenter() {
089: called++;
090: return item;
091: }
092: }
093:
094: final CloneAction[] arr = { new CloneAction(), new MyAction() };
095:
096: final javax.swing.JPopupMenu menu = org.openide.util.Utilities
097: .actionsToPopup(arr, Lookup.EMPTY);
098:
099: assertEquals("Presenter called", 1,
100: ((MyAction) arr[1].clone).called);
101: assertEquals("Lookup is fine",
102: ((CloneAction) arr[0].clone).lookup, Lookup.EMPTY);
103: assertEquals("The other as well",
104: ((CloneAction) arr[1].clone).lookup, Lookup.EMPTY);
105:
106: // We need to do this stuff in AWT; cf. Actions.Bridge.propertyChange.
107: final AssertionFailedError[] err = new AssertionFailedError[1];
108: Mutex.EVENT.readAccess(new Runnable() {
109: public void run() {
110: try {
111:
112: javax.swing.MenuElement[] elem = menu
113: .getSubElements();
114: assertEquals("Two subitems", 2, elem.length);
115:
116: // Otherwise there may be no listener:
117: elem[0].getComponent().addNotify();
118:
119: arr[0].clone.setEnabled(false);
120: assertTrue("Menu item is disabled",
121: !((JMenuItem) elem[0].getComponent())
122: .isEnabled());
123: arr[0].clone.setEnabled(true);
124: assertTrue("Menu item is enabled",
125: ((JMenuItem) elem[0].getComponent())
126: .isEnabled());
127:
128: assertEquals("The presenter is really used",
129: ((MyAction) arr[1].clone).item, elem[1]
130: .getComponent());
131:
132: } catch (AssertionFailedError e) {
133: err[0] = e;
134: }
135: }
136: });
137: if (err[0] != null)
138: throw err[0];
139: }
140:
141: public void testActionsToPopupFromComponent() throws Exception {
142: Lookup lookup = org.openide.util.lookup.Lookups.singleton(this );
143: final Lookup[] returnedLookup = { lookup };
144:
145: class Provider extends javax.swing.JComponent implements
146: Lookup.Provider {
147: public Lookup getLookup() {
148: return returnedLookup[0];
149: }
150: }
151:
152: CloneAction sample = new CloneAction();
153:
154: javax.swing.JComponent outerMost = new javax.swing.JPanel();
155: outerMost.getActionMap().put("1", sample);
156:
157: javax.swing.JComponent provider = new Provider();
158: outerMost.add(provider);
159: provider.getActionMap().put("2", sample);
160:
161: javax.swing.JComponent child = new javax.swing.JPanel();
162: provider.add(child);
163: child.getActionMap().put("3", sample);
164: javax.swing.JComponent menuOwner = new javax.swing.JPanel();
165: child.add(menuOwner);
166: menuOwner.getActionMap().put("4", sample);
167:
168: javax.swing.JComponent sibling = new javax.swing.JPanel();
169: provider.add(child);
170: sibling.getActionMap().put("5", sample);
171:
172: CloneAction[] arr = { new CloneAction() };
173:
174: javax.swing.JPopupMenu menu = org.openide.util.Utilities
175: .actionsToPopup(arr, menuOwner);
176:
177: Lookup actionLookup = arr[0].clone.lookup;
178: assertNotNull("Clone lookup is not null", actionLookup);
179: assertEquals(
180: "The lookup returned by 'provider' is assigned to the clonned actions",
181: this , actionLookup.lookup(this .getClass()));
182:
183: // Now try it without a (valid) Lookup.Provider in the component hierarchy.
184: returnedLookup[0] = null;
185: arr[0].clone = null;
186: menu = org.openide.util.Utilities
187: .actionsToPopup(arr, menuOwner);
188:
189: actionLookup = arr[0].clone.lookup;
190: assertNotNull("Clone lookup is not null", actionLookup);
191:
192: javax.swing.ActionMap map = (javax.swing.ActionMap) actionLookup
193: .lookup(javax.swing.ActionMap.class);
194: assertNotNull("Action map is in the lookup", map);
195: assertNull(
196: "ActionMap of parent of Lookup.Provider is not included",
197: map.get("1"));
198: assertNotNull("ActionMap of Lookup.Provider is included", map
199: .get("2"));
200: assertNotNull("So is the child's one", map.get("3"));
201: assertNotNull("And also of the menuOwner", map.get("4"));
202: assertNull("But of course the sibling's one is not", map
203: .get("5"));
204: }
205:
206: class CloneAction extends javax.swing.AbstractAction implements
207: ContextAwareAction {
208: public Lookup lookup;
209: public CloneAction clone;
210:
211: public void actionPerformed(java.awt.event.ActionEvent ev) {
212: }
213:
214: protected CloneAction cloneAction() {
215: return new CloneAction();
216: }
217:
218: public Action createContextAwareInstance(Lookup actionContext) {
219: assertNull("We support just one clone right now", clone);
220: clone = cloneAction();
221: clone.lookup = actionContext;
222: return clone;
223: }
224:
225: } // end of CloneAction
226:
227: }
|