Skip to content

Commit 4068754

Browse files
committed
8342508: Use latch in BasicMenuUI/bug4983388.java instead of delay
Backport-of: 02ec8ca2d6ccbabc6740b60be8fe1f8b2110f0ca
1 parent 0dadc44 commit 4068754

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

test/jdk/javax/swing/plaf/basic/BasicMenuUI/4983388/bug4983388.java

+33-24
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,41 @@
2626
* @key headful
2727
* @bug 4983388 8015600
2828
* @summary shortcuts on menus do not work on JDS
29-
* @author Oleg Mokhovikov
3029
* @library ../../../../regtesthelpers
3130
* @build Util
3231
* @run main bug4983388
3332
*/
3433

35-
import java.awt.*;
36-
import javax.swing.*;
37-
import javax.swing.event.MenuListener;
38-
import javax.swing.event.MenuEvent;
34+
import java.awt.Robot;
3935
import java.awt.event.KeyEvent;
36+
import java.util.concurrent.CountDownLatch;
37+
38+
import javax.swing.JFrame;
39+
import javax.swing.JMenu;
40+
import javax.swing.JMenuBar;
41+
import javax.swing.SwingUtilities;
42+
import javax.swing.UIManager;
43+
import javax.swing.UnsupportedLookAndFeelException;
44+
import javax.swing.event.MenuEvent;
45+
import javax.swing.event.MenuListener;
46+
47+
import static java.util.concurrent.TimeUnit.SECONDS;
4048

4149
public class bug4983388 {
42-
static volatile boolean bMenuSelected = false;
4350
static JFrame frame;
4451

52+
private static final CountDownLatch menuSelected = new CountDownLatch(1);
53+
4554
private static class TestMenuListener implements MenuListener {
55+
@Override
4656
public void menuCanceled(MenuEvent e) {}
57+
@Override
4758
public void menuDeselected(MenuEvent e) {}
59+
60+
@Override
4861
public void menuSelected(MenuEvent e) {
4962
System.out.println("menuSelected");
50-
bMenuSelected = true;
63+
menuSelected.countDown();
5164
}
5265
}
5366

@@ -56,42 +69,38 @@ private static void createAndShowGUI() {
5669
JMenu menu = new JMenu("File");
5770
menu.setMnemonic('F');
5871
menuBar.add(menu);
59-
frame = new JFrame();
72+
menu.addMenuListener(new TestMenuListener());
73+
74+
frame = new JFrame("bug4983388");
6075
frame.setJMenuBar(menuBar);
6176
frame.setLocationRelativeTo(null);
62-
frame.pack();
77+
frame.setSize(250, 100);
6378
frame.setVisible(true);
64-
MenuListener listener = new TestMenuListener();
65-
menu.addMenuListener(listener);
6679
}
6780

6881
public static void main(String[] args) throws Exception {
69-
7082
try {
7183
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
7284
} catch (UnsupportedLookAndFeelException | ClassNotFoundException ex) {
73-
System.err.println("GTKLookAndFeel is not supported on this platform. Using defailt LaF for this platform.");
85+
System.err.println("GTKLookAndFeel is not supported on this platform. "
86+
+ "Using default LaF for this platform.");
7487
}
7588

76-
SwingUtilities.invokeAndWait(new Runnable() {
77-
public void run() {
78-
createAndShowGUI();
79-
}
80-
});
89+
SwingUtilities.invokeAndWait(bug4983388::createAndShowGUI);
8190

8291
Robot robot = new Robot();
8392
robot.setAutoDelay(50);
8493
robot.waitForIdle();
8594
robot.delay(500);
8695

8796
Util.hitMnemonics(robot, KeyEvent.VK_F);
88-
robot.waitForIdle();
89-
robot.delay(500);
90-
91-
SwingUtilities.invokeAndWait(() -> frame.dispose());
9297

93-
if (!bMenuSelected) {
94-
throw new RuntimeException("shortcuts on menus do not work");
98+
try {
99+
if (!menuSelected.await(1, SECONDS)) {
100+
throw new RuntimeException("shortcuts on menus do not work");
101+
}
102+
} finally {
103+
SwingUtilities.invokeAndWait(frame::dispose);
95104
}
96105
}
97106
}

0 commit comments

Comments
 (0)