Path: blob/master/test/jdk/java/awt/Multiscreen/LocationRelativeToTest/LocationRelativeToTest.java
41153 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* @key headful26* @bug 623268727* @summary Tests that Window.setLocationRelativeTo() method works correctly28* for different multiscreen configurations29* @author artem.ananiev, area=awt.multiscreen30* @library ../../regtesthelpers31* @build Util32* @run main LocationRelativeToTest33*/3435import java.awt.*;36import java.awt.event.*;3738import javax.swing.*;3940import test.java.awt.regtesthelpers.Util;4142public class LocationRelativeToTest43{44private static int FRAME_WIDTH = 400;45private static int FRAME_HEIGHT = 300;4647public static void main(String[] args)48{49Robot r = Util.createRobot();5051GraphicsEnvironment ge =52GraphicsEnvironment.getLocalGraphicsEnvironment();53System.out.println("Center point: " + ge.getCenterPoint());54GraphicsDevice[] gds = ge.getScreenDevices();55GraphicsDevice gdDef = ge.getDefaultScreenDevice();56GraphicsConfiguration gcDef =57gdDef.getDefaultConfiguration();5859Frame f = new Frame("F", gcDef);60f.setSize(FRAME_WIDTH, FRAME_HEIGHT);61f.setVisible(true);62Util.waitForIdle(r);6364// first, check setLocationRelativeTo(null)65f.setLocationRelativeTo(null);66Util.waitForIdle(r);67checkLocation(f, ge.getCenterPoint());6869for (GraphicsDevice gd : gds)70{71GraphicsConfiguration gc = gd.getDefaultConfiguration();72Rectangle gcBounds = gc.getBounds();73Frame f2 = new Frame("F2", gc);74f2.setBounds(gcBounds.x + 100, gcBounds.y + 100,75FRAME_WIDTH, FRAME_HEIGHT);7677// second, check setLocationRelativeTo(invisible)78f.setLocationRelativeTo(f2);79Util.waitForIdle(r);80checkLocation(f, new Point(gcBounds.x + gcBounds.width / 2,81gcBounds.y + gcBounds.height / 2));8283// third, check setLocationRelativeTo(visible)84f2.setVisible(true);85Util.waitForIdle(r);86Point f2Loc = f2.getLocationOnScreen();87f.setLocationRelativeTo(f2);88Util.waitForIdle(r);89checkLocation(f, new Point(f2Loc.x + f2.getWidth() / 2,90f2Loc.y + f2.getHeight() / 2));91}92}9394/*95* Here the check is performed. Note this check works correctly both96* for virtual (Win32, X11/Xinerama) and non-virtual (X11/non-Xinerama)97* screen configurations.98*/99private static void checkLocation(Frame f, Point rightLoc)100{101Point actualLoc = new Point(f.getBounds().x + f.getWidth() / 2,102f.getBounds().y + f.getHeight() / 2);103if (!rightLoc.equals(actualLoc))104{105System.err.println("Right location for the window center: " + rightLoc);106System.err.println("Actual location for the window center: " + actualLoc);107throw new RuntimeException("Test FAILED: setLocationRelativeTo() operates incorrectly");108}109}110}111112113