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 /*
  25    @test
  26    @key headful
  27    @bug 4199622
  28    @summary RFE: JComboBox shouldn't send ActionEvents for keyboard navigation
  29    @author Vladislav Karnaukhov
  30    @run main bug4199622
  31  */
  32 
  33 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
  34 import sun.awt.OSInfo;
  35 import sun.awt.SunToolkit;
  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     static SunToolkit toolkit = null;
  80 
  81     static void doTest() {
  82         if (robot == null) {
  83             try {
  84                 robot = new Robot();
  85                 robot.setAutoDelay(20);
  86             } catch (AWTException e) {
  87                 throw new RuntimeException("Can't create robot. Test failed", e);
  88             }
  89         }
  90 
  91         toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  92         if (toolkit == null) {
  93             throw new RuntimeException("Can't get the toolkit. Test failed");
  94         }
  95         toolkit.realSync();
  96 
  97         doActualTest();
  98 
  99         try {
 100             SwingUtilities.invokeAndWait(new Runnable() {
 101                 @Override
 102                 public void run() {
 103                     cb.hidePopup();
 104                     cb.setEditable(true);
 105                     cb.updateUI();
 106                 }
 107             });
 108         } catch (InterruptedException e) {
 109             throw new RuntimeException("Test failed", e);
 110         } catch (InvocationTargetException e) {
 111             throw new RuntimeException("Test failed", e);
 112         }
 113 
 114         toolkit.realSync();
 115         doActualTest();
 116     }
 117 
 118     static void doActualTest() {
 119         UIManager.put("ComboBox.noActionOnKeyNavigation", true);
 120         doTestUpDown();
 121         UIManager.put("ComboBox.noActionOnKeyNavigation", false);
 122         doTestUpDown();
 123 
 124         UIManager.put("ComboBox.noActionOnKeyNavigation", true);
 125         doTestPgUpDown();
 126         UIManager.put("ComboBox.noActionOnKeyNavigation", false);
 127         doTestPgUpDown();
 128 
 129         UIManager.put("ComboBox.noActionOnKeyNavigation", true);
 130         doTestHomeEnd();
 131         UIManager.put("ComboBox.noActionOnKeyNavigation", false);
 132         doTestHomeEnd();
 133     }
 134 
 135     static void doTestHomeEnd() {
 136         try {
 137             SwingUtilities.invokeAndWait(new Runnable() {
 138                 @Override
 139                 public void run() {
 140                     cb.hidePopup();
 141                     cb.setSelectedIndex(0);
 142                 }
 143             });
 144         } catch (InterruptedException e) {
 145             throw new RuntimeException("Test failed", e);
 146         } catch (InvocationTargetException e) {
 147             throw new RuntimeException("Test failed", e);
 148         }
 149         toolkit.realSync();
 150 
 151         robot.keyPress(KeyEvent.VK_END);
 152         toolkit.realSync();
 153         robot.keyPress(KeyEvent.VK_HOME);
 154         toolkit.realSync();
 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         toolkit.realSync();
 172 
 173         for (int i = 0; i < nElems; i++) {
 174             robot.keyPress(KeyEvent.VK_DOWN);
 175             toolkit.realSync();
 176         }
 177 
 178         for (int i = 0; i < nElems; i++) {
 179             robot.keyPress(KeyEvent.VK_UP);
 180             toolkit.realSync();
 181         }
 182     }
 183 
 184     static void doTestPgUpDown() {
 185         try {
 186             SwingUtilities.invokeAndWait(new Runnable() {
 187                 @Override
 188                 public void run() {
 189                     cb.hidePopup();
 190                     cb.setSelectedIndex(0);
 191                 }
 192             });
 193         } catch (InterruptedException e) {
 194             throw new RuntimeException("Test failed", e);
 195         } catch (InvocationTargetException e) {
 196             throw new RuntimeException("Test failed", e);
 197         }
 198         toolkit.realSync();
 199 
 200         int listHeight = cb.getMaximumRowCount();
 201         for (int i = 0; i < nElems; i += listHeight) {
 202             robot.keyPress(KeyEvent.VK_PAGE_DOWN);
 203             toolkit.realSync();
 204         }
 205 
 206         for (int i = 0; i < nElems; i += listHeight) {
 207             robot.keyPress(KeyEvent.VK_PAGE_UP);
 208             toolkit.realSync();
 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 }