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