bug6544309.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/* @test
24   @bug 6544309
25   @summary Checks that 'Select Input Method' popup menu allows to select
26            items with keyboard.
27   @author Mikhail Lapshin
28   @library ../../../../lib/testlibrary
29   @build ExtendedRobot
30   @run main bug6544309
31*/
32
33import javax.swing.*;
34import java.awt.event.*;
35import java.awt.*;
36
37public class bug6544309 {
38    private JDialog dialog;
39    private boolean passed;
40    private static ExtendedRobot robot;
41
42    public static void main(String[] args) throws Exception {
43        robot = new ExtendedRobot();
44        final bug6544309 test = new bug6544309();
45        try {
46            SwingUtilities.invokeAndWait(new Runnable() {
47                public void run() {
48                    test.setupUI();
49                }
50            });
51            test.test();
52            System.out.println("Test passed");
53        } finally {
54            if (test.dialog != null) {
55                test.dialog.dispose();
56            }
57        }
58    }
59
60    private void setupUI() {
61        dialog = new JDialog();
62        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
63        dialog.setSize(200, 100);
64        dialog.setLocationRelativeTo(null);
65        dialog.setVisible(true);
66
67        JPopupMenu popup = new JPopupMenu();
68        popup.add(new JMenuItem("one"));
69        JMenuItem two = new JMenuItem("two");
70        two.addActionListener(new ActionListener() {
71            public void actionPerformed(ActionEvent e) {
72                passed = true;
73            }
74        });
75        popup.add(two);
76        popup.add(new JMenuItem("three"));
77        popup.show(dialog, 50, 50);
78    }
79
80    private void test() throws Exception {
81        testImpl();
82        checkResult();
83    }
84
85
86    private void testImpl() throws Exception {
87        robot.waitForIdle();
88        System.out.println("Pressing DOWN ARROW");
89        robot.type(KeyEvent.VK_DOWN);
90        robot.waitForIdle();
91        System.out.println("Pressing DOWN ARROW");
92        robot.type(KeyEvent.VK_DOWN);
93        robot.waitForIdle();
94        System.out.println("Pressing SPACE");
95        robot.type(KeyEvent.VK_SPACE);
96    }
97
98    private void checkResult() {
99        robot.waitForIdle();
100        if (!passed) {
101            throw new RuntimeException("If a JDialog is invoker for JPopupMenu, " +
102                    "the menu cannot be handled by keyboard.");
103        }
104    }
105}
106