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