Path: blob/master/test/jdk/javax/swing/JComboBox/4515752/DefaultButtonTest.java
41153 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* @key headful30* @bug 4515752 433707131* @author Mark Davidson32* @summary Tests the invocation of the default button within the JComboBox.33*/34public class DefaultButtonTest extends JFrame implements ActionListener {3536private static boolean defaultButtonPressed = false;37private static boolean editChanged = false;3839private static String[] strData = {40"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"41};4243private static String[] strData2 = {44"One", "Two", "Three", "Four", "Five", "Six", "Seven"45};4647public static void main(String[] args) throws Throwable {48SwingUtilities.invokeAndWait(new Runnable(){49public void run() {50new DefaultButtonTest();51}52});53test();54System.out.println("Test Passed");55}5657public DefaultButtonTest() {58getContentPane().add(new DefaultPanel(this));59pack();60setVisible(true);61setLocationRelativeTo(null);62}6364public static void test() {65// Use Robot to automate the test66Robot robot = null;67try {68robot = new Robot();69} catch (Exception ex) {70ex.printStackTrace();71}72robot.setAutoDelay(125);7374for (int i = 0; i < 3; i++) {75// Test ENTER press on the non editable combo.76robot.waitForIdle();77robot.keyPress(KeyEvent.VK_ENTER);78robot.waitForIdle();79robot.keyRelease(KeyEvent.VK_ENTER);80robot.waitForIdle();81testDefaultButton(true);8283// Test the ENTER press on the editable combo box84robot.keyPress(KeyEvent.VK_TAB);85robot.waitForIdle();86robot.keyRelease(KeyEvent.VK_TAB);87robot.waitForIdle();88robot.keyPress(KeyEvent.VK_ENTER);89robot.waitForIdle();90robot.keyRelease(KeyEvent.VK_ENTER);91robot.waitForIdle();92testDefaultButton(true);9394// Change the value, should generate a change but not a Default Button press.95robot.waitForIdle();96robot.keyPress(KeyEvent.VK_D);97robot.waitForIdle();98robot.keyRelease(KeyEvent.VK_D);99robot.waitForIdle();100robot.keyPress(KeyEvent.VK_ENTER);101robot.waitForIdle();102robot.keyRelease(KeyEvent.VK_ENTER);103robot.waitForIdle();104testEditChange(true);105robot.waitForIdle();106testDefaultButton(true);107108// Change value, changing focus should fire an ActionEvent.109robot.waitForIdle();110robot.keyPress(KeyEvent.VK_BACK_SPACE);111robot.keyRelease(KeyEvent.VK_BACK_SPACE);112robot.waitForIdle();113robot.keyPress(KeyEvent.VK_SHIFT);114robot.keyPress(KeyEvent.VK_TAB);115robot.keyRelease(KeyEvent.VK_TAB);116robot.keyRelease(KeyEvent.VK_SHIFT);117robot.waitForIdle();118testEditChange(true);119robot.waitForIdle();120testDefaultButton(false);121}122}123124public void actionPerformed(ActionEvent evt) {125String cmd = evt.getActionCommand();126System.out.println("ActionEvent: " + cmd);127128if (cmd.equals("OK")) {129defaultButtonPressed = true;130}131132if (cmd.equals("comboBoxChanged")) {133editChanged = true;134}135}136137public static void testDefaultButton(boolean flag) {138if (defaultButtonPressed != flag) {139new RuntimeException("defaultButtonPressed unexpectedly = " + defaultButtonPressed);140}141// reset142defaultButtonPressed = false;143}144145public static void testEditChange(boolean flag) {146if (editChanged != flag) {147new RuntimeException("editChanged unexpectedly = " + editChanged);148}149// reset150editChanged = false;151}152153class DefaultPanel extends JPanel {154155public JComboBox combo;156public JComboBox combo2;157158private JButton okButton = new JButton("OK");159private JButton cancelButton = new JButton("Cancel");160161public DefaultPanel(JFrame root) {162setLayout(new BorderLayout());163add(createPanel(), BorderLayout.NORTH);164add(createInfoPanel(), BorderLayout.CENTER);165add(createButtonPanel(root), BorderLayout.SOUTH);166}167168private JPanel createPanel() {169combo = new JComboBox(strData);170combo.addActionListener(DefaultButtonTest.this);171combo2 = new JComboBox(strData2);172combo2.setEditable(true);173combo2.addActionListener(DefaultButtonTest.this);174175JPanel panel = new JPanel();176177panel.add(combo);178panel.add(combo2);179180return panel;181}182183private JScrollPane createInfoPanel() {184StringBuffer txt = new StringBuffer("Test for 4337071:\n");185txt.append("ENTER pressed in NON-EDITABLE combo box should be passed to the OK button.\n");186txt.append("For an EDITABLE combo box, the combo box should fire an action event.");187txt.append("\n\nTest for 4515752:\n");188txt.append("ENTER on an EDITABLE combo box in which the contents has not changed\n");189txt.append("should be passed to the default button");190191JTextArea text = new JTextArea(txt.toString());192text.setEditable(false);193194return new JScrollPane(text);195}196197198private JPanel createButtonPanel(JFrame frame) {199frame.getRootPane().setDefaultButton(okButton);200201// This is just to check when the OK Button was pressed.202okButton.addActionListener(DefaultButtonTest.this);203204JPanel panel = new JPanel();205panel.add(okButton);206panel.add(cancelButton);207return panel;208}209}210211}212213214