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 * @key headful 27 * @bug 6232687 28 * @summary Tests that Window.setLocationRelativeTo() method works correctly 29 * for different multiscreen configurations 30 * @author artem.ananiev, area=awt.multiscreen 31 * @library ../../regtesthelpers 32 * @build Util 33 * @run main LocationRelativeToTest 34 */ 35 36 import java.awt.*; 37 import java.awt.event.*; 38 39 import javax.swing.*; 40 41 import test.java.awt.regtesthelpers.Util; 42 43 public class LocationRelativeToTest 44 { 45 private static int FRAME_WIDTH = 400; 46 private static int FRAME_HEIGHT = 300; 47 48 public static void main(String[] args) 49 { 50 Robot r = Util.createRobot(); 51 52 GraphicsEnvironment ge = 53 GraphicsEnvironment.getLocalGraphicsEnvironment(); 54 System.out.println("Center point: " + ge.getCenterPoint()); 55 GraphicsDevice[] gds = ge.getScreenDevices(); 56 GraphicsDevice gdDef = ge.getDefaultScreenDevice(); 57 GraphicsConfiguration gcDef = 58 gdDef.getDefaultConfiguration(); 59 60 Frame f = new Frame("F", gcDef); 61 f.setSize(FRAME_WIDTH, FRAME_HEIGHT); 62 f.setVisible(true); 63 Util.waitForIdle(r); 64 65 // first, check setLocationRelativeTo(null) 66 f.setLocationRelativeTo(null); 67 Util.waitForIdle(r); 68 checkLocation(f, ge.getCenterPoint()); 69 70 for (GraphicsDevice gd : gds) 71 { 72 GraphicsConfiguration gc = gd.getDefaultConfiguration(); 73 Rectangle gcBounds = gc.getBounds(); 74 Frame f2 = new Frame("F2", gc); 75 f2.setBounds(gcBounds.x + 100, gcBounds.y + 100, 76 FRAME_WIDTH, FRAME_HEIGHT); 77 78 // second, check setLocationRelativeTo(invisible) 79 f.setLocationRelativeTo(f2); 80 Util.waitForIdle(r); 81 checkLocation(f, new Point(gcBounds.x + gcBounds.width / 2, 82 gcBounds.y + gcBounds.height / 2)); 83 84 // third, check setLocationRelativeTo(visible) 85 f2.setVisible(true); 86 Util.waitForIdle(r); 87 Point f2Loc = f2.getLocationOnScreen(); 88 f.setLocationRelativeTo(f2); 89 Util.waitForIdle(r); 90 checkLocation(f, new Point(f2Loc.x + f2.getWidth() / 2, 91 f2Loc.y + f2.getHeight() / 2)); 92 } 93 } 94 95 /* 96 * Here the check is performed. Note this check works correctly both 97 * for virtual (Win32, X11/Xinerama) and non-virtual (X11/non-Xinerama) 98 * screen configurations. 99 */ 100 private static void checkLocation(Frame f, Point rightLoc) 101 { 102 Point actualLoc = new Point(f.getBounds().x + f.getWidth() / 2, 103 f.getBounds().y + f.getHeight() / 2); 104 if (!rightLoc.equals(actualLoc)) 105 { 106 System.err.println("Right location for the window center: " + rightLoc); 107 System.err.println("Actual location for the window center: " + actualLoc); 108 throw new RuntimeException("Test FAILED: setLocationRelativeTo() operates incorrectly"); 109 } 110 } 111 }