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 import com.sun.awt.AWTUtilities;
  25 import sun.awt.SunToolkit;
  26 
  27 import javax.swing.*;
  28 import java.awt.*;
  29 import java.awt.image.BufferedImage;
  30 import java.util.concurrent.Callable;
  31 
  32 /* @test
  33    @bug 7156657
  34    @summary Version 7 doesn't support translucent popup menus against a translucent window
  35    @library ../../regtesthelpers
  36    @author Pavel Porvatov
  37 */
  38 public class bug7156657 {
  39     private static JFrame lowerFrame;
  40 
  41     private static JFrame frame;
  42 
  43     private static JPopupMenu popupMenu;
  44 
  45     public static void main(String[] args) throws Exception {
  46         final Robot robot = new Robot();
  47         final SunToolkit toolkit = ((SunToolkit) Toolkit.getDefaultToolkit());
  48 
  49         Boolean skipTest = Util.invokeOnEDT(new Callable<Boolean>() {
  50             @Override
  51             public Boolean call() throws Exception {
  52                 frame = createFrame();
  53 
  54                 if (!AWTUtilities.isTranslucencyCapable(frame.getGraphicsConfiguration())) {
  55                     System.out.println("Translucency is not supported, the test skipped");
  56 
  57                     return true;
  58                 }
  59 
  60                 lowerFrame = createFrame();
  61                 lowerFrame.getContentPane().setBackground(Color.RED);
  62                 lowerFrame.setVisible(true);
  63 
  64                 popupMenu = new JPopupMenu();
  65                 popupMenu.setOpaque(false);
  66                 popupMenu.add(new TransparentMenuItem("1111"));
  67                 popupMenu.add(new TransparentMenuItem("2222"));
  68                 popupMenu.add(new TransparentMenuItem("3333"));
  69 
  70                 AWTUtilities.setWindowOpaque(frame, false);
  71                 JPanel pnContent = new JPanel();
  72                 pnContent.setBackground(new Color(255, 255, 255, 128));
  73                 frame.add(pnContent);
  74                 frame.setVisible(true);
  75 
  76                 return false;
  77             }
  78         });
  79 
  80         if (skipTest) {
  81             return;
  82         }
  83 
  84         toolkit.realSync();
  85 
  86         SwingUtilities.invokeAndWait(new Runnable() {
  87             @Override
  88             public void run() {
  89                 popupMenu.show(frame, 0, 0);
  90             }
  91         });
  92 
  93         toolkit.realSync();
  94 
  95         Rectangle popupRectangle = Util.invokeOnEDT(new Callable<Rectangle>() {
  96             @Override
  97             public Rectangle call() throws Exception {
  98                 return popupMenu.getBounds();
  99             }
 100         });
 101 
 102         BufferedImage redBackgroundCapture = robot.createScreenCapture(popupRectangle);
 103 
 104         SwingUtilities.invokeAndWait(new Runnable() {
 105             @Override
 106             public void run() {
 107                 lowerFrame.getContentPane().setBackground(Color.GREEN);
 108             }
 109         });
 110 
 111         toolkit.realSync();
 112 
 113         BufferedImage greenBackgroundCapture = robot.createScreenCapture(popupRectangle);
 114 
 115         if (Util.compareBufferedImages(redBackgroundCapture, greenBackgroundCapture)) {
 116             throw new RuntimeException("The test failed");
 117         }
 118 
 119         SwingUtilities.invokeAndWait(new Runnable() {
 120             @Override
 121             public void run() {
 122                 popupMenu.setVisible(false);
 123                 frame.dispose();
 124                 lowerFrame.dispose();
 125             }
 126         });
 127 
 128         System.out.println("The test passed");
 129     }
 130 
 131 
 132     private static JFrame createFrame() {
 133         JFrame result = new JFrame();
 134 
 135         result.setLocation(0, 0);
 136         result.setSize(400, 300);
 137         result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 138         result.setUndecorated(true);
 139 
 140         return result;
 141     }
 142 
 143     private static class TransparentMenuItem extends JMenuItem {
 144         public TransparentMenuItem(String text) {
 145             super(text);
 146             setOpaque(false);
 147         }
 148 
 149         @Override
 150         public void paint(Graphics g) {
 151             Graphics2D g2 = (Graphics2D) g.create();
 152             g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
 153             super.paint(g2);
 154             g2.dispose();
 155         }
 156     }
 157 }