1 /*
   2  * Copyright (c) 2016, 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  @test
  26  @key headful
  27  @bug 8062946
  28  @summary Verify Transparency upon iconify/deiconify sequence
  29  @run main TransparencyTest
  30  */
  31 import java.awt.Color;
  32 import java.awt.Point;
  33 import java.awt.Robot;
  34 import javax.swing.JDialog;
  35 import javax.swing.JFrame;
  36 import javax.swing.SwingUtilities;
  37 
  38 public class TransparencyTest {
  39 
  40     private static JFrame frame;
  41     private static JDialog dialog;
  42     private static JDialog backgroundDialog;
  43     private static final int WIDTH = 250;
  44     private static final int HEIGHT = 250;
  45     private static final float OPACITY = 0.60f;
  46     private static Point dlgPos;
  47 
  48     public static void createAndShowGUI() {
  49         frame = new JFrame("JFrame");
  50         frame.setSize(WIDTH, HEIGHT);
  51         frame.setLocation(100, 300);
  52 
  53         dialog = new JDialog(frame, false);
  54         dialog.setSize(250, 250);
  55         dialog.setUndecorated(true);
  56         dialog.setLocation(400, 300);
  57         dlgPos = dialog.getLocation();
  58         backgroundDialog = new JDialog(frame, false);
  59         backgroundDialog.setSize(250, 250);
  60         backgroundDialog.getContentPane().setBackground(Color.red);
  61         backgroundDialog.setLocation(dlgPos.x, dlgPos.y);
  62 
  63         frame.setVisible(true);
  64         backgroundDialog.setVisible(true);
  65         dialog.setVisible(true);
  66     }
  67 
  68     public static void main(String[] args) throws Exception {
  69 
  70         Robot robot = new Robot();
  71         // create a GUI
  72         SwingUtilities.invokeAndWait(new Runnable() {
  73 
  74             @Override
  75             public void run() {
  76                 createAndShowGUI();
  77             }
  78         });
  79         robot.waitForIdle();
  80         Color opaque = robot.getPixelColor(dlgPos.x + 100, dlgPos.y + 100);
  81 
  82         // set Dialog Opacity
  83         SwingUtilities.invokeAndWait(new Runnable() {
  84 
  85             @Override
  86             public void run() {
  87                 dialog.setOpacity(OPACITY);
  88             }
  89         });
  90         robot.waitForIdle();
  91 
  92         // iconify frame
  93         SwingUtilities.invokeAndWait(new Runnable() {
  94 
  95             @Override
  96             public void run() {
  97                 frame.setExtendedState(JFrame.ICONIFIED);
  98             }
  99         });
 100         robot.waitForIdle();
 101 
 102         // deiconify frame
 103         SwingUtilities.invokeAndWait(new Runnable() {
 104 
 105             @Override
 106             public void run() {
 107                 frame.setExtendedState(JFrame.NORMAL);
 108             }
 109         });
 110         robot.waitForIdle();
 111 
 112         Color transparent = robot.getPixelColor(dlgPos.x + 100, dlgPos.y + 100);
 113         if (transparent.equals(opaque)) {
 114             frame.dispose();
 115             throw new RuntimeException("JDialog transparency lost "
 116                     + "upon iconify/deiconify sequence");
 117         }
 118         frame.dispose();
 119     }
 120 }