1 /*
   2  * Copyright (c) 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 import java.awt.AWTException;
  25 import java.awt.GraphicsConfiguration;
  26 import java.awt.GraphicsDevice;
  27 import java.awt.GraphicsEnvironment;
  28 import java.awt.MouseInfo;
  29 import java.awt.Point;
  30 import java.awt.Rectangle;
  31 import java.awt.Robot;
  32 
  33 /**
  34  * @test
  35  * @key headful
  36  * @bug 8196030
  37  * @summary checks that Robot and MouseInfo use the same coordinates
  38  */
  39 public final class MouseLocationOnScreen {
  40 
  41     public static void main(final String[] args) throws AWTException {
  42         GraphicsEnvironment lge =
  43                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  44 
  45         for (final GraphicsDevice device : lge.getScreenDevices()) {
  46             GraphicsConfiguration dc = device.getDefaultConfiguration();
  47             Robot robot = new Robot(device);
  48 
  49             Rectangle bounds = dc.getBounds();
  50             int x1 = bounds.x;
  51             int x2 = x1 + bounds.width - 1;
  52             int y1 = bounds.y;
  53             int y2 = y1 + bounds.height - 1;
  54 
  55             // We'll check all edge (two pixels in a width) of the each screen
  56             edge(robot, device, x1, y1, x2, y1);         // top
  57             edge(robot, device, x1, y1 + 1, x2, y1 + 1); // top
  58 
  59             edge(robot, device, x2, y1, x2, y2);         // right
  60             edge(robot, device, x2 - 1, y1, x2 - 1, y2); // right
  61 
  62             edge(robot, device, x1, y1, x1, y2);         // left
  63             edge(robot, device, x1 + 1, y1, x1 + 1, y2); // left
  64 
  65             edge(robot, device, x1, y2, x2, y2);         // bottom
  66             edge(robot, device, x1, y2 - 1, x2, y2 - 1); // bottom
  67 
  68             // We'll check crossing of diagonals of each screen
  69             cross(robot, device, x1, y1, x2, y2); // cross left-bottom
  70             cross(robot, device, x1, y2, x2, y1); // cross left-top
  71         }
  72     }
  73 
  74     /**
  75      * This method checks the coordinates which were passed to robot and
  76      * returned by MouseInfo. Note that this method will be called for each
  77      * pixel and for performance reasons we try will try to skip waitForIdle()
  78      * a few times.
  79      */
  80     static void validate(Robot robot, GraphicsDevice device, int x, int y) {
  81         int attempt = 0;
  82         while (true) {
  83             attempt++;
  84             Point actLoc = MouseInfo.getPointerInfo().getLocation();
  85             GraphicsDevice actDevice = MouseInfo.getPointerInfo().getDevice();
  86 
  87             if (actLoc.x != x || actLoc.y != y || actDevice != device) {
  88                 if (attempt <= 10) {
  89                     if (attempt >= 8) {
  90                         robot.waitForIdle();
  91                     }
  92                     continue;
  93                 }
  94                 System.err.println("Expected device: " + device);
  95                 System.err.println("Actual device: " + actDevice);
  96                 System.err.println("Expected X: " + x);
  97                 System.err.println("Actual X: " + actLoc.x);
  98                 System.err.println("Expected Y: " + y);
  99                 System.err.println("Actual Y: " + actLoc.y);
 100                 throw new RuntimeException();
 101             }
 102             return;
 103         }
 104     }
 105 
 106     private static void edge(Robot robot, GraphicsDevice device,
 107                              int x1, int y1, int x2, int y2) {
 108         for (int x = x1; x <= x2; x++) {
 109             for (int y = y1; y <= y2; y++) {
 110                 robot.mouseMove(x, y);
 111                 validate(robot, device, x, y);
 112             }
 113         }
 114     }
 115 
 116     private static void cross(Robot robot, GraphicsDevice device,
 117                               int x0, int y0, int x1, int y1) {
 118         float dmax = (float) Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0));
 119         float dx = (x1 - x0) / dmax;
 120         float dy = (y1 - y0) / dmax;
 121 
 122         robot.mouseMove(x0, y0);
 123         validate(robot, device, x0, y0);
 124         for (int i = 1; i <= dmax; i++) {
 125             int x = (int) (x0 + dx * i);
 126             int y = (int) (y0 + dy * i);
 127             robot.mouseMove(x, y);
 128             validate(robot, device, x, y);
 129         }
 130     }
 131 }