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