Path: blob/master/test/jdk/javax/swing/JComponent/7154030/bug7154030.java
66646 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Portions Copyright (c) 2012 IBM Corporation25*/2627import javax.swing.JButton;28import javax.swing.JDesktopPane;29import javax.swing.JFrame;30import javax.swing.SwingUtilities;3132import java.awt.Color;33import java.awt.Dimension;34import java.awt.Graphics;35import java.awt.Insets;36import java.awt.Rectangle;37import java.awt.Toolkit;38import java.awt.image.BufferedImage;39import javax.imageio.ImageIO;40import java.io.File;4142/*43* @test44* @key headful45* @bug 715403046* @summary Swing components fail to hide after calling hide()47* @author Jonathan Lu48* @library ../../regtesthelpers/49* @library /lib/client/50* @build Util51* @build ExtendedRobot52* @run main bug715403053*/5455public class bug7154030 {5657private static JButton button = null;58private static JFrame frame;59private static volatile int locx, locy, frw, frh;6061public static void main(String[] args) throws Exception {62try {63BufferedImage imageInit = null;6465BufferedImage imageShow = null;6667BufferedImage imageHide = null;6869ExtendedRobot robot = new ExtendedRobot();7071SwingUtilities.invokeAndWait(new Runnable() {7273@Override74public void run() {75JDesktopPane desktop = new JDesktopPane();76button = new JButton("button");77frame = new JFrame();78frame.setUndecorated(true);7980button.setSize(200, 200);81button.setLocation(100, 100);82button.setForeground(Color.RED);83button.setBackground(Color.RED);84button.setOpaque(true);85button.setVisible(false);86desktop.add(button);87desktop.setMinimumSize(new Dimension(300, 300));88desktop.setMaximumSize(new Dimension(300, 300));8990frame.setContentPane(desktop);91frame.setMinimumSize(new Dimension(350, 350));92frame.setMaximumSize(new Dimension(350, 350));93frame.pack();94frame.setLocationRelativeTo(null);95frame.setVisible(true);96frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);97}98});99100robot.waitForIdle(1000);101robot.delay(1000);102103Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();104Rectangle screen = new Rectangle(0, 0, (int) screenSize.getWidth(), (int) screenSize.getHeight());105SwingUtilities.invokeAndWait(() -> {106Rectangle bounds = frame.getBounds();107Insets insets = frame.getInsets();108locx = bounds.x + insets.left;109locy = bounds.y + insets.top;110frw = bounds.width - insets.left - insets.right;111frh = bounds.height - insets.top - insets.bottom;112});113114BufferedImage fullScreen = robot.createScreenCapture(screen);115Graphics g = fullScreen.getGraphics();116g.setColor(Color.RED);117g.drawRect(locx - 1, locy - 1, frw + 1, frh + 1);118imageInit = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));119120SwingUtilities.invokeAndWait(new Runnable() {121122@Override123public void run() {124button.show();125}126});127128robot.waitForIdle(500);129imageShow = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));130if (Util.compareBufferedImages(imageInit, imageShow)) {131ImageIO.write(imageInit, "png", new File("imageInit.png"));132ImageIO.write(imageShow, "png", new File("imageShow.png"));133ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));134throw new Exception("Failed to show opaque button");135}136137robot.waitForIdle();138139SwingUtilities.invokeAndWait(new Runnable() {140@Override141public void run() {142button.hide();143}144});145146robot.waitForIdle(500);147imageHide = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));148149if (!Util.compareBufferedImages(imageInit, imageHide)) {150ImageIO.write(imageInit, "png", new File("imageInit.png"));151ImageIO.write(imageHide, "png", new File("imageHide.png"));152ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));153throw new Exception("Failed to hide opaque button");154}155156SwingUtilities.invokeAndWait(new Runnable() {157158@Override159public void run() {160button.setOpaque(false);161button.setBackground(new Color(128, 128, 0));162button.setVisible(false);163}164});165166robot.waitForIdle(500);167imageInit = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));168169SwingUtilities.invokeAndWait(new Runnable() {170171@Override172public void run() {173button.show();174}175});176177robot.waitForIdle(500);178imageShow = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));179180if (Util.compareBufferedImages(imageInit, imageShow)) {181ImageIO.write(imageInit, "png", new File("imageInit.png"));182ImageIO.write(imageShow, "png", new File("imageShow.png"));183ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));184throw new Exception("Failed to show non-opaque button");185}186187SwingUtilities.invokeAndWait(new Runnable() {188189@Override190public void run() {191button.hide();192}193});194195robot.waitForIdle(500);196imageHide = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));197198if (!Util.compareBufferedImages(imageInit, imageHide)) {199ImageIO.write(imageInit, "png", new File("imageInit.png"));200ImageIO.write(imageHide, "png", new File("imageHide.png"));201ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));202throw new Exception("Failed to hide non-opaque button");203}204} finally {205if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());206}207}208}209210211