rev 60071 : 8211999: Window positioning bugs due to overlapping GraphicsDevice bounds (Windows/HiDPI)
Reviewed-by: XXX

   1 /*
   2  * Copyright (c) 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.Dimension;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsDevice;
  27 import java.awt.GraphicsEnvironment;
  28 import java.awt.Rectangle;
  29 import java.awt.Robot;
  30 import java.awt.Toolkit;
  31 
  32 /**
  33  * @test
  34  * @bug 8176359 8231564 8211999
  35  * @key headful
  36  * @requires (os.family == "windows" | os.family == "mac")
  37  * @summary setMaximizedBounds() should work if set to the screen other than
  38  *          current screen of the Frame, the size of the frame is intentionally
  39  *          small
  40  */
  41 public final class MaximizedToOppositeScreenSmall {
  42 
  43     public static void main(String[] args) throws Exception {
  44         //Supported platforms are Windows and OS X.
  45         String os = System.getProperty("os.name").toLowerCase();
  46         if (!os.contains("windows") && !os.contains("os x")) {
  47             return;
  48         }
  49 
  50         if (!Toolkit.getDefaultToolkit().
  51                 isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {
  52             return;
  53         }
  54 
  55         var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  56         GraphicsDevice[] gds = ge.getScreenDevices();
  57         Robot robot = new Robot();
  58         for (GraphicsDevice gd1 : gds) {
  59             Rectangle framAt = gd1.getDefaultConfiguration().getBounds();
  60             framAt.grow(-framAt.width / 2 + 100, -framAt.height / 2 + 100);
  61             for (GraphicsDevice gd2 : gds) {


  62                 Frame frame = new Frame(gd1.getDefaultConfiguration());
  63                 try {
  64                     frame.setLayout(null); // trigger use the minimum size of
  65                                            // the peer
  66                     frame.setBounds(framAt);
  67 
  68                     frame.setVisible(true);
  69                     robot.waitForIdle();
  70                     robot.delay(1000);
  71 
  72                     Dimension minimumSize = frame.getMinimumSize();
  73                     minimumSize.width = Math.max(minimumSize.width, 120);
  74                     minimumSize.height = Math.max(minimumSize.height, 120);
  75                     Rectangle maxTo = gd2.getDefaultConfiguration().getBounds();
  76                     maxTo.grow(-maxTo.width / 2 + minimumSize.width,
  77                                -maxTo.height / 2 + minimumSize.height);
  78 
  79                     frame.setMaximizedBounds(maxTo);
  80                     frame.setExtendedState(Frame.MAXIMIZED_BOTH);
  81                     robot.waitForIdle();
  82                     robot.delay(1000);
  83 
  84                     Rectangle actual = frame.getBounds();
  85                     if (!actual.equals(maxTo)) {
  86                         System.err.println("Actual: " + actual);
  87                         System.err.println("Expected: " + maxTo);
  88                         throw new RuntimeException("Wrong bounds");
  89                     }
  90                 } finally {
  91                     frame.dispose();
  92                 }
  93             }
  94         }
  95     }
  96 }
  97 
--- EOF ---