1 /*
   2  * Copyright (c) 2007, 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.*;
  25 import java.lang.Math;
  26 
  27 /**
  28  * @test
  29  * @key headful
  30  * @bug 8065739 8131339 8196006
  31  * @requires (os.family == "windows" | os.family == "mac")
  32  * @summary When Frame.setExtendedState(Frame.MAXIMIZED_BOTH)
  33  *          is called for a Frame after been called setMaximizedBounds() with
  34  *          certain value, Frame bounds must equal to this value.
  35  *
  36  * @run main SetMaximizedBounds
  37  */
  38 
  39 public class SetMaximizedBounds {
  40 
  41     public static void main(String[] args) throws Exception {
  42 
  43         if (!Toolkit.getDefaultToolkit().
  44                 isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {
  45             return;
  46         }
  47 
  48         GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
  49                             .getDefaultScreenDevice().getDefaultConfiguration();
  50 
  51         testMaximizedBounds(gc, false);
  52         testMaximizedBounds(gc, true);
  53     }
  54 
  55     static void testMaximizedBounds(GraphicsConfiguration gc, boolean undecorated)
  56             throws Exception {
  57 
  58         Frame frame = null;
  59         try {
  60             Rectangle maxArea = getMaximizedScreenArea(gc);
  61 
  62             Robot robot = new Robot();
  63             robot.setAutoDelay(50);
  64 
  65             frame = new Frame();
  66             frame.setUndecorated(undecorated);
  67             Rectangle maximizedBounds = new Rectangle(
  68                     maxArea.x + maxArea.width / 6,
  69                     maxArea.y + maxArea.height / 6,
  70                     maxArea.width / 3,
  71                     maxArea.height / 3);
  72             frame.setMaximizedBounds(maximizedBounds);
  73             frame.setSize(maxArea.width / 8, maxArea.height / 8);
  74             frame.setVisible(true);
  75             robot.waitForIdle();
  76 
  77             frame.setExtendedState(Frame.MAXIMIZED_BOTH);
  78             robot.waitForIdle();
  79             robot.delay(1000);
  80 
  81             Rectangle bounds = frame.getBounds();
  82             applyScale(bounds, gc.getDefaultTransform().getScaleX(), gc.getDefaultTransform().getScaleY());
  83             if (!compare(bounds, maximizedBounds)) {
  84                 throw new RuntimeException("The bounds of the Frame do not equal to what"
  85                         + " is specified when the frame is in Frame.MAXIMIZED_BOTH");
  86             }
  87 
  88             frame.setExtendedState(Frame.NORMAL);
  89             robot.waitForIdle();
  90             robot.delay(1000);
  91 
  92             maximizedBounds = new Rectangle(
  93                     maxArea.x + maxArea.width / 10,
  94                     maxArea.y + maxArea.height / 10,
  95                     maxArea.width / 5,
  96                     maxArea.height / 5);
  97             frame.setMaximizedBounds(maximizedBounds);
  98             frame.setExtendedState(Frame.MAXIMIZED_BOTH);
  99             robot.waitForIdle();
 100             robot.delay(1000);
 101 
 102             bounds = frame.getBounds();
 103             applyScale(bounds, gc.getDefaultTransform().getScaleX(), gc.getDefaultTransform().getScaleY());
 104             if (!compare(bounds, maximizedBounds)) {
 105                 throw new RuntimeException("The bounds of the Frame do not equal to what"
 106                         + " is specified when the frame is in Frame.MAXIMIZED_BOTH");
 107             }
 108         } finally {
 109             if (frame != null) {
 110                 frame.dispose();
 111             }
 112         }
 113     }
 114 
 115     static Rectangle getMaximizedScreenArea(GraphicsConfiguration gc) {
 116         Rectangle bounds = gc.getBounds();
 117         Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
 118         return new Rectangle(
 119                 bounds.x + insets.left,
 120                 bounds.y + insets.top,
 121                 bounds.width - insets.left - insets.right,
 122                 bounds.height - insets.top - insets.bottom);
 123     }
 124 
 125     static void applyScale(Rectangle rect, double scaleX, double scaleY) {
 126         rect.x = (int)Math.ceil(((double)rect.x * scaleX));
 127         rect.y = (int)Math.ceil(((double)rect.y * scaleY));
 128         rect.width = (int)Math.ceil(((double)rect.width * scaleX));
 129         rect.height = (int)Math.ceil(((double)rect.height * scaleY));
 130     }
 131 
 132     static boolean compare(Rectangle r1, Rectangle r2) {
 133         return (Math.abs(r1.x - r2.x) <= 2 &&
 134             Math.abs(r1.y - r2.y) <= 2 &&
 135             Math.abs(r1.width - r2.width) <= 2 &&
 136             Math.abs(r1.height - r2.height) <= 2);
 137     }
 138 }