1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 /* 26 @test 27 @key headful 28 @bug 4718897 29 @summary tests that a Unicode string can be transferred between JVMs. 30 @author das@sparc.spb.su area=datatransfer 31 @library ../../regtesthelpers/process 32 @build ProcessResults ProcessCommunicator 33 @run main UnicodeTransferTest 34 */ 35 36 import java.awt.datatransfer.*; 37 import java.awt.*; 38 import java.text.Normalizer; 39 40 import test.java.awt.regtesthelpers.process.ProcessResults; 41 import test.java.awt.regtesthelpers.process.ProcessCommunicator; 42 43 public class UnicodeTransferTest { 44 private static final Toolkit tk = Toolkit.getDefaultToolkit(); 45 private static final Clipboard clipboard = tk.getSystemClipboard(); 46 private static final Transferable t = new StringSelection(Util.getTestString()); 47 48 public static void main(String[] args) throws Exception { 49 Util.setClipboardContents(clipboard, t, null); 50 ProcessResults result = ProcessCommunicator.executeChildProcess( 51 UnicodeTransferTestChild.class, new String[0]); 52 verifyTestResults(result); 53 } 54 55 private static void verifyTestResults(ProcessResults processResults) { 56 if (processResults.getExitValue() != 0) { 57 processResults.printProcessErrorOutput(System.err); 58 throw new RuntimeException("TEST IS FAILED. See child stderr"); 59 } 60 processResults.verifyStdErr(System.err); 61 processResults.verifyProcessExitValue(System.err); 62 processResults.printProcessStandartOutput(System.out); 63 } 64 65 } 66 67 class Util { 68 private static String testString = null; 69 70 static { 71 StringBuilder buf = new StringBuilder(); 72 for (int i = 1; i < 0x10000; i++) { 73 // Skip surrogates. 74 if (i < 0xD800 || (i > 0xDFFF && i < 0xFFF0)) { 75 buf.append((char) i); 76 } else { 77 buf.append(0x20); 78 } 79 } 80 // On OS X the unicode string is normalized but the clipboard, 81 // so we need to use normalized strings as well to be able to 82 // check the result 83 testString = Normalizer.normalize(buf.toString(), Normalizer.Form.NFC); 84 } 85 86 public static String getTestString() { 87 return testString; 88 } 89 90 public static void setClipboardContents(Clipboard cb, 91 Transferable contents, 92 ClipboardOwner owner) { 93 94 boolean set = false; 95 while (!set) { 96 try { 97 cb.setContents(contents, owner); 98 set = true; 99 } catch (IllegalStateException ise) { 100 try { 101 Thread.sleep(100); 102 } catch (InterruptedException e) { 103 e.printStackTrace(); 104 } 105 } 106 } 107 } 108 109 public static Transferable getClipboardContents(Clipboard cb, 110 Object requestor) { 111 while (true) { 112 try { 113 return cb.getContents(requestor); 114 } catch (IllegalStateException ise) { 115 try { 116 Thread.sleep(100); 117 } catch (InterruptedException e) { 118 e.printStackTrace(); 119 } 120 } 121 } 122 } 123 } 124 125 class UnicodeTransferTestChild { 126 private static final Toolkit tk = Toolkit.getDefaultToolkit(); 127 private static final Clipboard clipboard = tk.getSystemClipboard(); 128 129 public static void main(String[] args) { 130 Transferable t = Util.getClipboardContents(clipboard, null); 131 132 if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 133 Object o = null; 134 try { 135 o = t.getTransferData(DataFlavor.stringFlavor); 136 } catch (Exception e) { 137 e.printStackTrace(); 138 } 139 String testStr = Util.getTestString(); 140 141 if (!testStr.equals(o)) { 142 if (o instanceof String) { 143 String s = (String)o; 144 if (s.length() != testStr.length()) { 145 System.err.println("Received length:" + s.length() + 146 " Expected length: " + 147 testStr.length()); 148 } else { 149 for (int i = 0; i < s.length(); i++) { 150 char ch = s.charAt(i); 151 char expected = testStr.charAt(i); 152 if (ch != expected) { 153 System.err.println("i=" + i + 154 " char=" + 155 Integer.toHexString((int)ch) + 156 " expected=" + 157 Integer.toHexString(expected)); 158 } 159 } 160 } 161 } else { 162 System.err.println("Received object:" + o); 163 } 164 throw new RuntimeException("String doesn't match."); 165 } 166 } else { 167 throw new RuntimeException("Clipboard content was not set"); 168 } 169 } 170 }