Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/4515752/DefaultButtonTest.java
38853 views
/*1* Copyright (c) 2002, 2014, 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*/22import java.awt.*;23import java.awt.event.*;2425import javax.swing.*;2627/**28* @test29* @bug 4515752 433707130* @author Mark Davidson31* @summary Tests the invocation of the default button within the JComboBox.32*/33public class DefaultButtonTest extends JFrame implements ActionListener {3435private static boolean defaultButtonPressed = false;36private static boolean editChanged = false;3738private static String[] strData = {39"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"40};4142private static String[] strData2 = {43"One", "Two", "Three", "Four", "Five", "Six", "Seven"44};4546public static void main(String[] args) throws Throwable {47SwingUtilities.invokeAndWait(new Runnable(){48public void run() {49new DefaultButtonTest();50}51});52test();53System.out.println("Test Passed");54}5556public DefaultButtonTest() {57getContentPane().add(new DefaultPanel(this));58pack();59setVisible(true);60}6162public static void test() {63// Use Robot to automate the test64Robot robot = null;65try {66robot = new Robot();67} catch (Exception ex) {68ex.printStackTrace();69}70robot.setAutoDelay(125);7172for (int i = 0; i < 3; i++) {73// Test ENTER press on the non editable combo.74robot.waitForIdle();75robot.keyPress(KeyEvent.VK_ENTER);76robot.waitForIdle();77robot.keyRelease(KeyEvent.VK_ENTER);78robot.waitForIdle();79testDefaultButton(true);8081// Test the ENTER press on the editable combo box82robot.keyPress(KeyEvent.VK_TAB);83robot.waitForIdle();84robot.keyRelease(KeyEvent.VK_TAB);85robot.waitForIdle();86robot.keyPress(KeyEvent.VK_ENTER);87robot.waitForIdle();88robot.keyRelease(KeyEvent.VK_ENTER);89robot.waitForIdle();90testDefaultButton(true);9192// Change the value, should generate a change but not a Default Button press.93robot.waitForIdle();94robot.keyPress(KeyEvent.VK_D);95robot.waitForIdle();96robot.keyRelease(KeyEvent.VK_D);97robot.waitForIdle();98robot.keyPress(KeyEvent.VK_ENTER);99robot.waitForIdle();100robot.keyRelease(KeyEvent.VK_ENTER);101robot.waitForIdle();102testEditChange(true);103robot.waitForIdle();104testDefaultButton(true);105106// Change value, changing focus should fire an ActionEvent.107robot.waitForIdle();108robot.keyPress(KeyEvent.VK_BACK_SPACE);109robot.waitForIdle();110robot.keyRelease(KeyEvent.VK_BACK_SPACE);111robot.waitForIdle();112robot.keyPress(KeyEvent.VK_SHIFT);113robot.waitForIdle();114robot.keyPress(KeyEvent.VK_TAB);115robot.waitForIdle();116robot.keyRelease(KeyEvent.VK_SHIFT);117robot.waitForIdle();118robot.keyRelease(KeyEvent.VK_TAB);119robot.waitForIdle();120testEditChange(true);121robot.waitForIdle();122testDefaultButton(false);123}124}125126public void actionPerformed(ActionEvent evt) {127String cmd = evt.getActionCommand();128System.out.println("ActionEvent: " + cmd);129130if (cmd.equals("OK")) {131defaultButtonPressed = true;132}133134if (cmd.equals("comboBoxChanged")) {135editChanged = true;136}137}138139public static void testDefaultButton(boolean flag) {140if (defaultButtonPressed != flag) {141new RuntimeException("defaultButtonPressed unexpectedly = " + defaultButtonPressed);142}143// reset144defaultButtonPressed = false;145}146147public static void testEditChange(boolean flag) {148if (editChanged != flag) {149new RuntimeException("editChanged unexpectedly = " + editChanged);150}151// reset152editChanged = false;153}154155class DefaultPanel extends JPanel {156157public JComboBox combo;158public JComboBox combo2;159160private JButton okButton = new JButton("OK");161private JButton cancelButton = new JButton("Cancel");162163public DefaultPanel(JFrame root) {164setLayout(new BorderLayout());165add(createPanel(), BorderLayout.NORTH);166add(createInfoPanel(), BorderLayout.CENTER);167add(createButtonPanel(root), BorderLayout.SOUTH);168}169170private JPanel createPanel() {171combo = new JComboBox(strData);172combo.addActionListener(DefaultButtonTest.this);173combo2 = new JComboBox(strData2);174combo2.setEditable(true);175combo2.addActionListener(DefaultButtonTest.this);176177JPanel panel = new JPanel();178179panel.add(combo);180panel.add(combo2);181182return panel;183}184185private JScrollPane createInfoPanel() {186StringBuffer txt = new StringBuffer("Test for 4337071:\n");187txt.append("ENTER pressed in NON-EDITABLE combo box should be passed to the OK button.\n");188txt.append("For an EDITABLE combo box, the combo box should fire an action event.");189txt.append("\n\nTest for 4515752:\n");190txt.append("ENTER on an EDITABLE combo box in which the contents has not changed\n");191txt.append("should be passed to the default button");192193JTextArea text = new JTextArea(txt.toString());194text.setEditable(false);195196return new JScrollPane(text);197}198199200private JPanel createButtonPanel(JFrame frame) {201frame.getRootPane().setDefaultButton(okButton);202203// This is just to check when the OK Button was pressed.204okButton.addActionListener(DefaultButtonTest.this);205206JPanel panel = new JPanel();207panel.add(okButton);208panel.add(cancelButton);209return panel;210}211}212213}214215216