jdk/test/java/awt/MouseInfo/ComponentMousePositionTest.java
changeset 44138 cb6ec1a590a9
equal deleted inserted replaced
44137:30c24220ea10 44138:cb6ec1a590a9
       
     1 /*
       
     2  * Copyright (c) 2017, 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 4009555 7119774
       
    28  @summary Test for Component's method getMousePosition()
       
    29  @run main ComponentMousePositionTest
       
    30  */
       
    31 import java.awt.Frame;
       
    32 import java.awt.Panel;
       
    33 import java.awt.Button;
       
    34 import java.awt.Dimension;
       
    35 import java.awt.Point;
       
    36 import java.awt.Robot;
       
    37 import java.awt.AWTException;
       
    38 
       
    39 public class ComponentMousePositionTest {
       
    40 
       
    41     public void changeMousePosition() {
       
    42         Frame frame = new Frame();
       
    43         Panel panel = new Panel();
       
    44         Button button = new Button("Button");
       
    45         Button overlappedButton = new Button("Overlapped button");
       
    46         Dimension BUTTON_DIMENSION = new Dimension(100, 100);
       
    47         Dimension FRAME_DIMENSION = new Dimension(200, 200);
       
    48         Point POINT_WITHOUT_COMPONENTS = new Point(10, 10);
       
    49         Point FIRST_BUTTON_LOCATION = new Point(20, 20);
       
    50         Point SECOND_BUTTON_LOCATION = new Point(30, 30);
       
    51 
       
    52         button.setSize(BUTTON_DIMENSION);
       
    53         button.setLocation(FIRST_BUTTON_LOCATION);
       
    54         overlappedButton.setSize(BUTTON_DIMENSION);
       
    55         overlappedButton.setLocation(SECOND_BUTTON_LOCATION);
       
    56 
       
    57         panel.setLayout(null);
       
    58         panel.add(button);
       
    59         panel.add(overlappedButton);
       
    60         frame.add(panel);
       
    61         Robot robot;
       
    62 
       
    63         try {
       
    64             robot = new Robot();
       
    65             frame.setSize(FRAME_DIMENSION);
       
    66             frame.setVisible(true);
       
    67             robot.delay(2000);
       
    68 
       
    69             Point p = button.getLocationOnScreen();
       
    70             robot.mouseMove(p.x + button.getWidth()
       
    71                     / 2, p.y + button.getHeight() / 2);
       
    72             robot.waitForIdle();
       
    73 
       
    74             Point pMousePosition = button.getMousePosition();
       
    75             if (pMousePosition == null) {
       
    76                 throw new RuntimeException("Test failed: "
       
    77                         + "Component.getMousePosition() returned null result "
       
    78                         + "inside Component");
       
    79             }
       
    80 
       
    81             if (pMousePosition.x != button.getWidth() / 2
       
    82                     || pMousePosition.y != button.getHeight() / 2) {
       
    83                 throw new RuntimeException("Test failed: "
       
    84                         + "Component.getMousePosition() returned incorrect "
       
    85                         + "result inside Component");
       
    86             }
       
    87 
       
    88             pMousePosition = overlappedButton.getMousePosition();
       
    89             if (pMousePosition != null) {
       
    90                 throw new RuntimeException("Test failed: Overlapped component "
       
    91                         + "did not return null result when a pointer was inside"
       
    92                         + " the components bounds.");
       
    93             }
       
    94             robot.mouseMove(panel.getLocationOnScreen().x
       
    95                     + POINT_WITHOUT_COMPONENTS.x,
       
    96                     panel.getLocationOnScreen().y
       
    97                     + POINT_WITHOUT_COMPONENTS.y);
       
    98             robot.waitForIdle();
       
    99 
       
   100             pMousePosition = button.getMousePosition();
       
   101             if (pMousePosition != null) {
       
   102                 throw new RuntimeException("FAILED: "
       
   103                         + "Component.getMousePosition() returned non-null "
       
   104                         + "results outside Component");
       
   105             }
       
   106 
       
   107         } catch (AWTException e) {
       
   108             throw new RuntimeException("FAILED: AWTException by Robot" + e);
       
   109         } finally {
       
   110             frame.dispose();
       
   111         }
       
   112     }
       
   113 
       
   114     public static void main(String args[]) {
       
   115         ComponentMousePositionTest mousePos = new ComponentMousePositionTest();
       
   116         mousePos.changeMousePosition();
       
   117     }
       
   118 }