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.KeyEvent;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.io.ObjectInputStream;
048: import java.lang.ref.Reference;
049: import java.lang.ref.WeakReference;
050: import java.util.Collections;
051: import java.util.logging.Level;
052: import javax.swing.Action;
053: import javax.swing.JComponent;
054: import javax.swing.KeyStroke;
055: import javax.swing.SwingUtilities;
056: import javax.swing.text.Keymap;
057: import junit.framework.TestCase;
058: import org.netbeans.junit.Log;
059: import org.netbeans.junit.MockServices;
060: import org.netbeans.junit.NbTestCase;
061: import org.openide.nodes.Node;
062: import org.openide.util.HelpCtx;
063: import org.openide.util.Lookup;
064: import org.openide.util.actions.CookieAction;
065: import org.openide.util.io.NbMarshalledObject;
066:
067: /**
068: *
069: * @author mkleint
070: */
071: public class TopComponentTest extends NbTestCase {
072: static {
073: System.setProperty(
074: "org.openide.windows.DummyWindowManager.VISIBLE",
075: "false");
076: }
077:
078: public TopComponentTest(String testName) {
079: super (testName);
080: }
081:
082: /**
083: * Test of readExternal method, of class org.openide.windows.TopComponent.
084: */
085: public void testReadExternal() throws Exception {
086: // first try tc with displayname
087: TopComponent tc = new TopComponent();
088: tc.setName("testName");
089: tc.setDisplayName("testdisplayName");
090: tc.setToolTipText("testTooltip");
091: NbMarshalledObject obj = new NbMarshalledObject(tc);
092: tc.close();
093:
094: tc = (TopComponent) obj.get();
095: assertNotNull("One again", tc);
096: assertEquals("testName", tc.getName());
097: assertEquals("testTooltip", tc.getToolTipText());
098: assertEquals("testdisplayName", tc.getDisplayName());
099:
100: // now try tc withOUT displayname
101: tc = new TopComponent();
102: tc.setName("testName");
103: tc.setToolTipText("testTooltip");
104: obj = new NbMarshalledObject(tc);
105: tc.close();
106:
107: tc = (TopComponent) obj.get();
108: assertNotNull("One again", tc);
109: assertEquals("testName", tc.getName());
110: assertEquals("testTooltip", tc.getToolTipText());
111: assertNull(tc.getDisplayName());
112:
113: }
114:
115: /**
116: * Test of readExternal method, of class org.openide.windows.TopComponent.
117: */
118: public void testOldReadExternal() throws Exception {
119: TopComponent tc = null;
120: try {
121: ObjectInputStream stream = new ObjectInputStream(getClass()
122: .getResourceAsStream(
123: "data/oldTcWithoutDisplayName.ser"));
124: tc = (TopComponent) stream.readObject();
125: stream.close();
126: } catch (Exception exc) {
127: exc.printStackTrace();
128: fail("Cannot read tc");
129: }
130:
131: assertNotNull("One again", tc);
132: assertEquals("testName", tc.getName());
133: assertEquals("testTooltip", tc.getToolTipText());
134: assertEquals(
135: "If the old component does not have a display name, then keep it null",
136: null, tc.getDisplayName());
137: }
138:
139: TopComponent tcOpened = null;
140: TopComponent tcClosed = null;
141:
142: public void testOpenedClosed() throws Exception {
143: System.out
144: .println("Testing property firing of TopComponent's registry");
145: tcOpened = null;
146: tcClosed = null;
147:
148: TopComponent.getRegistry().addPropertyChangeListener(
149: new PropertyChangeListener() {
150: public void propertyChange(PropertyChangeEvent evt) {
151: if (TopComponent.Registry.PROP_TC_OPENED
152: .equals(evt.getPropertyName())) {
153: tcOpened = (TopComponent) evt.getNewValue();
154: }
155: if (TopComponent.Registry.PROP_TC_CLOSED
156: .equals(evt.getPropertyName())) {
157: tcClosed = (TopComponent) evt.getNewValue();
158: }
159: }
160: });
161:
162: TopComponent tc = new TopComponent();
163:
164: tc.open();
165: assertNotNull(
166: "Property change was not fired, tcOpened is null",
167: tcOpened);
168: assertEquals("New value in property change is wrong", tc,
169: tcOpened);
170:
171: tc.close();
172: assertNotNull(
173: "Property change was not fired, tcClosed is null",
174: tcClosed);
175: assertEquals("New value in property change is wrong", tc,
176: tcClosed);
177:
178: }
179:
180: public void testToStringOfDelegateContainsNameOfOriginalAction()
181: throws Exception {
182: SaveAction sa = SaveAction.get(SaveAction.class);
183: Action a = sa.createContextAwareInstance(Lookup.EMPTY);
184: if (a.toString().indexOf("SaveAction") == -1) {
185: fail("We need name of the original action:\n"
186: + a.toString());
187: }
188:
189: CharSequence log = Log.enable("org.netbeans.ui", Level.FINER);
190:
191: final TopComponent tc = new TopComponent();
192: tc.getActionMap().put("A", a);
193: final KeyEvent ke = new KeyEvent(tc, KeyEvent.KEY_PRESSED,
194: System.currentTimeMillis(), KeyEvent.CTRL_MASK, 0, 'S');
195: final KeyStroke ks = KeyStroke.getKeyStrokeForEvent(ke);
196: tc.getInputMap().put(ks, a);
197: MockServices.setServices(MyKM.class);
198: SwingUtilities.invokeAndWait(new Runnable() {
199:
200: public void run() {
201: tc.processKeyBinding(ks, ke,
202: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
203: true);
204: }
205:
206: });
207:
208: if (log.toString().indexOf("SaveAction") == -1) {
209: fail(log.toString());
210: }
211: }
212:
213: public void testCanGCTopComponentWhenItsActionMapIsHeld()
214: throws Exception {
215: TopComponent tc = new TopComponent();
216:
217: Object map = tc.getActionMap();
218: Reference<Object> ref = new WeakReference<Object>(tc);
219: tc = null;
220:
221: assertGC("TC can disappear", ref, Collections.singleton(map));
222: }
223:
224: public static final class SaveAction extends CookieAction {
225: static int cnt;
226:
227: protected void performAction(Node[] activatedNodes) {
228: cnt++;
229: }
230:
231: protected boolean enable(Node[] activatedNodes) {
232: return true;
233: }
234:
235: public String getName() {
236: return "FakeName";
237: }
238:
239: public HelpCtx getHelpCtx() {
240: return HelpCtx.DEFAULT_HELP;
241: }
242:
243: protected int mode() {
244: return 0;
245: }
246:
247: protected Class<?>[] cookieClasses() {
248: return new Class[0];
249: }
250:
251: }
252:
253: public static final class MyKM implements Keymap {
254:
255: public String getName() {
256: throw new UnsupportedOperationException(
257: "Not supported yet.");
258: }
259:
260: public Action getDefaultAction() {
261: throw new UnsupportedOperationException(
262: "Not supported yet.");
263: }
264:
265: public void setDefaultAction(Action a) {
266: throw new UnsupportedOperationException(
267: "Not supported yet.");
268: }
269:
270: public Action getAction(KeyStroke key) {
271: return SaveAction.get(SaveAction.class);
272: }
273:
274: public KeyStroke[] getBoundKeyStrokes() {
275: throw new UnsupportedOperationException(
276: "Not supported yet.");
277: }
278:
279: public Action[] getBoundActions() {
280: throw new UnsupportedOperationException(
281: "Not supported yet.");
282: }
283:
284: public KeyStroke[] getKeyStrokesForAction(Action a) {
285: throw new UnsupportedOperationException(
286: "Not supported yet.");
287: }
288:
289: public boolean isLocallyDefined(KeyStroke key) {
290: throw new UnsupportedOperationException(
291: "Not supported yet.");
292: }
293:
294: public void addActionForKeyStroke(KeyStroke key, Action a) {
295: throw new UnsupportedOperationException(
296: "Not supported yet.");
297: }
298:
299: public void removeKeyStrokeBinding(KeyStroke keys) {
300: throw new UnsupportedOperationException(
301: "Not supported yet.");
302: }
303:
304: public void removeBindings() {
305: throw new UnsupportedOperationException(
306: "Not supported yet.");
307: }
308:
309: public Keymap getResolveParent() {
310: throw new UnsupportedOperationException(
311: "Not supported yet.");
312: }
313:
314: public void setResolveParent(Keymap parent) {
315: throw new UnsupportedOperationException(
316: "Not supported yet.");
317: }
318:
319: }
320: }
|