Path: blob/master/test/jdk/javax/swing/JPopupMenu/7156657/bug7156657.java
41155 views
/*1* Copyright (c) 2012, 2021, 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 java.awt.AlphaComposite;24import java.awt.Color;25import java.awt.Graphics;26import java.awt.Graphics2D;27import java.awt.GraphicsConfiguration;28import java.awt.GraphicsDevice;29import java.awt.GraphicsEnvironment;30import java.awt.Point;31import java.awt.Rectangle;32import java.awt.Robot;33import java.awt.Window;34import java.awt.image.BufferedImage;35import java.io.File;36import java.util.concurrent.Callable;3738import javax.imageio.ImageIO;39import javax.swing.JFrame;40import javax.swing.JMenuItem;41import javax.swing.JPanel;42import javax.swing.JPopupMenu;43import javax.swing.SwingUtilities;4445/*46@test47@key headful48@bug 7156657 818661749@summary Version 7 doesn't support translucent popup menus against a50translucent window51@library ../../regtesthelpers52*/53public class bug7156657 {54private static JFrame lowerFrame;5556private static JFrame frame;5758private static JPopupMenu popupMenu;5960public static void main(String[] args) throws Exception {61final Robot robot = new Robot();6263Boolean skipTest = Util.invokeOnEDT(new Callable<Boolean>() {64@Override65public Boolean call() throws Exception {66frame = createFrame();67if (!frame.getGraphicsConfiguration().isTranslucencyCapable()) {68System.out.println("Translucency is not supported, the test skipped");6970return true;71}7273lowerFrame = createFrame();74lowerFrame.getContentPane().setBackground(Color.RED);75lowerFrame.setVisible(true);7677popupMenu = new JPopupMenu();78popupMenu.setOpaque(false);79popupMenu.add(new TransparentMenuItem("1111"));80popupMenu.add(new TransparentMenuItem("2222"));81popupMenu.add(new TransparentMenuItem("3333"));8283setOpaque(frame, false);84JPanel pnContent = new JPanel();85pnContent.setBackground(new Color(255, 255, 255, 128));86frame.add(pnContent);87frame.setVisible(true);8889return false;90}91});9293if (skipTest) {94return;95}9697robot.waitForIdle();9899SwingUtilities.invokeAndWait(new Runnable() {100@Override101public void run() {102popupMenu.show(frame, 0, 0);103}104});105106robot.waitForIdle();107108Rectangle popupRectangle = Util.invokeOnEDT(new Callable<Rectangle>() {109@Override110public Rectangle call() throws Exception {111return new Rectangle(popupMenu.getLocationOnScreen(),112popupMenu.getSize());113}114});115116BufferedImage redBackgroundCapture = robot.createScreenCapture(popupRectangle);117BufferedImage redFrame = robot.createScreenCapture(frame.getBounds());118119SwingUtilities.invokeAndWait(new Runnable() {120@Override121public void run() {122lowerFrame.getContentPane().setBackground(Color.GREEN);123lowerFrame.invalidate();124}125});126127robot.waitForIdle();128robot.delay(1000); // Give frame time to repaint129130BufferedImage greenBackgroundCapture = robot.createScreenCapture(popupRectangle);131BufferedImage greenFrame = robot.createScreenCapture(frame.getBounds());132133if (Util.compareBufferedImages(redBackgroundCapture, greenBackgroundCapture)) {134try {135GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();136for (int i = 0; i < devices.length; i++) {137GraphicsConfiguration[] screens = devices[i].getConfigurations();138for (int j = 0; j < screens.length; j++) {139BufferedImage fullScreen = robot.createScreenCapture(screens[j].getBounds());140if (screens[j].getBounds().intersects(popupRectangle)) {141Graphics g = fullScreen.getGraphics();142g.setColor(Color.CYAN);143g.drawRect(popupRectangle.x - 1, popupRectangle.y - 1,144popupRectangle.width + 2, popupRectangle.height + 2);145g.dispose();146}147ImageIO.write(fullScreen, "png", new File("dev" + i + "scr" + j + ".png"));148}149}150ImageIO.write(redFrame, "png", new File("redframe.png"));151ImageIO.write(redBackgroundCapture, "png", new File("redbg.png"));152ImageIO.write(greenFrame, "png", new File("greenframe.png"));153ImageIO.write(greenBackgroundCapture, "png", new File("greenbg.png"));154} finally {155SwingUtilities.invokeAndWait(() -> {156frame.dispose();157lowerFrame.dispose();158});159}160robot.waitForIdle();161throw new RuntimeException("The test failed");162}163164SwingUtilities.invokeAndWait(new Runnable() {165@Override166public void run() {167popupMenu.setVisible(false);168frame.dispose();169lowerFrame.dispose();170}171});172173System.out.println("The test passed");174}175176public static void setOpaque(Window window, boolean opaque) {177Color bg = window.getBackground();178if (bg == null) {179bg = new Color(0, 0, 0, 0);180}181window.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue(),182opaque ? 255 : 0));183}184185private static JFrame createFrame() {186JFrame result = new JFrame();187188result.setSize(400, 300);189result.setLocationRelativeTo(null);190result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);191result.setUndecorated(true);192193return result;194}195196private static class TransparentMenuItem extends JMenuItem {197public TransparentMenuItem(String text) {198super(text);199setOpaque(false);200}201202@Override203public void paint(Graphics g) {204Graphics2D g2 = (Graphics2D) g.create();205g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));206super.paint(g2);207g2.dispose();208}209}210}211212213