1 /*
   2  * Copyright (c) 2006, 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 import java.awt.Frame;
  25 import java.awt.Point;
  26 import java.awt.Robot;
  27 import java.awt.datatransfer.StringSelection;
  28 import java.awt.dnd.DnDConstants;
  29 import java.awt.dnd.DragGestureEvent;
  30 import java.awt.dnd.DragGestureListener;
  31 import java.awt.dnd.DragSource;
  32 import java.awt.dnd.DragSourceAdapter;
  33 import java.awt.dnd.DragSourceDropEvent;
  34 import java.awt.dnd.DragSourceListener;
  35 import java.awt.dnd.DropTarget;
  36 import java.awt.dnd.DropTargetAdapter;
  37 import java.awt.dnd.DropTargetDropEvent;
  38 import java.awt.event.InputEvent;
  39 
  40 import test.java.awt.regtesthelpers.Util;
  41 
  42 /**
  43  * @test
  44  * @bug 4955110
  45  * @summary tests that DragSourceDragEvent.getDropAction() accords to its new
  46  *          spec (does not depend on the user drop action)
  47  * @library ../../regtesthelpers
  48  * @build Util
  49  * @run main/othervm Button2DragTest
  50  * @author Alexander.Gerasimov area=dnd
  51  */
  52 public final class Button2DragTest {
  53 
  54     private volatile boolean dropSuccess;
  55 
  56     private static Frame frame;
  57 
  58     public static void main(final String[] args) {
  59         Button2DragTest test = new Button2DragTest();
  60         try {
  61             test.run();
  62         } finally {
  63             if (frame != null) {
  64                 frame.dispose();
  65             }
  66         }
  67     }
  68 
  69     public void run() {
  70         frame = new Frame();
  71 
  72         final DragSourceListener dragSourceListener = new DragSourceAdapter() {
  73             public void dragDropEnd(DragSourceDropEvent e) {
  74                 dropSuccess = e.getDropSuccess();
  75                 System.err.println("Drop was successful: " + dropSuccess);
  76             }
  77         };
  78         DragGestureListener dragGestureListener = new DragGestureListener() {
  79             public void dragGestureRecognized(DragGestureEvent dge) {
  80                 dge.startDrag(null, new StringSelection("OK"), dragSourceListener);
  81             }
  82         };
  83         new DragSource().createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_MOVE,
  84                                                             dragGestureListener);
  85 
  86         DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
  87             public void drop(DropTargetDropEvent dtde) {
  88                 dtde.acceptDrop(DnDConstants.ACTION_MOVE);
  89                 dtde.dropComplete(true);
  90                 System.err.println("Drop");
  91             }
  92         };
  93         new DropTarget(frame, dropTargetListener);
  94 
  95         //What would normally go into main() will probably go here.
  96         //Use System.out.println for diagnostic messages that you want
  97         //to read after the test is done.
  98         frame.setUndecorated(true);
  99         frame.setBounds(100, 100, 200, 200);
 100         frame.setLocationRelativeTo(null);
 101         frame.setVisible(true);
 102 
 103         Robot robot = Util.createRobot();
 104 
 105         Util.waitForIdle(robot);
 106 
 107         Point startPoint = frame.getLocationOnScreen();
 108         Point endPoint = new Point(startPoint);
 109         startPoint.translate(50, 50);
 110         endPoint.translate(150, 150);
 111 
 112         Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK);
 113 
 114         Util.waitForIdle(robot);
 115         robot.delay(500);
 116 
 117         if (dropSuccess) {
 118             System.err.println("test passed");
 119         } else {
 120             throw new RuntimeException("test failed: drop was not successful");
 121         }
 122     }
 123 
 124 }