blob: 5d465c9d79d5384f8896125244c318477987da20 [file] [log] [blame]
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 6607130
* @summary Checks that JComboBox cell editor is hidden if the same
* item is selected with keyboard.
* Also checks that JComboBox cell editor is hidden if F2 and then
* ENTER were pressed.
* @author Mikhail Lapshin
*/
import sun.awt.SunToolkit;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.KeyEvent;
public class bug6607130 {
private JFrame frame;
private JComboBox cb;
private Robot robot;
public static void main(String[] args) throws Exception {
final bug6607130 test = new bug6607130();
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
test.setupUI();
}
});
test.test();
} finally {
if (test.frame != null) {
test.frame.dispose();
}
}
}
public bug6607130() throws AWTException {
robot = new Robot();
}
private void setupUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultTableModel model = new DefaultTableModel(1, 1);
JTable table = new JTable(model);
cb = new JComboBox(new String[]{"one", "two", "three"});
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));
frame.add(table);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void test() throws Exception {
realSync();
test1();
realSync();
checkResult("First test");
test2();
realSync();
checkResult("Second test");
}
private void test1() throws Exception {
// Select 'one'
hitKey(KeyEvent.VK_TAB);
realSync();
hitKey(KeyEvent.VK_F2);
realSync();
hitKey(KeyEvent.VK_DOWN);
realSync();
hitKey(KeyEvent.VK_DOWN);
realSync();
hitKey(KeyEvent.VK_ENTER);
realSync();
// Select 'one' again
hitKey(KeyEvent.VK_F2);
realSync();
hitKey(KeyEvent.VK_DOWN);
realSync();
hitKey(KeyEvent.VK_ENTER);
realSync();
}
private void test2() throws Exception {
// Press F2 and then press ENTER
// Editor should be shown and then closed
hitKey(KeyEvent.VK_F2);
realSync();
hitKey(KeyEvent.VK_ENTER);
realSync();
}
private void checkResult(String testName) {
if (!cb.isShowing()) {
System.out.println(testName + " passed");
} else {
System.out.println(testName + " failed");
throw new RuntimeException("JComboBox is showing " +
"after item selection.");
}
}
private static void realSync() {
((SunToolkit) (Toolkit.getDefaultToolkit())).realSync();
}
public void hitKey(int keycode) {
robot.keyPress(keycode);
robot.keyRelease(keycode);
delay();
}
private void delay() {
try {
Thread.sleep(1000);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
}