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/*
25 * @test
26 * @key headful
27 * @bug 6559152
28 * @summary Checks that you can select an item in JComboBox with keyboard
29 *          when it is a JTable cell editor.
30 * @author Mikhail Lapshin
31 * @library ../../../../lib/testlibrary
32 * @build ExtendedRobot
33 * @run main bug6559152
34 */
35
36import javax.swing.*;
37import javax.swing.table.DefaultTableModel;
38import java.awt.*;
39import java.awt.event.KeyEvent;
40
41public class bug6559152 {
42    private JFrame frame;
43    private JComboBox cb;
44    private ExtendedRobot robot;
45
46    public static void main(String[] args) throws Exception {
47        final bug6559152 test = new bug6559152();
48        try {
49            SwingUtilities.invokeAndWait(new Runnable() {
50                public void run() {
51                    test.setupUI();
52                }
53            });
54            test.test();
55        } finally {
56            if (test.frame != null) {
57                test.frame.dispose();
58            }
59        }
60    }
61
62    private void setupUI() {
63        frame = new JFrame();
64        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65
66        DefaultTableModel model = new DefaultTableModel(1, 1);
67        JTable table = new JTable(model);
68
69        cb = new JComboBox(new String[]{"one", "two", "three"});
70        cb.setEditable(true);
71        table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));
72        frame.add(cb);
73
74        frame.pack();
75        frame.setLocationRelativeTo(null);
76        frame.setVisible(true);
77    }
78
79    private void test() throws Exception {
80        robot = new ExtendedRobot();
81        robot.waitForIdle();
82        testImpl();
83        robot.waitForIdle();
84        checkResult();
85    }
86
87    private void testImpl() throws Exception {
88        robot.type(KeyEvent.VK_DOWN);
89        robot.waitForIdle();
90        robot.type(KeyEvent.VK_DOWN);
91        robot.waitForIdle();
92        robot.type(KeyEvent.VK_ENTER);
93    }
94
95    private void checkResult() {
96        if (cb.getSelectedItem().equals("two")) {
97            System.out.println("Test passed");
98        } else {
99            System.out.println("Test failed");
100            throw new RuntimeException("Cannot select an item " +
101                    "from popup with the ENTER key.");
102        }
103    }
104}
105