Path: blob/master/test/jdk/javax/swing/JComboBox/4199622/bug4199622.java
41154 views
/*1* Copyright (c) 2013, 2018, 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/*24@test25@key headful26@bug 419962227@requires (os.family == "windows")28@summary RFE: JComboBox shouldn't send ActionEvents for keyboard navigation29@library /test/lib30@modules java.desktop/com.sun.java.swing.plaf.windows31@build jdk.test.lib.Platform32@run main bug419962233*/3435import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;36import jdk.test.lib.Platform;3738import javax.swing.*;39import javax.swing.plaf.metal.MetalLookAndFeel;40import java.awt.*;41import java.awt.event.ActionEvent;42import java.awt.event.ActionListener;43import java.awt.event.KeyEvent;44import java.lang.reflect.InvocationTargetException;4546public class bug4199622 extends JFrame implements ActionListener {4748static final int nElems = 20;49static JComboBox<String> cb = null;5051bug4199622(LookAndFeel laf) {52super();5354try {55UIManager.setLookAndFeel(laf);56} catch (UnsupportedLookAndFeelException e) {57throw new RuntimeException("Test failed", e);58}5960setDefaultCloseOperation(DISPOSE_ON_CLOSE);61cb = new JComboBox<>();62for (int i = 0; i < nElems; i++) {63cb.addItem(String.valueOf(i + 1));64}65cb.addActionListener(this);66add(cb);6768setSize(300, 300);69pack();70setLocationRelativeTo(null);71}7273@Override74public void actionPerformed(ActionEvent e) {75if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && cb.isPopupVisible()) {76throw new RuntimeException("Test failed. actionPerformed generated");77}78}7980static Robot robot = null;8182static void doTest() {83if (robot == null) {84try {85robot = new Robot();86robot.setAutoDelay(100);87} catch (AWTException e) {88throw new RuntimeException("Can't create robot. Test failed", e);89}90}9192robot.waitForIdle();9394doActualTest();9596try {97SwingUtilities.invokeAndWait(new Runnable() {98@Override99public void run() {100cb.hidePopup();101cb.setEditable(true);102cb.updateUI();103}104});105} catch (InterruptedException e) {106throw new RuntimeException("Test failed", e);107} catch (InvocationTargetException e) {108throw new RuntimeException("Test failed", e);109}110111robot.waitForIdle();112doActualTest();113}114115static void doActualTest() {116UIManager.put("ComboBox.noActionOnKeyNavigation", true);117doTestUpDown();118UIManager.put("ComboBox.noActionOnKeyNavigation", false);119doTestUpDown();120121UIManager.put("ComboBox.noActionOnKeyNavigation", true);122doTestPgUpDown();123UIManager.put("ComboBox.noActionOnKeyNavigation", false);124doTestPgUpDown();125126UIManager.put("ComboBox.noActionOnKeyNavigation", true);127doTestHomeEnd();128UIManager.put("ComboBox.noActionOnKeyNavigation", false);129doTestHomeEnd();130}131132static void doTestHomeEnd() {133try {134SwingUtilities.invokeAndWait(new Runnable() {135@Override136public void run() {137cb.hidePopup();138cb.setSelectedIndex(0);139}140});141} catch (InterruptedException e) {142throw new RuntimeException("Test failed", e);143} catch (InvocationTargetException e) {144throw new RuntimeException("Test failed", e);145}146robot.waitForIdle();147148robot.keyPress(KeyEvent.VK_END);149robot.keyRelease(KeyEvent.VK_END);150robot.waitForIdle();151robot.keyPress(KeyEvent.VK_HOME);152robot.keyRelease(KeyEvent.VK_HOME);153robot.waitForIdle();154}155156static void doTestUpDown() {157try {158SwingUtilities.invokeAndWait(new Runnable() {159@Override160public void run() {161cb.hidePopup();162cb.setSelectedIndex(0);163}164});165} catch (InterruptedException e) {166throw new RuntimeException("Test failed", e);167} catch (InvocationTargetException e) {168throw new RuntimeException("Test failed", e);169}170robot.waitForIdle();171172for (int i = 0; i < nElems; i++) {173robot.keyPress(KeyEvent.VK_DOWN);174robot.keyRelease(KeyEvent.VK_DOWN);175robot.waitForIdle();176}177178for (int i = 0; i < nElems; i++) {179robot.keyPress(KeyEvent.VK_UP);180robot.keyRelease(KeyEvent.VK_UP);181robot.waitForIdle();182}183}184185static void doTestPgUpDown() {186try {187SwingUtilities.invokeAndWait(new Runnable() {188@Override189public void run() {190cb.hidePopup();191cb.setSelectedIndex(0);192}193});194} catch (InterruptedException e) {195throw new RuntimeException("Test failed", e);196} catch (InvocationTargetException e) {197throw new RuntimeException("Test failed", e);198}199robot.waitForIdle();200201int listHeight = cb.getMaximumRowCount();202for (int i = 0; i < nElems; i += listHeight) {203robot.keyPress(KeyEvent.VK_PAGE_DOWN);204robot.keyRelease(KeyEvent.VK_PAGE_DOWN);205robot.waitForIdle();206}207208for (int i = 0; i < nElems; i += listHeight) {209robot.keyPress(KeyEvent.VK_PAGE_UP);210robot.keyRelease(KeyEvent.VK_PAGE_UP);211robot.waitForIdle();212}213}214215public static void main(String[] args) {216try {217SwingUtilities.invokeAndWait(new Runnable() {218@Override219public void run() {220bug4199622 test = new bug4199622(new MetalLookAndFeel());221test.setVisible(true);222}223});224} catch (InterruptedException e) {225throw new RuntimeException("Test failed", e);226} catch (InvocationTargetException e) {227throw new RuntimeException("Test failed", e);228}229doTest();230231if (Platform.isWindows()) {232try {233SwingUtilities.invokeAndWait(new Runnable() {234@Override235public void run() {236bug4199622 test = new bug4199622(new WindowsLookAndFeel());237test.setVisible(true);238}239});240} catch (InterruptedException e) {241throw new RuntimeException("Test failed", e);242} catch (InvocationTargetException e) {243throw new RuntimeException("Test failed", e);244}245doTest();246}247}248}249250251