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.Color;
  25 import java.awt.Dialog;
  26 import java.awt.Frame;
  27 import java.awt.GraphicsDevice;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Point;
  30 import java.awt.Rectangle;
  31 import java.awt.Robot;
  32 import java.awt.Window;
  33 
  34 /**
  35  * @test
  36  * @key headful
  37  * @bug 8211999
  38  * @summary The test creates the packed/unpacked top level components on the
  39  *          different screens and compares their bounds
  40  * @run main/othervm WindowSizeDifferentScreens
  41  * @run main/othervm -Dsun.java2d.uiScale=1 WindowSizeDifferentScreens
  42  * @run main/othervm -Dsun.java2d.uiScale=1.25 WindowSizeDifferentScreens
  43  */
  44 public final class WindowSizeDifferentScreens {
  45 
  46     public static void main(String[] args) throws Exception {
  47         test("window");
  48         test("dialog");
  49         test("frame");
  50     }
  51 
  52     private static void test(String top) throws Exception {
  53         var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  54         Robot robot = new Robot();
  55         Window main = getTopLevel(top);
  56         try {
  57             main.setVisible(true);
  58             robot.waitForIdle();
  59             main.setSize(500, 500);
  60             robot.waitForIdle();
  61             for (GraphicsDevice gd : ge.getScreenDevices()) {
  62                 Rectangle bounds = gd.getDefaultConfiguration().getBounds();
  63                 Point point = bounds.getLocation();
  64                 point.translate(100, 100);
  65                 main.setLocation(point);
  66                 main.setBackground(Color.RED);
  67                 robot.waitForIdle();
  68 
  69                 Window packed = getTopLevel(top);
  70                 Window unpacked = getTopLevel(top);
  71                 try {
  72                     packed.pack();
  73                     robot.waitForIdle();
  74                     packed.setBackground(Color.GREEN);
  75                     unpacked.setBackground(Color.BLUE);
  76                     packed.setSize(500, 500);
  77                     unpacked.setSize(500, 500);
  78                     packed.setLocation(point);
  79                     unpacked.setLocation(point);
  80                     robot.waitForIdle();
  81                     packed.setVisible(true);
  82                     unpacked.setVisible(true);
  83                     robot.waitForIdle();
  84                     Rectangle mBounds = main.getBounds();
  85                     Rectangle pBounds = packed.getBounds();
  86                     Rectangle uBounds = unpacked.getBounds();
  87 
  88                     if (!mBounds.equals(uBounds) ||
  89                             !mBounds.equals(pBounds)) {
  90                         System.err.println("Expected bounds: " + mBounds);
  91                         System.err.println("Actual unpacked: " + uBounds);
  92                         System.err.println("Actual packed: " + pBounds);
  93                         throw new RuntimeException();
  94                     }
  95                 } finally {
  96                     packed.dispose();
  97                     unpacked.dispose();
  98                 }
  99             }
 100         } finally {
 101             main.dispose();
 102         }
 103     }
 104 
 105     private static Window getTopLevel(String top) {
 106         return switch (top) {
 107             case "window" -> new Window(null);
 108             case "dialog" -> new Dialog((Dialog) null);
 109             case "frame" -> new Frame();
 110             default -> throw new IllegalStateException("Unexpected: " + top);
 111         };
 112     }
 113 }