Path: blob/master/test/jdk/javax/swing/JMenu/6359669/bug6359669.java
41153 views
/*1* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @key headful25* @bug 635966926* @summary REGRESSION: Submenu does not work if populated in PopupMenuListener.popupMenuWillBecomeVisible27* @author Alexander Potochkin28* @library /lib/client29* @build ExtendedRobot30* @run main bug635966931*/3233import javax.swing.*;34import javax.swing.event.PopupMenuListener;35import javax.swing.event.PopupMenuEvent;36import java.awt.*;37import java.awt.event.InputEvent;3839public class bug6359669 {40static JMenu menu;41static JFrame f;4243public static void main(String[] args) throws Exception {44try {45ExtendedRobot robot = new ExtendedRobot();46robot.setAutoDelay(10);4748SwingUtilities.invokeLater(new Runnable() {49public void run() {50f = new JFrame();51JMenuBar menuBar = new JMenuBar();52menu = new JMenu("Test");53menu.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {54public void popupMenuCanceled(PopupMenuEvent e) {55}5657public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {58}5960public void popupMenuWillBecomeVisible(PopupMenuEvent e) {61menu.add(new JMenuItem("An item"));62}63});6465menuBar.add(menu);66f.setJMenuBar(menuBar);6768f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);69f.setSize(200, 200);70f.setVisible(true);71}72});73robot.waitForIdle();74Point p = menu.getLocationOnScreen();75Dimension size = menu.getSize();76p.x += size.width / 2;77p.y += size.height / 2;78robot.mouseMove(p.x, p.y);79robot.mousePress(InputEvent.BUTTON1_MASK);80robot.mouseRelease(InputEvent.BUTTON1_MASK);81robot.waitForIdle();82if (menu.getPopupMenu().getComponentCount() == 0) {83throw new RuntimeException("Where is a menuItem ?");84}85} finally {86if (f != null) SwingUtilities.invokeAndWait(() -> f.dispose());87}88}89}909192