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    @run main bug4199622
  29 */
  30 
  31 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
  32 import sun.awt.OSInfo;
  33 import sun.awt.SunToolkit;
  34 
  35 import javax.swing.*;
  36 import javax.swing.plaf.metal.MetalLookAndFeel;
  37 import java.awt.*;
  38 import java.awt.event.ActionEvent;
  39 import java.awt.event.ActionListener;
  40 import java.awt.event.KeyEvent;
  41 import java.lang.reflect.InvocationTargetException;
  42 
  43 public class bug4199622 extends JFrame implements ActionListener {
  44 
  45     static final int nElems = 20;
  46     static JComboBox<String> cb = null;
  47 
  48     bug4199622(LookAndFeel laf) {
  49         super();
  50 
  51         try {
  52             UIManager.setLookAndFeel(laf);
  53         } catch (UnsupportedLookAndFeelException e) {
  54             throw new RuntimeException("Test failed", e);
  55         }
  56 
  57         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  58         cb = new JComboBox<>();
  59         for (int i = 0; i < nElems; i++) {
  60             cb.addItem(String.valueOf(i + 1));
  61         }
  62         cb.addActionListener(this);
  63         add(cb);
  64 
  65         setSize(300, 300);
  66         pack();
  67     }
  68 
  69     @Override
  70     public void actionPerformed(ActionEvent e) {
  71         if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && cb.isPopupVisible()) {
  72             throw new RuntimeException("Test failed. actionPerformed generated");
  73         }
  74     }
  75 
  76     static Robot robot = null;
  77     static SunToolkit toolkit = 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         toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  90         if (toolkit == null) {
  91             throw new RuntimeException("Can't get the toolkit. Test failed");
  92         }
  93         toolkit.realSync();
  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         toolkit.realSync();
 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         toolkit.realSync();
 148 
 149         robot.keyPress(KeyEvent.VK_END);
 150         toolkit.realSync();
 151         robot.keyPress(KeyEvent.VK_HOME);
 152         toolkit.realSync();
 153     }
 154 
 155     static void doTestUpDown() {
 156         try {
 157             SwingUtilities.invokeAndWait(new Runnable() {
 158                 @Override
 159                 public void run() {
 160                     cb.hidePopup();
 161                     cb.setSelectedIndex(0);
 162                 }
 163             });
 164         } catch (InterruptedException e) {
 165             throw new RuntimeException("Test failed", e);
 166         } catch (InvocationTargetException e) {
 167             throw new RuntimeException("Test failed", e);
 168         }
 169         toolkit.realSync();
 170 
 171         for (int i = 0; i < nElems; i++) {
 172             robot.keyPress(KeyEvent.VK_DOWN);
 173             toolkit.realSync();
 174         }
 175 
 176         for (int i = 0; i < nElems; i++) {
 177             robot.keyPress(KeyEvent.VK_UP);
 178             toolkit.realSync();
 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         toolkit.realSync();
 197 
 198         int listHeight = cb.getMaximumRowCount();
 199         for (int i = 0; i < nElems; i += listHeight) {
 200             robot.keyPress(KeyEvent.VK_PAGE_DOWN);
 201             toolkit.realSync();
 202         }
 203 
 204         for (int i = 0; i < nElems; i += listHeight) {
 205             robot.keyPress(KeyEvent.VK_PAGE_UP);
 206             toolkit.realSync();
 207         }
 208     }
 209 
 210     public static void main(String[] args) {
 211         try {
 212             SwingUtilities.invokeAndWait(new Runnable() {
 213                 @Override
 214                 public void run() {
 215                     bug4199622 test = new bug4199622(new MetalLookAndFeel());
 216                     test.setVisible(true);
 217                 }
 218             });
 219         } catch (InterruptedException e) {
 220             throw new RuntimeException("Test failed", e);
 221         } catch (InvocationTargetException e) {
 222             throw new RuntimeException("Test failed", e);
 223         }
 224         doTest();
 225 
 226         if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {
 227             try {
 228                 SwingUtilities.invokeAndWait(new Runnable() {
 229                     @Override
 230                     public void run() {
 231                         bug4199622 test = new bug4199622(new WindowsLookAndFeel());
 232                         test.setVisible(true);
 233                     }
 234                 });
 235             } catch (InterruptedException e) {
 236                 throw new RuntimeException("Test failed", e);
 237             } catch (InvocationTargetException e) {
 238                 throw new RuntimeException("Test failed", e);
 239             }
 240             doTest();
 241         }
 242     }
 243 }