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