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