1 /*
   2  * Copyright (c) 2013, 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 /* @test
  25  * @bug 8026143
  26  * @summary [macosx] Maximized state could be inconsistent between peer and frame
  27  * @author Petr Pchelko
  28  * @library ../../../../lib/testlibrary
  29  * @build jdk.testlibrary.OSInfo
  30  * @run main MaximizedByPlatform
  31  */
  32 
  33 import jdk.testlibrary.OSInfo;
  34 
  35 import java.awt.*;
  36 
  37 public class MaximizedByPlatform {
  38     private static Frame frame;
  39     private static Rectangle availableScreenBounds;
  40 
  41     public static void main(String[] args) {
  42         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
  43             // Test only for macosx. Pass
  44             return;
  45         }
  46 
  47         Robot robot;
  48         try {
  49             robot = new Robot();
  50         }catch(Exception ex) {
  51             ex.printStackTrace();
  52             throw new RuntimeException("Unexpected failure");
  53         }
  54         availableScreenBounds = getAvailableScreenBounds();
  55 
  56         // Test 1. The maximized state is set in setBounds
  57         try {
  58             frame = new Frame();
  59             frame.setBounds(100, 100, 100, 100);
  60             frame.setVisible(true);
  61 
  62             robot.waitForIdle();
  63 
  64             frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
  65                     availableScreenBounds.width, availableScreenBounds.height);
  66 
  67             robot.waitForIdle();
  68 
  69             if (frame.getExtendedState() != Frame.MAXIMIZED_BOTH) {
  70                 throw new RuntimeException("Maximized state was not set for frame in setBounds");
  71             }
  72         } finally {
  73             frame.dispose();
  74         }
  75 
  76 
  77         // Test 2. The maximized state is set in setVisible
  78         try {
  79             frame = new Frame();
  80             frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
  81                     availableScreenBounds.width + 100, availableScreenBounds.height);
  82             frame.setVisible(true);
  83 
  84             robot.waitForIdle();
  85 
  86             if (frame.getExtendedState() != Frame.MAXIMIZED_BOTH) {
  87                 throw new RuntimeException("Maximized state was not set for frame in setVisible");
  88             }
  89         } finally {
  90             frame.dispose();
  91         }
  92     }
  93 
  94     private static Rectangle getAvailableScreenBounds() {
  95         final Toolkit toolkit = Toolkit.getDefaultToolkit();
  96         final GraphicsEnvironment graphicsEnvironment =
  97                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  98         final GraphicsDevice graphicsDevice =
  99                 graphicsEnvironment.getDefaultScreenDevice();
 100 
 101         final Dimension screenSize = toolkit.getScreenSize();
 102         final Insets screenInsets = toolkit.getScreenInsets(
 103                 graphicsDevice.getDefaultConfiguration());
 104 
 105         final Rectangle availableScreenBounds = new Rectangle(screenSize);
 106 
 107         availableScreenBounds.x += screenInsets.left;
 108         availableScreenBounds.y += screenInsets.top;
 109         availableScreenBounds.width -= (screenInsets.left + screenInsets.right);
 110         availableScreenBounds.height -= (screenInsets.top + screenInsets.bottom);
 111         return availableScreenBounds;
 112     }
 113 }