1 /*
   2  * Copyright (c) 2009, 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 4874070
  28   @summary Tests basic DnD functionality
  29   @run main ImageDecoratedDnDInOut
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.Robot;
  34 import java.awt.event.InputEvent;
  35 import java.awt.dnd.DragSource;
  36 
  37 /*
  38     "Automatic test.",
  39     "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
  40     "a red panel, will appear below. ",
  41     "1. The button would be clicked and dragged to the red panel. ",
  42     "2. When the mouse enters the red panel during the drag, the panel ",
  43     "should turn yellow. On the systems that supports pictured drag, ",
  44     "the image under the drag-cursor should appear (ancor is shifted ",
  45     "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
  46     "In WIN32 systems the image under cursor would be visible ONLY over ",
  47     "the drop targets with activated extended OLE D\'n\'D support (that are ",
  48     "the desktop and IE ).",
  49     "3. The mouse would be released.",
  50     "The panel should turn red again and a yellow button labeled ",
  51     "\"Drag ME!\" should appear inside the panel. "
  52  */
  53 public class ImageDecoratedDnDInOut {
  54 
  55     public static void main(final String[] args) {
  56         Frame f = new Frame("Use keyboard for DnD change");
  57         Panel mainPanel;
  58         Component dragSource, dropTarget;
  59 
  60         f.setSize(200, 200);
  61         f.setUndecorated(true);
  62         f.setLocationRelativeTo(null);
  63         f.setLayout(new BorderLayout());
  64 
  65         mainPanel = new Panel();
  66         mainPanel.setLayout(new BorderLayout());
  67 
  68         mainPanel.setBackground(Color.blue);
  69 
  70         dropTarget = new DnDTarget(Color.red, Color.yellow);
  71         dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
  72 
  73         mainPanel.add(dragSource, "North");
  74         mainPanel.add(dropTarget, "Center");
  75         f.add(mainPanel, BorderLayout.CENTER);
  76 
  77         f.setVisible(true);
  78         try {
  79             Point sourcePoint = dragSource.getLocationOnScreen();
  80             Dimension d = dragSource.getSize();
  81             sourcePoint.translate(d.width / 2, d.height / 2);
  82 
  83             Robot robot = new Robot();
  84             robot.waitForIdle();
  85             robot.mouseMove(sourcePoint.x, sourcePoint.y);
  86             robot.mousePress(InputEvent.BUTTON1_MASK);
  87             Thread.sleep(2000);
  88             for(int i = 0; i <100; ++i) {
  89                 robot.mouseMove(
  90                     sourcePoint.x + d.width / 2 + 10,
  91                     sourcePoint.y + d.height);
  92                 Thread.sleep(100);
  93 
  94                 robot.mouseMove(sourcePoint.x, sourcePoint.y);
  95                 Thread.sleep(100);
  96 
  97                 robot.mouseMove(
  98                     sourcePoint.x,
  99                     sourcePoint.y + d.height);
 100                 Thread.sleep(100);
 101             }
 102             sourcePoint.y += d.height;
 103             robot.mouseMove(sourcePoint.x, sourcePoint.y);
 104             Thread.sleep(100);
 105 
 106             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 107             Thread.sleep(4000);
 108         } catch( Exception e){
 109         e.printStackTrace();
 110             throw new RuntimeException("test failed: drop was not successful with exception " + e);
 111         }
 112 
 113     }
 114 }// class DnDAcceptanceTest