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 ImageDecoratedDnDNegative
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.Robot;
  34 import java.awt.event.InputEvent;
  35 import java.awt.event.KeyEvent;
  36 import java.awt.geom.Point2D;
  37 
  38 
  39 import java.awt.dnd.DragSource;
  40 
  41 /*
  42     "Automatic test.",
  43     "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
  44     "a red panel, will appear below. ",
  45     "1. The button would be clicked and dragged to the red panel. ",
  46     "2. When the mouse enters the red panel during the drag, the panel ",
  47     "should turn yellow. On the systems that supports pictured drag, ",
  48     "the image under the drag-cursor should appear (ancor is shifted ",
  49     "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
  50     "In WIN32 systems the image under cursor would be visible ONLY over ",
  51     "the drop targets with activated extended OLE D\'n\'D support (that are ",
  52     "the desktop and IE ).",
  53     "3. The mouse would be released.",
  54     "The panel should turn red again and a yellow button labeled ",
  55     "\"Drag ME!\" should appear inside the panel. You should be able ",
  56     "to repeat this operation multiple times."
  57  */
  58 public class ImageDecoratedDnDNegative {
  59 
  60     public static void moveTo(
  61         Robot r,
  62         Point b,
  63         Point e)
  64     {
  65         Point2D.Double ee = new Point2D.Double(e.getX(), e.getY());
  66         Point2D.Double bb = new Point2D.Double(b.getX(), b.getY());
  67         final int count = (int)(ee.distance(bb));
  68         Point2D.Double c = new Point2D.Double(bb.getX(), bb.getY());
  69         for(int i=0; i<count; ++i){
  70             c.setLocation(
  71                     bb.getX() + (ee.getX()-bb.getX())*i/count,
  72                     bb.getY() + (ee.getY()-bb.getY())*i/count);
  73             r.mouseMove(
  74                     (int)c.getX(),
  75                     (int)c.getY());
  76             r.delay(5);
  77         }
  78         r.mouseMove(
  79                 (int)ee.getX(),
  80                 (int)ee.getY());
  81         r.delay(5);
  82     }
  83 
  84     public static void main(final String[] args) {
  85         Frame f = new Frame("Use keyboard for DnD change");
  86         Panel mainPanel;
  87         Component dragSource, dropTarget;
  88 
  89         f.setBounds(0, 400, 200, 200);
  90         f.setLayout(new BorderLayout());
  91 
  92         mainPanel = new Panel();
  93         mainPanel.setLayout(new BorderLayout());
  94 
  95         mainPanel.setBackground(Color.blue);
  96 
  97         dropTarget = new DnDTarget(Color.red, Color.yellow);
  98         dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
  99 
 100         mainPanel.add(dragSource, "North");
 101         mainPanel.add(dropTarget, "Center");
 102         f.add(mainPanel, BorderLayout.CENTER);
 103         f.setUndecorated(true);
 104         f.setLocationRelativeTo(null);
 105         f.setVisible(true);
 106 
 107         Point sourcePoint = dragSource.getLocationOnScreen();
 108         Dimension d = dragSource.getSize();
 109         sourcePoint.translate(d.width / 2, d.height / 2);
 110 
 111         try {
 112             Robot robot = new Robot();
 113             robot.waitForIdle();
 114             robot.mouseMove(sourcePoint.x, sourcePoint.y);
 115             Point start = new Point(
 116                     sourcePoint.x,
 117                     sourcePoint.y);
 118             Point out = new Point(
 119                     sourcePoint.x + d.width / 2 + 10,
 120                     sourcePoint.y + d.height);
 121 
 122             Point cur = start;
 123             for(int i = 2; i < 5; ++i){
 124                 moveTo(robot, cur, start);
 125                 robot.delay(500);
 126                 robot.mousePress(InputEvent.BUTTON1_MASK);
 127                 robot.delay(500);
 128                 moveTo(robot, start, out);
 129                 robot.keyPress(KeyEvent.VK_CONTROL);
 130                 Point drop = new Point(
 131                         (int)start.getX(),
 132                         (int)start.getY() + (d.height + 5) * i );
 133                 moveTo(robot, out, drop);
 134 
 135                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 136                 robot.delay(10);
 137                 robot.keyRelease(KeyEvent.VK_CONTROL);
 138                 robot.delay(1000);
 139 
 140                 cur = drop;
 141             }
 142         } catch( Exception e){
 143             e.printStackTrace();
 144             throw new RuntimeException("test failed: drop was not successful with exception " + e);
 145         }
 146     }
 147 }// class DnDAcceptanceTest