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  * @bug 6800513
  28  * @summary GTK-LaF renders menus incompletely
  29  * @author Mario Torre
  30  * @library ../../regtesthelpers/
  31  * @build Util
  32  * @run main bug6800513
  33  */
  34 
  35 import sun.awt.SunToolkit;
  36 
  37 import javax.swing.*;
  38 import java.awt.*;
  39 import java.awt.event.InputEvent;
  40 import java.beans.PropertyChangeEvent;
  41 import java.beans.PropertyChangeListener;
  42 import java.lang.reflect.Field;
  43 import java.util.concurrent.Callable;
  44 
  45 public class bug6800513 {
  46 
  47     private static JPopupMenu popupMenu;
  48     private static JMenu menu;
  49     private static JFrame frame;
  50 
  51     public static void testFrame(final boolean defaultLightWeightPopupEnabled,
  52             String expectedPopupClass) throws Exception {
  53         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  54 
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56             public void run() {
  57                 JPopupMenu.setDefaultLightWeightPopupEnabled(defaultLightWeightPopupEnabled);
  58                 createAndShowUI();
  59             }
  60         });
  61 
  62         toolkit.realSync();
  63 
  64         clickOnMenu();
  65 
  66         toolkit.realSync();
  67 
  68         Field getPopup = JPopupMenu.class.getDeclaredField("popup");
  69         getPopup.setAccessible(true);
  70         Popup popup = (Popup) getPopup.get(popupMenu);
  71 
  72         if (popup == null) {
  73             throw new Exception("popup is null!");
  74         }
  75 
  76         String className = popup.getClass().getName();
  77         if (!className.equals(expectedPopupClass)) {
  78             throw new Exception("popup class is: " + className +
  79                     ", expected: " + expectedPopupClass);
  80         }
  81 
  82         SwingUtilities.invokeAndWait(new Runnable() {
  83             @Override
  84             public void run() {
  85                 frame.dispose();
  86                 popupMenu = null;
  87             }
  88         });
  89 
  90         toolkit.realSync();
  91     }
  92 
  93 
  94     public static void clickOnMenu() throws Exception {
  95         Rectangle bounds = Util.invokeOnEDT(new Callable<Rectangle>() {
  96             @Override
  97             public Rectangle call() throws Exception {
  98                 return new Rectangle(menu.getLocationOnScreen(), menu.getSize());
  99             }
 100         });
 101 
 102         Robot robot = new Robot();
 103         robot.setAutoDelay(100);
 104 
 105         robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
 106 
 107         robot.mousePress(InputEvent.BUTTON1_MASK);
 108         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 109     }
 110 
 111     private static class PopupListener implements PropertyChangeListener {
 112         @Override
 113         public void propertyChange(PropertyChangeEvent evt) {
 114             if (evt.toString().contains("visible") && ((Boolean) evt.getNewValue() == true)) {
 115                 popupMenu = (JPopupMenu) evt.getSource();
 116             }
 117         }
 118     }
 119 
 120     public static void createAndShowUI() {
 121         frame = new JFrame();
 122 
 123         JMenuBar menuBar = new JMenuBar();
 124         menu = new JMenu("Menu");
 125 
 126         menu.add(new JMenuItem("Menu Item #1"));
 127         menu.add(new JMenuItem("Menu Item #2"));
 128         menu.add(new JMenuItem("Menu Item #3"));
 129 
 130         menuBar.add(menu);
 131 
 132         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 133         frame.setJMenuBar(menuBar);
 134         frame.setSize(500, 500);
 135 
 136         PopupListener listener = new PopupListener();
 137         menu.getPopupMenu().addPropertyChangeListener(listener);
 138 
 139         frame.setVisible(true);
 140     }
 141 
 142     public static void main(String[] args) throws Exception {
 143         testFrame(false, "javax.swing.PopupFactory$HeavyWeightPopup");
 144 
 145         testFrame(true, "javax.swing.PopupFactory$LightWeightPopup");
 146     }
 147 }