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
  28   @author Andrei.Dmitriev area=event
  29   @run applet MenuDragMouseEventAbsoluteCoordsTest.html
  30 */
  31 
  32 import java.applet.Applet;
  33 import java.awt.*;
  34 import javax.swing.event.*;
  35 import java.awt.event.*;
  36 import javax.swing.MenuSelectionManager;
  37 import javax.swing.MenuElement;
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 // The test consits of several parts:
  41 // 1. create MenuDragMouseEvent with new Ctor and checking get(X|Y)OnScreen(),
  42 // getLocationOnScreen(), get(X|Y), getPoint().
  43 // 2. create MenuDragMouseEvent with old Ctor and checking get(X|Y)OnScreen(),
  44 // getLocationOnScreen(),  get(X|Y), getPoint() .
  45 
  46 
  47 public class MenuDragMouseEventAbsoluteCoordsTest extends Applet implements MouseListener
  48 {
  49     Frame frame = new Frame("MenuDragMouseEvent Test Frame");
  50 
  51     Point mousePositionOnScreen = new Point(200, 200);
  52     Point mousePosition = new Point(100, 100);
  53     public void init()
  54     {
  55         frame.addMouseListener(this);
  56     }//End  init()
  57 
  58     public void start ()
  59     {
  60         //Get things going.  Request focus, set size, et cetera
  61         frame.setSize (200,200);
  62         frame.setVisible(true);
  63         validate();
  64 
  65         try {
  66             Util.waitForIdle(new Robot());
  67         }catch (Exception e){
  68             throw new RuntimeException("Test failed.", e);
  69         }
  70 
  71         // use new MenuDragMouseEvent's Ctor with user-defined absolute
  72         // coordinates
  73 
  74         System.out.println("New Ctor Stage MOUSE_PRESSED");
  75         postMenuDragMouseEventNewCtor(MouseEvent.MOUSE_PRESSED);
  76         // now we are going to use old MenuDragMouseEvent's Ctor thus absolute
  77         // position calculates as frame's location + relative coords
  78         // of the event.
  79         mousePositionOnScreen = new Point(frame.getLocationOnScreen().x + mousePosition.x,
  80                                           frame.getLocationOnScreen().y + mousePosition.y);
  81 
  82         System.out.println("Old Ctor Stage MOUSE_PRESSED");
  83         postMenuDragMouseEventOldCtor(MouseEvent.MOUSE_PRESSED);
  84     }// start()
  85 
  86     public void mousePressed(MouseEvent e){
  87         checkEventAbsolutePosition(e, "MousePressed OK");
  88     };
  89 
  90     public void mouseExited(MouseEvent e){
  91         System.out.println("mouse exited");
  92     };
  93     public void mouseReleased(MouseEvent e){
  94         checkEventAbsolutePosition(e, "MousePressed OK");
  95     };
  96     public void mouseEntered(MouseEvent e){
  97         System.out.println("mouse entered");
  98     };
  99     public void mouseClicked(MouseEvent e){
 100         checkEventAbsolutePosition(e, "MousePressed OK");
 101     };
 102 
 103     public void postMenuDragMouseEventNewCtor(int MouseEventType)    {
 104         MouseEvent me = new MenuDragMouseEvent(frame,
 105                                                MouseEventType,
 106                                                System.currentTimeMillis(),
 107                                                MouseEvent.BUTTON1_DOWN_MASK,
 108                                                mousePosition.x, mousePosition.y,
 109                                                mousePositionOnScreen.x,
 110                                                mousePositionOnScreen.y,
 111                                                1,                   //clickCount
 112                                                false,              //popupTrigger
 113                                                null,                //MenuElement
 114                                                null                 //MenuSelectionManager
 115                                                );
 116         frame.dispatchEvent( ( AWTEvent )me );
 117     }
 118 
 119     public void postMenuDragMouseEventOldCtor(int MouseEventType)    {
 120         MouseEvent meOld = new MenuDragMouseEvent(frame,
 121                                           MouseEventType,
 122                                           System.currentTimeMillis(),
 123                                           MouseEvent.BUTTON1_DOWN_MASK,
 124                                           mousePosition.x, mousePosition.y,
 125                                           1,
 126                                           false,              //popupTrigger
 127                                           null, null
 128                                           );
 129         frame.dispatchEvent( ( AWTEvent )meOld );
 130     }
 131 
 132     public void checkEventAbsolutePosition(MouseEvent evt, String message){
 133             if (evt.getXOnScreen() != mousePositionOnScreen.x ||
 134                 evt.getYOnScreen() != mousePositionOnScreen.y ||
 135                 !evt.getLocationOnScreen().equals( mousePositionOnScreen )  ){
 136                 System.out.println("evt.location = "+evt.getLocationOnScreen());
 137                 System.out.println("mouse.location = "+mousePositionOnScreen);
 138                 throw new RuntimeException("get(X|Y)OnScreen() or getPointOnScreen() work incorrectly");
 139             }
 140 
 141             if (evt.getX() != mousePosition.x ||
 142                 evt.getY() != mousePosition.y ||
 143                 !evt.getPoint().equals( mousePosition )  ){
 144                 throw new RuntimeException("get(X|Y)() or getPoint() work incorrectly");
 145             }
 146         System.out.println(message);
 147     }
 148 }// class AutomaticAppletTest