test/jdk/java/awt/dnd/ImageDecoratedDnDInOut/ImageDecoratedDnDInOut.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   @key headful
    26   @bug 4874070
    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 ImageDecoratedDnDInOut
    29   @run applet ImageDecoratedDnDInOut.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.dnd.DragSource;
    35 import java.awt.dnd.DragSource;
    37 
    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 {
    38 
    54 
    39 public class ImageDecoratedDnDInOut extends Applet {
    55     public static void main(final String[] args) {
    40     //Declare things used in the test, like buttons and labels here
       
    41 
       
    42     public void init() {
       
    43         //Create instructions for the user here, as well as set up
       
    44         // the environment -- set the layout manager, add buttons,
       
    45         // etc.
       
    46         this.setLayout(new BorderLayout());
       
    47 
       
    48         String[] instructions =
       
    49                 {
       
    50                         "Automatic test.",
       
    51                         "A Frame, which contains a yellow button labeled \"Drag ME!\" and ",
       
    52                         "a red panel, will appear below. ",
       
    53                         "1. The button would be clicked and dragged to the red panel. ",
       
    54                         "2. When the mouse enters the red panel during the drag, the panel ",
       
    55                         "should turn yellow. On the systems that supports pictured drag, ",
       
    56                         "the image under the drag-cursor should appear (ancor is shifted ",
       
    57                         "from top-left corner of the picture inside the picture to 10pt in both dimensions ). ",
       
    58                         "In WIN32 systems the image under cursor would be visible ONLY over ",
       
    59                         "the drop targets with activated extended OLE D\'n\'D support (that are ",
       
    60                         "the desktop and IE ).",
       
    61                         "3. The mouse would be released.",
       
    62                         "The panel should turn red again and a yellow button labeled ",
       
    63                         "\"Drag ME!\" should appear inside the panel. "
       
    64                 };
       
    65         Sysout.createDialogWithInstructions(instructions);
       
    66 
       
    67     }//End  init()
       
    68 
       
    69     public void start() {
       
    70         Frame f = new Frame("Use keyboard for DnD change");
    56         Frame f = new Frame("Use keyboard for DnD change");
    71         Panel mainPanel;
    57         Panel mainPanel;
    72         Component dragSource, dropTarget;
    58         Component dragSource, dropTarget;
    73 
    59 
    74         f.setBounds(0, 400, 200, 200);
    60         f.setSize(200, 200);
       
    61         f.setUndecorated(true);
       
    62         f.setLocationRelativeTo(null);
    75         f.setLayout(new BorderLayout());
    63         f.setLayout(new BorderLayout());
    76 
    64 
    77         mainPanel = new Panel();
    65         mainPanel = new Panel();
    78         mainPanel.setLayout(new BorderLayout());
    66         mainPanel.setLayout(new BorderLayout());
    79 
    67 
    91             Point sourcePoint = dragSource.getLocationOnScreen();
    79             Point sourcePoint = dragSource.getLocationOnScreen();
    92             Dimension d = dragSource.getSize();
    80             Dimension d = dragSource.getSize();
    93             sourcePoint.translate(d.width / 2, d.height / 2);
    81             sourcePoint.translate(d.width / 2, d.height / 2);
    94 
    82 
    95             Robot robot = new Robot();
    83             Robot robot = new Robot();
       
    84             robot.waitForIdle();
    96             robot.mouseMove(sourcePoint.x, sourcePoint.y);
    85             robot.mouseMove(sourcePoint.x, sourcePoint.y);
    97             robot.mousePress(InputEvent.BUTTON1_MASK);
    86             robot.mousePress(InputEvent.BUTTON1_MASK);
    98             Thread.sleep(2000);
    87             Thread.sleep(2000);
    99             for(int i = 0; i <100; ++i) {
    88             for(int i = 0; i <100; ++i) {
   100                 robot.mouseMove(
    89                 robot.mouseMove(
   119         } catch( Exception e){
   108         } catch( Exception e){
   120         e.printStackTrace();
   109         e.printStackTrace();
   121             throw new RuntimeException("test failed: drop was not successful with exception " + e);
   110             throw new RuntimeException("test failed: drop was not successful with exception " + e);
   122         }
   111         }
   123 
   112 
   124     }// start()
   113     }
   125 }// class DnDAcceptanceTest
   114 }// class DnDAcceptanceTest
   126 
       
   127 
       
   128 /**
       
   129  * *************************************************
       
   130  * Standard Test Machinery
       
   131  * DO NOT modify anything below -- it's a standard
       
   132  * chunk of code whose purpose is to make user
       
   133  * interaction uniform, and thereby make it simpler
       
   134  * to read and understand someone else's test.
       
   135  * **************************************************
       
   136  */
       
   137 class Sysout {
       
   138     private static TestDialog dialog;
       
   139 
       
   140     public static void createDialogWithInstructions(String[] instructions) {
       
   141         dialog = new TestDialog(new Frame(), "Instructions");
       
   142         dialog.printInstructions(instructions);
       
   143         dialog.show();
       
   144         println("Any messages for the tester will display here.");
       
   145     }
       
   146 
       
   147     public static void createDialog() {
       
   148         dialog = new TestDialog(new Frame(), "Instructions");
       
   149         String[] defInstr = {"Instructions will appear here. ", ""};
       
   150         dialog.printInstructions(defInstr);
       
   151         dialog.show();
       
   152         println("Any messages for the tester will display here.");
       
   153     }
       
   154 
       
   155 
       
   156     public static void printInstructions(String[] instructions) {
       
   157         dialog.printInstructions(instructions);
       
   158     }
       
   159 
       
   160 
       
   161     public static void println(String messageIn) {
       
   162         dialog.displayMessage(messageIn);
       
   163     }
       
   164 
       
   165 }// Sysout  class
       
   166 
       
   167 
       
   168 class TestDialog extends Dialog {
       
   169 
       
   170     TextArea instructionsText;
       
   171     TextArea messageText;
       
   172     int maxStringLength = 80;
       
   173 
       
   174     //DO NOT call this directly, go through Sysout
       
   175     public TestDialog(Frame frame, String name) {
       
   176         super(frame, name);
       
   177         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   178         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
       
   179         add("North", instructionsText);
       
   180 
       
   181         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
       
   182         add("South", messageText);
       
   183 
       
   184         pack();
       
   185 
       
   186         show();
       
   187     }// TestDialog()
       
   188 
       
   189     //DO NOT call this directly, go through Sysout
       
   190     public void printInstructions(String[] instructions) {
       
   191         //Clear out any current instructions
       
   192         instructionsText.setText("");
       
   193 
       
   194         //Go down array of instruction strings
       
   195 
       
   196         String printStr, remainingStr;
       
   197         for (int i = 0; i < instructions.length; i++) {
       
   198             //chop up each into pieces maxSringLength long
       
   199             remainingStr = instructions[i];
       
   200             while (remainingStr.length() > 0) {
       
   201                 //if longer than max then chop off first max chars to print
       
   202                 if (remainingStr.length() >= maxStringLength) {
       
   203                     //Try to chop on a word boundary
       
   204                     int posOfSpace = remainingStr.
       
   205                             lastIndexOf(' ', maxStringLength - 1);
       
   206 
       
   207                     if (posOfSpace <= 0) posOfSpace = maxStringLength - 1;
       
   208 
       
   209                     printStr = remainingStr.substring(0, posOfSpace + 1);
       
   210                     remainingStr = remainingStr.substring(posOfSpace + 1);
       
   211                 }
       
   212                 //else just print
       
   213                 else {
       
   214                     printStr = remainingStr;
       
   215                     remainingStr = "";
       
   216                 }
       
   217 
       
   218                 instructionsText.append(printStr + "\n");
       
   219 
       
   220             }// while
       
   221 
       
   222         }// for
       
   223 
       
   224     }//printInstructions()
       
   225 
       
   226     //DO NOT call this directly, go through Sysout
       
   227     public void displayMessage(String messageIn) {
       
   228         messageText.append(messageIn + "\n");
       
   229     }
       
   230 
       
   231 }// TestDialog  class
       
   232