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-2007 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.netbeans.core;
043:
044: import java.awt.event.ActionEvent;
045: import javax.swing.SwingUtilities;
046: import org.netbeans.core.actions.GlobalPropertiesAction;
047: import org.netbeans.junit.NbTestCase;
048: import org.openide.nodes.AbstractNode;
049: import org.openide.nodes.Children;
050: import org.openide.nodes.Node;
051: import org.openide.windows.TopComponent;
052:
053: /**
054: *
055: * @author Jaroslav Tulach
056: */
057: public class NbSheetTest extends NbTestCase {
058: static {
059: System.setProperty(
060: "org.openide.windows.DummyWindowManager.VISIBLE",
061: "false");
062: }
063:
064: NbSheet s;
065: GlobalPropertiesAction a;
066: TopComponent tc;
067:
068: public NbSheetTest(String testName) {
069: super (testName);
070: }
071:
072: protected void setUp() throws Exception {
073: super .setUp();
074: s = NbSheet.findDefault();
075: assertNotNull("Sheet found", s);
076: assertFalse("Not yet visible", s.isShowing());
077: a = GlobalPropertiesAction.get(GlobalPropertiesAction.class);
078: tc = new TopComponent();
079: }
080:
081: protected void tearDown() throws Exception {
082: super .tearDown();
083: }
084:
085: public void testIssue97069EgUseSetActivatedNodesNull()
086: throws Exception {
087: class Empty implements Runnable {
088: public void run() {
089: }
090: }
091: Empty empty = new Empty();
092:
093: class R implements Runnable {
094: N node = new N("node1");
095:
096: public void run() {
097: tc.setActivatedNodes(new Node[] { node });
098: tc.open();
099: tc.requestActive();
100:
101: assertTrue("action enabled", a.isEnabled());
102: a.actionPerformed(new ActionEvent(a, 0, ""));
103: }
104: }
105: R activate = new R();
106: SwingUtilities.invokeAndWait(activate);
107: SwingUtilities.invokeAndWait(empty);
108:
109: for (int i = 0; i < 5; i++) {
110: if (s == TopComponent.getRegistry().getActivated()) {
111: break;
112: }
113: Thread.sleep(500);
114: }
115: assertEquals("sheet activated", s, TopComponent.getRegistry()
116: .getActivated());
117: assertEquals("One node displayed", 1, s.getNodes().length);
118: assertEquals("it is node", activate.node, s.getNodes()[0]);
119: assertEquals("No activated nodes", null, s.getActivatedNodes());
120:
121: s.close();
122:
123: final N another = new N("another");
124:
125: tc.setActivatedNodes(new Node[] { another });
126: tc.requestActive();
127:
128: class R2 implements Runnable {
129: public void run() {
130: assertTrue("action enabled", a.isEnabled());
131: a.actionPerformed(new ActionEvent(a, 0, ""));
132: }
133: }
134: R2 anotherAct = new R2();
135: SwingUtilities.invokeAndWait(anotherAct);
136: SwingUtilities.invokeAndWait(empty);
137:
138: for (int i = 0; i < 5; i++) {
139: if (s == TopComponent.getRegistry().getActivated()) {
140: break;
141: }
142: Thread.sleep(500);
143: }
144: assertEquals("sheet activated another time", s, TopComponent
145: .getRegistry().getActivated());
146:
147: assertEquals("One node displayed", 1, s.getNodes().length);
148: assertEquals("it is another", another, s.getNodes()[0]);
149: assertEquals("No activated nodes", null, s.getActivatedNodes());
150:
151: }
152:
153: private static final class N extends AbstractNode {
154: public N(String n) {
155: super (Children.LEAF);
156: setName(n);
157: }
158: }
159:
160: public void testMemoryLeakIssue125057() throws Exception {
161: NbSheet sheet = NbSheet.getDefault();
162: SwingUtilities.invokeAndWait(new Runnable() {
163: public void run() {
164: N node = new N("node1");
165: tc.setActivatedNodes(new Node[] { node });
166: tc.open();
167: tc.requestActive();
168:
169: NbSheet sheet = NbSheet.getDefault();
170: sheet.open();
171: Node[] activated = sheet.getNodes();
172: assertNotNull(activated);
173: assertEquals(activated.length, 1);
174: assertEquals(activated[0], node);
175: sheet.close();
176:
177: }
178: });
179: assertEquals(
180: "PropertySheet still holds activated nodes after closing",
181: 0, sheet.getNodes().length);
182: }
183: }
|