5077N/A/*
5077N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5077N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5077N/A *
5077N/A * This code is free software; you can redistribute it and/or modify it
5077N/A * under the terms of the GNU General Public License version 2 only, as
5077N/A * published by the Free Software Foundation.
5077N/A *
5077N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5077N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5077N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5077N/A * version 2 for more details (a copy is included in the LICENSE file that
5077N/A * accompanied this code).
5077N/A *
5077N/A * You should have received a copy of the GNU General Public License version
5077N/A * 2 along with this work; if not, write to the Free Software Foundation,
5077N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5077N/A *
5077N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5077N/A * or visit www.oracle.com if you need additional information or have any
5077N/A * questions.
5077N/A */
5077N/A
5077N/Aimport com.sun.awt.AWTUtilities;
5077N/Aimport sun.awt.SunToolkit;
5077N/A
5077N/Aimport javax.swing.*;
5077N/Aimport java.awt.*;
5077N/Aimport java.awt.image.BufferedImage;
5077N/Aimport java.util.concurrent.Callable;
5077N/A
5077N/A/* @test
5077N/A @bug 7156657
5077N/A @summary Version 7 doesn't support translucent popup menus against a translucent window
5077N/A @library ../../regtesthelpers
5077N/A @author Pavel Porvatov
5077N/A*/
5077N/Apublic class bug7156657 {
5077N/A private static JFrame lowerFrame;
5077N/A
5077N/A private static JFrame frame;
5077N/A
5077N/A private static JPopupMenu popupMenu;
5077N/A
5077N/A public static void main(String[] args) throws Exception {
5077N/A final Robot robot = new Robot();
5077N/A final SunToolkit toolkit = ((SunToolkit) Toolkit.getDefaultToolkit());
5077N/A
5077N/A Boolean skipTest = Util.invokeOnEDT(new Callable<Boolean>() {
5077N/A @Override
5077N/A public Boolean call() throws Exception {
5077N/A frame = createFrame();
5077N/A
5077N/A if (!AWTUtilities.isTranslucencyCapable(frame.getGraphicsConfiguration())) {
5077N/A System.out.println("Translucency is not supported, the test skipped");
5077N/A
5077N/A return true;
5077N/A }
5077N/A
5077N/A lowerFrame = createFrame();
5077N/A lowerFrame.getContentPane().setBackground(Color.RED);
5077N/A lowerFrame.setVisible(true);
5077N/A
5077N/A popupMenu = new JPopupMenu();
5077N/A popupMenu.setOpaque(false);
5077N/A popupMenu.add(new TransparentMenuItem("1111"));
5077N/A popupMenu.add(new TransparentMenuItem("2222"));
5077N/A popupMenu.add(new TransparentMenuItem("3333"));
5077N/A
5077N/A AWTUtilities.setWindowOpaque(frame, false);
5077N/A JPanel pnContent = new JPanel();
5077N/A pnContent.setBackground(new Color(255, 255, 255, 128));
5077N/A frame.add(pnContent);
5077N/A frame.setVisible(true);
5077N/A
5077N/A return false;
5077N/A }
5077N/A });
5077N/A
5077N/A if (skipTest) {
5077N/A return;
5077N/A }
5077N/A
5077N/A toolkit.realSync();
5077N/A
5077N/A SwingUtilities.invokeAndWait(new Runnable() {
5077N/A @Override
5077N/A public void run() {
5077N/A popupMenu.show(frame, 0, 0);
5077N/A }
5077N/A });
5077N/A
5077N/A toolkit.realSync();
5077N/A
5077N/A Rectangle popupRectangle = Util.invokeOnEDT(new Callable<Rectangle>() {
5077N/A @Override
5077N/A public Rectangle call() throws Exception {
5077N/A return popupMenu.getBounds();
5077N/A }
5077N/A });
5077N/A
5077N/A BufferedImage redBackgroundCapture = robot.createScreenCapture(popupRectangle);
5077N/A
5077N/A SwingUtilities.invokeAndWait(new Runnable() {
5077N/A @Override
5077N/A public void run() {
5077N/A lowerFrame.getContentPane().setBackground(Color.GREEN);
5077N/A }
5077N/A });
5077N/A
5077N/A toolkit.realSync();
5077N/A
5077N/A BufferedImage greenBackgroundCapture = robot.createScreenCapture(popupRectangle);
5077N/A
5077N/A if (Util.compareBufferedImages(redBackgroundCapture, greenBackgroundCapture)) {
5077N/A throw new RuntimeException("The test failed");
5077N/A }
5077N/A
5077N/A SwingUtilities.invokeAndWait(new Runnable() {
5077N/A @Override
5077N/A public void run() {
5077N/A popupMenu.setVisible(false);
5077N/A frame.dispose();
5077N/A lowerFrame.dispose();
5077N/A }
5077N/A });
5077N/A
5077N/A System.out.println("The test passed");
5077N/A }
5077N/A
5077N/A
5077N/A private static JFrame createFrame() {
5077N/A JFrame result = new JFrame();
5077N/A
5077N/A result.setLocation(0, 0);
5077N/A result.setSize(400, 300);
5077N/A result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5077N/A result.setUndecorated(true);
5077N/A
5077N/A return result;
5077N/A }
5077N/A
5077N/A private static class TransparentMenuItem extends JMenuItem {
5077N/A public TransparentMenuItem(String text) {
5077N/A super(text);
5077N/A setOpaque(false);
5077N/A }
5077N/A
5077N/A @Override
5077N/A public void paint(Graphics g) {
5077N/A Graphics2D g2 = (Graphics2D) g.create();
5077N/A g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
5077N/A super.paint(g2);
5077N/A g2.dispose();
5077N/A }
5077N/A }
5077N/A}