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