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  * @bug 8012026
  33  * @summary Component.getMousePosition() does not work in an applet on MacOS
  34  * @author Petr Pchelko
  35  * @library ../../regtesthelpers
  36  * @build Util
  37  * @compile GetMousePositionWithOverlay.java
  38  * @run main/othervm GetMousePositionWithOverlay
  39  */
  40 
  41 public class GetMousePositionWithOverlay {
  42 
  43     static Frame backFrame;
  44     static Frame frontFrame;
  45 
  46     public static void main(String[] args) throws Throwable {
  47         try {
  48             SwingUtilities.invokeAndWait(new Runnable() {
  49                 @Override
  50                 public void run() {
  51                     constructTestUI();
  52                 }
  53             });
  54             Util.waitForIdle(null);
  55 
  56             Robot r = new Robot();
  57             Util.pointOnComp(frontFrame, r);
  58             Util.waitForIdle(null);
  59 
  60             Point pos = getMousePosition(backFrame);
  61             if (pos != null) {
  62                 throw new RuntimeException("Test failed. Mouse position should be null but was" + pos);
  63             }
  64 
  65             pos = getMousePosition(frontFrame);
  66             if (pos == null) {
  67                 throw new RuntimeException("Test failed. Mouse position should not be null");
  68             }
  69 
  70             r.mouseMove(189, 189);
  71             Util.waitForIdle(null);
  72 
  73             pos = getMousePosition(backFrame);
  74             if (pos == null) {
  75                 throw new RuntimeException("Test failed. Mouse position should not be null");
  76             }
  77         } finally {
  78             SwingUtilities.invokeLater(new Runnable() {
  79                 @Override
  80                 public void run() {
  81                     backFrame.dispose();
  82                     frontFrame.dispose();
  83                 }
  84             });
  85         }
  86     }
  87 
  88     private static Point getMousePosition(final Component component) throws Exception {
  89         final AtomicReference<Point> pos = new AtomicReference<Point>();
  90         SwingUtilities.invokeAndWait(new Runnable() {
  91             @Override
  92             public void run() {
  93                 pos.set(component.getMousePosition());
  94             }
  95         });
  96         return pos.get();
  97     }
  98 
  99     private static void constructTestUI() {
 100         backFrame = new Frame();
 101         backFrame.setBounds(100, 100, 100, 100);
 102         backFrame.setVisible(true);
 103 
 104         frontFrame = new Frame();
 105         frontFrame.setBounds(120, 120, 60, 60);
 106         frontFrame.setVisible(true);
 107     }
 108 }