1 /*
   2  * Copyright (c) 2014, 2016, 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 8017472 8211999
  28   @summary MouseEvent has wrong coordinates when using multiple monitors
  29   @run main MouseEventTest
  30  */
  31 
  32 import java.awt.*;
  33 import java.awt.event.MouseAdapter;
  34 import java.awt.event.MouseEvent;
  35 
  36 public class MouseEventTest {
  37     static volatile boolean crossed = false;
  38 
  39     static void sleep(Robot robot) throws InterruptedException {
  40         robot.waitForIdle();
  41         Thread.sleep(500);
  42     }
  43 
  44     public static void main(String[] args) throws AWTException, InterruptedException {
  45         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  46         GraphicsDevice[] gds = ge.getScreenDevices();
  47         if (gds.length < 2) {
  48             System.out.println("It's a multiscreen test... skipping!");
  49             return;
  50         }
  51 
  52         for (int i = 0; i < gds.length; ++i) {
  53             GraphicsDevice gd = gds[i];
  54             GraphicsConfiguration gc = gd.getDefaultConfiguration();
  55             Rectangle screen = gc.getBounds();
  56             Robot robot = new Robot(gd);
  57             robot.setAutoDelay(100);
  58 
  59 
  60             Frame frame = new Frame(gc);
  61             frame.setUndecorated(true);
  62             frame.setSize(200, 200);
  63             frame.setLocation(screen.x + 200, screen.y + 200);
  64             frame.setBackground(Color.YELLOW);
  65             frame.setVisible(true);
  66             sleep(robot);
  67 
  68             Point loc = frame.getLocationOnScreen();
  69             Dimension size = frame.getSize();
  70             final Point point = new Point(
  71                     loc.x + size.width / 2,
  72                     loc.y + size.height / 2);
  73 
  74             crossed = false;
  75 
  76             frame.addMouseMotionListener(new MouseAdapter() {
  77                 @Override
  78                 public void mouseMoved(MouseEvent e) {
  79                     if (point.equals(e.getLocationOnScreen())) {
  80                         crossed = true;
  81                     }
  82                 }
  83             });
  84 
  85             robot.mouseMove(point.x - 1, point.y - 1);
  86             robot.mouseMove(point.x, point.y);
  87 
  88             sleep(robot);
  89             frame.dispose();
  90 
  91             if (!crossed) {
  92                 throw new RuntimeException("An expected mouse motion event was not received on the screen #" + i);
  93             }
  94         }
  95 
  96         System.out.println("Test PASSED!");
  97     }
  98 }