bug6217905.java revision 11111:4ef86895869c
1/*
2 * Copyright (c) 2011, 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/*
24@test
25@bug 6217905
26@summary JPopupMenu keyboard navigation stops working
27@author Alexander Potochkin
28@library ../../../../lib/testlibrary
29@build ExtendedRobot
30@run main bug6217905
31*/
32
33import javax.swing.*;
34import java.awt.*;
35import java.awt.event.InputEvent;
36import java.awt.event.KeyEvent;
37
38public class bug6217905 {
39    private static JPanel popupPanel;
40    private static JMenuItem firstItem;
41    private static JMenuItem lastItem;
42
43    private static void createGui() {
44        final JFrame frame = new JFrame();
45        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
46
47        JPopupMenu popup = new JPopupMenu("Menu");
48        firstItem = new JMenuItem("MenuItem");
49        popup.add(firstItem);
50        popup.add(new JMenuItem("MenuItem"));
51        lastItem = new JMenuItem("MenuItem");
52        popup.add(lastItem);
53
54        popupPanel = new JPanel();
55        popupPanel.setComponentPopupMenu(popup);
56        frame.add(popupPanel);
57        frame.setSize(100, 100);
58        frame.setVisible(true);
59    }
60
61    public static void main(String[] args) throws Exception {
62        try {
63            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
64        } catch (Exception e) {
65            // This test is for WinLaf only
66            System.out.println("This test is for Windows LaF only.");
67            return;
68        }
69
70        ExtendedRobot robot = new ExtendedRobot();
71        robot.setAutoDelay(10);
72        SwingUtilities.invokeLater(new Runnable() {
73            public void run() {
74                bug6217905.createGui();
75            }
76        });
77        robot.waitForIdle();
78        Point loc = popupPanel.getLocationOnScreen();
79        int x = loc.x + popupPanel.getWidth()/2;
80        int y = loc.y + popupPanel.getHeight()/2;
81        robot.glide(0, 0, x, y);
82        robot.mousePress(InputEvent.BUTTON3_MASK);
83        robot.mouseRelease(InputEvent.BUTTON3_MASK);
84        robot.waitForIdle();
85        if (getSelectedPathLength() != 1) {
86            throw new RuntimeException("Only popup must be selected");
87        }
88        robot.glide(x, y, 0, 0);
89        robot.type(KeyEvent.VK_DOWN);
90        robot.waitForIdle();
91        if (getSelectedPathLength() != 2 || !firstItem.isArmed()) {
92            throw new RuntimeException("First item must be selected");
93        }
94        robot.type(KeyEvent.VK_ESCAPE);
95        robot.waitForIdle();
96        if (getSelectedPathLength() != 0) {
97            throw new RuntimeException("There must be no selected items");
98        }
99        robot.glide(0, 0, x, y);
100        robot.mousePress(InputEvent.BUTTON3_MASK);
101        robot.mouseRelease(InputEvent.BUTTON3_MASK);
102        robot.waitForIdle();
103        robot.glide(x, y, 0, 0);
104        robot.type(KeyEvent.VK_UP);
105        robot.waitForIdle();
106        if (getSelectedPathLength() != 2 || !lastItem.isArmed()) {
107            throw new RuntimeException("Last item must be selected");
108        }
109    }
110
111    private static int getSelectedPathLength() {
112        return MenuSelectionManager.defaultManager().getSelectedPath().length;
113    }
114}
115