1 /*
   2  * Copyright (c) 2006, 2016, 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.*;
  36 import java.awt.event.*;
  37 
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class ScreenInsetsTest
  41 {
  42     public static void main(String[] args)
  43     {
  44         if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH))
  45         {
  46             // this state is used in the test - sorry
  47             return;
  48         }
  49 
  50         boolean passed = true;
  51 
  52         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  53         GraphicsDevice[] gds = ge.getScreenDevices();
  54         for (GraphicsDevice gd : gds)
  55         {
  56             GraphicsConfiguration gc = gd.getDefaultConfiguration();
  57             Rectangle gcBounds = gc.getBounds();
  58             Insets gcInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  59 
  60             Frame f = new Frame("Test", gc);
  61             f.setUndecorated(true);
  62             f.setBounds(gcBounds.x + 100, gcBounds.y + 100, 320, 240);
  63             f.setVisible(true);
  64             Util.waitForIdle(null);
  65 
  66             f.setExtendedState(Frame.MAXIMIZED_BOTH);
  67             Util.waitForIdle(null);
  68 
  69             Rectangle fBounds = f.getBounds();
  70             // workaround: on Windows maximized windows have negative coordinates
  71             if (fBounds.x < gcBounds.x)
  72             {
  73                 fBounds.width -= (gcBounds.x - fBounds.x) * 2; // width is decreased
  74                 fBounds.x = gcBounds.x;
  75             }
  76             if (fBounds.y < gcBounds.y)
  77             {
  78                 fBounds.height -= (gcBounds.y - fBounds.y) * 2; // height is decreased
  79                 fBounds.y = gcBounds.y;
  80             }
  81             Insets expected = new Insets(fBounds.y - gcBounds.y,
  82                                          fBounds.x - gcBounds.x,
  83                                          gcBounds.y + gcBounds.height - fBounds.y - fBounds.height,
  84                                          gcBounds.x + gcBounds.width - fBounds.x - fBounds.width);
  85 
  86             if (!expected.equals(gcInsets))
  87             {
  88                 passed = false;
  89                 System.err.println("Wrong insets for GraphicsConfig: " + gc);
  90                 System.err.println("\tExpected: " + expected);
  91                 System.err.println("\tActual: " + gcInsets);
  92             }
  93 
  94             f.dispose();
  95         }
  96 
  97         if (!passed)
  98         {
  99             throw new RuntimeException("TEST FAILED: Toolkit.getScreenInsets() returns wrong value for some screens");
 100         }
 101     }
 102 }