5822N/A/*
5822N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
5822N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5822N/A *
5822N/A * This code is free software; you can redistribute it and/or modify it
5822N/A * under the terms of the GNU General Public License version 2 only, as
5822N/A * published by the Free Software Foundation.
5822N/A *
5822N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5822N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5822N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5822N/A * version 2 for more details (a copy is included in the LICENSE file that
5822N/A * accompanied this code).
5822N/A *
5822N/A * You should have received a copy of the GNU General Public License version
5822N/A * 2 along with this work; if not, write to the Free Software Foundation,
5822N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5822N/A *
5822N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5822N/A * or visit www.oracle.com if you need additional information or have any
5822N/A * questions.
5822N/A */
5822N/A
5822N/A/* @test
5822N/A @bug 4199622
5822N/A @summary RFE: JComboBox shouldn't send ActionEvents for keyboard navigation
5822N/A @author Vladislav Karnaukhov
5822N/A @run main bug4199622
5822N/A*/
5822N/A
5822N/Aimport com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
5822N/Aimport sun.awt.OSInfo;
5822N/Aimport sun.awt.SunToolkit;
5822N/A
5822N/Aimport javax.swing.*;
5822N/Aimport javax.swing.plaf.metal.MetalLookAndFeel;
5822N/Aimport java.awt.*;
5822N/Aimport java.awt.event.ActionEvent;
5822N/Aimport java.awt.event.ActionListener;
5822N/Aimport java.awt.event.KeyEvent;
5822N/Aimport java.lang.reflect.InvocationTargetException;
5822N/A
5822N/Apublic class bug4199622 extends JFrame implements ActionListener {
5822N/A
5822N/A static final int nElems = 20;
5822N/A static JComboBox<String> cb = null;
5822N/A
5822N/A bug4199622(LookAndFeel laf) {
5822N/A super();
5822N/A
5822N/A try {
5822N/A UIManager.setLookAndFeel(laf);
5822N/A } catch (UnsupportedLookAndFeelException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A }
5822N/A
5822N/A setDefaultCloseOperation(DISPOSE_ON_CLOSE);
5822N/A cb = new JComboBox<>();
5822N/A for (int i = 0; i < nElems; i++) {
5822N/A cb.addItem(String.valueOf(i + 1));
5822N/A }
5822N/A cb.addActionListener(this);
5822N/A add(cb);
5822N/A
5822N/A setSize(300, 300);
5822N/A pack();
5822N/A }
5822N/A
5822N/A @Override
5822N/A public void actionPerformed(ActionEvent e) {
5822N/A if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && cb.isPopupVisible()) {
5822N/A throw new RuntimeException("Test failed. actionPerformed generated");
5822N/A }
5822N/A }
5822N/A
5822N/A static Robot robot = null;
5822N/A static SunToolkit toolkit = null;
5822N/A
5822N/A static void doTest() {
5822N/A if (robot == null) {
5822N/A try {
5822N/A robot = new Robot();
5822N/A robot.setAutoDelay(20);
5822N/A } catch (AWTException e) {
5822N/A throw new RuntimeException("Can't create robot. Test failed", e);
5822N/A }
5822N/A }
5822N/A
5822N/A toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
5822N/A if (toolkit == null) {
5822N/A throw new RuntimeException("Can't get the toolkit. Test failed");
5822N/A }
5822N/A toolkit.realSync();
5822N/A
5822N/A doActualTest();
5822N/A
5822N/A try {
5822N/A SwingUtilities.invokeAndWait(new Runnable() {
5822N/A @Override
5822N/A public void run() {
5822N/A cb.hidePopup();
5822N/A cb.setEditable(true);
5822N/A cb.updateUI();
5822N/A }
5822N/A });
5822N/A } catch (InterruptedException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A } catch (InvocationTargetException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A }
5822N/A
5822N/A toolkit.realSync();
5822N/A doActualTest();
5822N/A }
5822N/A
5822N/A static void doActualTest() {
5822N/A UIManager.put("ComboBox.noActionOnKeyNavigation", true);
5822N/A doTestUpDown();
5822N/A UIManager.put("ComboBox.noActionOnKeyNavigation", false);
5822N/A doTestUpDown();
5822N/A
5822N/A UIManager.put("ComboBox.noActionOnKeyNavigation", true);
5822N/A doTestPgUpDown();
5822N/A UIManager.put("ComboBox.noActionOnKeyNavigation", false);
5822N/A doTestPgUpDown();
5822N/A
5822N/A UIManager.put("ComboBox.noActionOnKeyNavigation", true);
5822N/A doTestHomeEnd();
5822N/A UIManager.put("ComboBox.noActionOnKeyNavigation", false);
5822N/A doTestHomeEnd();
5822N/A }
5822N/A
5822N/A static void doTestHomeEnd() {
5822N/A try {
5822N/A SwingUtilities.invokeAndWait(new Runnable() {
5822N/A @Override
5822N/A public void run() {
5822N/A cb.hidePopup();
5822N/A cb.setSelectedIndex(0);
5822N/A }
5822N/A });
5822N/A } catch (InterruptedException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A } catch (InvocationTargetException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A }
5822N/A toolkit.realSync();
5822N/A
5822N/A robot.keyPress(KeyEvent.VK_END);
5822N/A toolkit.realSync();
5822N/A robot.keyPress(KeyEvent.VK_HOME);
5822N/A toolkit.realSync();
5822N/A }
5822N/A
5822N/A static void doTestUpDown() {
5822N/A try {
5822N/A SwingUtilities.invokeAndWait(new Runnable() {
5822N/A @Override
5822N/A public void run() {
5822N/A cb.hidePopup();
5822N/A cb.setSelectedIndex(0);
5822N/A }
5822N/A });
5822N/A } catch (InterruptedException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A } catch (InvocationTargetException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A }
5822N/A toolkit.realSync();
5822N/A
5822N/A for (int i = 0; i < nElems; i++) {
5822N/A robot.keyPress(KeyEvent.VK_DOWN);
5822N/A toolkit.realSync();
5822N/A }
5822N/A
5822N/A for (int i = 0; i < nElems; i++) {
5822N/A robot.keyPress(KeyEvent.VK_UP);
5822N/A toolkit.realSync();
5822N/A }
5822N/A }
5822N/A
5822N/A static void doTestPgUpDown() {
5822N/A try {
5822N/A SwingUtilities.invokeAndWait(new Runnable() {
5822N/A @Override
5822N/A public void run() {
5822N/A cb.hidePopup();
5822N/A cb.setSelectedIndex(0);
5822N/A }
5822N/A });
5822N/A } catch (InterruptedException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A } catch (InvocationTargetException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A }
5822N/A toolkit.realSync();
5822N/A
5822N/A int listHeight = cb.getMaximumRowCount();
5822N/A for (int i = 0; i < nElems; i += listHeight) {
5822N/A robot.keyPress(KeyEvent.VK_PAGE_DOWN);
5822N/A toolkit.realSync();
5822N/A }
5822N/A
5822N/A for (int i = 0; i < nElems; i += listHeight) {
5822N/A robot.keyPress(KeyEvent.VK_PAGE_UP);
5822N/A toolkit.realSync();
5822N/A }
5822N/A }
5822N/A
5822N/A public static void main(String[] args) {
5822N/A try {
5822N/A SwingUtilities.invokeAndWait(new Runnable() {
5822N/A @Override
5822N/A public void run() {
5822N/A bug4199622 test = new bug4199622(new MetalLookAndFeel());
5822N/A test.setVisible(true);
5822N/A }
5822N/A });
5822N/A } catch (InterruptedException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A } catch (InvocationTargetException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A }
5822N/A doTest();
5822N/A
5822N/A if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {
5822N/A try {
5822N/A SwingUtilities.invokeAndWait(new Runnable() {
5822N/A @Override
5822N/A public void run() {
5822N/A bug4199622 test = new bug4199622(new WindowsLookAndFeel());
5822N/A test.setVisible(true);
5822N/A }
5822N/A });
5822N/A } catch (InterruptedException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A } catch (InvocationTargetException e) {
5822N/A throw new RuntimeException("Test failed", e);
5822N/A }
5822N/A doTest();
5822N/A }
5822N/A }
5822N/A}