Path: blob/master/test/jdk/javax/swing/JRootPane/DefaultButtonTest.java
66644 views
/*1* Copyright (c) 2022, 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*/2223import java.awt.Robot;24import java.awt.event.KeyEvent;25import javax.swing.JButton;26import javax.swing.JFrame;27import javax.swing.JPanel;28import javax.swing.JTextField;29import javax.swing.SwingUtilities;30import javax.swing.UIManager;31import javax.swing.UnsupportedLookAndFeelException;3233/*34* @test35* @key headful36* @bug 828091337* @summary Check whether the default button is honored when <Enter> key is pressed.38* @run main DefaultButtonTest39*/40public class DefaultButtonTest {41private volatile boolean buttonPressed;42private JFrame frame;4344public static void main(String[] s) throws Exception {45DefaultButtonTest test = new DefaultButtonTest();46test.runTest();47}4849private static void setLookAndFeel(String lafName) {50try {51UIManager.setLookAndFeel(lafName);52} catch (UnsupportedLookAndFeelException ignored) {53System.out.println("Ignoring Unsupported L&F: " + lafName);54} catch (ClassNotFoundException | InstantiationException55| IllegalAccessException e) {56throw new RuntimeException(e);57}58}5960private void disposeFrame() {61if (frame != null) {62frame.dispose();63frame = null;64}65}6667private void createUI() {68frame = new JFrame("Default Button Test");69JPanel panel = new JPanel();70panel.add(new JTextField("Text field"));71JButton button1 = new JButton("Default");72button1.addActionListener(e -> buttonPressed = true);73panel.add(button1);74panel.add(new JButton("Button2"));75frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);76frame.add(panel);77frame.pack();78frame.setLocationRelativeTo(null);79frame.getRootPane().setDefaultButton(button1);80frame.setVisible(true);81}8283public void runTest() throws Exception {84Robot robot = new Robot();85robot.setAutoDelay(100);86for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {87try {88buttonPressed = false;89String lafName = laf.getClassName();90System.out.println("Testing L&F: " + lafName);91SwingUtilities.invokeAndWait(() -> {92setLookAndFeel(lafName);93createUI();94});95robot.waitForIdle();96robot.keyPress(KeyEvent.VK_ENTER);97robot.keyRelease(KeyEvent.VK_ENTER);98robot.waitForIdle();99100if (buttonPressed) {101System.out.println("Test Passed for L&F: " + lafName);102} else {103throw new RuntimeException("Test Failed, Default Button not pressed for L&F: " + lafName);104}105} finally {106SwingUtilities.invokeAndWait(this::disposeFrame);107}108}109}110}111112113