1 19 20 package org.netbeans.core; 21 22 import java.awt.datatransfer.ClipboardOwner ; 23 import java.awt.datatransfer.StringSelection ; 24 import java.awt.datatransfer.Transferable ; 25 import junit.textui.TestRunner; 26 import org.netbeans.junit.MockServices; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.util.Lookup; 29 import org.openide.util.datatransfer.ExClipboard; 30 31 35 public class NbClipboardIsUsedBySwingComponentsTest extends NbTestCase { 36 private Clip clip; 37 private javax.swing.JTextField field; 38 39 public NbClipboardIsUsedBySwingComponentsTest (String name) { 40 super(name); 41 } 42 43 protected void setUp() throws Exception { 44 MockServices.setServices(Clip.class); 45 System.setProperty ("netbeans.security.nocheck", "true"); 46 Object clip = Lookup.getDefault ().lookup (ExClipboard.class); 47 assertNotNull ("Some clipboard found", clip); 48 assertEquals ("Correct clipboard found", Clip.class, clip.getClass()); 49 this.clip = (Clip)clip; 50 51 if (System.getSecurityManager () == null) { 52 java.text.NumberFormat.getInstance (); 53 54 Object clazz = org.netbeans.TopSecurityManager.class; 55 SecurityManager m = new org.netbeans.TopSecurityManager (); 56 System.setSecurityManager (m); 57 58 inMiddleOfSettingUpTheManager(); 59 60 org.netbeans.TopSecurityManager.makeSwingUseSpecialClipboard (this.clip); 61 } else { 62 inMiddleOfSettingUpTheManager(); 63 } 64 65 field = new javax.swing.JTextField (); 66 } 67 protected boolean runInEQ () { 68 return true; 69 } 70 71 protected javax.swing.JTextField getField () { 72 return field; 73 } 74 75 76 public void testClipboardOurClipboardUsedDuringCopy () { 77 javax.swing.JTextField f = getField (); 78 f.setText ("Ahoj"); 79 f.selectAll (); 80 assertEquals ("Ahoj", f.getSelectedText ()); 81 f.copy (); 82 83 Clip.assertCalls ("Copy should call setContent", 1, 0); 84 assertClipboard ("Ahoj"); 85 } 86 87 public void testClipboardOurClipboardUsedDuringCut () { 88 javax.swing.JTextField f = getField (); 89 f.setText ("DoCut"); 90 f.selectAll (); 91 assertEquals ("DoCut", f.getSelectedText ()); 92 f.cut (); 93 94 Clip.assertCalls ("Cut should call setContent", 1, 0); 95 assertClipboard ("DoCut"); 96 97 assertEquals ("Empty", "", f.getText ()); 98 } 99 100 public void testClipboardOurClipboardUsedDuringPaste () { 101 javax.swing.JTextField f = getField (); 102 103 StringSelection sel = new StringSelection ("DoPaste"); 104 clip.setContents (sel, sel); 105 Clip.assertCalls ("Of course there is one set", 1, 0); 106 107 assertClipboard ("DoPaste"); 108 f.paste (); 109 110 Clip.assertCalls ("Paste should call getContent", 0, 1); 111 assertEquals ("Text is there", "DoPaste", f.getText ()); 112 } 113 114 public void testCopyFromEditorPasteToTheSameOneIssue40785 () { 115 javax.swing.JTextField f = getField (); 116 f.setText (getName ()); 117 f.selectAll (); 118 assertEquals ("Selection is correct", getName (), f.getSelectedText ()); 119 f.copy (); 120 Clip.assertCalls ("Once in, none out", 1, 0); 121 f.setText (""); 122 f.paste (); 123 Clip.assertCalls ("Once out, none in", 0, 1); 124 125 assertEquals ("Test is again the same", getName (), f.getText ()); 126 } 127 128 public void testItIsStillPossibleToGetTheClipboardForNormalCode () throws Exception { 129 assertNotNull ( 130 java.awt.Toolkit.getDefaultToolkit ().getSystemClipboard () 131 ); 132 } 133 134 public void assertClipboard (String text) { 135 try { 136 Transferable t = clip.getContentsSuper (this); 137 Object obj = t.getTransferData (java.awt.datatransfer.DataFlavor.stringFlavor); 138 assertEquals ("Clipboard is the same", text, obj); 139 } catch (java.io.IOException ex) { 140 fail (ex.getMessage ()); 141 } catch (java.awt.datatransfer.UnsupportedFlavorException ex) { 142 fail (ex.getMessage ()); 143 } 144 } 145 146 protected void inMiddleOfSettingUpTheManager() { 147 } 148 149 public static final class Clip extends ExClipboard { 150 private static int setContents; 151 private static int getContents; 152 153 public Clip () { 154 super ("Clip"); 155 } 156 157 protected ExClipboard.Convertor[] getConvertors () { 158 return new ExClipboard.Convertor[0]; 159 } 160 161 public void setContents (Transferable contents, ClipboardOwner owner) { 162 super.setContents (contents, owner); 163 setContents++; 164 } 165 166 public Transferable getContents (Object requestor) { 167 Transferable retValue; 168 getContents++; 169 retValue = super.getContents (requestor); 170 return retValue; 171 } 172 public Transferable getContentsSuper (Object requestor) { 173 return super.getContents (requestor); 174 } 175 176 public static void assertCalls (String msg, int setContents, int getContents) { 177 if (setContents != -1) assertEquals (msg + " setContents", setContents, Clip.setContents); 178 if (getContents != -1) assertEquals (msg + " getContents", getContents, Clip.getContents); 179 180 Clip.setContents = 0; 181 Clip.getContents = 0; 182 } 183 } } 185 | Popular Tags |