|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.Robot; |
| 25 | +import java.awt.event.KeyEvent; |
| 26 | +import javax.swing.JButton; |
| 27 | +import javax.swing.JFrame; |
| 28 | +import javax.swing.JPanel; |
| 29 | +import javax.swing.JTextField; |
| 30 | +import javax.swing.SwingUtilities; |
| 31 | +import javax.swing.UIManager; |
| 32 | +import javax.swing.UnsupportedLookAndFeelException; |
| 33 | + |
| 34 | +/* |
| 35 | + * @test |
| 36 | + * @key headful |
| 37 | + * @bug 8280913 |
| 38 | + * @summary Check whether the default button is honored when <Enter> key is pressed. |
| 39 | + * @run main DefaultButtonTest |
| 40 | + */ |
| 41 | +public class DefaultButtonTest { |
| 42 | + private volatile boolean buttonPressed; |
| 43 | + private JFrame frame; |
| 44 | + |
| 45 | + public static void main(String[] s) throws Exception { |
| 46 | + DefaultButtonTest test = new DefaultButtonTest(); |
| 47 | + test.runTest(); |
| 48 | + } |
| 49 | + |
| 50 | + private static void setLookAndFeel(String lafName) { |
| 51 | + try { |
| 52 | + UIManager.setLookAndFeel(lafName); |
| 53 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 54 | + System.out.println("Ignoring Unsupported L&F: " + lafName); |
| 55 | + } catch (ClassNotFoundException | InstantiationException |
| 56 | + | IllegalAccessException e) { |
| 57 | + throw new RuntimeException(e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void disposeFrame() { |
| 62 | + if (frame != null) { |
| 63 | + frame.dispose(); |
| 64 | + frame = null; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void createUI() { |
| 69 | + frame = new JFrame("Default Button Test"); |
| 70 | + JPanel panel = new JPanel(); |
| 71 | + panel.add(new JTextField("Text field")); |
| 72 | + JButton button1 = new JButton("Default"); |
| 73 | + button1.addActionListener(e -> buttonPressed = true); |
| 74 | + panel.add(button1); |
| 75 | + panel.add(new JButton("Button2")); |
| 76 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 77 | + frame.add(panel); |
| 78 | + frame.pack(); |
| 79 | + frame.setLocationRelativeTo(null); |
| 80 | + frame.getRootPane().setDefaultButton(button1); |
| 81 | + frame.setVisible(true); |
| 82 | + } |
| 83 | + |
| 84 | + public void runTest() throws Exception { |
| 85 | + Robot robot = new Robot(); |
| 86 | + robot.setAutoDelay(100); |
| 87 | + for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { |
| 88 | + try { |
| 89 | + buttonPressed = false; |
| 90 | + String lafName = laf.getClassName(); |
| 91 | + System.out.println("Testing L&F: " + lafName); |
| 92 | + SwingUtilities.invokeAndWait(() -> { |
| 93 | + setLookAndFeel(lafName); |
| 94 | + createUI(); |
| 95 | + }); |
| 96 | + robot.waitForIdle(); |
| 97 | + robot.keyPress(KeyEvent.VK_ENTER); |
| 98 | + robot.keyRelease(KeyEvent.VK_ENTER); |
| 99 | + robot.waitForIdle(); |
| 100 | + |
| 101 | + if (buttonPressed) { |
| 102 | + System.out.println("Test Passed for L&F: " + lafName); |
| 103 | + } else { |
| 104 | + throw new RuntimeException("Test Failed, Default Button not pressed for L&F: " + lafName); |
| 105 | + } |
| 106 | + } finally { |
| 107 | + SwingUtilities.invokeAndWait(this::disposeFrame); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments