1 /*
   2  * Copyright (c) 2013, 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 test.java.awt.regtesthelpers.Util;
  25 
  26 import javax.swing.*;
  27 import java.awt.*;
  28 import java.util.concurrent.atomic.AtomicReference;
  29 
  30 /**
  31  * @test
  32  * @key headful
  33  * @bug 8012026
  34  * @summary Component.getMousePosition() does not work in an applet on MacOS
  35  * @author Petr Pchelko
  36  * @library ../../regtesthelpers
  37  * @build Util
  38  * @compile GetMousePositionWithOverlay.java
  39  * @run main/othervm GetMousePositionWithOverlay
  40  */
  41 
  42 public class GetMousePositionWithOverlay {
  43 
  44     static Frame backFrame;
  45     static Frame frontFrame;
  46 
  47     public static void main(String[] args) throws Throwable {
  48         try {
  49             SwingUtilities.invokeAndWait(new Runnable() {
  50                 @Override
  51                 public void run() {
  52                     constructTestUI();
  53                 }
  54             });
  55             Util.waitForIdle(null);
  56 
  57             Robot r = new Robot();
  58             Util.pointOnComp(frontFrame, r);
  59             Util.waitForIdle(null);
  60 
  61             Point pos = getMousePosition(backFrame);
  62             if (pos != null) {
  63                 throw new RuntimeException("Test failed. Mouse position should be null but was" + pos);
  64             }
  65 
  66             pos = getMousePosition(frontFrame);
  67             if (pos == null) {
  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     }
  88 
  89     private static Point getMousePosition(final Component component) throws Exception {
  90         final AtomicReference<Point> pos = new AtomicReference<Point>();
  91         SwingUtilities.invokeAndWait(new Runnable() {
  92             @Override
  93             public void run() {
  94                 pos.set(component.getMousePosition());
  95             }
  96         });
  97         return pos.get();
  98     }
  99 
 100     private static void constructTestUI() {
 101         backFrame = new Frame();
 102         backFrame.setBounds(100, 100, 100, 100);
 103         backFrame.setVisible(true);
 104 
 105         frontFrame = new Frame();
 106         frontFrame.setBounds(120, 120, 60, 60);
 107         frontFrame.setVisible(true);
 108     }
 109 }