Path: blob/master/test/jdk/javax/swing/JComponent/7154030/bug7154030.java
41153 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 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();7879button.setSize(200, 200);80button.setLocation(100, 100);81button.setForeground(Color.RED);82button.setBackground(Color.RED);83button.setOpaque(true);84button.setVisible(false);85desktop.add(button);86desktop.setMinimumSize(new Dimension(300, 300));87desktop.setMaximumSize(new Dimension(300, 300));8889frame.setContentPane(desktop);90frame.setMinimumSize(new Dimension(350, 350));91frame.setMaximumSize(new Dimension(350, 350));92frame.pack();93frame.setLocationRelativeTo(null);94frame.setVisible(true);95frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);96}97});9899robot.waitForIdle(1000);100robot.delay(1000);101102Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();103Rectangle screen = new Rectangle(0, 0, (int) screenSize.getWidth(), (int) screenSize.getHeight());104Rectangle bounds = frame.getBounds();105Insets insets = frame.getInsets();106locx = bounds.x + insets.left;107locy = bounds.y + insets.top;108frw = bounds.width - insets.left - insets.right;109frh = bounds.height - insets.top - insets.bottom;110111BufferedImage fullScreen = robot.createScreenCapture(screen);112Graphics g = fullScreen.getGraphics();113g.setColor(Color.RED);114g.drawRect(locx - 1, locy - 1, frw + 1, frh + 1);115imageInit = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));116117SwingUtilities.invokeAndWait(new Runnable() {118119@Override120public void run() {121button.show();122}123});124125robot.waitForIdle(500);126imageShow = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));127if (Util.compareBufferedImages(imageInit, imageShow)) {128ImageIO.write(imageInit, "png", new File("imageInit.png"));129ImageIO.write(imageShow, "png", new File("imageShow.png"));130ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));131throw new Exception("Failed to show opaque button");132}133134robot.waitForIdle();135136SwingUtilities.invokeAndWait(new Runnable() {137@Override138public void run() {139button.hide();140}141});142143robot.waitForIdle(500);144imageHide = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));145146if (!Util.compareBufferedImages(imageInit, imageHide)) {147ImageIO.write(imageInit, "png", new File("imageInit.png"));148ImageIO.write(imageHide, "png", new File("imageHide.png"));149ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));150throw new Exception("Failed to hide opaque button");151}152153SwingUtilities.invokeAndWait(new Runnable() {154155@Override156public void run() {157button.setOpaque(false);158button.setBackground(new Color(128, 128, 0));159button.setVisible(false);160}161});162163robot.waitForIdle(500);164imageInit = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));165166SwingUtilities.invokeAndWait(new Runnable() {167168@Override169public void run() {170button.show();171}172});173174robot.waitForIdle(500);175imageShow = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));176177if (Util.compareBufferedImages(imageInit, imageShow)) {178ImageIO.write(imageInit, "png", new File("imageInit.png"));179ImageIO.write(imageShow, "png", new File("imageShow.png"));180ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));181throw new Exception("Failed to show non-opaque button");182}183184SwingUtilities.invokeAndWait(new Runnable() {185186@Override187public void run() {188button.hide();189}190});191192robot.waitForIdle(500);193imageHide = robot.createScreenCapture(new Rectangle(locx, locy, frw, frh));194195if (!Util.compareBufferedImages(imageInit, imageHide)) {196ImageIO.write(imageInit, "png", new File("imageInit.png"));197ImageIO.write(imageHide, "png", new File("imageHide.png"));198ImageIO.write(fullScreen, "png", new File("fullScreenInit.png"));199throw new Exception("Failed to hide non-opaque button");200}201} finally {202if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());203}204}205}206207208