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