< prev index next >

test/jdk/java/awt/dnd/ImageTransferTest/ImageTransferTest.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 2016, 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 4397404 4720930
  28   @summary tests that images of all supported native image formats are transfered properly
  29   @library ../../../../lib/testlibrary
  30   @library ../../regtesthelpers/process/
  31   @build jdk.testlibrary.OSInfo ProcessResults ProcessCommunicator
  32   @author gas@sparc.spb.su area=Clipboard
  33   @run main ImageTransferTest
  34 */
  35 
  36 import test.java.awt.regtesthelpers.process.ProcessCommunicator;
  37 import test.java.awt.regtesthelpers.process.ProcessResults;
  38 import jdk.testlibrary.OSInfo;
  39 
  40 import java.awt.*;
  41 import java.awt.datatransfer.DataFlavor;
  42 import java.awt.datatransfer.SystemFlavorMap;
  43 import java.awt.datatransfer.Transferable;
  44 import java.awt.datatransfer.UnsupportedFlavorException;
  45 import java.awt.dnd.DnDConstants;
  46 import java.awt.dnd.DragSource;
  47 import java.awt.dnd.DragSourceAdapter;
  48 import java.awt.dnd.DragSourceDropEvent;
  49 import java.awt.dnd.DragSourceListener;
  50 import java.awt.dnd.DropTarget;
  51 import java.awt.dnd.DropTargetAdapter;
  52 import java.awt.dnd.DropTargetDropEvent;
  53 import java.awt.event.InputEvent;
  54 import java.awt.image.BufferedImage;
  55 import java.awt.image.MemoryImageSource;
  56 import java.util.stream.Stream;
  57 
  58 public class ImageTransferTest {
  59     public static void main(String[] arg) throws Exception {
  60         ImageDragSource ids = new ImageDragSource();


  61         ids.frame.setLocation(100, 100);
  62         ids.frame.setVisible(true);
  63         Util.sync();
  64         String classpath = System.getProperty("java.class.path");
  65         String[] args = new String[ids.formats.length + 4];
  66         args[0] = "200";
  67         args[1] = "100";
  68         args[2] = args[3] = "150";
  69 
  70         System.arraycopy(ids.formats, 0, args, 4, ids.formats.length);
  71         ProcessResults pres = ProcessCommunicator.executeChildProcess(ImageDropTarget.class, classpath, args);
  72 
  73         if (pres.getStdErr() != null && pres.getStdErr().length() > 0) {
  74             System.err.println("========= Child VM System.err ========");
  75             System.err.print(pres.getStdErr());
  76             System.err.println("======================================");
  77         }
  78 
  79         if (pres.getStdOut() != null && pres.getStdOut().length() > 0) {
  80             System.err.println("========= Child VM System.out ========");


  84 
  85         boolean failed = false;
  86         String passedFormats = "";
  87         String failedFormats = "";
  88 
  89         for (int i = 0; i < ids.passedArray.length; i++) {
  90             if (ids.passedArray[i]) passedFormats += ids.formats[i] + " ";
  91             else {
  92                 failed = true;
  93                 failedFormats += ids.formats[i] + " ";
  94             }
  95         }
  96 
  97         if (failed) {
  98             throw new RuntimeException("test failed: images in following " +
  99                     "native formats are not transferred properly: " + failedFormats);
 100         } else {
 101             System.err.println("images in following " +
 102                     "native formats are transferred properly: " + passedFormats);
 103         }





 104     }
 105 }
 106 
 107 
 108 class Util {
 109     private static Robot srobot = null;
 110     public static void sync() {
 111         try {
 112             if(srobot == null) {
 113                 srobot = new Robot();
 114             }
 115             srobot.waitForIdle();
 116             Thread.sleep(500);
 117         } catch (Exception e) {
 118             throw new RuntimeException(e);
 119         }
 120     }
 121 }
 122 
 123 abstract class ImageTransferer {


 244                     leaveFormat(formats[fi]);
 245                 }
 246             }
 247         };
 248 
 249         new DragSource().createDefaultDragGestureRecognizer(frame,
 250                 DnDConstants.ACTION_COPY,
 251                 dge -> dge.startDrag(null, new ImageSelection(image), dsl));
 252         leaveFormat(formats[fi]);
 253     }
 254 
 255 
 256     void notifyTransferSuccess(boolean status) {
 257         passedArray[fi] = status;
 258     }
 259 }
 260 
 261 
 262 class ImageDropTarget extends ImageTransferer {
 263     private final Robot robot;

 264     private static Point startPoint, endPoint = new Point(250, 150);
 265 
 266     ImageDropTarget() throws AWTException {
 267         DropTargetAdapter dropTargetAdapter = new DropTargetAdapter() {
 268             @Override
 269             public void drop(DropTargetDropEvent dtde) {
 270                 checkImage(dtde);
 271                 startImageDrag();
 272             }
 273         };
 274         new DropTarget(frame, dropTargetAdapter);
 275         robot = new Robot();
 276     }
 277 
 278 
 279     void checkImage(DropTargetDropEvent dtde) {
 280         final Transferable t = dtde.getTransferable();
 281         if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
 282             dtde.acceptDrop(DnDConstants.ACTION_COPY);
 283             Image im;


 300                 notifyTransferSuccess(true);
 301             } else {
 302                 System.err.println("transferred image is different from initial image");
 303                 dtde.dropComplete(false);
 304                 notifyTransferSuccess(false);
 305             }
 306 
 307         } else {
 308             System.err.println("imageFlavor is not supported by Transferable");
 309             dtde.rejectDrop();
 310             notifyTransferSuccess(false);
 311         }
 312     }
 313 
 314     void startImageDrag() {
 315         leaveFormat(formats[fi]);
 316         new Thread(() -> {
 317             try {
 318                 Thread.sleep(1000);
 319             } catch (InterruptedException e) {



 320                 e.printStackTrace();
 321                 // Exit from the child process
 322                 System.exit(1);
 323             }
 324             robot.mouseMove(startPoint.x, startPoint.y);
 325             robot.mousePress(InputEvent.BUTTON1_MASK);
 326             for (Point p = new Point(startPoint); !p.equals(endPoint);
 327                  p.translate(sign(endPoint.x - p.x), sign(endPoint.y - p.y))) {
 328                 robot.mouseMove(p.x, p.y);
 329                 try {
 330                     Thread.sleep(50);
 331                 } catch (InterruptedException e) {
 332                     e.printStackTrace();
 333                 }
 334             }
 335 
 336             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 337         }).start();
 338     }
 339 
 340     void notifyTransferSuccess(boolean status) {
 341         if (status) {
 342             System.err.println("format passed: " + formats[fi]);
 343         } else {



 344             System.err.println("format failed: " + formats[fi]);
 345             System.exit(1);
 346         }
 347         if (fi < formats.length - 1) {
 348             leaveFormat(formats[++fi]);
 349         } else {
 350             new Thread(() -> {
 351                 try {
 352                     Thread.sleep(500);
 353                 } catch (InterruptedException e) {
 354                     e.printStackTrace();
 355                 }
 356                 System.exit(0);
 357             }).start();
 358         }
 359     }
 360 
 361 
 362     public static void main(String[] args) {

 363         try {
 364             ImageDropTarget idt = new ImageDropTarget();
 365 
 366             int x = Integer.parseInt(args[0]);
 367             int y = Integer.parseInt(args[1]);
 368             startPoint = new Point(Integer.parseInt(args[2]), Integer.parseInt(args[3]));
 369 
 370             idt.formats = new String[args.length - 4];
 371             System.arraycopy(args, 4, idt.formats, 0, args.length - 4);
 372             leaveFormat(idt.formats[0]);
 373 
 374             idt.frame.setLocation(x, y);
 375             idt.frame.setVisible(true);
 376             Util.sync();
 377 
 378             idt.startImageDrag();
 379         } catch (Throwable e) {



 380             e.printStackTrace();
 381             System.exit(1);
 382         }
 383     }
 384 
 385 }
 386 
 387 
 388 class ImageSelection implements Transferable {
 389     private static final int IMAGE = 0;
 390     private static final DataFlavor[] flavors = {DataFlavor.imageFlavor};
 391     private Image data;
 392 
 393     public ImageSelection(Image data) {
 394         this.data = data;
 395     }
 396 
 397     @Override
 398     public DataFlavor[] getTransferDataFlavors() {
 399         // returning flavors itself would allow client code to modify
 400         // our internal behavior
 401         return flavors.clone();
 402     }
 403 
 404     @Override
 405     public boolean isDataFlavorSupported(DataFlavor flavor) {
 406         return Stream.of(flavor).anyMatch(flavor::equals);
 407     }
 408 
 409     @Override
 410     public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
 411         if (flavor.equals(flavors[IMAGE])) {
 412             return data;
 413         } else {
 414             throw new UnsupportedFlavorException(flavor);
 415         }
 416     }
 417 }

   1 /*
   2  * Copyright (c) 2014, 2018, 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 4397404 4720930 8197926
  28  * @summary tests that images of all supported native image formats are transfered properly
  29  * @library ../../../../lib/testlibrary
  30  * @library ../../regtesthelpers/process/
  31  * @build jdk.testlibrary.OSInfo ProcessResults ProcessCommunicator
  32  * @author gas@sparc.spb.su area=Clipboard
  33  * @run main ImageTransferTest
  34  */
  35 
  36 import test.java.awt.regtesthelpers.process.ProcessCommunicator;
  37 import test.java.awt.regtesthelpers.process.ProcessResults;
  38 import jdk.testlibrary.OSInfo;
  39 
  40 import java.awt.*;
  41 import java.awt.datatransfer.DataFlavor;
  42 import java.awt.datatransfer.SystemFlavorMap;
  43 import java.awt.datatransfer.Transferable;
  44 import java.awt.datatransfer.UnsupportedFlavorException;
  45 import java.awt.dnd.DnDConstants;
  46 import java.awt.dnd.DragSource;
  47 import java.awt.dnd.DragSourceAdapter;
  48 import java.awt.dnd.DragSourceDropEvent;
  49 import java.awt.dnd.DragSourceListener;
  50 import java.awt.dnd.DropTarget;
  51 import java.awt.dnd.DropTargetAdapter;
  52 import java.awt.dnd.DropTargetDropEvent;
  53 import java.awt.event.InputEvent;
  54 import java.awt.image.BufferedImage;
  55 import java.awt.image.MemoryImageSource;
  56 import java.util.stream.Stream;
  57 
  58 public class ImageTransferTest {
  59     public static void main(String[] arg) throws Exception {
  60         ImageDragSource ids = new ImageDragSource();
  61         try {
  62             ids.frame.setUndecorated(true);
  63             ids.frame.setLocation(100, 100);
  64             ids.frame.setVisible(true);
  65             Util.sync();
  66             String classpath = System.getProperty("java.class.path");
  67             String[] args = new String[ids.formats.length + 4];
  68             args[0] = "200";
  69             args[1] = "100";
  70             args[2] = args[3] = "150";
  71 
  72             System.arraycopy(ids.formats, 0, args, 4, ids.formats.length);
  73             ProcessResults pres = ProcessCommunicator.executeChildProcess(ImageDropTarget.class, classpath, args);
  74 
  75             if (pres.getStdErr() != null && pres.getStdErr().length() > 0) {
  76                 System.err.println("========= Child VM System.err ========");
  77                 System.err.print(pres.getStdErr());
  78                 System.err.println("======================================");
  79             }
  80     
  81             if (pres.getStdOut() != null && pres.getStdOut().length() > 0) {
  82                 System.err.println("========= Child VM System.out ========");


  86     
  87             boolean failed = false;
  88             String passedFormats = "";
  89             String failedFormats = "";
  90     
  91             for (int i = 0; i < ids.passedArray.length; i++) {
  92                 if (ids.passedArray[i]) passedFormats += ids.formats[i] + " ";
  93                 else {
  94                     failed = true;
  95                     failedFormats += ids.formats[i] + " ";
  96                 }
  97             }
  98     
  99             if (failed) {
 100                 throw new RuntimeException("test failed: images in following " +
 101                         "native formats are not transferred properly: " + failedFormats);
 102             } else {
 103                 System.err.println("images in following " +
 104                         "native formats are transferred properly: " + passedFormats);
 105             }
 106         } finally {
 107             if (ids.frame != null) {
 108                 ids.frame.dispose();
 109             }
 110         }
 111     }
 112 }
 113 
 114 
 115 class Util {
 116     private static Robot srobot = null;
 117     public static void sync() {
 118         try {
 119             if(srobot == null) {
 120                 srobot = new Robot();
 121             }
 122             srobot.waitForIdle();
 123             Thread.sleep(500);
 124         } catch (Exception e) {
 125             throw new RuntimeException(e);
 126         }
 127     }
 128 }
 129 
 130 abstract class ImageTransferer {


 251                     leaveFormat(formats[fi]);
 252                 }
 253             }
 254         };
 255 
 256         new DragSource().createDefaultDragGestureRecognizer(frame,
 257                 DnDConstants.ACTION_COPY,
 258                 dge -> dge.startDrag(null, new ImageSelection(image), dsl));
 259         leaveFormat(formats[fi]);
 260     }
 261 
 262 
 263     void notifyTransferSuccess(boolean status) {
 264         passedArray[fi] = status;
 265     }
 266 }
 267 
 268 
 269 class ImageDropTarget extends ImageTransferer {
 270     private final Robot robot;
 271     private static ImageDropTarget idt;
 272     private static Point startPoint, endPoint = new Point(250, 150);
 273 
 274     ImageDropTarget() throws AWTException {
 275         DropTargetAdapter dropTargetAdapter = new DropTargetAdapter() {
 276             @Override
 277             public void drop(DropTargetDropEvent dtde) {
 278                 checkImage(dtde);
 279                 startImageDrag();
 280             }
 281         };
 282         new DropTarget(frame, dropTargetAdapter);
 283         robot = new Robot();
 284     }
 285 
 286 
 287     void checkImage(DropTargetDropEvent dtde) {
 288         final Transferable t = dtde.getTransferable();
 289         if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
 290             dtde.acceptDrop(DnDConstants.ACTION_COPY);
 291             Image im;


 308                 notifyTransferSuccess(true);
 309             } else {
 310                 System.err.println("transferred image is different from initial image");
 311                 dtde.dropComplete(false);
 312                 notifyTransferSuccess(false);
 313             }
 314 
 315         } else {
 316             System.err.println("imageFlavor is not supported by Transferable");
 317             dtde.rejectDrop();
 318             notifyTransferSuccess(false);
 319         }
 320     }
 321 
 322     void startImageDrag() {
 323         leaveFormat(formats[fi]);
 324         new Thread(() -> {
 325             try {
 326                 Thread.sleep(1000);
 327             } catch (InterruptedException e) {
 328                 if (idt.frame != null) {
 329                     idt.frame.dispose();
 330                 }
 331                 e.printStackTrace();
 332                 // Exit from the child process
 333                 System.exit(1);
 334             }
 335             robot.mouseMove(startPoint.x, startPoint.y);
 336             robot.mousePress(InputEvent.BUTTON1_MASK);
 337             for (Point p = new Point(startPoint); !p.equals(endPoint);
 338                  p.translate(sign(endPoint.x - p.x), sign(endPoint.y - p.y))) {
 339                 robot.mouseMove(p.x, p.y);
 340                 try {
 341                     Thread.sleep(50);
 342                 } catch (InterruptedException e) {
 343                     e.printStackTrace();
 344                 }
 345             }
 346 
 347             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 348         }).start();
 349     }
 350 
 351     void notifyTransferSuccess(boolean status) {
 352         if (status) {
 353             System.err.println("format passed: " + formats[fi]);
 354         } else {
 355             if (idt.frame != null) {
 356                 idt.frame.dispose();
 357             }
 358             System.err.println("format failed: " + formats[fi]);
 359             System.exit(1);
 360         }
 361         if (fi < formats.length - 1) {
 362             leaveFormat(formats[++fi]);
 363         } else {
 364             new Thread(() -> {
 365                 try {
 366                     Thread.sleep(500);
 367                 } catch (InterruptedException e) {
 368                     e.printStackTrace();
 369                 }
 370                 System.exit(0);
 371             }).start();
 372         }
 373     }
 374 
 375 
 376     public static void main(String[] args) throws Exception {
 377         idt = new ImageDropTarget();
 378         try {
 379             idt.frame.setUndecorated(true);
 380 
 381             int x = Integer.parseInt(args[0]);
 382             int y = Integer.parseInt(args[1]);
 383             startPoint = new Point(Integer.parseInt(args[2]), Integer.parseInt(args[3]));
 384 
 385             idt.formats = new String[args.length - 4];
 386             System.arraycopy(args, 4, idt.formats, 0, args.length - 4);
 387             leaveFormat(idt.formats[0]);
 388 
 389             idt.frame.setLocation(x, y);
 390             idt.frame.setVisible(true);
 391             Util.sync();
 392 
 393             idt.startImageDrag();
 394         } catch (Throwable e) {
 395             if (idt.frame != null) {
 396                 idt.frame.dispose();
 397             }
 398             e.printStackTrace();
 399             System.exit(1);
 400         }
 401     }
 402 
 403 }
 404 
 405 
 406 class ImageSelection implements Transferable {
 407     private static final int IMAGE = 0;
 408     private static final DataFlavor[] flavors = {DataFlavor.imageFlavor};
 409     private Image data;
 410 
 411     public ImageSelection(Image data) {
 412         this.data = data;
 413     }
 414 
 415     @Override
 416     public DataFlavor[] getTransferDataFlavors() {
 417         // returning flavors itself would allow client code to modify
 418         // our internal behavior
 419         return flavors.clone();
 420     }
 421 
 422     @Override
 423     public boolean isDataFlavorSupported(DataFlavor flavor) {
 424         return Stream.of(flavor).anyMatch(flavor::equals);
 425     }
 426 
 427     @Override
 428     public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
 429         if (flavor.equals(flavors[IMAGE])) {
 430             return data;
 431         } else {
 432             throw new UnsupportedFlavorException(flavor);
 433         }
 434     }
 435 }
 436 
< prev index next >