Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/6800513/bug6800513.java
38918 views
/*1* Copyright 2012 Red Hat, Inc. All Rights Reserved.2* Copyright (c) 2012, 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* @bug 680051327* @summary GTK-LaF renders menus incompletely28* @author Mario Torre29* @library ../../regtesthelpers/30* @build Util31* @run main bug680051332*/3334import javax.swing.*;35import java.awt.*;36import java.awt.event.InputEvent;37import java.beans.PropertyChangeEvent;38import java.beans.PropertyChangeListener;39import java.lang.reflect.Field;40import java.util.concurrent.Callable;4142public class bug6800513 {4344private static JPopupMenu popupMenu;45private static JMenu menu;46private static JFrame frame;47private static Robot robot;4849public static void testFrame(final boolean defaultLightWeightPopupEnabled,50String expectedPopupClass) throws Exception {5152SwingUtilities.invokeAndWait(new Runnable() {53public void run() {54JPopupMenu.setDefaultLightWeightPopupEnabled(defaultLightWeightPopupEnabled);55createAndShowUI();56}57});5859robot.waitForIdle();6061clickOnMenu();6263robot.waitForIdle();6465Field getPopup = JPopupMenu.class.getDeclaredField("popup");66getPopup.setAccessible(true);67Popup popup = (Popup) getPopup.get(popupMenu);6869if (popup == null) {70throw new Exception("popup is null!");71}7273String className = popup.getClass().getName();74if (!className.equals(expectedPopupClass)) {75throw new Exception("popup class is: " + className +76", expected: " + expectedPopupClass);77}7879SwingUtilities.invokeAndWait(new Runnable() {80@Override81public void run() {82frame.dispose();83popupMenu = null;84}85});8687robot.waitForIdle();88}899091public static void clickOnMenu() throws Exception {92Rectangle bounds = Util.invokeOnEDT(new Callable<Rectangle>() {93@Override94public Rectangle call() throws Exception {95return new Rectangle(menu.getLocationOnScreen(), menu.getSize());96}97});9899robot.setAutoDelay(100);100101robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);102103robot.mousePress(InputEvent.BUTTON1_MASK);104robot.mouseRelease(InputEvent.BUTTON1_MASK);105}106107private static class PopupListener implements PropertyChangeListener {108@Override109public void propertyChange(PropertyChangeEvent evt) {110if (evt.toString().contains("visible") && ((Boolean) evt.getNewValue() == true)) {111popupMenu = (JPopupMenu) evt.getSource();112}113}114}115116public static void createAndShowUI() {117frame = new JFrame();118119JMenuBar menuBar = new JMenuBar();120menu = new JMenu("Menu");121122menu.add(new JMenuItem("Menu Item #1"));123menu.add(new JMenuItem("Menu Item #2"));124menu.add(new JMenuItem("Menu Item #3"));125126menuBar.add(menu);127128frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);129frame.setJMenuBar(menuBar);130frame.setSize(500, 500);131132PopupListener listener = new PopupListener();133menu.getPopupMenu().addPropertyChangeListener(listener);134135frame.setVisible(true);136}137138public static void main(String[] args) throws Exception {139robot = new Robot();140testFrame(false, "javax.swing.PopupFactory$HeavyWeightPopup");141142testFrame(true, "javax.swing.PopupFactory$LightWeightPopup");143}144}145146147