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