1 /* 2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 @test 26 @bug 6232687 27 @summary Tests that Window.setLocationRelativeTo() method works correctly 28 for different multiscreen configurations 29 @author artem.ananiev, area=awt.multiscreen 30 @library ../../regtesthelpers 31 @build Util 32 @run main LocationRelativeToTest 33 */ 34 35 import java.awt.*; 36 import java.awt.event.*; 37 38 import javax.swing.*; 39 40 import test.java.awt.regtesthelpers.Util; 41 42 public class LocationRelativeToTest 43 { 44 private static int FRAME_WIDTH = 400; 45 private static int FRAME_HEIGHT = 300; 46 47 public static void main(String[] args) 48 { 49 Robot r = Util.createRobot(); 50 51 GraphicsEnvironment ge = 52 GraphicsEnvironment.getLocalGraphicsEnvironment(); 53 System.out.println("Center point: " + ge.getCenterPoint()); 54 GraphicsDevice[] gds = ge.getScreenDevices(); 55 GraphicsDevice gdDef = ge.getDefaultScreenDevice(); 56 GraphicsConfiguration gcDef = 57 gdDef.getDefaultConfiguration(); 58 59 Frame f = new Frame("F", gcDef); 60 f.setSize(FRAME_WIDTH, FRAME_HEIGHT); 61 f.setVisible(true); 62 Util.waitForIdle(r); 63 64 // first, check setLocationRelativeTo(null) 65 f.setLocationRelativeTo(null); 66 Util.waitForIdle(r); 67 checkLocation(f, ge.getCenterPoint()); 68 69 for (GraphicsDevice gd : gds) 70 { 71 GraphicsConfiguration gc = gd.getDefaultConfiguration(); 72 Rectangle gcBounds = gc.getBounds(); 73 Frame f2 = new Frame("F2", gc); 74 f2.setBounds(gcBounds.x + 100, gcBounds.y + 100, 75 FRAME_WIDTH, FRAME_HEIGHT); 76 77 // second, check setLocationRelativeTo(invisible) 78 f.setLocationRelativeTo(f2); 79 Util.waitForIdle(r); 80 checkLocation(f, new Point(gcBounds.x + gcBounds.width / 2, 81 gcBounds.y + gcBounds.height / 2)); 82 83 // third, check setLocationRelativeTo(visible) 84 f2.setVisible(true); 85 Util.waitForIdle(r); 86 Point f2Loc = f2.getLocationOnScreen(); 87 f.setLocationRelativeTo(f2); 88 Util.waitForIdle(r); 89 checkLocation(f, new Point(f2Loc.x + f2.getWidth() / 2, 90 f2Loc.y + f2.getHeight() / 2)); 91 } 92 } 93 94 /* 95 * Here the check is performed. Note this check works correctly both 96 * for virtual (Win32, X11/Xinerama) and non-virtual (X11/non-Xinerama) 97 * screen configurations. 98 */ 99 private static void checkLocation(Frame f, Point rightLoc) 100 { 101 Point actualLoc = new Point(f.getBounds().x + f.getWidth() / 2, 102 f.getBounds().y + f.getHeight() / 2); 103 if (!rightLoc.equals(actualLoc)) 104 { 105 System.err.println("Right location for the window center: " + rightLoc); 106 System.err.println("Actual location for the window center: " + actualLoc); 107 throw new RuntimeException("Test FAILED: setLocationRelativeTo() operates incorrectly"); 108 } 109 } 110 }