001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.AWTPermission;
023: import java.awt.Color;
024: import java.awt.datatransfer.Clipboard;
025: import java.awt.datatransfer.DataFlavor;
026: import java.awt.datatransfer.StringSelection;
027: import java.awt.datatransfer.Transferable;
028: import java.awt.datatransfer.UnsupportedFlavorException;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.InputEvent;
031: import java.awt.event.MouseEvent;
032: import java.io.IOException;
033: import java.security.Permission;
034:
035: public class TransferHandlerTest extends SwingTestCase {
036: TransferHandler transferHandler;
037:
038: JButton button;
039:
040: SimpleTransferHandler trH;
041:
042: TransferHandler insetsTransferHandler;
043:
044: class SimpleTransferHandler extends TransferHandler {
045: private static final long serialVersionUID = 1L;
046:
047: public SimpleTransferHandler(final String s) {
048: super (s);
049: }
050:
051: boolean wasCallExportToClipBoard;
052:
053: int parameter = -3;
054:
055: boolean wasCallImportData;
056:
057: Clipboard clipboard;
058:
059: JComponent component;
060:
061: Transferable transferable;
062:
063: @Override
064: public void exportToClipboard(final JComponent comp,
065: final Clipboard clip, final int action) {
066: component = comp;
067: clipboard = clip;
068: parameter = action;
069: wasCallExportToClipBoard = true;
070: }
071:
072: @Override
073: public boolean importData(final JComponent comp,
074: final Transferable t) {
075: component = comp;
076: transferable = t;
077: wasCallImportData = true;
078: return super .importData(comp, t);
079: }
080:
081: @Override
082: protected Transferable createTransferable(final JComponent c) {
083: Transferable t = super .createTransferable(c);
084: return t;
085: }
086: }
087:
088: @Override
089: protected void setUp() throws Exception {
090: super .setUp();
091: transferHandler = new TransferHandler("text");
092: trH = new SimpleTransferHandler("text");
093: button = new JButton();
094: button.setTransferHandler(trH);
095: insetsTransferHandler = new TransferHandler("insets");
096: }
097:
098: @Override
099: protected void tearDown() throws Exception {
100: super .tearDown();
101: }
102:
103: public void testConstants() {
104: assertEquals(0, TransferHandler.NONE);
105: assertEquals(1, TransferHandler.COPY);
106: assertEquals(2, TransferHandler.MOVE);
107: assertEquals(3, TransferHandler.COPY_OR_MOVE);
108: }
109:
110: public void testTransferHandlerString() {
111: }
112:
113: public void testTransferHandler() {
114: }
115:
116: public void testGetVisualRepresentation() {
117: Transferable transferable = new StringSelection("***");
118: assertNull(transferHandler.getVisualRepresentation(null));
119: assertNull(transferHandler
120: .getVisualRepresentation(transferable));
121: }
122:
123: public void testExportAsDrag() {
124: MouseEvent e = new MouseEvent(button, MouseEvent.MOUSE_PRESSED,
125: 0, InputEvent.BUTTON1_DOWN_MASK, 50, 50, 0, true);
126: trH.exportAsDrag(button, e, TransferHandler.COPY_OR_MOVE);
127: }
128:
129: public void testImportData() {
130: TransferHandler handler = new TransferHandler("background");
131: JTextArea textArea = new JTextArea();
132: textArea.setBackground(Color.RED);
133: Transferable transferable = handler
134: .createTransferable(textArea);
135: try {
136: assertEquals(Color.RED, transferable
137: .getTransferData(transferable
138: .getTransferDataFlavors()[0]));
139: } catch (UnsupportedFlavorException e) {
140: assertFalse("Unexpected exception: " + e.getMessage(), true);
141: } catch (IOException e) {
142: assertFalse("Unexpected exception: " + e.getMessage(), true);
143: }
144: JTextArea distArea = new JTextArea();
145: distArea.setBackground(Color.GREEN);
146: handler.importData(distArea, transferable);
147: assertEquals(Color.RED, distArea.getBackground());
148: }
149:
150: public void testCreateTransferable() {
151: Transferable transferable = insetsTransferHandler
152: .createTransferable(button);
153: transferable.getTransferDataFlavors();
154: DataFlavor[] flavors = transferable.getTransferDataFlavors();
155: try {
156: assertEquals(button.getInsets(), transferable
157: .getTransferData(flavors[0]));
158: } catch (IOException e) {
159: assertFalse("Unexpected exception :" + e.getMessage(), true);
160: } catch (UnsupportedFlavorException e) {
161: assertFalse("Unexpected exception :" + e.getMessage(), true);
162: }
163: assertEquals(1, flavors.length);
164: DataFlavor flavor = flavors[0];
165: assertEquals(DataFlavor.javaJVMLocalObjectMimeType, flavor
166: .getHumanPresentableName());
167: }
168:
169: public void testCanImport() {
170: //no text property
171: assertFalse(transferHandler.canImport(new JPanel(), null));
172: DataFlavor[] flavors = new DataFlavor[] {
173: DataFlavor.imageFlavor, DataFlavor.javaFileListFlavor,
174: DataFlavor.stringFlavor };
175: assertFalse(transferHandler.canImport(button, flavors));
176: DataFlavor flavor;
177: try {
178: flavor = new DataFlavor(
179: "application/x-java-jvm-local-objectref;"
180: + "class=java.lang.String");
181: assertTrue(transferHandler.canImport(button,
182: new DataFlavor[] { flavor }));
183: } catch (ClassNotFoundException e) {
184: assertFalse("Unexpected exception : " + e.getMessage(),
185: true);
186: }
187: }
188:
189: public void testExportToClipboard_text() {
190: JTextArea textArea = new JTextArea("ABCD");
191: Clipboard clip = new Clipboard("non system");
192: transferHandler.exportToClipboard(textArea, clip,
193: TransferHandler.COPY);
194: DataFlavor flavor = null;
195: try {
196: flavor = new DataFlavor(
197: "application/x-java-jvm-local-objectref;"
198: + "class=java.lang.String");
199: } catch (ClassNotFoundException e) {
200: assertFalse("Unexpected exception : " + e.getMessage(),
201: true);
202: }
203: try {
204: Transferable trans = clip.getContents(null);
205: Object obj = trans.getTransferData(flavor);
206: assertEquals("ABCD", obj);
207: } catch (IOException e) {
208: } catch (UnsupportedFlavorException e) {
209: }
210: }
211:
212: public void testExportToClipboard_background() {
213: TransferHandler handler = new TransferHandler("background");
214: JTextArea textArea = new JTextArea("ABCD");
215: textArea.setBackground(Color.RED);
216: Clipboard clip = new Clipboard("non system");
217: handler.exportToClipboard(textArea, clip, TransferHandler.COPY);
218: DataFlavor flavor = null;
219: try {
220: flavor = new DataFlavor(
221: "application/x-java-jvm-local-objectref;"
222: + "class=java.awt.Color");
223: } catch (ClassNotFoundException e) {
224: assertFalse("Unexpected exception : " + e.getMessage(),
225: true);
226: }
227: try {
228: Transferable trans = clip.getContents(null);
229: Object obj = trans.getTransferData(flavor);
230: assertEquals(Color.RED, obj);
231: } catch (IOException e) {
232: } catch (UnsupportedFlavorException e) {
233: }
234: }
235:
236: public void testGetSourceActions() {
237: assertEquals(TransferHandler.COPY, transferHandler
238: .getSourceActions(new JButton()));
239: assertEquals(TransferHandler.NONE, transferHandler
240: .getSourceActions(new JPanel()));
241: assertEquals(TransferHandler.COPY, transferHandler
242: .getSourceActions(new JLabel()));
243: assertEquals(TransferHandler.NONE, transferHandler
244: .getSourceActions(new JLayeredPane()));
245: }
246:
247: public void testGetPasteAction() {
248: // TODO: uncomment when System clipboard is properly supported
249: // StringSelection data = new StringSelection("TEST");
250: // Toolkit.getDefaultToolkit().getSystemClipboard().setContents(data, null);
251: // Action action = TransferHandler.getPasteAction();
252: // ActionEvent event = new ActionEvent(button,
253: // ActionEvent.ACTION_PERFORMED, "");
254: // action.actionPerformed(event);
255: // assertTrue(trH.wasCallImportData);
256: // assertEquals(button, trH.component);
257: // assertEquals("paste", action.getValue(Action.NAME));
258: // assertTrue(action == TransferHandler.getPasteAction());
259: }
260:
261: public void testGetCutAction() {
262: // TODO: uncomment when System clipboard is properly supported
263: // Action action = TransferHandler.getCutAction();
264: // ActionEvent event = new ActionEvent(button,
265: // ActionEvent.ACTION_PERFORMED, "");
266: // action.actionPerformed(event);
267: // assertTrue(trH.wasCallExportToClipBoard);
268: // assertEquals(TransferHandler.MOVE, trH.parameter);
269: // assertEquals(Toolkit.getDefaultToolkit().getSystemClipboard(),
270: // trH.clipboard);
271: // assertEquals(button, trH.component);
272: // assertEquals("cut", action.getValue(Action.NAME));
273: // assertTrue(action == TransferHandler.getCutAction());
274: }
275:
276: public void testGetCopyAction() {
277: // TODO: uncomment when System clipboard is properly supported
278: // Action action = TransferHandler.getCopyAction();
279: // ActionEvent event = new ActionEvent(button,
280: // ActionEvent.ACTION_PERFORMED, "");
281: // action.actionPerformed(event);
282: // assertTrue(trH.wasCallExportToClipBoard);
283: // assertEquals(TransferHandler.COPY, trH.parameter);
284: // assertEquals(Toolkit.getDefaultToolkit().getSystemClipboard(),
285: // trH.clipboard);
286: // assertEquals(button, trH.component);
287: // assertEquals("copy", action.getValue(Action.NAME));
288: // assertTrue(action == TransferHandler.getCopyAction());
289: }
290:
291: public void testAccessSystemClipboard() {
292: // Regression test for HARMONY-3479
293:
294: class TestSecurityManager extends SecurityManager {
295:
296: public boolean flag;
297:
298: public void checkPermission(Permission perm) {
299: if ((perm instanceof AWTPermission)
300: && "accessClipboard".equals(perm.getName())) {
301: flag = true;
302: throw new SecurityException("test");
303: }
304: }
305: }
306:
307: SecurityManager oldSecurityManager = System
308: .getSecurityManager();
309: TestSecurityManager testSecurityManager = new TestSecurityManager();
310: System.setSecurityManager(testSecurityManager);
311:
312: try {
313: ActionEvent event = new ActionEvent(new JPanel(), 0, "");
314: Action action;
315:
316: testSecurityManager.flag = false;
317: TransferHandler.getCopyAction().actionPerformed(event);
318: assertTrue(testSecurityManager.flag);
319:
320: testSecurityManager.flag = false;
321: TransferHandler.getCutAction().actionPerformed(event);
322: assertTrue(testSecurityManager.flag);
323:
324: testSecurityManager.flag = false;
325: TransferHandler.getPasteAction().actionPerformed(event);
326: assertTrue(testSecurityManager.flag);
327: } finally {
328: System.setSecurityManager(oldSecurityManager);
329: }
330: }
331: }
|