1 /*
   2  * Copyright (c) 2006, 2014, 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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /* @test
  24    @bug 6359669
  25    @summary REGRESSION: Submenu does not work if populated in PopupMenuListener.popupMenuWillBecomeVisible
  26    @author Alexander Potochkin
  27    @library ../../../../lib/testlibrary
  28    @build ExtendedRobot
  29    @run main bug6359669
  30 */
  31 
  32 import javax.swing.*;
  33 import javax.swing.event.PopupMenuListener;
  34 import javax.swing.event.PopupMenuEvent;
  35 import java.awt.*;
  36 import java.awt.event.InputEvent;
  37 
  38 public class bug6359669 {
  39     static JMenu menu;
  40 
  41     public static void main(String[] args) throws Exception {
  42 
  43         ExtendedRobot robot = new ExtendedRobot();
  44         robot.setAutoDelay(10);
  45 
  46         SwingUtilities.invokeLater(new Runnable() {
  47             public void run() {
  48                 JFrame f = new JFrame();
  49                 JMenuBar menuBar = new JMenuBar();
  50                 menu = new JMenu("Test");
  51                 menu.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {
  52                     public void popupMenuCanceled(PopupMenuEvent e) {
  53                     }
  54 
  55                     public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
  56                     }
  57 
  58                     public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
  59                         menu.add(new JMenuItem("An item"));
  60                     }
  61                 });
  62 
  63                 menuBar.add(menu);
  64                 f.setJMenuBar(menuBar);
  65 
  66                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67                 f.setSize(200, 200);
  68                 f.setVisible(true);
  69             }
  70         });
  71         robot.waitForIdle();
  72         Point p = menu.getLocationOnScreen();
  73         Dimension size = menu.getSize();
  74         p.x += size.width / 2;
  75         p.y += size.height / 2;
  76         robot.mouseMove(p.x, p.y);
  77         robot.mousePress(InputEvent.BUTTON1_MASK);
  78         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  79         robot.waitForIdle();
  80         if (menu.getPopupMenu().getComponentCount() == 0) {
  81             throw new RuntimeException("Where is a menuItem ?");
  82         }
  83     }
  84 }