KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NbClipboardIsUsedBySwingComponentsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.awt.datatransfer.ClipboardOwner JavaDoc;
23 import java.awt.datatransfer.StringSelection JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
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 /** Test that verifies that Clipboard is used by swing components.
32  * @author Jaroslav Tulach
33  * @see "#40693"
34  */

35 public class NbClipboardIsUsedBySwingComponentsTest extends NbTestCase {
36     private Clip clip;
37     private javax.swing.JTextField JavaDoc field;
38     
39     public NbClipboardIsUsedBySwingComponentsTest (String JavaDoc name) {
40         super(name);
41     }
42     
43     protected void setUp() throws Exception JavaDoc {
44         MockServices.setServices(Clip.class);
45         System.setProperty ("netbeans.security.nocheck", "true");
46         Object JavaDoc 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 JavaDoc clazz = org.netbeans.TopSecurityManager.class;
55             SecurityManager JavaDoc 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 JavaDoc ();
66     }
67     protected boolean runInEQ () {
68         return true;
69     }
70     
71     protected javax.swing.JTextField JavaDoc getField () {
72         return field;
73     }
74     
75     
76     public void testClipboardOurClipboardUsedDuringCopy () {
77         javax.swing.JTextField JavaDoc 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 JavaDoc 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 JavaDoc f = getField ();
102         
103         StringSelection JavaDoc sel = new StringSelection JavaDoc ("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 JavaDoc 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 JavaDoc {
129         assertNotNull (
130             java.awt.Toolkit.getDefaultToolkit ().getSystemClipboard ()
131         );
132     }
133     
134     public void assertClipboard (String JavaDoc text) {
135         try {
136             Transferable JavaDoc t = clip.getContentsSuper (this);
137             Object JavaDoc obj = t.getTransferData (java.awt.datatransfer.DataFlavor.stringFlavor);
138             assertEquals ("Clipboard is the same", text, obj);
139         } catch (java.io.IOException JavaDoc ex) {
140             fail (ex.getMessage ());
141         } catch (java.awt.datatransfer.UnsupportedFlavorException JavaDoc 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 JavaDoc contents, ClipboardOwner JavaDoc owner) {
162             super.setContents (contents, owner);
163             setContents++;
164         }
165         
166         public Transferable JavaDoc getContents (Object JavaDoc requestor) {
167             Transferable JavaDoc retValue;
168             getContents++;
169             retValue = super.getContents (requestor);
170             return retValue;
171         }
172         public Transferable JavaDoc getContentsSuper (Object JavaDoc requestor) {
173             return super.getContents (requestor);
174         }
175         
176         public static void assertCalls (String JavaDoc 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     } // Clip
184
}
185
Popular Tags