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