1 /*
   2  * Copyright (c) 2012, 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 import sun.awt.SunToolkit;
  33 
  34 import java.awt.AWTException;
  35 import java.awt.AlphaComposite;
  36 import java.awt.Color;
  37 import java.awt.Graphics;
  38 import java.awt.Graphics2D;
  39 import java.awt.Rectangle;
  40 import java.awt.Robot;
  41 import java.awt.Toolkit;
  42 import java.awt.image.BufferedImage;
  43 
  44 /*
  45  * @test
  46  * @key headful
  47  * @bug 7154030
  48  * @summary Swing components fail to hide after calling hide()
  49  * @author Jonathan Lu
  50  * @library ../../regtesthelpers/
  51  * @build Util
  52  * @run main bug7154030
  53  */
  54 
  55 public class bug7154030 {
  56 
  57     private static JButton button = null;
  58 
  59     public static void main(String[] args) throws Exception {
  60         BufferedImage imageInit = null;
  61 
  62         BufferedImage imageShow = null;
  63 
  64         BufferedImage imageHide = null;
  65 
  66         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  67 
  68         Robot robot = new Robot();
  69 
  70         SwingUtilities.invokeAndWait(new Runnable() {
  71 
  72             @Override
  73             public void run() {
  74                 JDesktopPane desktop = new JDesktopPane();
  75                 button = new JButton("button");
  76                 JFrame frame = new JFrame();
  77 
  78                 button.setSize(200, 200);
  79                 button.setLocation(100, 100);
  80                 button.setForeground(Color.RED);
  81                 button.setBackground(Color.RED);
  82                 button.setOpaque(true);
  83                 button.setVisible(false);
  84                 desktop.add(button);
  85 
  86                 frame.setContentPane(desktop);
  87                 frame.setSize(300, 300);
  88                 frame.setLocation(0, 0);
  89                 frame.setVisible(true);
  90                 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  91             }
  92         });
  93 
  94         toolkit.realSync();
  95         imageInit = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
  96 
  97         SwingUtilities.invokeAndWait(new Runnable() {
  98 
  99             @Override
 100             public void run() {
 101                 button.show();
 102             }
 103         });
 104 
 105         toolkit.realSync();
 106         imageShow = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
 107         if (Util.compareBufferedImages(imageInit, imageShow)) {
 108             throw new Exception("Failed to show opaque button");
 109         }
 110 
 111         toolkit.realSync();
 112 
 113         SwingUtilities.invokeAndWait(new Runnable() {
 114 
 115             @Override
 116             public void run() {
 117                 button.hide();
 118             }
 119         });
 120 
 121         toolkit.realSync();
 122         imageHide = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
 123 
 124         if (!Util.compareBufferedImages(imageInit, imageHide)) {
 125             throw new Exception("Failed to hide opaque button");
 126         }
 127 
 128         SwingUtilities.invokeAndWait(new Runnable() {
 129 
 130             @Override
 131             public void run() {
 132                 button.setOpaque(false);
 133                 button.setBackground(new Color(128, 128, 0));
 134                 button.setVisible(false);
 135             }
 136         });
 137 
 138         toolkit.realSync();
 139         imageInit = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
 140 
 141         SwingUtilities.invokeAndWait(new Runnable() {
 142 
 143             @Override
 144             public void run() {
 145                 button.show();
 146             }
 147         });
 148 
 149         toolkit.realSync();
 150         imageShow = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
 151 
 152         SwingUtilities.invokeAndWait(new Runnable() {
 153 
 154             @Override
 155             public void run() {
 156                 button.hide();
 157             }
 158         });
 159 
 160         if (Util.compareBufferedImages(imageInit, imageShow)) {
 161             throw new Exception("Failed to show non-opaque button");
 162         }
 163 
 164         toolkit.realSync();
 165         imageHide = robot.createScreenCapture(new Rectangle(0, 0, 300, 300));
 166 
 167         if (!Util.compareBufferedImages(imageInit, imageHide)) {
 168             throw new Exception("Failed to hide non-opaque button");
 169         }
 170     }
 171 }