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