test/jdk/java/awt/dnd/ImageDecoratedDnDNegative/ImageDecoratedDnDNegative.java
changeset 54231 e4813eded7cb
parent 47216 71c04702a3d5
equal deleted inserted replaced
54230:d9d9655bb077 54231:e4813eded7cb
     1 /*
     1 /*
     2  * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25   test %W% %E%
    25   @test
    26   @bug 4874070 7146550
    26   @key headful
       
    27   @bug 4874070
    27   @summary Tests basic DnD functionality
    28   @summary Tests basic DnD functionality
    28   @author Your Name: Alexey Utkin area=dnd
    29   @run main ImageDecoratedDnDNegative
    29   @run applet ImageDecoratedDnDNegative.html
       
    30 */
    30 */
    31 
    31 
    32 import java.applet.Applet;
       
    33 import java.awt.*;
    32 import java.awt.*;
    34 import java.awt.Robot;
    33 import java.awt.Robot;
    35 import java.awt.event.InputEvent;
    34 import java.awt.event.InputEvent;
    36 import java.awt.event.KeyEvent;
    35 import java.awt.event.KeyEvent;
    37 import java.awt.geom.Point2D;
    36 import java.awt.geom.Point2D;
    38 
    37 
    39 
    38 
    40 import java.awt.dnd.DragSource;
    39 import java.awt.dnd.DragSource;
    41 
    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 {
    42 
    59 
    43 public class ImageDecoratedDnDNegative extends Applet {
    60     public static void moveTo(
    44     //Declare things used in the test, like buttons and labels here
       
    45 
       
    46     public void init() {
       
    47         //Create instructions for the user here, as well as set up
       
    48         // the environment -- set the layout manager, add buttons,
       
    49         // etc.
       
    50         this.setLayout(new BorderLayout());
       
    51 
       
    52         String[] instructions =
       
    53                 {
       
    54                         "Automatic test.",
       
    55                         "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
       
    56                         "a red panel, will appear below. ",
       
    57                         "1. The button would be clicked and dragged to the red panel. ",
       
    58                         "2. When the mouse enters the red panel during the drag, the panel ",
       
    59                         "should turn yellow. On the systems that supports pictured drag, ",
       
    60                         "the image under the drag-cursor should appear (ancor is shifted ",
       
    61                         "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
       
    62                         "In WIN32 systems the image under cursor would be visible ONLY over ",
       
    63                         "the drop targets with activated extended OLE D\'n\'D support (that are ",
       
    64                         "the desktop and IE ).",
       
    65                         "3. The mouse would be released.",
       
    66                         "The panel should turn red again and a yellow button labeled ",
       
    67                         "\"Drag ME!\" should appear inside the panel. You should be able ",
       
    68                         "to repeat this operation multiple times."
       
    69                 };
       
    70         Sysout.createDialogWithInstructions(instructions);
       
    71 
       
    72     }//End  init()
       
    73 
       
    74     public void moveTo(
       
    75         Robot r,
    61         Robot r,
    76         Point b,
    62         Point b,
    77         Point e)
    63         Point e)
    78     {
    64     {
    79         Point2D.Double ee = new Point2D.Double(e.getX(), e.getY());
    65         Point2D.Double ee = new Point2D.Double(e.getX(), e.getY());
    93                 (int)ee.getX(),
    79                 (int)ee.getX(),
    94                 (int)ee.getY());
    80                 (int)ee.getY());
    95         r.delay(5);
    81         r.delay(5);
    96     }
    82     }
    97 
    83 
    98     public void start() {
    84     public static void main(final String[] args) {
    99         Frame f = new Frame("Use keyboard for DnD change");
    85         Frame f = new Frame("Use keyboard for DnD change");
   100         Panel mainPanel;
    86         Panel mainPanel;
   101         Component dragSource, dropTarget;
    87         Component dragSource, dropTarget;
   102 
    88 
   103         f.setBounds(0, 400, 200, 200);
    89         f.setBounds(0, 400, 200, 200);
   112         dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
    98         dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
   113 
    99 
   114         mainPanel.add(dragSource, "North");
   100         mainPanel.add(dragSource, "North");
   115         mainPanel.add(dropTarget, "Center");
   101         mainPanel.add(dropTarget, "Center");
   116         f.add(mainPanel, BorderLayout.CENTER);
   102         f.add(mainPanel, BorderLayout.CENTER);
   117 
   103         f.setUndecorated(true);
       
   104         f.setLocationRelativeTo(null);
   118         f.setVisible(true);
   105         f.setVisible(true);
   119 
   106 
   120         Point sourcePoint = dragSource.getLocationOnScreen();
   107         Point sourcePoint = dragSource.getLocationOnScreen();
   121         Dimension d = dragSource.getSize();
   108         Dimension d = dragSource.getSize();
   122         sourcePoint.translate(d.width / 2, d.height / 2);
   109         sourcePoint.translate(d.width / 2, d.height / 2);
   123 
   110 
   124         try {
   111         try {
   125             Robot robot = new Robot();
   112             Robot robot = new Robot();
       
   113             robot.waitForIdle();
   126             robot.mouseMove(sourcePoint.x, sourcePoint.y);
   114             robot.mouseMove(sourcePoint.x, sourcePoint.y);
   127             Point start = new Point(
   115             Point start = new Point(
   128                     sourcePoint.x,
   116                     sourcePoint.x,
   129                     sourcePoint.y);
   117                     sourcePoint.y);
   130             Point out = new Point(
   118             Point out = new Point(
   153             }
   141             }
   154         } catch( Exception e){
   142         } catch( Exception e){
   155             e.printStackTrace();
   143             e.printStackTrace();
   156             throw new RuntimeException("test failed: drop was not successful with exception " + e);
   144             throw new RuntimeException("test failed: drop was not successful with exception " + e);
   157         }
   145         }
   158     }// start()
   146     }
   159 }// class DnDAcceptanceTest
   147 }// class DnDAcceptanceTest
   160 
       
   161 
       
   162 /**
       
   163  * *************************************************
       
   164  * Standard Test Machinery
       
   165  * DO NOT modify anything below -- it's a standard
       
   166  * chunk of code whose purpose is to make user
       
   167  * interaction uniform, and thereby make it simpler
       
   168  * to read and understand someone else's test.
       
   169  * **************************************************
       
   170  */
       
   171 class Sysout {
       
   172     private static TestDialog dialog;
       
   173 
       
   174     public static void createDialogWithInstructions(String[] instructions) {
       
   175         dialog = new TestDialog(new Frame(), "Instructions");
       
   176         dialog.printInstructions(instructions);
       
   177         dialog.show();
       
   178         println("Any messages for the tester will display here.");
       
   179     }
       
   180 
       
   181     public static void createDialog() {
       
   182         dialog = new TestDialog(new Frame(), "Instructions");
       
   183         String[] defInstr = {"Instructions will appear here. ", ""};
       
   184         dialog.printInstructions(defInstr);
       
   185         dialog.show();
       
   186         println("Any messages for the tester will display here.");
       
   187     }
       
   188 
       
   189 
       
   190     public static void printInstructions(String[] instructions) {
       
   191         dialog.printInstructions(instructions);
       
   192     }
       
   193 
       
   194 
       
   195     public static void println(String messageIn) {
       
   196         dialog.displayMessage(messageIn);
       
   197     }
       
   198 
       
   199 }// Sysout  class
       
   200 
       
   201 
       
   202 class TestDialog extends Dialog {
       
   203 
       
   204     TextArea instructionsText;
       
   205     TextArea messageText;
       
   206     int maxStringLength = 80;
       
   207 
       
   208     //DO NOT call this directly, go through Sysout
       
   209     public TestDialog(Frame frame, String name) {
       
   210         super(frame, name);
       
   211         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   212         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
       
   213         add("North", instructionsText);
       
   214 
       
   215         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
       
   216         add("South", messageText);
       
   217 
       
   218         pack();
       
   219 
       
   220         show();
       
   221     }// TestDialog()
       
   222 
       
   223     //DO NOT call this directly, go through Sysout
       
   224     public void printInstructions(String[] instructions) {
       
   225         //Clear out any current instructions
       
   226         instructionsText.setText("");
       
   227 
       
   228         //Go down array of instruction strings
       
   229 
       
   230         String printStr, remainingStr;
       
   231         for (int i = 0; i < instructions.length; i++) {
       
   232             //chop up each into pieces maxSringLength long
       
   233             remainingStr = instructions[i];
       
   234             while (remainingStr.length() > 0) {
       
   235                 //if longer than max then chop off first max chars to print
       
   236                 if (remainingStr.length() >= maxStringLength) {
       
   237                     //Try to chop on a word boundary
       
   238                     int posOfSpace = remainingStr.
       
   239                             lastIndexOf(' ', maxStringLength - 1);
       
   240 
       
   241                     if (posOfSpace <= 0) posOfSpace = maxStringLength - 1;
       
   242 
       
   243                     printStr = remainingStr.substring(0, posOfSpace + 1);
       
   244                     remainingStr = remainingStr.substring(posOfSpace + 1);
       
   245                 }
       
   246                 //else just print
       
   247                 else {
       
   248                     printStr = remainingStr;
       
   249                     remainingStr = "";
       
   250                 }
       
   251 
       
   252                 instructionsText.append(printStr + "\n");
       
   253 
       
   254             }// while
       
   255 
       
   256         }// for
       
   257 
       
   258     }//printInstructions()
       
   259 
       
   260     //DO NOT call this directly, go through Sysout
       
   261     public void displayMessage(String messageIn) {
       
   262         messageText.append(messageIn + "\n");
       
   263     }
       
   264 
       
   265 }// TestDialog  class
       
   266