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 Michael Danilov
019: * @version $Revision$
020: */package java.awt.datatransfer;
021:
022: import java.io.IOException;
023: import java.lang.reflect.InvocationTargetException;
024: import java.util.Arrays;
025: import java.awt.*;
026:
027: import junit.framework.TestCase;
028:
029: public class ClipboardTest extends TestCase {
030:
031: private boolean calledBack;
032: private boolean listenerCalled;
033:
034: public final void testClipboard() {
035: assertNull(new Clipboard(null).getName());
036: assertEquals(new Clipboard("Clipboard").getName(), "Clipboard");
037: }
038:
039: public final void testGetName() {
040: testClipboard();
041: }
042:
043: public final void testAddFlavorListener() {
044: Clipboard c = new Clipboard("");
045: FlavorListener l = new FlavorListener() {
046: public void flavorsChanged(FlavorEvent e) {
047: }
048: };
049:
050: c.addFlavorListener(null);
051: assertTrue(c.getFlavorListeners().length == 0);
052:
053: c.addFlavorListener(l);
054: assertTrue(c.getFlavorListeners().length > 0);
055: assertTrue(Arrays.asList(c.getFlavorListeners()).contains(l));
056: }
057:
058: public final void testRemoveFlavorListener() {
059: Clipboard c = new Clipboard("");
060: FlavorListener l = new FlavorListener() {
061: public void flavorsChanged(FlavorEvent e) {
062: }
063: };
064:
065: c.addFlavorListener(l);
066: c.removeFlavorListener(null);
067: assertTrue(c.getFlavorListeners().length == 1);
068:
069: c.removeFlavorListener(l);
070: assertTrue(c.getFlavorListeners().length == 0);
071:
072: c.removeFlavorListener(l);
073: assertTrue(c.getFlavorListeners().length == 0);
074: }
075:
076: public final void testGetFlavorListeners() {
077: Clipboard c = new Clipboard("");
078: FlavorListener l = new FlavorListener() {
079: public void flavorsChanged(FlavorEvent e) {
080: }
081: };
082:
083: assertTrue(c.getFlavorListeners().length == 0);
084:
085: c.addFlavorListener(l);
086: assertTrue(c.getFlavorListeners().length > 0);
087: assertTrue(Arrays.asList(c.getFlavorListeners()).contains(l));
088:
089: c.removeFlavorListener(l);
090: assertTrue(c.getFlavorListeners().length == 0);
091: }
092:
093: public final void testSetContents() throws InterruptedException,
094: InvocationTargetException {
095: Clipboard c = new Clipboard("");
096: FlavorListener l = new FlavorListener() {
097: public void flavorsChanged(FlavorEvent e) {
098: listenerCalled = true;
099: }
100: };
101: StringSelection t = new StringSelection("") {
102: @Override
103: public void lostOwnership(Clipboard clipboard,
104: Transferable contents) {
105: calledBack = true;
106: }
107: };
108:
109: c.addFlavorListener(l);
110: c.setContents(t, t);
111: listenerCalled = calledBack = false;
112: c.setContents(new Transferable() {
113: public Object getTransferData(DataFlavor flavor)
114: throws UnsupportedFlavorException, IOException {
115: return null;
116: }
117:
118: public boolean isDataFlavorSupported(DataFlavor flavor) {
119: return false;
120: }
121:
122: public DataFlavor[] getTransferDataFlavors() {
123: return new DataFlavor[0];
124: }
125: }, new ClipboardOwner() {
126: public void lostOwnership(Clipboard clipboard,
127: Transferable contents) {
128: }
129: });
130: EventQueue.invokeAndWait(new Runnable() {
131: public void run() {
132: }
133: });
134: assertTrue(listenerCalled);
135: assertTrue(calledBack);
136: }
137:
138: public final void testGetContents() {
139: Clipboard c = new Clipboard("");
140: StringSelection s = new StringSelection("");
141:
142: c.setContents(s, s);
143: assertEquals(c.getContents(null), s);
144: }
145:
146: @SuppressWarnings("deprecation")
147: public final void testIsDataFlavorAvailable() {
148: Clipboard c = new Clipboard("");
149: StringSelection s = new StringSelection("");
150:
151: c.setContents(s, s);
152: assertTrue(c.isDataFlavorAvailable(DataFlavor.stringFlavor));
153: assertTrue(c.isDataFlavorAvailable(DataFlavor.plainTextFlavor));
154: assertFalse(c.isDataFlavorAvailable(new DataFlavor(
155: Rectangle.class, "")));
156: }
157:
158: @SuppressWarnings("deprecation")
159: public final void testGetAvailableDataFlavors() {
160: Clipboard c = new Clipboard("");
161: StringSelection s = new StringSelection("");
162:
163: c.setContents(s, s);
164: assertTrue(Arrays.asList(c.getAvailableDataFlavors()).contains(
165: DataFlavor.stringFlavor));
166: assertTrue(Arrays.asList(c.getAvailableDataFlavors()).contains(
167: DataFlavor.plainTextFlavor));
168: assertFalse(Arrays.asList(c.getAvailableDataFlavors())
169: .contains(new DataFlavor(Rectangle.class, "")));
170: }
171:
172: public final void testGetData() {
173: Clipboard c = new Clipboard("");
174: StringSelection s = new StringSelection("");
175: boolean string = false;
176: boolean unsupported = false;
177:
178: c.setContents(s, s);
179:
180: try {
181: string = c.getData(DataFlavor.stringFlavor).equals("");
182: c.getData(new DataFlavor(Rectangle.class, "")).equals("");
183: } catch (Exception e) {
184: unsupported = true;
185: }
186: assertTrue(string);
187: assertTrue(unsupported);
188: }
189:
190: public final void testNullContent() throws NullPointerException {
191: // Regression for HARMONY-2067
192: Clipboard c = new Clipboard("");
193: c.setContents(null, null);
194: }
195:
196: }
|