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