1 /*
   2  * Copyright (c) 2013, 2016 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 java.awt.Frame;
  27 import java.awt.Point;
  28 import java.awt.Robot;
  29 import java.awt.event.MouseEvent;
  30 import java.awt.event.MouseMotionAdapter;
  31 import java.awt.event.MouseAdapter;
  32 
  33 /*
  34  * @test
  35  * @bug 8012026 8027154
  36  * @summary Component.getMousePosition() does not work in an applet on MacOS.
  37  * @library ../../regtesthelpers
  38  * @build Util
  39  * @compile GetMousePositionWithPopup.java
  40  * @run main/othervm GetMousePositionWithPopup
  41  */
  42 
  43 public class GetMousePositionWithPopup {
  44 
  45     private static Frame frame1;
  46     private static Frame frame2;
  47     private static Robot robot = null;
  48     private static volatile boolean verified = false;
  49 
  50     public static void main(String[] args) throws Exception {
  51         try {
  52             frame1 = new Frame("frame1");
  53             frame1.setBounds(100, 100, 100, 100);
  54             frame2 = new Frame("frame2");
  55             frame2.setBounds(120, 120, 120, 120);
  56             frame1.setVisible(true);
  57             frame2.setVisible(true);
  58             robot = Util.createRobot();
  59             robot.mouseMove(0, 0);
  60             Util.waitForIdle(robot);
  61 
  62             frame2.addMouseMotionListener(new MouseMotionAdapter() {
  63                 @Override
  64                 public void mouseMoved(MouseEvent e) {
  65                     if (!verified) {
  66                         verifyMousePosition();
  67                     }
  68                 }
  69             });
  70 
  71             frame2.addMouseListener(new MouseAdapter() {
  72                 @Override
  73                 public void mouseEntered(MouseEvent e) {
  74                     if (!verified) {
  75                         verifyMousePosition();
  76                     }
  77                 }
  78             });
  79 
  80             Util.waitForIdle(robot);
  81             Point loc = frame2.getLocationOnScreen();
  82             robot.mouseMove(loc.x + 30, loc.y + 30);
  83             Util.waitForIdle(robot);
  84             robot.delay(200);
  85             if (!verified) {
  86                 throw new RuntimeException(
  87                     "No mouse events generated for frame");
  88             }
  89         } finally {
  90             frame2.dispose();
  91             frame1.dispose();
  92             frame1 = null;
  93             frame2 = null;
  94         }
  95     }
  96 
  97     public static void verifyMousePosition() {
  98         verified = true;
  99         Point posInFrm2 = frame2.getMousePosition();
 100         if (!(isValueInRange(posInFrm2.x) && isValueInRange(posInFrm2.y))) {
 101             throw new RuntimeException("Wrong position reported. Should be in"
 102                 + " range from [25, 25] to [35, 35] but was ["
 103                 + posInFrm2.x + ", " + posInFrm2.y + "]");
 104         }
 105 
 106         Point positionInFrame1 = frame1.getMousePosition();
 107         if (positionInFrame1 != null) {
 108             throw new RuntimeException(
 109                 "Wrong position reported. Should be null");
 110         }
 111     }
 112 
 113     public static boolean isValueInRange(int value) {
 114         /*
 115          * Since all operating systems do not return exact value, hence range
 116          * is used.
 117          */
 118         return value >= 25 && value <= 35;
 119     }
 120 }