1 /* 2 * Copyright (c) 2004, 2018, 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 /* 25 @test 26 @key headful 27 @bug 4992908 28 @summary Need way to get location of MouseEvent in screen coordinates (Unit-test) 29 @library ../../../regtesthelpers 30 @build Util 31 @run main FrameMouseEventAbsoluteCoordsTest 32 */ 33 34 import java.awt.*; 35 import java.awt.event.*; 36 import test.java.awt.regtesthelpers.Util; 37 38 // Part II 39 // Create Frame. 40 // Click on this frame. 41 // verify that our MouseEvent contain correct xAbs, yAbs values 42 43 public class FrameMouseEventAbsoluteCoordsTest implements MouseListener 44 { 45 Robot robot; 46 Frame frame = new Frame("MouseEvent Test Frame II"); 47 Button button = new Button("Just Button"); 48 Point mousePositionAbsolute; 49 Point mousePosition; 50 51 public static void main(final String[] args) { 52 FrameMouseEventAbsoluteCoordsTest app = new FrameMouseEventAbsoluteCoordsTest(); 53 app.init(); 54 app.start(); 55 } 56 57 public void init() 58 { 59 button.addMouseListener(this); 60 frame.add(button); 61 frame.pack(); 62 frame.setLocationRelativeTo(null); 63 }//End init() 64 65 public void start () 66 { 67 frame.setVisible(true); 68 Util.waitForIdle(robot); 69 70 try { 71 robot = new Robot(); 72 robot.setAutoWaitForIdle(true); 73 mousePositionAbsolute = new Point(button.getLocationOnScreen().x + button.getWidth()/2, 74 button.getLocationOnScreen().y + button.getHeight()/2); 75 mousePosition = new Point(button.getWidth()/2, 76 button.getHeight()/2); 77 robot.mouseMove(mousePositionAbsolute.x, 78 mousePositionAbsolute.y ); 79 // robot.delay(1000); 80 robot.mousePress(InputEvent.BUTTON1_MASK); 81 robot.mouseRelease(InputEvent.BUTTON1_MASK); 82 }catch(AWTException e) { 83 throw new RuntimeException("Test Failed. AWTException thrown."); 84 } 85 }// start() 86 87 public void mousePressed(MouseEvent e){ 88 checkEventAbsolutePosition(e, "MousePressed OK"); 89 }; 90 public void mouseReleased(MouseEvent e){ 91 checkEventAbsolutePosition(e, "MouseReleased OK"); 92 }; 93 public void mouseClicked(MouseEvent e){ 94 checkEventAbsolutePosition(e, "MouseClicked OK"); 95 }; 96 public void mouseEntered(MouseEvent e){ 97 System.out.println("mouse entered"); 98 }; 99 public void mouseExited(MouseEvent e){ 100 System.out.println("mouse exited"); 101 }; 102 103 public void checkEventAbsolutePosition(MouseEvent evt, String message){ 104 if (evt.getXOnScreen() != mousePositionAbsolute.x || 105 evt.getYOnScreen() != mousePositionAbsolute.y || 106 !evt.getLocationOnScreen().equals( mousePositionAbsolute ) ){ 107 throw new RuntimeException("get(X|Y)OnScreen() or getLocationOnScreen() works incorrectly: expected"+ 108 mousePositionAbsolute.x+":"+mousePositionAbsolute.y+ 109 "\n Got:"+ evt.getXOnScreen()+":"+evt.getYOnScreen()); 110 } 111 if (evt.getX() != mousePosition.x || 112 evt.getY() != mousePosition.y || 113 !evt.getPoint().equals( mousePosition ) ){ 114 throw new RuntimeException("get(X|Y)() or getLocationOnScreen() works incorrectly: expected"+ 115 mousePositionAbsolute.x+":"+mousePositionAbsolute.y+"\n Got:" 116 +evt.getX()+":"+evt.getY()); 117 } 118 System.out.println(message); 119 } 120 121 }// class