Skip to content

Commit 436f148

Browse files
amosshiGoeLin
authored andcommitted
8296275: Write a test to verify setAccelerator method of JMenuItem
Backport-of: b005013a0015656b7f6ccc26f8a13c44d61f77b9
1 parent b07d8c2 commit 436f148

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2016, 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.Dimension;
25+
import java.awt.EventQueue;
26+
import java.awt.FlowLayout;
27+
import java.awt.Point;
28+
import java.awt.Robot;
29+
import java.awt.event.InputEvent;
30+
import java.awt.event.KeyEvent;
31+
import java.util.concurrent.CountDownLatch;
32+
import java.util.concurrent.TimeUnit;
33+
import javax.swing.JFrame;
34+
import javax.swing.JMenu;
35+
import javax.swing.JMenuBar;
36+
import javax.swing.JMenuItem;
37+
import javax.swing.KeyStroke;
38+
import javax.swing.SwingUtilities;
39+
40+
/*
41+
* @test
42+
* @key headful
43+
* @bug 8296275
44+
* @summary To verify the setAccelerator method of JMenuItem.
45+
* @requires (os.family=="mac")
46+
* @run main JMenuItemSetAcceleratorTest
47+
*/
48+
49+
public class JMenuItemSetAcceleratorTest {
50+
private static JFrame frame;
51+
private static final CountDownLatch actionLatch = new CountDownLatch(1);
52+
private volatile static Point frameAt;
53+
private volatile static Dimension frameSize;
54+
55+
private static void createAndShow() {
56+
frame = new JFrame("JMenuItem.setAccelerator");
57+
frame.setLayout(new FlowLayout());
58+
59+
JMenuBar bar = new JMenuBar();
60+
JMenu menu = new JMenu("File");
61+
JMenuItem menuItem = new JMenuItem("Menu Item");
62+
63+
menuItem.setAccelerator(
64+
KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.META_DOWN_MASK));
65+
menuItem.addActionListener(e -> {
66+
System.out.println("menu item action.");
67+
actionLatch.countDown();
68+
});
69+
70+
menu.add(menuItem);
71+
bar.add(menu);
72+
73+
frame.setJMenuBar(bar);
74+
frame.setSize(200, 200);
75+
frame.setLocationRelativeTo(null);
76+
frame.setVisible(true);
77+
}
78+
79+
public static void main(String[] args) throws Exception {
80+
try {
81+
SwingUtilities.invokeAndWait(JMenuItemSetAcceleratorTest::createAndShow);
82+
83+
Robot robot = new Robot();
84+
robot.setAutoDelay(50);
85+
robot.setAutoWaitForIdle(true);
86+
87+
EventQueue.invokeAndWait(() -> {
88+
frameAt = frame.getLocationOnScreen();
89+
frameSize = frame.getSize();
90+
});
91+
92+
robot.mouseMove(frameAt.x + frameSize.width / 2,
93+
frameAt.y + frameSize.height / 2);
94+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
95+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
96+
robot.waitForIdle();
97+
98+
robot.keyPress(KeyEvent.VK_META);
99+
robot.keyPress(KeyEvent.VK_M);
100+
robot.keyRelease(KeyEvent.VK_M);
101+
robot.keyRelease(KeyEvent.VK_META);
102+
103+
if (!actionLatch.await(5, TimeUnit.SECONDS)) {
104+
throw new RuntimeException(
105+
"Hasn't received the JMenuItem action event by pressing "
106+
+ "accelerator keys, test fails.");
107+
}
108+
System.out.println("Test passed, received action event on menu item.");
109+
} finally {
110+
SwingUtilities.invokeAndWait(JMenuItemSetAcceleratorTest::disposeFrame);
111+
}
112+
}
113+
114+
public static void disposeFrame() {
115+
if (frame != null) {
116+
frame.dispose();
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)