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