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 
39 public class bug4458079 extends JFrame implements PopupMenuListener {
40     public JMenu menu;
41 
42     static volatile boolean itemASelected = false;
main(String[] args)43     public static void main(String[] args) throws Exception {
44         SwingUtilities.invokeAndWait(new Runnable() {
45             public void run() {
46                 new bug4458079().createAndShowGUI();
47             }
48         });
49         Robot robot = new Robot();
50         robot.waitForIdle();
51 
52         robot.setAutoDelay(50);
53 
54         Util.hitMnemonics(robot, KeyEvent.VK_M);
55 
56         robot.waitForIdle();
57         Thread.sleep(1000);
58 
59         Util.hitKeys(robot, KeyEvent.VK_DOWN);
60         Util.hitKeys(robot, KeyEvent.VK_ENTER);
61 
62         robot.waitForIdle();
63         Thread.sleep(1000);
64 
65         if (!itemASelected) {
66             throw new RuntimeException("Test failed: arrow key traversal in JMenu broken!");
67         }
68     }
createAndShowGUI()69     public void createAndShowGUI() {
70         JMenuBar bar = new JMenuBar();
71         menu = new JMenu("Menu");
72         menu.add(new JMenuItem("1"));
73         menu.add(new JMenuItem("2"));
74         menu.setMnemonic(KeyEvent.VK_M);
75         menu.getPopupMenu().addPopupMenuListener(this);
76         bar.add(menu);
77 
78         setJMenuBar(bar);
79         getContentPane().add(new JButton(""));
80         setSize(300, 300);
81         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82         pack();
83         setVisible(true);
84     }
85 
rebuildMenu()86     public void rebuildMenu() {
87         menu.removeAll();
88         final String itemCommand = "A";
89         JMenuItem item = new JMenuItem(itemCommand);
90         item.addActionListener(new ActionListener() {
91             public void actionPerformed(ActionEvent e) {
92                 JMenuItem item = ((JMenuItem)e.getSource());
93                 if (e.getActionCommand() == itemCommand) {
94                     itemASelected = true;
95                 }
96             }
97         });
98         menu.add(item);
99         menu.add(new JMenuItem("B"));
100     }
101 
popupMenuWillBecomeVisible(PopupMenuEvent e)102     public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
103         rebuildMenu();
104     }
105 
popupMenuWillBecomeInvisible(PopupMenuEvent e)106     public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
popupMenuCanceled(PopupMenuEvent e)107     public void popupMenuCanceled(PopupMenuEvent e) {}
108 }
109