1 /*
   2  * Copyright (c) 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 /* @test
  25  * @bug 8032878
  26  * @summary Checks that JComboBox as JTable cell editor processes key events
  27  *          even where setSurrendersFocusOnKeystroke flag in JTable is false and
  28  *          that it does not lose the first key press where the flag is true.
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @author Alexey Ivanov
  32  */
  33 
  34 import java.awt.*;
  35 import java.awt.event.KeyEvent;
  36 import javax.swing.*;
  37 import javax.swing.text.JTextComponent;
  38 
  39 import sun.awt.SunToolkit;
  40 
  41 public class bug8032878 implements Runnable {
  42     private static final String ONE = "one";
  43     private static final String TWO = "two";
  44     private static final String THREE = "three";
  45 
  46     private static final String EXPECTED = "one123";
  47 
  48     private final Robot robot;
  49 
  50     private JFrame frame;
  51     private JComboBox cb;
  52 
  53     private volatile boolean surrender;
  54     private volatile String text;
  55 
  56     public static void main(String[] args) throws Exception {
  57         final bug8032878 test = new bug8032878();
  58 
  59         test.test(false);
  60         test.test(true);
  61     }
  62 
  63     public bug8032878() throws AWTException {
  64         robot = new Robot();
  65         robot.setAutoDelay(100);
  66     }
  67 
  68     private void setupUI() {
  69         frame = new JFrame();
  70         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  71 
  72         JTable table = new JTable(new String[][] {{ONE}, {TWO}, {THREE}},
  73                                   new String[] { "#"});
  74         table.setSurrendersFocusOnKeystroke(surrender);
  75 
  76         cb = new JComboBox(new String[]{ONE, TWO, THREE});
  77         cb.setEditable(true);
  78         DefaultCellEditor comboEditor = new DefaultCellEditor(cb);
  79         comboEditor.setClickCountToStart(1);
  80         table.getColumnModel().getColumn(0).setCellEditor(comboEditor);
  81         frame.add(table);
  82 
  83         frame.pack();
  84         frame.setVisible(true);
  85     }
  86 
  87     private void test(final boolean flag) throws Exception {
  88         try {
  89             surrender = flag;
  90             SwingUtilities.invokeAndWait(this);
  91 
  92             runTest();
  93             checkResult();
  94         } finally {
  95             if (frame != null) {
  96                 frame.dispose();
  97             }
  98         }
  99     }
 100 
 101     private void runTest() throws Exception {
 102         realSync();
 103         // Select 'one'
 104         Util.hitKeys(robot, KeyEvent.VK_TAB);
 105         realSync();
 106         Util.hitKeys(robot, KeyEvent.VK_1);
 107         Util.hitKeys(robot, KeyEvent.VK_2);
 108         Util.hitKeys(robot, KeyEvent.VK_3);
 109         Util.hitKeys(robot, KeyEvent.VK_ENTER);
 110         realSync();
 111     }
 112 
 113     private void checkResult() throws Exception {
 114         SwingUtilities.invokeAndWait(new Runnable() {
 115             public void run() {
 116                 text = ((JTextComponent) cb.getEditor().getEditorComponent()).getText();
 117             }
 118         });
 119         if (text.equals(EXPECTED)) {
 120             System.out.println("Test with surrender = " + surrender + " passed");
 121         } else {
 122             System.out.println("Test with surrender = " + surrender + " failed");
 123             throw new RuntimeException("Expected value in JComboBox editor '" +
 124                     EXPECTED + "' but found '" + text + "'.");
 125         }
 126     }
 127 
 128     private static void realSync() {
 129         ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync();
 130     }
 131 
 132     @Override
 133     public void run() {
 134         setupUI();
 135     }
 136 }