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