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