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