Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/4199622/bug4199622.java
38854 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 419962225@summary RFE: JComboBox shouldn't send ActionEvents for keyboard navigation26@author Vladislav Karnaukhov27@library ../../../../lib/testlibrary28@build jdk.testlibrary.OSInfo29@run main bug419962230*/3132import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;33import jdk.testlibrary.OSInfo;3435import javax.swing.*;36import javax.swing.plaf.metal.MetalLookAndFeel;37import java.awt.*;38import java.awt.event.ActionEvent;39import java.awt.event.ActionListener;40import java.awt.event.KeyEvent;41import java.lang.reflect.InvocationTargetException;4243public class bug4199622 extends JFrame implements ActionListener {4445static final int nElems = 20;46static JComboBox<String> cb = null;4748bug4199622(LookAndFeel laf) {49super();5051try {52UIManager.setLookAndFeel(laf);53} catch (UnsupportedLookAndFeelException e) {54throw new RuntimeException("Test failed", e);55}5657setDefaultCloseOperation(DISPOSE_ON_CLOSE);58cb = new JComboBox<>();59for (int i = 0; i < nElems; i++) {60cb.addItem(String.valueOf(i + 1));61}62cb.addActionListener(this);63add(cb);6465setSize(300, 300);66pack();67}6869@Override70public void actionPerformed(ActionEvent e) {71if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && cb.isPopupVisible()) {72throw new RuntimeException("Test failed. actionPerformed generated");73}74}7576static Robot robot = null;7778static void doTest() {79if (robot == null) {80try {81robot = new Robot();82robot.setAutoDelay(20);83} catch (AWTException e) {84throw new RuntimeException("Can't create robot. Test failed", e);85}86}8788robot.waitForIdle();8990doActualTest();9192try {93SwingUtilities.invokeAndWait(new Runnable() {94@Override95public void run() {96cb.hidePopup();97cb.setEditable(true);98cb.updateUI();99}100});101} catch (InterruptedException e) {102throw new RuntimeException("Test failed", e);103} catch (InvocationTargetException e) {104throw new RuntimeException("Test failed", e);105}106107robot.waitForIdle();108doActualTest();109}110111static void doActualTest() {112UIManager.put("ComboBox.noActionOnKeyNavigation", true);113doTestUpDown();114UIManager.put("ComboBox.noActionOnKeyNavigation", false);115doTestUpDown();116117UIManager.put("ComboBox.noActionOnKeyNavigation", true);118doTestPgUpDown();119UIManager.put("ComboBox.noActionOnKeyNavigation", false);120doTestPgUpDown();121122UIManager.put("ComboBox.noActionOnKeyNavigation", true);123doTestHomeEnd();124UIManager.put("ComboBox.noActionOnKeyNavigation", false);125doTestHomeEnd();126}127128static void doTestHomeEnd() {129try {130SwingUtilities.invokeAndWait(new Runnable() {131@Override132public void run() {133cb.hidePopup();134cb.setSelectedIndex(0);135}136});137} catch (InterruptedException e) {138throw new RuntimeException("Test failed", e);139} catch (InvocationTargetException e) {140throw new RuntimeException("Test failed", e);141}142robot.waitForIdle();143144robot.keyPress(KeyEvent.VK_END);145robot.keyRelease(KeyEvent.VK_END);146robot.waitForIdle();147robot.keyPress(KeyEvent.VK_HOME);148robot.keyRelease(KeyEvent.VK_HOME);149robot.waitForIdle();150}151152static void doTestUpDown() {153try {154SwingUtilities.invokeAndWait(new Runnable() {155@Override156public void run() {157cb.hidePopup();158cb.setSelectedIndex(0);159}160});161} catch (InterruptedException e) {162throw new RuntimeException("Test failed", e);163} catch (InvocationTargetException e) {164throw new RuntimeException("Test failed", e);165}166robot.waitForIdle();167168for (int i = 0; i < nElems; i++) {169robot.keyPress(KeyEvent.VK_DOWN);170robot.keyRelease(KeyEvent.VK_DOWN);171robot.waitForIdle();172}173174for (int i = 0; i < nElems; i++) {175robot.keyPress(KeyEvent.VK_UP);176robot.keyRelease(KeyEvent.VK_UP);177robot.waitForIdle();178}179}180181static void doTestPgUpDown() {182try {183SwingUtilities.invokeAndWait(new Runnable() {184@Override185public void run() {186cb.hidePopup();187cb.setSelectedIndex(0);188}189});190} catch (InterruptedException e) {191throw new RuntimeException("Test failed", e);192} catch (InvocationTargetException e) {193throw new RuntimeException("Test failed", e);194}195robot.waitForIdle();196197int listHeight = cb.getMaximumRowCount();198for (int i = 0; i < nElems; i += listHeight) {199robot.keyPress(KeyEvent.VK_PAGE_DOWN);200robot.keyRelease(KeyEvent.VK_PAGE_DOWN);201robot.waitForIdle();202}203204for (int i = 0; i < nElems; i += listHeight) {205robot.keyPress(KeyEvent.VK_PAGE_UP);206robot.keyRelease(KeyEvent.VK_PAGE_UP);207robot.waitForIdle();208}209}210211public static void main(String[] args) {212try {213SwingUtilities.invokeAndWait(new Runnable() {214@Override215public void run() {216bug4199622 test = new bug4199622(new MetalLookAndFeel());217test.setVisible(true);218}219});220} catch (InterruptedException e) {221throw new RuntimeException("Test failed", e);222} catch (InvocationTargetException e) {223throw new RuntimeException("Test failed", e);224}225doTest();226227if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {228try {229SwingUtilities.invokeAndWait(new Runnable() {230@Override231public void run() {232bug4199622 test = new bug4199622(new WindowsLookAndFeel());233test.setVisible(true);234}235});236} catch (InterruptedException e) {237throw new RuntimeException("Test failed", e);238} catch (InvocationTargetException e) {239throw new RuntimeException("Test failed", e);240}241doTest();242}243}244}245246247