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: package org.netbeans.core;
042:
043: import java.awt.KeyboardFocusManager;
044: import java.awt.Window;
045: import java.lang.ref.WeakReference;
046: import java.lang.reflect.Field;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import javax.swing.JFrame;
050: import javax.swing.SwingUtilities;
051: import org.netbeans.junit.NbTestCase;
052: import org.openide.util.Utilities;
053: import org.openide.windows.TopComponent;
054:
055: /** Basic tests on NbClipboard
056: *
057: * @author Jaroslav Tulach
058: */
059: public class NbClipboardTest extends NbTestCase {
060:
061: public NbClipboardTest(String testName) {
062: super (testName);
063: }
064:
065: protected void setUp() throws Exception {
066: System.getProperties().remove(
067: "netbeans.slow.system.clipboard.hack");
068: }
069:
070: protected void tearDown() throws Exception {
071: }
072:
073: public void testDefaultOnJDK15AndLater() {
074: if (System.getProperty("java.version").startsWith("1.4")) {
075: return;
076: }
077:
078: NbClipboard ec = new NbClipboard();
079: assertTrue("By default we still do use slow hacks",
080: ec.slowSystemClipboard);
081: }
082:
083: public void testPropOnJDK15AndLater() {
084: if (System.getProperty("java.version").startsWith("1.4")) {
085: return;
086: }
087:
088: System.setProperty("netbeans.slow.system.clipboard.hack",
089: "false");
090:
091: NbClipboard ec = new NbClipboard();
092: assertFalse("Property overrides default",
093: ec.slowSystemClipboard);
094: assertEquals("sun.awt.datatransfer.timeout is now 1000",
095: "1000", System
096: .getProperty("sun.awt.datatransfer.timeout"));
097: }
098:
099: public void testOnMacOSX() throws Exception {
100: String prev = System.getProperty("os.name");
101: try {
102: System.setProperty("os.name", "Darwin");
103: Field f = Class.forName(Utilities.class.getName())
104: .getDeclaredField("operatingSystem");
105: f.setAccessible(true);
106: f.set(null, -1);
107: assertTrue("Is mac", Utilities.isMac());
108:
109: NbClipboard ec = new NbClipboard();
110: assertFalse("MAC seems to have fast clipboard",
111: ec.slowSystemClipboard);
112: } finally {
113: System.setProperty("os.name", prev);
114: }
115: }
116:
117: public void testMemoryLeak89844() throws Exception {
118: class Safe implements Runnable {
119: WeakReference<Object> ref;
120: Window w;
121: TopComponent tc;
122:
123: public void beforeAWT() throws InterruptedException {
124: NbClipboard ec = new NbClipboard();
125:
126: tc = new TopComponent();
127: tc.open();
128:
129: for (;;) {
130: w = SwingUtilities.getWindowAncestor(tc);
131: if (w != null && w.isVisible()) {
132: break;
133: }
134: Thread.sleep(100);
135: }
136:
137: tc.close();
138: w.dispose();
139:
140: // opening new frame shall clear all the AWT references to previous frame
141: JFrame f = new JFrame("Focus stealer");
142: f.setVisible(true);
143: f.pack();
144: f.toFront();
145: f.requestFocus();
146: f.requestFocusInWindow();
147: }
148:
149: public void run() {
150: KeyboardFocusManager.getCurrentKeyboardFocusManager()
151: .clearGlobalFocusOwner();
152:
153: ref = new WeakReference<Object>(w);
154: w = null;
155: tc = null;
156: }
157: }
158:
159: Safe safe = new Safe();
160:
161: safe.beforeAWT();
162: SwingUtilities.invokeAndWait(safe);
163:
164: try {
165: assertGC("Top component can disappear", safe.ref);
166: } catch (junit.framework.AssertionFailedError ex) {
167: if (ex.getMessage().indexOf("NbClipboard") >= 0) {
168: throw ex;
169: }
170: Logger.getAnonymousLogger().log(Level.WARNING,
171: "Cannot do GC, but not due to NbClipboard itself",
172: ex);
173: }
174: }
175:
176: private static void waitEQ(final Window w) throws Exception {
177: class R implements Runnable {
178: boolean visible;
179:
180: public void run() {
181: visible = w.isShowing();
182: }
183: }
184: R r = new R();
185: while (!r.visible) {
186: SwingUtilities.invokeAndWait(r);
187: }
188: }
189: }
|