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 8016356
  26    @summary Any swing frame resizes ugly.
  27    @run main bug8016356
  28    @author Oleg Pekhovskiy
  29 */
  30 
  31 import java.awt.AWTException;
  32 import java.awt.Color;
  33 import java.awt.Dimension;
  34 import java.awt.GraphicsConfiguration;
  35 import java.awt.GraphicsEnvironment;
  36 import java.awt.Insets;
  37 import java.awt.Point;
  38 import java.awt.Rectangle;
  39 import java.awt.Robot;
  40 import java.awt.Toolkit;
  41 import java.awt.event.InputEvent;
  42 import javax.swing.JFrame;
  43 import javax.swing.JPanel;
  44 import javax.swing.SwingUtilities;
  45 import sun.awt.OSInfo;
  46 
  47 public class bug8016356 {
  48     private static JFrame frame;
  49     private static Color color;
  50     private static int scrTop;
  51 
  52     private static Point frLoc;
  53     private static Dimension frSize;
  54 
  55     public static void main(String[] args) throws Exception {
  56 
  57         // Windows only test
  58         if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {
  59 
  60             // Retrieving top edge of Desktop
  61             GraphicsConfiguration grConf = GraphicsEnvironment
  62                     .getLocalGraphicsEnvironment().getDefaultScreenDevice()
  63                     .getDefaultConfiguration();
  64             Rectangle scrRect = grConf.getBounds();
  65             Insets scrInsets = Toolkit.getDefaultToolkit().getScreenInsets(grConf);
  66             scrTop = scrRect.y + scrInsets.top;
  67 
  68             color = new Color(0, 255, 0);
  69 
  70             SwingUtilities.invokeAndWait(() -> {
  71                 createAndShowUI();
  72             });
  73 
  74             try {
  75                 Robot robot = new Robot();
  76                 robot.setAutoDelay(500);
  77                 robot.setAutoWaitForIdle(true);
  78                 robot.delay(1000);
  79 
  80                 // Resizing a window to invoke Windows Snap feature
  81                 readFrameInfo();
  82                 robot.mouseMove(frLoc.x + frSize.width / 2, frLoc.y);
  83                 robot.mousePress(InputEvent.BUTTON1_MASK);
  84                 robot.mouseMove(frLoc.x + frSize.width / 2, scrTop);
  85                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
  86 
  87                 // Retrieving the color of window expanded area
  88                 readFrameInfo();
  89                 Insets insets = frame.getInsets();
  90                 Color bgColor = robot.getPixelColor(frLoc.x + frSize.width / 2,
  91                         frLoc.y + frSize.height - insets.bottom - 1);
  92 
  93                 frame.dispose();
  94 
  95                 if (!bgColor.equals(color)) {
  96                     throw new RuntimeException("TEST FAILED: got "
  97                             + bgColor + " instead of " + color);
  98                 }
  99                 System.out.println("TEST PASSED!");
 100             } catch (AWTException ex) {
 101                 throw new RuntimeException("TEST FAILED!");
 102             }
 103         }
 104     }
 105 
 106     private static void createAndShowUI() {
 107         frame = new JFrame();
 108         frame.setBounds(10, scrTop + 10, 300, 100);
 109         JPanel panel = new JPanel();
 110         panel.setBackground(color);
 111         frame.getContentPane().add(panel);
 112         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 113         frame.setVisible(true);
 114     }
 115 
 116     private static void readFrameInfo() throws Exception {
 117         SwingUtilities.invokeAndWait(() -> {
 118             frLoc = frame.getLocationOnScreen();
 119             frSize = frame.getSize();
 120         });
 121     }
 122 }