1 19 package org.netbeans.core; 20 21 import java.awt.KeyboardFocusManager ; 22 import java.awt.Window ; 23 import java.lang.ref.WeakReference ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.JFrame ; 27 import javax.swing.SwingUtilities ; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.windows.TopComponent; 30 31 35 public class NbClipboardTest extends NbTestCase { 36 37 public NbClipboardTest(String testName) { 38 super(testName); 39 } 40 41 protected void setUp() throws Exception { 42 System.getProperties().remove("netbeans.slow.system.clipboard.hack"); 43 } 44 45 protected void tearDown() throws Exception { 46 } 47 48 public void testDefaultOnJDK15AndLater() { 49 if (System.getProperty("java.version").startsWith("1.4")) { 50 return; 51 } 52 53 NbClipboard ec = new NbClipboard(); 54 assertTrue("By default we still do use slow hacks", ec.slowSystemClipboard); 55 } 56 public void testPropOnJDK15AndLater() { 57 if (System.getProperty("java.version").startsWith("1.4")) { 58 return; 59 } 60 61 System.setProperty("netbeans.slow.system.clipboard.hack", "false"); 62 63 NbClipboard ec = new NbClipboard(); 64 assertFalse("Property overrides default", ec.slowSystemClipboard); 65 assertEquals("sun.awt.datatransfer.timeout is now 1000", "1000", System.getProperty("sun.awt.datatransfer.timeout")); 66 } 67 68 public void testMemoryLeak89844() throws Exception { 69 class Safe implements Runnable { 70 WeakReference <Object > ref; 71 Window w; 72 TopComponent tc; 73 74 75 public void beforeAWT() throws InterruptedException { 76 NbClipboard ec = new NbClipboard(); 77 78 tc = new TopComponent(); 79 tc.open(); 80 81 for(;;) { 82 w = SwingUtilities.getWindowAncestor(tc); 83 if (w != null && w.isVisible()) { 84 break; 85 } 86 Thread.sleep(100); 87 } 88 89 tc.close(); 90 w.dispose(); 91 92 JFrame f = new JFrame ("Focus stealer"); 94 f.setVisible(true); 95 f.pack(); 96 f.toFront(); 97 f.requestFocus(); 98 f.requestFocusInWindow(); 99 } 100 101 public void run() { 102 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner(); 103 104 ref = new WeakReference <Object >(w); 105 w = null; 106 tc = null; 107 } 108 } 109 110 Safe safe = new Safe(); 111 112 safe.beforeAWT(); 113 SwingUtilities.invokeAndWait(safe); 114 115 try { 116 assertGC("Top component can disappear", safe.ref); 117 } catch (junit.framework.AssertionFailedError ex) { 118 if (ex.getMessage().indexOf("NbClipboard") >= 0) { 119 throw ex; 120 } 121 Logger.getAnonymousLogger().log(Level.WARNING, "Cannot do GC, but not due to NbClipboard itself", ex); 122 } 123 } 124 125 private static void waitEQ(final Window w) throws Exception { 126 class R implements Runnable { 127 boolean visible; 128 129 public void run() { 130 visible = w.isShowing(); 131 } 132 } 133 R r = new R(); 134 while (!r.visible) { 135 SwingUtilities.invokeAndWait(r); 136 } 137 } 138 } 139 | Popular Tags |