001: package org.uispec4j;
002:
003: import org.uispec4j.utils.UnitTestCase;
004:
005: import java.awt.*;
006: import java.awt.datatransfer.DataFlavor;
007: import java.awt.datatransfer.Transferable;
008: import java.io.DataInputStream;
009: import java.io.InputStream;
010: import java.io.Reader;
011: import java.nio.ByteBuffer;
012: import java.nio.CharBuffer;
013:
014: public class ClipboardTest extends UnitTestCase {
015: private static final String CONTENT_STRING = "content";
016:
017: public void testSimplePutText() throws Exception {
018: checkSimplePutText(CONTENT_STRING);
019: checkSimplePutText("hello\t world\n bye!");
020: checkSimplePutText("1\t34");
021: }
022:
023: public void testPutTextWithHtmlUtf8MimeTypeAndAnInputStreamTransferClass()
024: throws Exception {
025: checkPutTextWithMimeType(Clipboard.HTML, Clipboard.UTF8,
026: Clipboard.INPUT_STREAM,
027: "text/html; charset=UTF-8; class=java.io.InputStream",
028: new ContentChecker() {
029: public void check(Object content) throws Exception {
030: InputStream stream = (InputStream) content;
031: DataInputStream data = new DataInputStream(
032: stream);
033: assertEquals(CONTENT_STRING, data.readLine());
034: assertEquals(-1, data.read());
035: data.close();
036: }
037: });
038: }
039:
040: public void testPutTextWithPlainAsciiMimeTypeAndACharBufferTransferClass()
041: throws Exception {
042: checkPutTextWithMimeType(
043: Clipboard.PLAIN,
044: Clipboard.US_ASCII,
045: Clipboard.CHAR_BUFFER,
046: "text/plain; charset=US-ASCII; class=java.nio.CharBuffer",
047: new ContentChecker() {
048: public void check(Object content) throws Exception {
049: CharBuffer buffer = (CharBuffer) content;
050: assertEquals(CONTENT_STRING, buffer.toString());
051: }
052: });
053: }
054:
055: public void testPutTextWithPlainUnicodeMimeTypeAndAReadaerTransferClass()
056: throws Exception {
057: checkPutTextWithMimeType(Clipboard.PLAIN, Clipboard.UNICODE,
058: Clipboard.READER,
059: "text/plain; charset=unicode; class=java.io.Reader",
060: new ContentChecker() {
061: public void check(Object content) throws Exception {
062: Reader reader = (Reader) content;
063: char[] chars = new char[CONTENT_STRING.length()];
064: reader.read(chars);
065: assertEquals(CONTENT_STRING, new String(chars));
066: assertEquals(-1, reader.read());
067: reader.close();
068: }
069: });
070: }
071:
072: public void testPutTextWithHtmlUtf16MimeTypeAndAByteBufferTransferClass()
073: throws Exception {
074: checkPutTextWithMimeType(Clipboard.HTML, Clipboard.UTF16,
075: Clipboard.BYTE_BUFFER,
076: "text/html; charset=UTF-16; class=java.nio.ByteBuffer",
077: new ContentChecker() {
078: public void check(Object content) throws Exception {
079: ByteBuffer buffer = (ByteBuffer) content;
080: assertEquals(CONTENT_STRING, new String(buffer
081: .array()));
082: }
083: });
084: }
085:
086: private void checkPutTextWithMimeType(Clipboard.TextType type,
087: Clipboard.Charset charset,
088: Clipboard.TransferType transferType,
089: String expectedMimeType, ContentChecker contentChecker)
090: throws Exception {
091: Clipboard.putText(type, charset, transferType, CONTENT_STRING);
092: Transferable transferable = getSystemClipboard().getContents(
093: null);
094: DataFlavor[] flavors = transferable.getTransferDataFlavors();
095: assertEquals(1, flavors.length);
096: DataFlavor flavor = flavors[0];
097: if (!flavor.isMimeTypeEqual(expectedMimeType)) {
098: assertEquals(expectedMimeType, flavor.getMimeType());
099: }
100: Object content = transferable.getTransferData(flavor);
101: assertTrue(transferType.getDataClass().isInstance(content));
102: contentChecker.check(content);
103: }
104:
105: private interface ContentChecker {
106: void check(Object content) throws Exception;
107: }
108:
109: private void checkSimplePutText(String data) throws Exception {
110: Clipboard.putText(data);
111: String actual = (String) getSystemClipboard().getContents(this )
112: .getTransferData(DataFlavor.stringFlavor);
113: assertEquals(data, actual);
114: }
115:
116: private java.awt.datatransfer.Clipboard getSystemClipboard() {
117: return Toolkit.getDefaultToolkit().getSystemClipboard();
118: }
119: }
|