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