< prev index next >

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

Print this page




   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
  29  * transferred properly
  30  * @library /test/lib
  31  * @library ../../regtesthelpers/process/
  32  * @build jdk.test.lib.Platform ProcessResults ProcessCommunicator
  33  * @author gas@sparc.spb.su area=Clipboard
  34  * @run main/timeout=240 ImageTransferTest
  35  */
  36 
  37 import jdk.test.lib.Platform;
  38 import test.java.awt.regtesthelpers.process.ProcessCommunicator;
  39 import test.java.awt.regtesthelpers.process.ProcessResults;
  40 
  41 import java.awt.*;
  42 import java.awt.datatransfer.DataFlavor;
  43 import java.awt.datatransfer.SystemFlavorMap;
  44 import java.awt.datatransfer.Transferable;
  45 import java.awt.datatransfer.UnsupportedFlavorException;
  46 import java.awt.dnd.DnDConstants;
  47 import java.awt.dnd.DragSource;


 117 }
 118 
 119 
 120 class Util {
 121     private static Robot srobot = null;
 122     public static void sync() {
 123         try {
 124             if(srobot == null) {
 125                 srobot = new Robot();
 126             }
 127             srobot.waitForIdle();
 128             Thread.sleep(500);
 129         } catch (Exception e) {
 130             throw new RuntimeException(e);
 131         }
 132     }
 133 }
 134 
 135 abstract class ImageTransferer {
 136     Image image;

 137     String[] formats;
 138     int fi; // current format index
 139     Frame frame = new Frame();
 140 
 141 
 142     ImageTransferer() {
 143         image = createImage();

 144         frame.setSize(100, 100);
 145     }
 146 
 147     private static Image createImage() {
 148         int w = 100;
 149         int h = 100;
 150         int[] pix = new int[w * h];
 151 






 152         int index = 0;
 153         for (int y = 0; y < h; y++) {
 154             for (int x = 0; x < w; x++) {
 155                 int red = 127;
 156                 int green = 127;
 157                 int blue = y > h / 2 ? 127 : 0;
 158                 int alpha = 255;
 159                 if (x < w / 4 && y < h / 4) {
 160                     alpha = 0;
 161                     red = 0;
 162                 }
 163                 pix[index++] =
 164                         (alpha << 24) | (red << 16) | (green << 8) | blue;







 165             }
 166         }
 167         return Toolkit.getDefaultToolkit().
 168                 createImage(new MemoryImageSource(w, h, pix, 0, w));
 169     }
 170 
 171 
 172     static String[] retrieveFormatsToTest() {
 173         SystemFlavorMap sfm =
 174                 (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
 175         java.util.List<String> ln =
 176                 sfm.getNativesForFlavor(DataFlavor.imageFlavor);
 177         if (Platform.isWindows() &&  !ln.contains("METAFILEPICT"))
 178         {
 179             // for test failing on JDK without this fix
 180             ln.add("METAFILEPICT");
 181         }
 182         return ln.toArray(new String[ln.size()]);
 183     }
 184 
 185     static void leaveFormat(String format) {
 186         SystemFlavorMap sfm =
 187                 (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
 188         sfm.setFlavorsForNative(format,


 251 
 252 
 253 class ImageDragSource extends ImageTransferer {
 254     boolean[] passedArray;
 255 
 256     ImageDragSource() {
 257         formats = retrieveFormatsToTest();
 258         passedArray = new boolean[formats.length];
 259         final DragSourceListener dsl = new DragSourceAdapter() {
 260             public void dragDropEnd(DragSourceDropEvent e) {
 261                 System.err.println("Drop was successful=" + e.getDropSuccess());
 262                 notifyTransferSuccess(e.getDropSuccess());
 263                 if (++fi < formats.length) {
 264                     leaveFormat(formats[fi]);
 265                 }
 266             }
 267         };
 268 
 269         new DragSource().createDefaultDragGestureRecognizer(frame,
 270         DnDConstants.ACTION_COPY,
 271         dge -> dge.startDrag(null, new ImageSelection(image), dsl));






 272         leaveFormat(formats[fi]);
 273     }
 274 
 275 
 276     void notifyTransferSuccess(boolean status) {
 277         passedArray[fi] = status;
 278     }
 279 }
 280 
 281 
 282 class ImageDropTarget extends ImageTransferer {
 283     private final Robot robot;
 284     private static ImageDropTarget idt;
 285     private static Point startPoint, endPoint = new Point(250, 150);
 286     private static int dropCount = 0;
 287 
 288     ImageDropTarget() throws AWTException {
 289         DropTargetAdapter dropTargetAdapter = new DropTargetAdapter() {
 290             @Override
 291             public void drop(DropTargetDropEvent dtde) {


 297         new DropTarget(frame, dropTargetAdapter);
 298         robot = new Robot();
 299     }
 300 
 301 
 302     void checkImage(DropTargetDropEvent dtde) {
 303         final Transferable t = dtde.getTransferable();
 304         if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
 305             dtde.acceptDrop(DnDConstants.ACTION_COPY);
 306             Image im;
 307             try {
 308                 im = (Image) t.getTransferData(DataFlavor.imageFlavor);
 309                 System.err.println("getTransferData was successful");
 310             } catch (Exception e) {
 311                 System.err.println("Can't getTransferData: " + e);
 312                 dtde.dropComplete(false);
 313                 notifyTransferSuccess(false);
 314                 return;
 315             }
 316 








 317             if (im == null) {
 318                 System.err.println("getTransferData returned null");
 319                 dtde.dropComplete(false);
 320                 notifyTransferSuccess(false);
 321             } else if (areImagesIdentical(image, im)) {
 322                 dtde.dropComplete(true);
 323                 notifyTransferSuccess(true);
 324             } else {
 325                 System.err.println("transferred image is different from" +
 326                         " initial image");
 327                 dtde.dropComplete(false);
 328                 notifyTransferSuccess(false);
 329             }
 330 
 331         } else {
 332             System.err.println("imageFlavor is not supported by Transferable");
 333             dtde.rejectDrop();
 334             notifyTransferSuccess(false);
 335         }
 336     }




   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 8176556
  28  * @summary tests that images of all supported native image formats are
  29  * transferred properly
  30  * @library /test/lib
  31  * @library ../../regtesthelpers/process/
  32  * @build jdk.test.lib.Platform ProcessResults ProcessCommunicator
  33  * @author gas@sparc.spb.su area=Clipboard
  34  * @run main/timeout=240 ImageTransferTest
  35  */
  36 
  37 import jdk.test.lib.Platform;
  38 import test.java.awt.regtesthelpers.process.ProcessCommunicator;
  39 import test.java.awt.regtesthelpers.process.ProcessResults;
  40 
  41 import java.awt.*;
  42 import java.awt.datatransfer.DataFlavor;
  43 import java.awt.datatransfer.SystemFlavorMap;
  44 import java.awt.datatransfer.Transferable;
  45 import java.awt.datatransfer.UnsupportedFlavorException;
  46 import java.awt.dnd.DnDConstants;
  47 import java.awt.dnd.DragSource;


 117 }
 118 
 119 
 120 class Util {
 121     private static Robot srobot = null;
 122     public static void sync() {
 123         try {
 124             if(srobot == null) {
 125                 srobot = new Robot();
 126             }
 127             srobot.waitForIdle();
 128             Thread.sleep(500);
 129         } catch (Exception e) {
 130             throw new RuntimeException(e);
 131         }
 132     }
 133 }
 134 
 135 abstract class ImageTransferer {
 136     Image image;
 137     Image imageForJpeg;
 138     String[] formats;
 139     int fi; // current format index
 140     Frame frame = new Frame();
 141 
 142 
 143     ImageTransferer() {
 144         image = createImage(false);
 145         imageForJpeg = createImage(true);
 146         frame.setSize(100, 100);
 147     }
 148 
 149     private static Image createImage(boolean forJpeg) {
 150         int w = 100;
 151         int h = 100;
 152         int[] pix = new int[w * h];
 153 
 154         BufferedImage img;
 155         if (!forJpeg) {
 156             img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
 157         } else {
 158             img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
 159         }
 160         int index = 0;
 161         for (int y = 0; y < h; y++) {
 162             for (int x = 0; x < w; x++) {
 163                 int red = 127;
 164                 int green = 127;
 165                 int blue = y > h / 2 ? 127 : 0;
 166                 int alpha = 255;
 167                 if (x < w / 4 && y < h / 4) {
 168                     alpha = 0;
 169                     red = 0;
 170                 }
 171                 if (!forJpeg) {
 172                     pix[index] =
 173                         (alpha <<24) | (red << 16) | (green << 8) | blue;
 174                 } else {
 175                     pix[index] =
 176                         (red << 16) | (green << 8) | blue;
 177                 }
 178                 img.setRGB(x, y, pix[index]);
 179                 index++;
 180             }
 181         }
 182         return (Image)img;

 183     }
 184 
 185 
 186     static String[] retrieveFormatsToTest() {
 187         SystemFlavorMap sfm =
 188                 (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
 189         java.util.List<String> ln =
 190                 sfm.getNativesForFlavor(DataFlavor.imageFlavor);
 191         if (Platform.isWindows() &&  !ln.contains("METAFILEPICT"))
 192         {
 193             // for test failing on JDK without this fix
 194             ln.add("METAFILEPICT");
 195         }
 196         return ln.toArray(new String[ln.size()]);
 197     }
 198 
 199     static void leaveFormat(String format) {
 200         SystemFlavorMap sfm =
 201                 (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
 202         sfm.setFlavorsForNative(format,


 265 
 266 
 267 class ImageDragSource extends ImageTransferer {
 268     boolean[] passedArray;
 269 
 270     ImageDragSource() {
 271         formats = retrieveFormatsToTest();
 272         passedArray = new boolean[formats.length];
 273         final DragSourceListener dsl = new DragSourceAdapter() {
 274             public void dragDropEnd(DragSourceDropEvent e) {
 275                 System.err.println("Drop was successful=" + e.getDropSuccess());
 276                 notifyTransferSuccess(e.getDropSuccess());
 277                 if (++fi < formats.length) {
 278                     leaveFormat(formats[fi]);
 279                 }
 280             }
 281         };
 282 
 283         new DragSource().createDefaultDragGestureRecognizer(frame,
 284         DnDConstants.ACTION_COPY,
 285         dge -> {
 286             if (formats[fi].equals("JFIF") || formats[fi].equals("image/jpeg")) {
 287                 dge.startDrag(null, new ImageSelection(imageForJpeg), dsl);
 288             } else {
 289                 dge.startDrag(null, new ImageSelection(image), dsl);
 290             }
 291         });
 292         leaveFormat(formats[fi]);
 293     }
 294 
 295 
 296     void notifyTransferSuccess(boolean status) {
 297         passedArray[fi] = status;
 298     }
 299 }
 300 
 301 
 302 class ImageDropTarget extends ImageTransferer {
 303     private final Robot robot;
 304     private static ImageDropTarget idt;
 305     private static Point startPoint, endPoint = new Point(250, 150);
 306     private static int dropCount = 0;
 307 
 308     ImageDropTarget() throws AWTException {
 309         DropTargetAdapter dropTargetAdapter = new DropTargetAdapter() {
 310             @Override
 311             public void drop(DropTargetDropEvent dtde) {


 317         new DropTarget(frame, dropTargetAdapter);
 318         robot = new Robot();
 319     }
 320 
 321 
 322     void checkImage(DropTargetDropEvent dtde) {
 323         final Transferable t = dtde.getTransferable();
 324         if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
 325             dtde.acceptDrop(DnDConstants.ACTION_COPY);
 326             Image im;
 327             try {
 328                 im = (Image) t.getTransferData(DataFlavor.imageFlavor);
 329                 System.err.println("getTransferData was successful");
 330             } catch (Exception e) {
 331                 System.err.println("Can't getTransferData: " + e);
 332                 dtde.dropComplete(false);
 333                 notifyTransferSuccess(false);
 334                 return;
 335             }
 336 
 337             /*
 338              * We are using RGB source image for jpeg
 339              * because there is no support for alpha channel.
 340              * Also we are not verifying pixel data for jpeg
 341              * in areImagesIdentical() since it is a lossy format.
 342              * So after image drop we are not handling any needed
 343              * special cases for jpeg.
 344              */
 345             if (im == null) {
 346                 System.err.println("getTransferData returned null");
 347                 dtde.dropComplete(false);
 348                 notifyTransferSuccess(false);
 349             } else if (areImagesIdentical(image, im)) {
 350                 dtde.dropComplete(true);
 351                 notifyTransferSuccess(true);
 352             } else {
 353                 System.err.println("transferred image is different from" +
 354                         " initial image");
 355                 dtde.dropComplete(false);
 356                 notifyTransferSuccess(false);
 357             }
 358 
 359         } else {
 360             System.err.println("imageFlavor is not supported by Transferable");
 361             dtde.rejectDrop();
 362             notifyTransferSuccess(false);
 363         }
 364     }


< prev index next >