Path: blob/master/test/jdk/java/awt/Multiscreen/MouseEventTest/MouseEventTest.java
41153 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 8017472 821199927@summary MouseEvent has wrong coordinates when using multiple monitors28@run main MouseEventTest29*/3031import java.awt.*;32import java.awt.event.MouseAdapter;33import java.awt.event.MouseEvent;3435public class MouseEventTest {36static volatile boolean crossed = false;3738static void sleep(Robot robot) throws InterruptedException {39robot.waitForIdle();40Thread.sleep(500);41}4243public static void main(String[] args) throws AWTException, InterruptedException {44GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();45GraphicsDevice[] gds = ge.getScreenDevices();46if (gds.length < 2) {47System.out.println("It's a multiscreen test... skipping!");48return;49}5051for (int i = 0; i < gds.length; ++i) {52GraphicsDevice gd = gds[i];53GraphicsConfiguration gc = gd.getDefaultConfiguration();54Rectangle screen = gc.getBounds();55Robot robot = new Robot(gd);56robot.setAutoDelay(100);575859Frame frame = new Frame(gc);60frame.setUndecorated(true);61frame.setSize(200, 200);62frame.setLocation(screen.x + 200, screen.y + 200);63frame.setBackground(Color.YELLOW);64frame.setVisible(true);65sleep(robot);6667Point loc = frame.getLocationOnScreen();68Dimension size = frame.getSize();69final Point point = new Point(70loc.x + size.width / 2,71loc.y + size.height / 2);7273crossed = false;7475frame.addMouseMotionListener(new MouseAdapter() {76@Override77public void mouseMoved(MouseEvent e) {78if (point.equals(e.getLocationOnScreen())) {79crossed = true;80}81}82});8384robot.mouseMove(point.x - 1, point.y - 1);85robot.mouseMove(point.x, point.y);8687sleep(robot);88frame.dispose();8990if (!crossed) {91throw new RuntimeException("An expected mouse motion event was not received on the screen #" + i);92}93}9495System.out.println("Test PASSED!");96}97}9899100