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