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