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
  29   @library ../../../regtesthelpers
  30   @build Util
  31   @run main MouseWheelEventAbsoluteCoordsTest
  32 */
  33 

  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import test.java.awt.regtesthelpers.Util;
  37 
  38 // The test consits of several parts:
  39 // 1. create MouseWheelEvent with new Ctor and checking get(X|Y)OnScreen(),
  40 // getLocationOnScreen(), get(X|Y), getPoint().
  41 // 2. create MouseWheelEvent with old Ctor and checking get(X|Y)OnScreen(),
  42 // getLocationOnScreen(),  get(X|Y), getPoint() .
  43 
  44 
  45 public class MouseWheelEventAbsoluteCoordsTest implements MouseWheelListener
  46 {
  47     Frame frame = new Frame("MouseWheelEvent Test Frame");
  48 
  49     Point mousePositionOnScreen = new Point(200, 200);
  50     Point mousePosition = new Point(100, 100);


  51 
  52     public static void main(final String[] args) {
  53         MouseWheelEventAbsoluteCoordsTest app = new MouseWheelEventAbsoluteCoordsTest();
  54         app.init();
  55         app.start();
  56     }
  57 
  58     public void init()
  59     {
  60         frame.addMouseWheelListener(this);
  61     }//End  init()
  62 
  63     public void start ()
  64     {
  65         //Get things going.  Request focus, set size, et cetera
  66         frame.setSize (200,200);
  67         frame.setLocation(47, 47);
  68 
  69         frame.setVisible(true);

  70         try {
  71             Util.waitForIdle(new Robot());
  72         }catch (Exception e){
  73             throw new RuntimeException("Test failed.", e);
  74         }
  75 
  76         // use new MouseEvent's Ctor with user-defined absolute
  77         // coordinates
  78         System.out.println("New Ctor Stage");
  79         postMouseWheelEventNewCtor(MouseEvent.MOUSE_CLICKED);
  80 
  81         // now we gonna use old MouseEvent'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         System.out.println("Old Ctor Stage");
  87         postMouseWheelEventOldCtor(MouseEvent.MOUSE_CLICKED);
  88 
  89     }// start()
  90     public void mouseWheelMoved(MouseWheelEvent e){
  91         checkEventAbsolutePosition(e, "MouseWheelMoved OK");
  92     };
  93 
  94     public void postMouseWheelEventNewCtor(int MouseEventType)    {
  95         MouseWheelEvent me = new MouseWheelEvent(frame,
  96                                                  MouseEventType,
  97                                                  System.currentTimeMillis(),
  98                                                  MouseEvent.BUTTON1_DOWN_MASK,
  99                                                  mousePosition.x, mousePosition.y,
 100                                                  mousePositionOnScreen.x,
 101                                                  mousePositionOnScreen.y,
 102                                                  1,
 103                                                  false,  //popupTrigger
 104                                                  MouseWheelEvent.WHEEL_UNIT_SCROLL,
 105                                                  1,  //scrollAmount
 106                                                  1  //wheelRotation
 107                                                  );
 108         frame.dispatchEvent( ( AWTEvent )me );
 109     }
 110 
 111     public void postMouseWheelEventOldCtor(int MouseEventType)    {
 112         MouseWheelEvent meOld = new MouseWheelEvent(frame,
 113                                                  MouseEventType,
 114                                                  System.currentTimeMillis(),
 115                                                  MouseEvent.BUTTON1_DOWN_MASK,
 116                                                  mousePosition.x, mousePosition.y,
 117                                                  1,
 118                                                  false,  //popupTrigger
 119                                                  MouseWheelEvent.WHEEL_UNIT_SCROLL,
 120                                                  1,  //scrollAmount
 121                                                  1  //wheelRotation
 122                                                  );
 123         frame.dispatchEvent( ( AWTEvent )meOld );
 124     }
 125 
 126     public void checkEventAbsolutePosition(MouseEvent evt, String message){
 127         //        if (newCtorUsed){
 128             if (evt.getXOnScreen() != mousePositionOnScreen.x ||
 129                 evt.getYOnScreen() != mousePositionOnScreen.y ||
 130                 !evt.getLocationOnScreen().equals( mousePositionOnScreen )  ){
 131                 throw new RuntimeException("get(X|Y)OnScreen() or getPointOnScreen() work incorrectly");
 132             }
 133 
 134             if (evt.getX() != mousePosition.x ||
 135                 evt.getY() != mousePosition.y ||
 136                 !evt.getPoint().equals( mousePosition )  ){
 137                 throw new RuntimeException("get(X|Y)() or getPoint() work incorrectly");
 138             }
 139         System.out.println(message);
 140     }
 141 }// class
--- EOF ---