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:
042: package org.netbeans.core;
043:
044: import java.awt.datatransfer.ClipboardOwner;
045: import java.awt.datatransfer.StringSelection;
046: import java.awt.datatransfer.Transferable;
047: import junit.textui.TestRunner;
048: import org.netbeans.junit.MockServices;
049: import org.netbeans.junit.NbTestCase;
050: import org.openide.util.Lookup;
051: import org.openide.util.datatransfer.ExClipboard;
052:
053: /** Test that verifies that Clipboard is used by swing components.
054: * @author Jaroslav Tulach
055: * @see "#40693"
056: */
057: public class NbClipboardIsUsedBySwingComponentsTest extends NbTestCase {
058: private Clip clip;
059: private javax.swing.JTextField field;
060:
061: public NbClipboardIsUsedBySwingComponentsTest(String name) {
062: super (name);
063: }
064:
065: protected void setUp() throws Exception {
066: MockServices.setServices(Clip.class);
067: System.setProperty("netbeans.security.nocheck", "true");
068: Object clip = Lookup.getDefault().lookup(ExClipboard.class);
069: assertNotNull("Some clipboard found", clip);
070: assertEquals("Correct clipboard found", Clip.class, clip
071: .getClass());
072: this .clip = (Clip) clip;
073:
074: if (System.getSecurityManager() == null) {
075: java.text.NumberFormat.getInstance();
076:
077: Object clazz = org.netbeans.TopSecurityManager.class;
078: SecurityManager m = new org.netbeans.TopSecurityManager();
079: System.setSecurityManager(m);
080:
081: inMiddleOfSettingUpTheManager();
082:
083: org.netbeans.TopSecurityManager
084: .makeSwingUseSpecialClipboard(this .clip);
085: } else {
086: inMiddleOfSettingUpTheManager();
087: }
088:
089: field = new javax.swing.JTextField();
090: }
091:
092: protected boolean runInEQ() {
093: return true;
094: }
095:
096: protected javax.swing.JTextField getField() {
097: return field;
098: }
099:
100: public void testClipboardOurClipboardUsedDuringCopy() {
101: javax.swing.JTextField f = getField();
102: f.setText("Ahoj");
103: f.selectAll();
104: assertEquals("Ahoj", f.getSelectedText());
105: f.copy();
106:
107: Clip.assertCalls("Copy should call setContent", 1, 0);
108: assertClipboard("Ahoj");
109: }
110:
111: public void testClipboardOurClipboardUsedDuringCut() {
112: javax.swing.JTextField f = getField();
113: f.setText("DoCut");
114: f.selectAll();
115: assertEquals("DoCut", f.getSelectedText());
116: f.cut();
117:
118: Clip.assertCalls("Cut should call setContent", 1, 0);
119: assertClipboard("DoCut");
120:
121: assertEquals("Empty", "", f.getText());
122: }
123:
124: public void testClipboardOurClipboardUsedDuringPaste() {
125: javax.swing.JTextField f = getField();
126:
127: StringSelection sel = new StringSelection("DoPaste");
128: clip.setContents(sel, sel);
129: Clip.assertCalls("Of course there is one set", 1, 0);
130:
131: assertClipboard("DoPaste");
132: f.paste();
133:
134: Clip.assertCalls("Paste should call getContent", 0, 1);
135: assertEquals("Text is there", "DoPaste", f.getText());
136: }
137:
138: public void testCopyFromEditorPasteToTheSameOneIssue40785() {
139: javax.swing.JTextField f = getField();
140: f.setText(getName());
141: f.selectAll();
142: assertEquals("Selection is correct", getName(), f
143: .getSelectedText());
144: f.copy();
145: Clip.assertCalls("Once in, none out", 1, 0);
146: f.setText("");
147: f.paste();
148: Clip.assertCalls("Once out, none in", 0, 1);
149:
150: assertEquals("Test is again the same", getName(), f.getText());
151: }
152:
153: public void testItIsStillPossibleToGetTheClipboardForNormalCode()
154: throws Exception {
155: assertNotNull(java.awt.Toolkit.getDefaultToolkit()
156: .getSystemClipboard());
157: }
158:
159: public void assertClipboard(String text) {
160: try {
161: Transferable t = clip.getContentsSuper(this );
162: Object obj = t
163: .getTransferData(java.awt.datatransfer.DataFlavor.stringFlavor);
164: assertEquals("Clipboard is the same", text, obj);
165: } catch (java.io.IOException ex) {
166: fail(ex.getMessage());
167: } catch (java.awt.datatransfer.UnsupportedFlavorException ex) {
168: fail(ex.getMessage());
169: }
170: }
171:
172: protected void inMiddleOfSettingUpTheManager() {
173: }
174:
175: public static final class Clip extends ExClipboard {
176: private static int setContents;
177: private static int getContents;
178:
179: public Clip() {
180: super ("Clip");
181: }
182:
183: protected ExClipboard.Convertor[] getConvertors() {
184: return new ExClipboard.Convertor[0];
185: }
186:
187: public void setContents(Transferable contents,
188: ClipboardOwner owner) {
189: super .setContents(contents, owner);
190: setContents++;
191: }
192:
193: public Transferable getContents(Object requestor) {
194: Transferable retValue;
195: getContents++;
196: retValue = super .getContents(requestor);
197: return retValue;
198: }
199:
200: public Transferable getContentsSuper(Object requestor) {
201: return super .getContents(requestor);
202: }
203:
204: public static void assertCalls(String msg, int setContents,
205: int getContents) {
206: if (setContents != -1)
207: assertEquals(msg + " setContents", setContents,
208: Clip.setContents);
209: if (getContents != -1)
210: assertEquals(msg + " getContents", getContents,
211: Clip.getContents);
212:
213: Clip.setContents = 0;
214: Clip.getContents = 0;
215: }
216: } // Clip
217: }
|