Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/7156657/bug7156657.java
38854 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.awt.AWTUtilities;24import sun.awt.SunToolkit;2526import javax.swing.*;27import java.awt.*;28import java.awt.image.BufferedImage;29import java.util.concurrent.Callable;3031/* @test32@bug 715665733@summary Version 7 doesn't support translucent popup menus against a translucent window34@library ../../regtesthelpers35@author Pavel Porvatov36*/37public class bug7156657 {38private static JFrame lowerFrame;3940private static JFrame frame;4142private static JPopupMenu popupMenu;4344public static void main(String[] args) throws Exception {45final Robot robot = new Robot();46final SunToolkit toolkit = ((SunToolkit) Toolkit.getDefaultToolkit());4748Boolean skipTest = Util.invokeOnEDT(new Callable<Boolean>() {49@Override50public Boolean call() throws Exception {51frame = createFrame();5253if (!AWTUtilities.isTranslucencyCapable(frame.getGraphicsConfiguration())) {54System.out.println("Translucency is not supported, the test skipped");5556return true;57}5859lowerFrame = createFrame();60lowerFrame.getContentPane().setBackground(Color.RED);61lowerFrame.setVisible(true);6263popupMenu = new JPopupMenu();64popupMenu.setOpaque(false);65popupMenu.add(new TransparentMenuItem("1111"));66popupMenu.add(new TransparentMenuItem("2222"));67popupMenu.add(new TransparentMenuItem("3333"));6869AWTUtilities.setWindowOpaque(frame, false);70JPanel pnContent = new JPanel();71pnContent.setBackground(new Color(255, 255, 255, 128));72frame.add(pnContent);73frame.setVisible(true);7475return false;76}77});7879if (skipTest) {80return;81}8283toolkit.realSync();8485SwingUtilities.invokeAndWait(new Runnable() {86@Override87public void run() {88popupMenu.show(frame, 0, 0);89}90});9192toolkit.realSync();9394Rectangle popupRectangle = Util.invokeOnEDT(new Callable<Rectangle>() {95@Override96public Rectangle call() throws Exception {97return popupMenu.getBounds();98}99});100101BufferedImage redBackgroundCapture = robot.createScreenCapture(popupRectangle);102103SwingUtilities.invokeAndWait(new Runnable() {104@Override105public void run() {106lowerFrame.getContentPane().setBackground(Color.GREEN);107}108});109110toolkit.realSync();111112BufferedImage greenBackgroundCapture = robot.createScreenCapture(popupRectangle);113114if (Util.compareBufferedImages(redBackgroundCapture, greenBackgroundCapture)) {115throw new RuntimeException("The test failed");116}117118SwingUtilities.invokeAndWait(new Runnable() {119@Override120public void run() {121popupMenu.setVisible(false);122frame.dispose();123lowerFrame.dispose();124}125});126127System.out.println("The test passed");128}129130131private static JFrame createFrame() {132JFrame result = new JFrame();133134result.setLocation(0, 0);135result.setSize(400, 300);136result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);137result.setUndecorated(true);138139return result;140}141142private static class TransparentMenuItem extends JMenuItem {143public TransparentMenuItem(String text) {144super(text);145setOpaque(false);146}147148@Override149public void paint(Graphics g) {150Graphics2D g2 = (Graphics2D) g.create();151g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));152super.paint(g2);153g2.dispose();154}155}156}157158159