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