< prev index next >

test/javax/swing/JPopupMenu/7156657/bug7156657.java

Print this page


   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 
  24 import com.sun.awt.AWTUtilities;
  25 import sun.awt.SunToolkit;
  26 
  27 import javax.swing.*;
  28 import java.awt.*;



  29 import java.awt.image.BufferedImage;
  30 import 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 */
  42 public 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             }


 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) {
   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             }


 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) {
< prev index next >