1 /* 2 * Copyright (c) 2013, 2015, 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 4199622 28 @summary RFE: JComboBox shouldn't send ActionEvents for keyboard navigation 29 @author Vladislav Karnaukhov 30 @library ../../../../lib/testlibrary 31 @modules java.desktop/com.sun.java.swing.plaf.windows 32 @build jdk.testlibrary.OSInfo 33 @run main bug4199622 34 */ 35 36 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel; 37 import jdk.testlibrary.OSInfo; 38 39 import javax.swing.*; 40 import javax.swing.plaf.metal.MetalLookAndFeel; 41 import java.awt.*; 42 import java.awt.event.ActionEvent; 43 import java.awt.event.ActionListener; 44 import java.awt.event.KeyEvent; 45 import java.lang.reflect.InvocationTargetException; 46 47 public class bug4199622 extends JFrame implements ActionListener { 48 49 static final int nElems = 20; 50 static JComboBox<String> cb = null; 51 52 bug4199622(LookAndFeel laf) { 53 super(); 54 55 try { 56 UIManager.setLookAndFeel(laf); 57 } catch (UnsupportedLookAndFeelException e) { 58 throw new RuntimeException("Test failed", e); 59 } 60 61 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 62 cb = new JComboBox<>(); 63 for (int i = 0; i < nElems; i++) { 64 cb.addItem(String.valueOf(i + 1)); 65 } 66 cb.addActionListener(this); 67 add(cb); 68 69 setSize(300, 300); 70 pack(); 71 } 72 73 @Override 74 public void actionPerformed(ActionEvent e) { 75 if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && cb.isPopupVisible()) { 76 throw new RuntimeException("Test failed. actionPerformed generated"); 77 } 78 } 79 80 static Robot robot = null; 81 82 static void doTest() { 83 if (robot == null) { 84 try { 85 robot = new Robot(); 86 robot.setAutoDelay(20); 87 } catch (AWTException e) { 88 throw new RuntimeException("Can't create robot. Test failed", e); 89 } 90 } 91 92 robot.waitForIdle(); 93 94 doActualTest(); 95 96 try { 97 SwingUtilities.invokeAndWait(new Runnable() { 98 @Override 99 public void run() { 100 cb.hidePopup(); 101 cb.setEditable(true); 102 cb.updateUI(); 103 } 104 }); 105 } catch (InterruptedException e) { 106 throw new RuntimeException("Test failed", e); 107 } catch (InvocationTargetException e) { 108 throw new RuntimeException("Test failed", e); 109 } 110 111 robot.waitForIdle(); 112 doActualTest(); 113 } 114 115 static void doActualTest() { 116 UIManager.put("ComboBox.noActionOnKeyNavigation", true); 117 doTestUpDown(); 118 UIManager.put("ComboBox.noActionOnKeyNavigation", false); 119 doTestUpDown(); 120 121 UIManager.put("ComboBox.noActionOnKeyNavigation", true); 122 doTestPgUpDown(); 123 UIManager.put("ComboBox.noActionOnKeyNavigation", false); 124 doTestPgUpDown(); 125 126 UIManager.put("ComboBox.noActionOnKeyNavigation", true); 127 doTestHomeEnd(); 128 UIManager.put("ComboBox.noActionOnKeyNavigation", false); 129 doTestHomeEnd(); 130 } 131 132 static void doTestHomeEnd() { 133 try { 134 SwingUtilities.invokeAndWait(new Runnable() { 135 @Override 136 public void run() { 137 cb.hidePopup(); 138 cb.setSelectedIndex(0); 139 } 140 }); 141 } catch (InterruptedException e) { 142 throw new RuntimeException("Test failed", e); 143 } catch (InvocationTargetException e) { 144 throw new RuntimeException("Test failed", e); 145 } 146 robot.waitForIdle(); 147 148 robot.keyPress(KeyEvent.VK_END); 149 robot.keyRelease(KeyEvent.VK_END); 150 robot.waitForIdle(); 151 robot.keyPress(KeyEvent.VK_HOME); 152 robot.keyRelease(KeyEvent.VK_HOME); 153 robot.waitForIdle(); 154 } 155 156 static void doTestUpDown() { 157 try { 158 SwingUtilities.invokeAndWait(new Runnable() { 159 @Override 160 public void run() { 161 cb.hidePopup(); 162 cb.setSelectedIndex(0); 163 } 164 }); 165 } catch (InterruptedException e) { 166 throw new RuntimeException("Test failed", e); 167 } catch (InvocationTargetException e) { 168 throw new RuntimeException("Test failed", e); 169 } 170 robot.waitForIdle(); 171 172 for (int i = 0; i < nElems; i++) { 173 robot.keyPress(KeyEvent.VK_DOWN); 174 robot.keyRelease(KeyEvent.VK_DOWN); 175 robot.waitForIdle(); 176 } 177 178 for (int i = 0; i < nElems; i++) { 179 robot.keyPress(KeyEvent.VK_UP); 180 robot.keyRelease(KeyEvent.VK_UP); 181 robot.waitForIdle(); 182 } 183 } 184 185 static void doTestPgUpDown() { 186 try { 187 SwingUtilities.invokeAndWait(new Runnable() { 188 @Override 189 public void run() { 190 cb.hidePopup(); 191 cb.setSelectedIndex(0); 192 } 193 }); 194 } catch (InterruptedException e) { 195 throw new RuntimeException("Test failed", e); 196 } catch (InvocationTargetException e) { 197 throw new RuntimeException("Test failed", e); 198 } 199 robot.waitForIdle(); 200 201 int listHeight = cb.getMaximumRowCount(); 202 for (int i = 0; i < nElems; i += listHeight) { 203 robot.keyPress(KeyEvent.VK_PAGE_DOWN); 204 robot.keyRelease(KeyEvent.VK_PAGE_DOWN); 205 robot.waitForIdle(); 206 } 207 208 for (int i = 0; i < nElems; i += listHeight) { 209 robot.keyPress(KeyEvent.VK_PAGE_UP); 210 robot.keyRelease(KeyEvent.VK_PAGE_UP); 211 robot.waitForIdle(); 212 } 213 } 214 215 public static void main(String[] args) { 216 try { 217 SwingUtilities.invokeAndWait(new Runnable() { 218 @Override 219 public void run() { 220 bug4199622 test = new bug4199622(new MetalLookAndFeel()); 221 test.setVisible(true); 222 } 223 }); 224 } catch (InterruptedException e) { 225 throw new RuntimeException("Test failed", e); 226 } catch (InvocationTargetException e) { 227 throw new RuntimeException("Test failed", e); 228 } 229 doTest(); 230 231 if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) { 232 try { 233 SwingUtilities.invokeAndWait(new Runnable() { 234 @Override 235 public void run() { 236 bug4199622 test = new bug4199622(new WindowsLookAndFeel()); 237 test.setVisible(true); 238 } 239 }); 240 } catch (InterruptedException e) { 241 throw new RuntimeException("Test failed", e); 242 } catch (InvocationTargetException e) { 243 throw new RuntimeException("Test failed", e); 244 } 245 doTest(); 246 } 247 } 248 }