1 /*
   2  * Copyright (c) 2013, 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 4458079
  25    @library ../../regtesthelpers
  26    @build Util
  27    @summary Tests calling removeAll() from PopupMenuListener
  28    @author Peter Zhelezniakov
  29    @run main bug4458079
  30 */
  31 import java.awt.Robot;
  32 import java.awt.Toolkit;
  33 import java.awt.event.*;
  34 import javax.swing.*;
  35 import javax.swing.event.*;
  36 import java.awt.event.KeyEvent;
  37 import java.util.ArrayList;
  38 import sun.awt.SunToolkit;
  39 
  40 public class bug4458079 extends JFrame implements PopupMenuListener {
  41     public JMenu menu;
  42 
  43     static volatile boolean itemASelected = false;
  44     public static void main(String[] args) throws Exception {
  45         SwingUtilities.invokeAndWait(new Runnable() {
  46             public void run() {
  47                 new bug4458079().createAndShowGUI();
  48             }
  49         });
  50         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  51         toolkit.realSync();
  52 
  53         Robot robot = new Robot();
  54         robot.setAutoDelay(50);
  55 
  56         Util.hitMnemonics(robot, KeyEvent.VK_M);
  57 
  58         toolkit.realSync();
  59         Thread.sleep(1000);
  60 
  61         Util.hitKeys(robot, KeyEvent.VK_DOWN);
  62         Util.hitKeys(robot, KeyEvent.VK_ENTER);
  63 
  64         toolkit.realSync();
  65         Thread.sleep(1000);
  66 
  67         if (!itemASelected) {
  68             throw new RuntimeException("Test failed: arrow key traversal in JMenu broken!");
  69         }
  70     }
  71     public void createAndShowGUI() {
  72         JMenuBar bar = new JMenuBar();
  73         menu = new JMenu("Menu");
  74         menu.add(new JMenuItem("1"));
  75         menu.add(new JMenuItem("2"));
  76         menu.setMnemonic(KeyEvent.VK_M);
  77         menu.getPopupMenu().addPopupMenuListener(this);
  78         bar.add(menu);
  79 
  80         setJMenuBar(bar);
  81         getContentPane().add(new JButton(""));
  82         setSize(300, 300);
  83         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  84         pack();
  85         setVisible(true);
  86     }
  87 
  88     public void rebuildMenu() {
  89         menu.removeAll();
  90         final String itemCommand = "A";
  91         JMenuItem item = new JMenuItem(itemCommand);
  92         item.addActionListener(new ActionListener() {
  93             public void actionPerformed(ActionEvent e) {
  94                 JMenuItem item = ((JMenuItem)e.getSource());
  95                 if (e.getActionCommand() == itemCommand) {
  96                     itemASelected = true;
  97                 }
  98             }
  99         });
 100         menu.add(item);
 101         menu.add(new JMenuItem("B"));
 102     }
 103 
 104     public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
 105         rebuildMenu();
 106     }
 107 
 108     public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
 109     public void popupMenuCanceled(PopupMenuEvent e) {}
 110 }