1 /*
2  * Copyright (c) 2012, 2021, 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  * Portions Copyright (c) 2012 IBM Corporation
26  */
27 
28 import javax.swing.JButton;
29 import javax.swing.JDesktopPane;
30 import javax.swing.JFrame;
31 import javax.swing.SwingUtilities;
32 
33 import java.awt.Color;
34 import java.awt.Insets;
35 import java.awt.Rectangle;
36 import java.awt.image.BufferedImage;
37 import javax.imageio.ImageIO;
38 import java.io.File;
39 
40 /*
41  * @test
42  * @key headful
43  * @bug 7154030
44  * @summary Swing components fail to hide after calling hide()
45  * @author Jonathan Lu
46  * @library ../../regtesthelpers/
47  * @library /lib/client/
48  * @build Util
49  * @build ExtendedRobot
50  * @run main bug7154030
51  */
52 
53 public class bug7154030 {
54 
55     private static JButton button = null;
56     private static JFrame frame;
57     private static int locx, locy, frw, frh;
58 
main(String[] args)59     public static void main(String[] args) throws Exception {
60         try {
61             BufferedImage imageInit = null;
62 
63             BufferedImage imageShow = null;
64 
65             BufferedImage imageHide = null;
66 
67             ExtendedRobot robot = new ExtendedRobot();
68 
69             SwingUtilities.invokeAndWait(new Runnable() {
70 
71                 @Override
72                 public void run() {
73                     JDesktopPane desktop = new JDesktopPane();
74                     button = new JButton("button");
75                     frame = new JFrame();
76 
77                     button.setSize(200, 200);
78                     button.setLocation(100, 100);
79                     button.setForeground(Color.RED);
80                     button.setBackground(Color.RED);
81                     button.setOpaque(true);
82                     button.setVisible(false);
83                     desktop.add(button);
84 
85                     frame.setContentPane(desktop);
86                     frame.setSize(300, 300);
87                     frame.setLocationRelativeTo(null);
88                     frame.setVisible(true);
89                     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
90                 }
91             });
92 
93             robot.delay(1000);
94             robot.waitForIdle(1000);
95 
96             Rectangle bounds = frame.getBounds();
97             Insets insets = frame.getInsets();
98             locx = bounds.x + insets.left;
99             locy = bounds.y + insets.top;
100             frw = bounds.width - insets.left - insets.right;
101             frh = bounds.height - insets.top - insets.bottom;
102 
103             imageInit = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));
104 
105             SwingUtilities.invokeAndWait(new Runnable() {
106 
107                 @Override
108                 public void run() {
109                     button.show();
110                 }
111             });
112 
113             robot.waitForIdle(500);
114             imageShow = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));
115             if (Util.compareBufferedImages(imageInit, imageShow)) {
116                 ImageIO.write(imageInit, "png", new File("imageInit.png"));
117                 ImageIO.write(imageShow, "png", new File("imageShow.png"));
118                 throw new Exception("Failed to show opaque button");
119             }
120 
121             robot.waitForIdle();
122 
123             SwingUtilities.invokeAndWait(new Runnable() {
124                 @Override
125                 public void run() {
126                     button.hide();
127                 }
128             });
129 
130             robot.waitForIdle(500);
131             imageHide = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));
132 
133             if (!Util.compareBufferedImages(imageInit, imageHide)) {
134                 ImageIO.write(imageInit, "png", new File("imageInit.png"));
135                 ImageIO.write(imageHide, "png", new File("imageHide.png"));
136                 throw new Exception("Failed to hide opaque button");
137             }
138 
139             SwingUtilities.invokeAndWait(new Runnable() {
140 
141                 @Override
142                 public void run() {
143                     button.setOpaque(false);
144                     button.setBackground(new Color(128, 128, 0));
145                     button.setVisible(false);
146                 }
147             });
148 
149             robot.waitForIdle(500);
150             imageInit = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));
151 
152             SwingUtilities.invokeAndWait(new Runnable() {
153 
154                 @Override
155                 public void run() {
156                     button.show();
157                 }
158             });
159 
160             robot.waitForIdle(500);
161             imageShow = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));
162 
163             SwingUtilities.invokeAndWait(new Runnable() {
164 
165                 @Override
166                 public void run() {
167                     button.hide();
168                 }
169             });
170 
171             if (Util.compareBufferedImages(imageInit, imageShow)) {
172                 ImageIO.write(imageInit, "png", new File("imageInit.png"));
173                 ImageIO.write(imageShow, "png", new File("imageShow.png"));
174                 throw new Exception("Failed to show non-opaque button");
175             }
176 
177             robot.waitForIdle(500);
178             imageHide = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));
179 
180             if (!Util.compareBufferedImages(imageInit, imageHide)) {
181                 ImageIO.write(imageInit, "png", new File("imageInit.png"));
182                 ImageIO.write(imageHide, "png", new File("imageHide.png"));
183                 throw new Exception("Failed to hide non-opaque button");
184             }
185         } finally {
186             if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
187         }
188     }
189 }
190