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