import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;

public final class MouseLocationOnScreen {

    public static void main(final String[] args) throws AWTException {
        GraphicsEnvironment lge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screenDevices = lge.getScreenDevices();
        Robot robot = new Robot();

        for (final GraphicsDevice device : screenDevices) {
            GraphicsConfiguration dc = device.getDefaultConfiguration();

            Rectangle bounds = dc.getBounds();
            int x1 = bounds.x;
            int x2 = x1 + bounds.width - 1;
            int y1 = bounds.y;
            int y2 = y1 + bounds.height - 1;

            test(robot, device, x1, x2, y1, y1); // top
            test(robot, device, x2, x2, y1, y2); // right
            test(robot, device, x1, x1, y1, y2); // left
            test(robot, device, x1, x2, y2, y2); // bottom
        }
    }

    static void test(Robot robot, GraphicsDevice device, int x1, int x2, int y1,
                     int y2) {
        for (int x = x1; x <= x2; x++) {
            for (int y = y1; y <= y2; y++) {
                robot.mouseMove(x, y);
                validate(robot, device, x, y);
            }
        }
    }

    static void validate(Robot robot, GraphicsDevice device, int x, int y) {
        int attempt = 0;
        while (true) {
            attempt++;
            Point actLoc = MouseInfo.getPointerInfo().getLocation();
            GraphicsDevice actDevice = MouseInfo.getPointerInfo().getDevice();

            if (actLoc.x != x || actLoc.y != y || actDevice != device) {
                if (attempt <= 10) {
                    if (attempt == 10) {
                        robot.waitForIdle();
                    }
                    continue;
                }
                System.err.println("Expected device: " + device);
                System.err.println("Actual device: " + actDevice);
                System.err.println("Expected X: " + x);
                System.err.println("Actual X: " + actLoc.x);
                System.err.println("Expected Y: " + y);
                System.err.println("Actual Y: " + actLoc.y);
                throw new RuntimeException();
            }
            return;
        }
    }
}
