1 /*
   2  * Copyright (c) 2008, 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 6607130
  26  * @summary Checks that JComboBox cell editor is hidden if the same
  27  *          item is selected with keyboard.
  28  *          Also checks that JComboBox cell editor is hidden if F2 and then
  29  *          ENTER were pressed.
  30  * @author Mikhail Lapshin
  31  */
  32 
  33 import javax.swing.*;
  34 import javax.swing.table.DefaultTableModel;
  35 import java.awt.*;
  36 import java.awt.event.KeyEvent;
  37 
  38 public class bug6607130 {
  39     private JFrame frame;
  40     private JComboBox cb;
  41     private Robot robot;
  42 
  43     public static void main(String[] args) throws Exception {
  44         final bug6607130 test = new bug6607130();
  45         try {
  46             SwingUtilities.invokeAndWait(new Runnable() {
  47                 public void run() {
  48                     test.setupUI();
  49                 }
  50             });
  51             test.test();
  52         } finally {
  53             if (test.frame != null) {
  54                 test.frame.dispose();
  55             }
  56         }
  57     }
  58 
  59     public bug6607130() throws AWTException {
  60         robot = new Robot();
  61     }
  62 
  63     private void setupUI() {
  64         frame = new JFrame();
  65         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66 
  67         DefaultTableModel model = new DefaultTableModel(1, 1);
  68         JTable table = new JTable(model);
  69 
  70         cb = new JComboBox(new String[]{"one", "two", "three"});
  71         table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));
  72         frame.add(table);
  73 
  74         frame.pack();
  75         frame.setLocationRelativeTo(null);
  76         frame.setVisible(true);
  77     }
  78 
  79     private void test() throws Exception {
  80         robot.waitForIdle();
  81         test1();
  82         robot.waitForIdle();
  83         checkResult("First test");
  84         test2();
  85         robot.waitForIdle();
  86         checkResult("Second test");
  87     }
  88 
  89     private void test1() throws Exception {
  90         // Select 'one'
  91         hitKey(KeyEvent.VK_TAB);
  92         robot.waitForIdle();
  93         hitKey(KeyEvent.VK_F2);
  94         robot.waitForIdle();
  95         hitKey(KeyEvent.VK_DOWN);
  96         robot.waitForIdle();
  97         hitKey(KeyEvent.VK_DOWN);
  98         robot.waitForIdle();
  99         hitKey(KeyEvent.VK_ENTER);
 100         robot.waitForIdle();
 101 
 102         // Select 'one' again
 103         hitKey(KeyEvent.VK_F2);
 104         robot.waitForIdle();
 105         hitKey(KeyEvent.VK_DOWN);
 106         robot.waitForIdle();
 107         hitKey(KeyEvent.VK_ENTER);
 108         robot.waitForIdle();
 109     }
 110 
 111     private void test2() throws Exception {
 112         // Press F2 and then press ENTER
 113         // Editor should be shown and then closed
 114         hitKey(KeyEvent.VK_F2);
 115         robot.waitForIdle();
 116         hitKey(KeyEvent.VK_ENTER);
 117         robot.waitForIdle();
 118     }
 119 
 120     private void checkResult(String testName) {
 121         if (!cb.isShowing()) {
 122             System.out.println(testName + " passed");
 123         } else {
 124             System.out.println(testName + " failed");
 125             throw new RuntimeException("JComboBox is showing " +
 126                     "after item selection.");
 127         }
 128     }
 129 
 130     public void hitKey(int keycode) {
 131         robot.keyPress(keycode);
 132         robot.keyRelease(keycode);
 133         delay();
 134     }
 135 
 136     private void delay() {
 137         try {
 138             Thread.sleep(1000);
 139         } catch(InterruptedException ie) {
 140             ie.printStackTrace();
 141         }
 142     }
 143 }