8342508: Use latch in BasicMenuUI/bug4983388.java instead of delay

Reviewed-by: azvegint, abhiscxk, serb
This commit is contained in:
Alexey Ivanov 2024-11-19 17:15:46 +00:00
parent 47ebf8d868
commit 02ec8ca2d6

View File

@ -26,28 +26,41 @@
* @key headful * @key headful
* @bug 4983388 8015600 * @bug 4983388 8015600
* @summary shortcuts on menus do not work on JDS * @summary shortcuts on menus do not work on JDS
* @author Oleg Mokhovikov
* @library ../../../../regtesthelpers * @library ../../../../regtesthelpers
* @build Util * @build Util
* @run main bug4983388 * @run main bug4983388
*/ */
import java.awt.*; import java.awt.Robot;
import javax.swing.*;
import javax.swing.event.MenuListener;
import javax.swing.event.MenuEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.util.concurrent.CountDownLatch;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import static java.util.concurrent.TimeUnit.SECONDS;
public class bug4983388 { public class bug4983388 {
static volatile boolean bMenuSelected = false;
static JFrame frame; static JFrame frame;
private static final CountDownLatch menuSelected = new CountDownLatch(1);
private static class TestMenuListener implements MenuListener { private static class TestMenuListener implements MenuListener {
@Override
public void menuCanceled(MenuEvent e) {} public void menuCanceled(MenuEvent e) {}
@Override
public void menuDeselected(MenuEvent e) {} public void menuDeselected(MenuEvent e) {}
@Override
public void menuSelected(MenuEvent e) { public void menuSelected(MenuEvent e) {
System.out.println("menuSelected"); System.out.println("menuSelected");
bMenuSelected = true; menuSelected.countDown();
} }
} }
@ -56,28 +69,24 @@ public class bug4983388 {
JMenu menu = new JMenu("File"); JMenu menu = new JMenu("File");
menu.setMnemonic('F'); menu.setMnemonic('F');
menuBar.add(menu); menuBar.add(menu);
frame = new JFrame(); menu.addMenuListener(new TestMenuListener());
frame = new JFrame("bug4983388");
frame.setJMenuBar(menuBar); frame.setJMenuBar(menuBar);
frame.setLocationRelativeTo(null); frame.setLocationRelativeTo(null);
frame.pack(); frame.setSize(250, 100);
frame.setVisible(true); frame.setVisible(true);
MenuListener listener = new TestMenuListener();
menu.addMenuListener(listener);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
try { try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (UnsupportedLookAndFeelException | ClassNotFoundException ex) { } catch (UnsupportedLookAndFeelException | ClassNotFoundException ex) {
System.err.println("GTKLookAndFeel is not supported on this platform. Using defailt LaF for this platform."); System.err.println("GTKLookAndFeel is not supported on this platform. "
+ "Using default LaF for this platform.");
} }
SwingUtilities.invokeAndWait(new Runnable() { SwingUtilities.invokeAndWait(bug4983388::createAndShowGUI);
public void run() {
createAndShowGUI();
}
});
Robot robot = new Robot(); Robot robot = new Robot();
robot.setAutoDelay(50); robot.setAutoDelay(50);
@ -85,13 +94,13 @@ public class bug4983388 {
robot.delay(500); robot.delay(500);
Util.hitMnemonics(robot, KeyEvent.VK_F); Util.hitMnemonics(robot, KeyEvent.VK_F);
robot.waitForIdle();
robot.delay(500);
SwingUtilities.invokeAndWait(() -> frame.dispose()); try {
if (!menuSelected.await(1, SECONDS)) {
if (!bMenuSelected) { throw new RuntimeException("shortcuts on menus do not work");
throw new RuntimeException("shortcuts on menus do not work"); }
} finally {
SwingUtilities.invokeAndWait(frame::dispose);
} }
} }
} }