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