1 /*
   2  * Copyright (c) 2018, 2020, 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 import java.awt.AWTException;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsDevice;
  27 import java.awt.GraphicsEnvironment;
  28 import java.awt.Point;
  29 import java.awt.Rectangle;
  30 import java.awt.Robot;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 8201364 8232433 8211999
  36  * @summary Component.getLocation() should returns correct location if
  37  *          Component.setBounds() was ignored by the OS
  38  */
  39 public final class LocationAtScreenCorner {
  40 
  41     public static void main(final String[] args) throws Exception {
  42         test(true);
  43         test(false);
  44     }
  45 
  46     private static void test(final boolean undecorated) throws AWTException {
  47         Robot robot = new Robot();
  48         Frame frame = new Frame();
  49         frame.setUndecorated(undecorated);
  50         frame.setSize(200, 200);
  51         frame.setLocationRelativeTo(null);
  52         frame.setVisible(true);
  53         robot.waitForIdle();
  54 
  55         GraphicsEnvironment lge =
  56                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  57         GraphicsDevice[] devices = lge.getScreenDevices();
  58 
  59         // The Component.setBounds() for corners of the screen can be ignored by
  60         // OS because of menubar, taskbar, dock etc. But in this case
  61         // getLocation() and getLocationOnScreen() should always return the same
  62         // coordinates.
  63         for (GraphicsDevice device : devices) {
  64             Rectangle bounds = device.getDefaultConfiguration().getBounds();
  65             test(robot, frame, bounds.x, bounds.y);
  66             test(robot, frame, bounds.width, bounds.y);
  67             test(robot, frame, bounds.x + bounds.width, bounds.y);
  68             test(robot, frame, bounds.x, bounds.height);
  69             test(robot, frame, bounds.x, bounds.y + bounds.height);
  70             test(robot, frame, bounds.width, bounds.height);
  71             test(robot, frame, bounds.x + bounds.width, bounds.y + bounds.height);
  72         }
  73         frame.dispose();
  74     }
  75 
  76     private static void test(Robot robot, Frame frame, int x, int y) {
  77         for (int i = 0; i < 10; ++i) {
  78             // intentionally set the same coordinates a few times
  79             frame.setLocation(x, y); // x and y are cached in the frame
  80             int attempt = 0;
  81             while (true) {
  82                 robot.waitForIdle();
  83                 // location was cached in the frame and should be updated to the
  84                 // real location by the native callback some time later.
  85                 // this is why we make a few attempts
  86                 Point location = frame.getLocation();
  87                 // locationOnScreen is fetched from the peer
  88                 Point locationOnScreen = frame.getLocationOnScreen();
  89                 if (location.equals(locationOnScreen)) {
  90                     break;
  91                 }
  92                 if (attempt++ > 10) {
  93                     frame.dispose();
  94                     System.err.println("Location: " + location);
  95                     System.err.println("Location on screen: " + locationOnScreen);
  96                     throw new RuntimeException("Wrong location");
  97                 }
  98             }
  99         }
 100     }
 101 }