Path: blob/master/test/jdk/javax/swing/JPopupMenu/6800513/bug6800513.java
41153 views
/*1* Copyright 2012 Red Hat, Inc. All Rights Reserved.2* Copyright (c) 2012, 2020, 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @key headful27* @bug 680051328* @summary GTK-LaF renders menus incompletely29* @author Mario Torre30* @modules java.desktop/javax.swing:open31* @library ../../regtesthelpers/32* @build Util33* @run main bug680051334*/3536import javax.swing.*;37import java.awt.*;38import java.awt.event.InputEvent;39import java.beans.PropertyChangeEvent;40import java.beans.PropertyChangeListener;41import java.lang.reflect.Field;42import java.util.concurrent.Callable;4344public class bug6800513 {4546private static JPopupMenu popupMenu;47private static JMenu menu;48private static JFrame frame;49private static Robot robot;5051public static void testFrame(final boolean defaultLightWeightPopupEnabled,52String expectedPopupClass) throws Exception {5354SwingUtilities.invokeAndWait(new Runnable() {55public void run() {56JPopupMenu.setDefaultLightWeightPopupEnabled(defaultLightWeightPopupEnabled);57createAndShowUI();58}59});6061robot.waitForIdle();6263clickOnMenu();6465robot.waitForIdle();6667Field getPopup = JPopupMenu.class.getDeclaredField("popup");68getPopup.setAccessible(true);69Popup popup = (Popup) getPopup.get(popupMenu);7071if (popup == null) {72throw new Exception("popup is null!");73}7475String className = popup.getClass().getName();76if (!className.equals(expectedPopupClass)) {77throw new Exception("popup class is: " + className +78", expected: " + expectedPopupClass);79}8081SwingUtilities.invokeAndWait(new Runnable() {82@Override83public void run() {84frame.dispose();85popupMenu = null;86}87});8889robot.waitForIdle();90}919293public static void clickOnMenu() throws Exception {94Rectangle bounds = Util.invokeOnEDT(new Callable<Rectangle>() {95@Override96public Rectangle call() throws Exception {97return new Rectangle(menu.getLocationOnScreen(), menu.getSize());98}99});100101robot.setAutoDelay(100);102103robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);104105robot.mousePress(InputEvent.BUTTON1_MASK);106robot.mouseRelease(InputEvent.BUTTON1_MASK);107}108109private static class PopupListener implements PropertyChangeListener {110@Override111public void propertyChange(PropertyChangeEvent evt) {112if (evt.toString().contains("visible") && ((Boolean) evt.getNewValue() == true)) {113popupMenu = (JPopupMenu) evt.getSource();114}115}116}117118public static void createAndShowUI() {119frame = new JFrame();120121JMenuBar menuBar = new JMenuBar();122menu = new JMenu("Menu");123124menu.add(new JMenuItem("Menu Item #1"));125menu.add(new JMenuItem("Menu Item #2"));126menu.add(new JMenuItem("Menu Item #3"));127128menuBar.add(menu);129130frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);131frame.setJMenuBar(menuBar);132frame.setSize(500, 500);133frame.setLocationRelativeTo(null);134135PopupListener listener = new PopupListener();136menu.getPopupMenu().addPropertyChangeListener(listener);137138frame.setVisible(true);139}140141public static void main(String[] args) throws Exception {142robot = new Robot();143testFrame(false, "javax.swing.PopupFactory$HeavyWeightPopup");144145testFrame(true, "javax.swing.PopupFactory$LightWeightPopup");146}147}148149150