1/*
2 * Copyright (c) 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 8062946 8159906
28 @summary Verify Transparency upon iconify/deiconify sequence
29 @run main TransparencyTest
30 */
31import java.awt.GraphicsEnvironment;
32import java.awt.GraphicsDevice;
33import java.awt.Color;
34import java.awt.Point;
35import java.awt.Robot;
36import javax.swing.JDialog;
37import javax.swing.JFrame;
38import javax.swing.SwingUtilities;
39
40public class TransparencyTest {
41
42    private static JFrame frame;
43    private static JDialog dialog;
44    private static JDialog backgroundDialog;
45    private static final int WIDTH = 250;
46    private static final int HEIGHT = 250;
47    private static final float OPACITY = 0.60f;
48    private static volatile Point dlgPos;
49
50    public static void createAndShowGUI() {
51        frame = new JFrame("JFrame");
52        frame.setSize(WIDTH, HEIGHT);
53        frame.setLocation(100, 300);
54
55        dialog = new JDialog(frame, false);
56        dialog.setSize(250, 250);
57        dialog.setUndecorated(true);
58        dialog.setLocation(400, 300);
59        dlgPos = dialog.getLocation();
60        backgroundDialog = new JDialog(frame, false);
61        backgroundDialog.setSize(250, 250);
62        backgroundDialog.getContentPane().setBackground(Color.red);
63        backgroundDialog.setLocation(dlgPos.x, dlgPos.y);
64
65        frame.setVisible(true);
66        backgroundDialog.setVisible(true);
67        dialog.setVisible(true);
68    }
69
70    public static void main(String[] args) throws Exception {
71
72        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
73        GraphicsDevice gd = ge.getDefaultScreenDevice();
74        GraphicsDevice.WindowTranslucency mode = GraphicsDevice.WindowTranslucency.TRANSLUCENT;
75        boolean translucencyCheck = gd.isWindowTranslucencySupported(mode);
76        if(!translucencyCheck) {
77            return;
78    }
79
80        Robot robot = new Robot();
81        // create a GUI
82        SwingUtilities.invokeAndWait(new Runnable() {
83
84            @Override
85            public void run() {
86                createAndShowGUI();
87            }
88        });
89        robot.waitForIdle();
90        Color opaque = robot.getPixelColor(dlgPos.x + 100, dlgPos.y + 100);
91
92        // set Dialog Opacity
93        SwingUtilities.invokeAndWait(new Runnable() {
94
95            @Override
96            public void run() {
97                dialog.setOpacity(OPACITY);
98            }
99        });
100        robot.waitForIdle();
101
102        // iconify frame
103        SwingUtilities.invokeAndWait(new Runnable() {
104
105            @Override
106            public void run() {
107                frame.setExtendedState(JFrame.ICONIFIED);
108            }
109        });
110        robot.waitForIdle();
111
112        // deiconify frame
113        SwingUtilities.invokeAndWait(new Runnable() {
114
115            @Override
116            public void run() {
117                frame.setExtendedState(JFrame.NORMAL);
118            }
119        });
120        robot.waitForIdle();
121
122        Color transparent = robot.getPixelColor(dlgPos.x + 100, dlgPos.y + 100);
123        if (transparent.equals(opaque)) {
124            frame.dispose();
125            throw new RuntimeException("JDialog transparency lost "
126                    + "upon iconify/deiconify sequence");
127        }
128        frame.dispose();
129    }
130}
131