1 /*
   2  * Copyright 2012 Red Hat, Inc.  All Rights Reserved.
   3  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @key headful
  28  * @bug 6800513
  29  * @summary GTK-LaF renders menus incompletely
  30  * @author Mario Torre
  31  * @library ../../regtesthelpers/
  32  * @build Util
  33  * @run main bug6800513
  34  */
  35 
  36 import sun.awt.SunToolkit;
  37 
  38 import javax.swing.*;
  39 import java.awt.*;
  40 import java.awt.event.InputEvent;
  41 import java.beans.PropertyChangeEvent;
  42 import java.beans.PropertyChangeListener;
  43 import java.lang.reflect.Field;
  44 import java.util.concurrent.Callable;
  45 
  46 public class bug6800513 {
  47 
  48     private static JPopupMenu popupMenu;
  49     private static JMenu menu;
  50     private static JFrame frame;
  51 
  52     public static void testFrame(final boolean defaultLightWeightPopupEnabled,
  53             String expectedPopupClass) throws Exception {
  54         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  55 
  56         SwingUtilities.invokeAndWait(new Runnable() {
  57             public void run() {
  58                 JPopupMenu.setDefaultLightWeightPopupEnabled(defaultLightWeightPopupEnabled);
  59                 createAndShowUI();
  60             }
  61         });
  62 
  63         toolkit.realSync();
  64 
  65         clickOnMenu();
  66 
  67         toolkit.realSync();
  68 
  69         Field getPopup = JPopupMenu.class.getDeclaredField("popup");
  70         getPopup.setAccessible(true);
  71         Popup popup = (Popup) getPopup.get(popupMenu);
  72 
  73         if (popup == null) {
  74             throw new Exception("popup is null!");
  75         }
  76 
  77         String className = popup.getClass().getName();
  78         if (!className.equals(expectedPopupClass)) {
  79             throw new Exception("popup class is: " + className +
  80                     ", expected: " + expectedPopupClass);
  81         }
  82 
  83         SwingUtilities.invokeAndWait(new Runnable() {
  84             @Override
  85             public void run() {
  86                 frame.dispose();
  87                 popupMenu = null;
  88             }
  89         });
  90 
  91         toolkit.realSync();
  92     }
  93 
  94 
  95     public static void clickOnMenu() throws Exception {
  96         Rectangle bounds = Util.invokeOnEDT(new Callable<Rectangle>() {
  97             @Override
  98             public Rectangle call() throws Exception {
  99                 return new Rectangle(menu.getLocationOnScreen(), menu.getSize());
 100             }
 101         });
 102 
 103         Robot robot = new Robot();
 104         robot.setAutoDelay(100);
 105 
 106         robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
 107 
 108         robot.mousePress(InputEvent.BUTTON1_MASK);
 109         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 110     }
 111 
 112     private static class PopupListener implements PropertyChangeListener {
 113         @Override
 114         public void propertyChange(PropertyChangeEvent evt) {
 115             if (evt.toString().contains("visible") && ((Boolean) evt.getNewValue() == true)) {
 116                 popupMenu = (JPopupMenu) evt.getSource();
 117             }
 118         }
 119     }
 120 
 121     public static void createAndShowUI() {
 122         frame = new JFrame();
 123 
 124         JMenuBar menuBar = new JMenuBar();
 125         menu = new JMenu("Menu");
 126 
 127         menu.add(new JMenuItem("Menu Item #1"));
 128         menu.add(new JMenuItem("Menu Item #2"));
 129         menu.add(new JMenuItem("Menu Item #3"));
 130 
 131         menuBar.add(menu);
 132 
 133         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 134         frame.setJMenuBar(menuBar);
 135         frame.setSize(500, 500);
 136 
 137         PopupListener listener = new PopupListener();
 138         menu.getPopupMenu().addPropertyChangeListener(listener);
 139 
 140         frame.setVisible(true);
 141     }
 142 
 143     public static void main(String[] args) throws Exception {
 144         testFrame(false, "javax.swing.PopupFactory$HeavyWeightPopup");
 145 
 146         testFrame(true, "javax.swing.PopupFactory$LightWeightPopup");
 147     }
 148 }