1 /*
   2  * Copyright (c) 2006, 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 /*
  25   @test
  26   @key headful
  27   @bug 4737732
  28   @summary Tests that Toolkit.getScreenInsets() returns correct insets
  29   @author artem.ananiev: area=awt.toplevel
  30   @library ../../regtesthelpers
  31   @build Util
  32   @run main ScreenInsetsTest
  33 */
  34 
  35 import java.awt.Frame;
  36 import java.awt.GraphicsConfiguration;
  37 import java.awt.GraphicsDevice;
  38 import java.awt.GraphicsEnvironment;
  39 import java.awt.Insets;
  40 import java.awt.Rectangle;
  41 import java.awt.Toolkit;
  42 
  43 import test.java.awt.regtesthelpers.Util;
  44 
  45 public class ScreenInsetsTest
  46 {
  47     public static void main(String[] args)
  48     {
  49         boolean passed = true;
  50 
  51         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  52         GraphicsDevice[] gds = ge.getScreenDevices();
  53         for (GraphicsDevice gd : gds) {
  54 
  55             GraphicsConfiguration gc = gd.getDefaultConfiguration();
  56             Rectangle gcBounds = gc.getBounds();
  57             Insets gcInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  58             int left = gcInsets.left;
  59             int right = gcInsets.right;
  60             int bottom = gcInsets.bottom;
  61             int top = gcInsets.top;
  62             if (left < 0 || right < 0 || bottom < 0 || top < 0) {
  63                 throw new RuntimeException("Negative value: " + gcInsets);
  64             }
  65             int maxW = gcBounds.width / 3;
  66             int maxH = gcBounds.height / 3;
  67             if (left > maxW || right > maxW || bottom > maxH || top > maxH) {
  68                 throw new RuntimeException("Big value: " + gcInsets);
  69             }
  70 
  71             if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH))
  72             {
  73                 // this state is used in the test - sorry
  74                 continue;
  75             }
  76 
  77             Frame f = new Frame("Test", gc);
  78             f.setUndecorated(true);
  79             f.setBounds(gcBounds.x + 100, gcBounds.y + 100, 320, 240);
  80             f.setVisible(true);
  81             Util.waitForIdle(null);
  82 
  83             f.setExtendedState(Frame.MAXIMIZED_BOTH);
  84             Util.waitForIdle(null);
  85 
  86             Rectangle fBounds = f.getBounds();
  87             // workaround: on Windows maximized windows have negative coordinates
  88             if (fBounds.x < gcBounds.x)
  89             {
  90                 fBounds.width -= (gcBounds.x - fBounds.x) * 2; // width is decreased
  91                 fBounds.x = gcBounds.x;
  92             }
  93             if (fBounds.y < gcBounds.y)
  94             {
  95                 fBounds.height -= (gcBounds.y - fBounds.y) * 2; // height is decreased
  96                 fBounds.y = gcBounds.y;
  97             }
  98             Insets expected = new Insets(fBounds.y - gcBounds.y,
  99                                          fBounds.x - gcBounds.x,
 100                                          gcBounds.y + gcBounds.height - fBounds.y - fBounds.height,
 101                                          gcBounds.x + gcBounds.width - fBounds.x - fBounds.width);
 102 
 103             if (!expected.equals(gcInsets))
 104             {
 105                 passed = false;
 106                 System.err.println("Wrong insets for GraphicsConfig: " + gc);
 107                 System.err.println("\tExpected: " + expected);
 108                 System.err.println("\tActual: " + gcInsets);
 109             }
 110 
 111             f.dispose();
 112         }
 113 
 114         if (!passed)
 115         {
 116             throw new RuntimeException("TEST FAILED: Toolkit.getScreenInsets() returns wrong value for some screens");
 117         }
 118     }
 119 }