test/jdk/java/awt/Mouse/GetMousePositionTest/GetMousePositionWithOverlay.java
changeset 49222 eb15e0ca2208
parent 47216 71c04702a3d5
equal deleted inserted replaced
49221:d8d1061ce34c 49222:eb15e0ca2208
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 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.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 import test.java.awt.regtesthelpers.Util;
    24 import java.awt.Frame;
       
    25 import java.awt.Point;
       
    26 import java.awt.Robot;
       
    27 import java.awt.Rectangle;
    25 
    28 
    26 import javax.swing.*;
    29 /*
    27 import java.awt.*;
       
    28 import java.util.concurrent.atomic.AtomicReference;
       
    29 
       
    30 /**
       
    31  * @test
    30  * @test
    32  * @key headful
    31  * @key headful
    33  * @bug 8012026
    32  * @bug 8012026 8196435
    34  * @summary Component.getMousePosition() does not work in an applet on MacOS
    33  * @summary Component.getMousePosition() does not work in an applet on MacOS
    35  * @author Petr Pchelko
    34  * @run main GetMousePositionWithOverlay
    36  * @library ../../regtesthelpers
       
    37  * @build Util
       
    38  * @compile GetMousePositionWithOverlay.java
       
    39  * @run main/othervm GetMousePositionWithOverlay
       
    40  */
    35  */
    41 
    36 
    42 public class GetMousePositionWithOverlay {
    37 public class GetMousePositionWithOverlay {
    43 
    38 
    44     static Frame backFrame;
    39     private static Frame backFrame;
    45     static Frame frontFrame;
    40     private static Frame frontFrame;
       
    41     private static Robot robot;
    46 
    42 
    47     public static void main(String[] args) throws Throwable {
    43     public static void main(String[] args) throws Throwable {
    48         try {
    44         robot = new Robot();
    49             SwingUtilities.invokeAndWait(new Runnable() {
       
    50                 @Override
       
    51                 public void run() {
       
    52                     constructTestUI();
       
    53                 }
       
    54             });
       
    55             Util.waitForIdle(null);
       
    56 
    45 
    57             Robot r = new Robot();
    46         try{
    58             Util.pointOnComp(frontFrame, r);
    47             constructTestUI();
    59             Util.waitForIdle(null);
    48         } catch (Exception e) {
       
    49             dispose();
       
    50             throw new RuntimeException("Unexpected Exception!");
       
    51         }
    60 
    52 
    61             Point pos = getMousePosition(backFrame);
    53         robot.waitForIdle();
    62             if (pos != null) {
       
    63                 throw new RuntimeException("Test failed. Mouse position should be null but was" + pos);
       
    64             }
       
    65 
    54 
    66             pos = getMousePosition(frontFrame);
    55         doTest();
    67             if (pos == null) {
    56         dispose();
    68                 throw new RuntimeException("Test failed. Mouse position should not be null");
       
    69             }
       
    70 
       
    71             r.mouseMove(189, 189);
       
    72             Util.waitForIdle(null);
       
    73 
       
    74             pos = getMousePosition(backFrame);
       
    75             if (pos == null) {
       
    76                 throw new RuntimeException("Test failed. Mouse position should not be null");
       
    77             }
       
    78         } finally {
       
    79             SwingUtilities.invokeLater(new Runnable() {
       
    80                 @Override
       
    81                 public void run() {
       
    82                     backFrame.dispose();
       
    83                     frontFrame.dispose();
       
    84                 }
       
    85             });
       
    86         }
       
    87     }
    57     }
    88 
    58 
    89     private static Point getMousePosition(final Component component) throws Exception {
    59     private static void doTest() {
    90         final AtomicReference<Point> pos = new AtomicReference<Point>();
    60 
    91         SwingUtilities.invokeAndWait(new Runnable() {
    61         frontFrame.toFront();
    92             @Override
    62         robot.waitForIdle();
    93             public void run() {
    63 
    94                 pos.set(component.getMousePosition());
    64         Rectangle bounds = new Rectangle(frontFrame.getLocationOnScreen(), frontFrame.getSize());
    95             }
    65         robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    96         });
    66         robot.waitForIdle();
    97         return pos.get();
    67 
       
    68         Point pos = backFrame.getMousePosition();
       
    69         if (pos != null) {
       
    70             dispose();
       
    71             throw new RuntimeException("Test failed. Mouse position should be null but was " + pos);
       
    72         }
       
    73 
       
    74         pos = frontFrame.getMousePosition();
       
    75         if (pos == null) {
       
    76             dispose();
       
    77             throw new RuntimeException("Test failed. Mouse position should not be null");
       
    78         }
       
    79 
       
    80         robot.mouseMove(189, 189);
       
    81         robot.waitForIdle();
       
    82 
       
    83         pos = backFrame.getMousePosition();
       
    84         if (pos == null) {
       
    85             dispose();
       
    86             throw new RuntimeException("Test failed. Mouse position should not be null");
       
    87         }
       
    88 
       
    89     }
       
    90 
       
    91     private static void dispose() {
       
    92 
       
    93         if (backFrame != null) {
       
    94             backFrame.dispose();
       
    95         }
       
    96 
       
    97         if (frontFrame != null) {
       
    98             frontFrame.dispose();
       
    99         }
    98     }
   100     }
    99 
   101 
   100     private static void constructTestUI() {
   102     private static void constructTestUI() {
   101         backFrame = new Frame();
   103         backFrame = new Frame();
   102         backFrame.setBounds(100, 100, 100, 100);
   104         backFrame.setBounds(100, 100, 100, 100);
       
   105         backFrame.setResizable(false);
   103         backFrame.setVisible(true);
   106         backFrame.setVisible(true);
   104 
   107 
   105         frontFrame = new Frame();
   108         frontFrame = new Frame();
   106         frontFrame.setBounds(120, 120, 60, 60);
   109         frontFrame.setBounds(120, 120, 60, 60);
       
   110         frontFrame.setResizable(false);
   107         frontFrame.setVisible(true);
   111         frontFrame.setVisible(true);
       
   112 
   108     }
   113     }
   109 }
   114 }
       
   115