Path: blob/master/test/jdk/javax/swing/JButton/4659800/SpaceKeyActivatesButton.java
66646 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.FocusAdapter;25import java.awt.event.FocusEvent;26import java.awt.event.KeyEvent;27import java.util.Arrays;28import java.util.List;29import java.util.concurrent.CountDownLatch;30import java.util.concurrent.TimeUnit;31import java.util.concurrent.atomic.AtomicBoolean;32import javax.swing.JButton;33import javax.swing.JFrame;34import javax.swing.JPanel;35import javax.swing.SwingUtilities;36import javax.swing.UIManager;37import javax.swing.UnsupportedLookAndFeelException;383940import static java.util.stream.Collectors.toList;4142/*43* @test44* @key headful45* @bug 828173846* @summary Check whether pressing <Space> key generates47* ActionEvent on focused Button or not.48* @run main SpaceKeyActivatesButton49*/50public class SpaceKeyActivatesButton {5152private static volatile boolean buttonPressed;53private static JFrame frame;54private static JButton focusedButton;55private static CountDownLatch buttonGainedFocusLatch;5657public static void main(String[] s) throws Exception {58runTest();59}6061public static void runTest() throws Exception {62Robot robot = new Robot();63robot.setAutoDelay(100);64robot.setAutoWaitForIdle(true);6566List<String> lafs = Arrays.stream(UIManager.getInstalledLookAndFeels())67.map(laf -> laf.getClassName())68.collect(toList());69for (String laf : lafs) {70buttonGainedFocusLatch = new CountDownLatch(1);71try {72buttonPressed = false;73System.out.println("Testing laf : " + laf);74AtomicBoolean lafSetSuccess = new AtomicBoolean(false);75SwingUtilities.invokeAndWait(() -> {76lafSetSuccess.set(setLookAndFeel(laf));77// Call createUI() only if setting laf succeeded78if (lafSetSuccess.get()) {79createUI();80}81});82// If setting laf failed, then just get next laf and continue83if (!lafSetSuccess.get()) {84continue;85}86robot.waitForIdle();8788// Wait until the button2 gains focus.89if (!buttonGainedFocusLatch.await(3, TimeUnit.SECONDS)) {90throw new RuntimeException("Test Failed, waited too long, " +91"but the button can't gain focus for laf : " + laf);92}9394robot.keyPress(KeyEvent.VK_SPACE);95robot.keyRelease(KeyEvent.VK_SPACE);9697if (buttonPressed) {98System.out.println("Test Passed for laf : " + laf);99} else {100throw new RuntimeException("Test Failed, button not pressed for laf : " + laf);101}102103} finally {104SwingUtilities.invokeAndWait(SpaceKeyActivatesButton::disposeFrame);105}106}107108}109110private static boolean setLookAndFeel(String lafName) {111try {112UIManager.setLookAndFeel(lafName);113} catch (UnsupportedLookAndFeelException ignored) {114System.out.println("Ignoring Unsupported laf : " + lafName);115return false;116} catch (ClassNotFoundException | InstantiationException117| IllegalAccessException e) {118throw new RuntimeException(e);119}120return true;121}122123private static void createUI() {124frame = new JFrame();125JPanel panel = new JPanel();126panel.add(new JButton("Button1"));127focusedButton = new JButton("Button2");128focusedButton.addActionListener(e -> buttonPressed = true);129focusedButton.addFocusListener(new FocusAdapter() {130@Override131public void focusGained(FocusEvent e) {132buttonGainedFocusLatch.countDown();133}134});135panel.add(focusedButton);136137frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);138frame.add(panel);139frame.pack();140frame.setLocationRelativeTo(null);141frame.setVisible(true);142focusedButton.requestFocusInWindow();143}144145private static void disposeFrame() {146if (frame != null) {147frame.dispose();148frame = null;149}150}151}152153154