1 /* 2 * Copyright (c) 2013, 2017, 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 * @test 26 * @key headful 27 * @bug 7075105 28 * @summary WIN: Provide a way to format HTML on drop 29 * @author Denis Fokin: area=datatransfer 30 * @requires (os.family == "windows") 31 * @library ../../../../lib/testlibrary 32 * @build HtmlTransferable PutAllHtmlFlavorsOnClipboard 33 * @build PutOnlyAllHtmlFlavorOnClipboard PutSelectionAndFragmentHtmlFlavorsOnClipboard 34 * @build jdk.testlibrary.OSInfo 35 * @run main HTMLDataFlavorTest 36 */ 37 38 import java.awt.*; 39 import java.awt.datatransfer.*; 40 import java.io.*; 41 import java.util.HashMap; 42 43 public class HTMLDataFlavorTest { 44 45 private static HashMap<DataFlavor, String> dataFlavors = new HashMap<DataFlavor, String>(); 46 47 48 public static void main(String[] args) throws IOException, UnsupportedFlavorException { 49 50 if (jdk.testlibrary.OSInfo.getOSType() != jdk.testlibrary.OSInfo.OSType.WINDOWS) { 51 System.err.println("This test is for MS Windows only. Considered passed."); 52 return; 53 } 54 55 dataFlavors.put(DataFlavor.allHtmlFlavor, HtmlTransferable.ALL_HTML_AS_STRING); 56 dataFlavors.put(DataFlavor.fragmentHtmlFlavor, HtmlTransferable.FRAGMENT_HTML_AS_STRING); 57 dataFlavors.put(DataFlavor.selectionHtmlFlavor, HtmlTransferable.SELECTION_HTML_AS_STRING); 58 59 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 60 resetClipboardContent(clipboard); 61 62 // 1. Put all three html flavors on clipboard. 63 // Get the data within the same JVM 64 // Expect that the resulted html is the selection 65 // wrapped in all three types 66 67 clipboard.setContents(new HtmlTransferable(HtmlTransferable.htmlDataFlavors),null); 68 69 // Test local transfer 70 testClipboardContent(clipboard, HtmlTransferable.htmlDataFlavors); 71 72 resetClipboardContent(clipboard); 73 74 // 2. Put only DataFlavor.allHtmlFlavor on clipboard. 75 // Expect that the resulted html is the all 76 // wrapped in all three types 77 78 putHtmlInAnotherProcess("PutOnlyAllHtmlFlavorOnClipboard"); 79 80 for (DataFlavor df : HtmlTransferable.htmlDataFlavors) { 81 if (!clipboard.isDataFlavorAvailable(df)) { 82 throw new RuntimeException("The data should be available."); 83 } 84 } 85 86 if (!clipboard.getData(DataFlavor.allHtmlFlavor).toString(). 87 equals(dataFlavors.get(DataFlavor.allHtmlFlavor).toString())) 88 { 89 throw new RuntimeException("DataFlavor.allHtmlFlavor data " + 90 "should be identical to the data put on the source side."); 91 } 92 93 resetClipboardContent(clipboard); 94 95 // 3. Put all three html flavors on clipboard. 96 // Expect that the resulted html is the selection 97 // wrapped in all three types 98 99 putHtmlInAnotherProcess("PutAllHtmlFlavorsOnClipboard"); 100 101 for (DataFlavor df : HtmlTransferable.htmlDataFlavors) { 102 if (!clipboard.isDataFlavorAvailable(df)) { 103 throw new RuntimeException("The data should be available."); 104 } 105 } 106 107 if (!clipboard.getData(DataFlavor.selectionHtmlFlavor).toString(). 108 equals(dataFlavors.get(DataFlavor.selectionHtmlFlavor))) 109 { 110 throw new RuntimeException("DataFlavor.allHtmlFlavor data " + 111 "should be identical to the data put on the source side."); 112 } 113 114 } 115 116 private static void resetClipboardContent(Clipboard clipboard) { 117 clipboard.setContents( 118 new StringSelection("The data is used to empty the clipboard content" 119 ),null); 120 } 121 122 123 private static void putHtmlInAnotherProcess(String putterCommand) { 124 try { 125 126 String command = System.getProperty("java.home") + "/bin/java -cp " + 127 System.getProperty("test.classes", ".") + " " + 128 putterCommand; 129 130 System.out.println("Execute process : " + command); 131 132 Process p = Runtime.getRuntime().exec(command); 133 134 try { 135 p.waitFor(); 136 } catch (InterruptedException e) { 137 e.printStackTrace(); 138 } 139 140 System.out.println("The data has been set remotely"); 141 142 try (BufferedReader stdstr = new BufferedReader(new InputStreamReader(p.getInputStream()))) { 143 String s; 144 while ((s = stdstr.readLine()) != null) { 145 s = stdstr.readLine(); 146 System.out.println(s); 147 } 148 } 149 150 try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()))) { 151 String s; 152 while ((s = br.readLine()) != null) { 153 s = br.readLine(); 154 System.err.println(s); 155 } 156 } 157 158 159 160 } catch (IOException e) { 161 e.printStackTrace(); 162 } 163 } 164 165 private static void testClipboardContent(Clipboard clipboard, 166 DataFlavor [] expectedDataFlavors) 167 throws UnsupportedFlavorException, IOException { 168 169 for (DataFlavor df : clipboard.getAvailableDataFlavors()) { 170 System.out.println("available df: " + df.getMimeType()); 171 } 172 173 for (DataFlavor df : expectedDataFlavors) { 174 175 if (!clipboard.isDataFlavorAvailable(df)) { 176 throw new RuntimeException("The data should be available."); 177 } 178 179 180 System.out.println("Checking \"" + df.getParameter("document") + "\" for correspondence"); 181 182 if (!dataFlavors.get(df).toString().equals(clipboard.getData(df).toString())) { 183 184 System.err.println("Expected data: " + dataFlavors.get(df).toString()); 185 System.err.println("Actual data: " + clipboard.getData(df).toString()); 186 187 188 throw new RuntimeException("An html flavor with parameter \"" + 189 df.getParameter("document") + "\" does not correspond to the transferred data."); 190 191 192 } 193 } 194 } 195 196 197 }