Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Multiscreen/LocationRelativeToTest/LocationRelativeToTest.java
38828 views
/*1* Copyright (c) 2007, 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 623268726@summary Tests that Window.setLocationRelativeTo() method works correctly27for different multiscreen configurations28@author artem.ananiev, area=awt.multiscreen29@library ../../regtesthelpers30@build Util31@run main LocationRelativeToTest32*/3334import java.awt.*;35import java.awt.event.*;3637import javax.swing.*;3839import test.java.awt.regtesthelpers.Util;4041public class LocationRelativeToTest42{43private static int FRAME_WIDTH = 400;44private static int FRAME_HEIGHT = 300;4546public static void main(String[] args)47{48Robot r = Util.createRobot();4950GraphicsEnvironment ge =51GraphicsEnvironment.getLocalGraphicsEnvironment();52System.out.println("Center point: " + ge.getCenterPoint());53GraphicsDevice[] gds = ge.getScreenDevices();54GraphicsDevice gdDef = ge.getDefaultScreenDevice();55GraphicsConfiguration gcDef =56gdDef.getDefaultConfiguration();5758Frame f = new Frame("F", gcDef);59f.setSize(FRAME_WIDTH, FRAME_HEIGHT);60f.setVisible(true);61Util.waitForIdle(r);6263// first, check setLocationRelativeTo(null)64f.setLocationRelativeTo(null);65Util.waitForIdle(r);66checkLocation(f, ge.getCenterPoint());6768for (GraphicsDevice gd : gds)69{70GraphicsConfiguration gc = gd.getDefaultConfiguration();71Rectangle gcBounds = gc.getBounds();72Frame f2 = new Frame("F2", gc);73f2.setBounds(gcBounds.x + 100, gcBounds.y + 100,74FRAME_WIDTH, FRAME_HEIGHT);7576// second, check setLocationRelativeTo(invisible)77f.setLocationRelativeTo(f2);78Util.waitForIdle(r);79checkLocation(f, new Point(gcBounds.x + gcBounds.width / 2,80gcBounds.y + gcBounds.height / 2));8182// third, check setLocationRelativeTo(visible)83f2.setVisible(true);84Util.waitForIdle(r);85Point f2Loc = f2.getLocationOnScreen();86f.setLocationRelativeTo(f2);87Util.waitForIdle(r);88checkLocation(f, new Point(f2Loc.x + f2.getWidth() / 2,89f2Loc.y + f2.getHeight() / 2));90}91}9293/*94* Here the check is performed. Note this check works correctly both95* for virtual (Win32, X11/Xinerama) and non-virtual (X11/non-Xinerama)96* screen configurations.97*/98private static void checkLocation(Frame f, Point rightLoc)99{100Point actualLoc = new Point(f.getBounds().x + f.getWidth() / 2,101f.getBounds().y + f.getHeight() / 2);102if (!rightLoc.equals(actualLoc))103{104System.err.println("Right location for the window center: " + rightLoc);105System.err.println("Actual location for the window center: " + actualLoc);106throw new RuntimeException("Test FAILED: setLocationRelativeTo() operates incorrectly");107}108}109}110111112