Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Multiscreen/MouseEventTest/MouseEventTest.java
38828 views
/*1* Copyright (c) 2014, 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@bug 801747226@summary MouseEvent has wrong coordinates when using multiple monitors27@run main MouseEventTest28*/2930import java.awt.*;31import java.awt.event.MouseAdapter;32import java.awt.event.MouseEvent;3334public class MouseEventTest {35static volatile boolean crossed = false;3637static void sleep(Robot robot) throws InterruptedException {38robot.waitForIdle();39Thread.sleep(500);40}4142public static void main(String[] args) throws AWTException, InterruptedException {43GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();44GraphicsDevice[] gds = ge.getScreenDevices();45if (gds.length < 2) {46System.out.println("It's a multiscreen test... skipping!");47return;48}4950for (int i = 0; i < gds.length; ++i) {51GraphicsDevice gd = gds[i];52GraphicsConfiguration gc = gd.getDefaultConfiguration();53Rectangle screen = gc.getBounds();54Robot robot = new Robot(gd);55robot.setAutoDelay(100);565758Frame frame = new Frame(gc);59frame.setUndecorated(true);60frame.setSize(200, 200);61frame.setLocation(screen.x + 200, screen.y + 200);62frame.setBackground(Color.YELLOW);63frame.setVisible(true);64sleep(robot);6566Point loc = frame.getLocationOnScreen();67Dimension size = frame.getSize();68final Point point = new Point(69loc.x + size.width / 2,70loc.y + size.height / 2);7172crossed = false;7374frame.addMouseMotionListener(new MouseAdapter() {75@Override76public void mouseMoved(MouseEvent e) {77if (point.equals(e.getLocationOnScreen())) {78crossed = true;79}80}81});8283robot.mouseMove(point.x - 1, point.y - 1);84robot.mouseMove(point.x, point.y);8586sleep(robot);87frame.dispose();8889if (!crossed) {90throw new RuntimeException("An expected mouse motion event was not received on the screen #" + i);91}92}9394System.out.println("Test PASSED!");95}96}979899