< prev index next >

jdk/test/java/awt/Mouse/GetMousePositionTest/GetMousePositionWithPopup.java

Print this page


   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.awt.event.MouseEvent;
  29 import java.awt.event.MouseMotionAdapter;

  30 
  31 /**
  32  * @test
  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 GetMousePositionWithPopup.java
  39  * @run main/othervm GetMousePositionWithPopup
  40  */
  41 
  42 public class GetMousePositionWithPopup {
  43 
  44     private static Frame frame1;
  45     private static Frame frame2;


  46 
  47     public static void main(String[] args) throws Exception {
  48         try {
  49         Robot r = Util.createRobot();
  50         r.mouseMove(0, 0);
  51         Util.waitForIdle(null);








  52 
  53         SwingUtilities.invokeAndWait(new Runnable() {
  54             @Override
  55             public void run() {
  56                 constructTestUI();



  57             }
  58         });
  59 
  60         Util.waitForIdle(null);
  61         r.mouseMove(149, 149);
  62         Util.waitForIdle(null);
  63         r.mouseMove(150, 150);
  64         Util.waitForIdle(null);
  65 
  66         } finally {
  67             SwingUtilities.invokeLater(new Runnable() {
  68                 @Override
  69                 public void run() {
  70                     frame1.dispose();
  71                     frame2.dispose();


  72                 }
  73             });








  74         }





  75     }
  76 
  77     private static void constructTestUI() {
  78         frame1 = new Frame();
  79         frame1.setBounds(100, 100, 100, 100);
  80         frame1.addMouseMotionListener(new MouseMotionAdapter() {
  81 
  82             private boolean shown = false;
  83 
  84             @Override
  85             public void mouseMoved(MouseEvent e) {
  86                 if (shown) {
  87                     return;
  88                 }
  89 
  90                 shown = true;
  91 
  92                 frame2 = new Frame();
  93                 frame2.setBounds(120, 120, 120, 120);
  94                 frame2.setVisible(true);
  95 
  96                 Point positionInFrame2 = frame2.getMousePosition();
  97                 if (positionInFrame2.x != 30 || positionInFrame2.y != 30) {
  98                     throw new RuntimeException("Wrong position reported. Should be [30, 30] but was [" +
  99                             positionInFrame2.x + ", " + positionInFrame2.y + "]");

 100                 }
 101 
 102                 Point positionInFrame1 = frame1.getMousePosition();
 103                 if (positionInFrame1 != null) {
 104                     throw new RuntimeException("Wrong position reported. Should be null");
 105                 }
 106 
 107             }
 108         });
 109         frame1.setVisible(true);





 110     }
 111 }
   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 

  64                 @Override
  65                 public void mouseMoved(MouseEvent e) {
  66                     System.out.println("Mouse Moved");
  67                     if (!verified) {
  68                         verifyMousePosition();
  69                     }
  70                 }
  71             });
  72 
  73             frame2.addMouseListener(new MouseAdapter() {







  74                 @Override
  75                 public void mouseEntered(MouseEvent e) {
  76                     System.out.println("Mouse Entered");
  77                     if (!verified) {
  78                         verifyMousePosition();
  79                     }
  80                 }
  81             });
  82 
  83             Util.waitForIdle(null);
  84             Point loc = frame2.getLocationOnScreen();
  85             robot.mouseMove(loc.x + 30, loc.y + 30);
  86             Util.waitForIdle(null);
  87             robot.delay(6000);
  88             if (!verified) {
  89                 throw new RuntimeException("No mouse events has been generated for the frame");
  90             }
  91         } finally {
  92             frame2.dispose();
  93             frame1.dispose();
  94             frame1 = null;
  95             frame2 = null;
  96         }












  97     }
  98 
  99     public static void verifyMousePosition() {
 100         verified = true;




 101         Point positionInFrame2 = frame2.getMousePosition();
 102         System.out.println(positionInFrame2);
 103         if (!(isValueInRange(positionInFrame2.x) && isValueInRange(positionInFrame2.y))) {
 104             throw new RuntimeException("Wrong position reported. Should be in range from [25, 25] to [35, 35] but was ["
 105                     + positionInFrame2.x + ", " + positionInFrame2.y + "]");
 106         }
 107 
 108         Point positionInFrame1 = frame1.getMousePosition();
 109         if (positionInFrame1 != null) {
 110             throw new RuntimeException("Wrong position reported. Should be null");
 111         }

 112     }
 113 
 114     public static boolean isValueInRange(int value) {
 115         /*
 116          * since all operating systems doesn't return exact value, hence range
 117          * is used.
 118          */
 119         return value >= 25 && value <= 35;
 120     }
 121 }
< prev index next >