1 /*
   2  * Copyright (c) 2015, 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 import java.awt.AWTException;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsConfiguration;
  27 import java.awt.GraphicsDevice;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Robot;
  30 import java.awt.Window;
  31 import java.awt.event.InputEvent;
  32 
  33 import static java.awt.GraphicsEnvironment.*;
  34 
  35 /**
  36  * @test
  37  * @bug 6778087
  38  */
  39 public final class MouseWheelAbsXY {
  40 
  41     private static boolean done;
  42     private static int wheelX;
  43     private static int wheelY;
  44     private static int mouseX;
  45     private static int mouseY;
  46 
  47     public static void main(final String[] args) throws AWTException {
  48         GraphicsEnvironment ge = getLocalGraphicsEnvironment();
  49         GraphicsDevice[] sds = ge.getScreenDevices();
  50         for (GraphicsDevice gd : sds) {
  51             test(gd.getDefaultConfiguration());
  52         }
  53     }
  54 
  55     private static void test(GraphicsConfiguration gc) throws AWTException {
  56         final Window frame = new Frame(gc);
  57         try {
  58             frame.addMouseWheelListener(e -> {
  59                 wheelX = e.getXOnScreen();
  60                 wheelY = e.getYOnScreen();
  61                 done = true;
  62             });
  63             frame.setSize(300, 300);
  64             frame.setVisible(true);
  65 
  66             final Robot robot = new Robot();
  67             robot.setAutoDelay(50);
  68             robot.setAutoWaitForIdle(true);
  69             mouseX = frame.getX() + frame.getWidth() / 2;
  70             mouseY = frame.getY() + frame.getHeight() / 2;
  71 
  72             robot.mouseMove(mouseX, mouseY);
  73             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  74             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  75             robot.mouseWheel(10);
  76 
  77             validate();
  78         } finally {
  79             frame.dispose();
  80         }
  81     }
  82 
  83     private static void validate() {
  84         if (!done || wheelX != mouseX || wheelY != mouseY) {
  85             System.err.println("Expected X: " + mouseX);
  86             System.err.println("Expected Y: " + mouseY);
  87             System.err.println("Actual X: " + wheelX);
  88             System.err.println("Actual Y: " + wheelY);
  89             throw new RuntimeException("Test failed");
  90         }
  91     }
  92 }