1 /*
   2   test
   3   @bug 4426805 4427837
   4   @summary tests that drag source provides data in the requested tymed
   5            in java-to-native drag-and-drop operation on Win32
   6   @author das@sparc.spb.su area=dnd
   7   @run applet Win32TYMEDSelectionTest.html
   8 */
   9 
  10 /**
  11  * Win32TYMEDSelectionTest.java
  12  *
  13  * summary: tests that drag source provides data in the requested tymed
  14  *          in java-to-native drag-and-drop operation on Win32
  15  */
  16 
  17 import java.applet.Applet;
  18 import java.awt.*;
  19 import java.awt.datatransfer.*;
  20 import java.awt.dnd.*;
  21 import java.awt.event.*;
  22 import java.io.*;
  23 
  24 
  25 public class Win32TYMEDSelectionTest extends Applet implements Runnable {
  26 
  27     /*
  28      * NOTE: These constants are duplicated in child.cpp.
  29      * Be sure to keep them is sync.
  30      */
  31     public static final int CODE_NOT_RETURNED = -1;
  32     public static final int CODE_OK = 0;
  33     public static final int CODE_INVALID_TYMED_FAILURE = 1;
  34     public static final int CODE_INVALID_DATA_FAILURE = 2;
  35     public static final String TEST_TEXT = "TEST TEXT";
  36     public static final int X = 300;
  37     public static final int Y = 300;
  38     public static final int WIDTH = 100;
  39     public static final int HEIGHT = 100;
  40 
  41     public static final int ACTIVATION_TIMEOUT = 2000;
  42 
  43     private transient int retCode = CODE_NOT_RETURNED;
  44 
  45     final Frame frame = new Frame();
  46     final Button button = new DragSourceButton();
  47     private Process process;
  48 
  49     public void run() {
  50         try {
  51             Thread.sleep(ACTIVATION_TIMEOUT);
  52 
  53             final Point sourcePoint = button.getLocationOnScreen();
  54             final Dimension d = button.getSize();
  55             sourcePoint.translate(d.width / 2, d.height / 2);
  56             final Point targetPoint = new Point(X + WIDTH / 2, Y + HEIGHT / 2);
  57 
  58             final Robot robot = new Robot();
  59             robot.mouseMove(sourcePoint.x, sourcePoint.y);
  60             robot.keyPress(KeyEvent.VK_CONTROL);
  61             robot.mousePress(InputEvent.BUTTON1_MASK);
  62             for (;!sourcePoint.equals(targetPoint);
  63                  sourcePoint.translate(sign(targetPoint.x - sourcePoint.x),
  64                                        sign(targetPoint.y - sourcePoint.y))) {
  65 
  66                 robot.mouseMove(sourcePoint.x, sourcePoint.y);
  67                 Thread.sleep(10);
  68             }
  69             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  70             robot.keyRelease(KeyEvent.VK_CONTROL);
  71 
  72             Thread.sleep(ACTIVATION_TIMEOUT);
  73 
  74             if (retCode == CODE_NOT_RETURNED) {
  75                 process.destroy();
  76             }
  77         } catch (Exception e) {
  78             throw new RuntimeException(e);
  79         }
  80     } // run()
  81 
  82     public static int sign(int n) {
  83         return n < 0 ? -1 : n == 0 ? 0 : 1;
  84     }
  85 
  86     public void start() {
  87 
  88         if (System.getProperty("os.name").startsWith("Windows")) {
  89             frame.setTitle("DragSource frame");
  90             frame.setLocation(200, 200);
  91             frame.add(button);
  92             frame.pack();
  93             frame.setVisible(true);
  94 
  95             String testPath = System.getProperty("test.src", ".");
  96 
  97             try {
  98                 Process process = Runtime.getRuntime().exec("cp " + testPath + File.separator + "child.exe .");
  99                 process.waitFor();
 100                 process = Runtime.getRuntime().exec("chmod 755 child.exe");
 101                 process.waitFor();
 102             }catch (InterruptedException ire) {
 103                 ire.printStackTrace();
 104             } catch (IOException ie) {
 105                 ie.printStackTrace();
 106             }
 107 
 108             String command = "./child.exe";
 109             try {
 110                 process = Runtime.getRuntime().exec(command);
 111                 new Thread(this).start();
 112 
 113                 retCode = process.waitFor();
 114 
 115                 InputStream errorStream = process.getErrorStream();
 116                 int count = errorStream.available();
 117                 if (count > 0) {
 118                     byte[] b = new byte[count];
 119                     errorStream.read(b);
 120                     System.err.println("========= Child VM System.err ========");
 121                     System.err.print(new String(b));
 122                     System.err.println("======================================");
 123                 }
 124 
 125             } catch (Exception e) {
 126                 e.printStackTrace();
 127             }
 128             System.err.println("Child VM: returned: " + retCode);
 129             switch (retCode) {
 130             case CODE_NOT_RETURNED:
 131                 System.err.println("Child VM: failed to start");
 132                 break;
 133             case CODE_OK:
 134                 System.err.println("Child VM: normal termination");
 135                 break;
 136             default:
 137                 System.err.println("Child VM: other failure");
 138                 break;
 139             case CODE_INVALID_TYMED_FAILURE:
 140                 System.err.println("Child VM: invalid tymed detected");
 141                 throw new RuntimeException("The test failed.");
 142             case CODE_INVALID_DATA_FAILURE:
 143                 System.err.println("Child VM: invalid transfer data detected");
 144                 throw new RuntimeException("The test failed.");
 145             }
 146         }
 147     } // start()
 148 } // class Win32TYMEDSelectionTest
 149 
 150 class DragSourceButton extends Button implements Serializable,
 151                                                  DragGestureListener,
 152                                                  Transferable {
 153     static final DataFlavor[] flavors =
 154         new DataFlavor[] { DataFlavor.stringFlavor, DataFlavor.imageFlavor };
 155 
 156     public DragSourceButton() {
 157         this("DragSourceButton");
 158     }
 159 
 160     public DragSourceButton(String str) {
 161         super(str);
 162 
 163         DragSource ds = DragSource.getDefaultDragSource();
 164         ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY,
 165                                               this);
 166     }
 167 
 168     public void dragGestureRecognized(DragGestureEvent dge) {
 169         dge.startDrag(null, this,
 170                       new DragSourceAdapter() {});
 171     }
 172 
 173     public DataFlavor[] getTransferDataFlavors() {
 174         return flavors;
 175     }
 176 
 177     public boolean isDataFlavorSupported(DataFlavor flavor) {
 178         return DataFlavor.stringFlavor.equals(flavor) ||
 179             DataFlavor.imageFlavor.equals(flavor);
 180     }
 181 
 182     public Object getTransferData(DataFlavor flavor)
 183       throws UnsupportedFlavorException, IOException {
 184         if (DataFlavor.stringFlavor.equals(flavor)) {
 185             return Win32TYMEDSelectionTest.TEST_TEXT;
 186         } else if (DataFlavor.imageFlavor.equals(flavor)) {
 187             return createImage(10, 10);
 188         } else {
 189             throw new UnsupportedFlavorException(flavor);
 190         }
 191     }
 192 }
 193