Skip to content

Commit 683a626

Browse files
committed
8341111: open source several AWT tests including menu shortcut tests
Backport-of: 04c9c5f0a7b49bbabfc2244411c6c995a3b464cf
1 parent c61d62b commit 683a626

File tree

6 files changed

+743
-0
lines changed

6 files changed

+743
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 1998, 2024, 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+
/*
25+
* @test
26+
* @bug 4079449
27+
* @key headful
28+
* @summary MenuItem objects return null if they are activated by shortcut
29+
*/
30+
31+
import java.awt.Dialog;
32+
import java.awt.EventQueue;
33+
import java.awt.Frame;
34+
import java.awt.Menu;
35+
import java.awt.MenuBar;
36+
import java.awt.MenuItem;
37+
import java.awt.MenuShortcut;
38+
import java.awt.Point;
39+
import java.awt.Robot;
40+
import java.awt.TextArea;
41+
import java.awt.event.ActionEvent;
42+
import java.awt.event.ActionListener;
43+
import java.awt.event.InputEvent;
44+
import java.awt.event.KeyEvent;
45+
46+
import static java.awt.event.KeyEvent.VK_CONTROL;
47+
import static java.awt.event.KeyEvent.VK_META;
48+
49+
public class ActionCommandTest implements ActionListener {
50+
51+
static volatile Frame frame;
52+
static volatile boolean event = false;
53+
static volatile boolean failed = false;
54+
static final String ITEMTEXT = "Testitem";
55+
56+
static void createUI() {
57+
frame = new Frame("ActionCommand Menu Shortcut Test");
58+
MenuBar mb = new MenuBar();
59+
Menu m = new Menu("Test");
60+
MenuItem mi = new MenuItem(ITEMTEXT, new MenuShortcut(KeyEvent.VK_T));
61+
mi.addActionListener(new ActionCommandTest());
62+
m.add(mi);
63+
mb.add(m);
64+
frame.setMenuBar(mb);
65+
frame.setBounds(50, 400, 200, 200);
66+
frame.setVisible(true);
67+
}
68+
69+
public static void main(String[] args ) throws Exception {
70+
71+
EventQueue.invokeAndWait(ActionCommandTest::createUI);
72+
try {
73+
Robot robot = new Robot();
74+
75+
robot.waitForIdle();
76+
robot.delay(2000);
77+
78+
// Ensure window has focus
79+
Point p = frame.getLocationOnScreen();
80+
robot.mouseMove(p.x + frame.getWidth() / 2, p.y + frame.getHeight() / 2);
81+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
82+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
83+
robot.waitForIdle();
84+
robot.delay(2000);
85+
86+
// invoke short cut.
87+
robot.keyPress(KeyEvent.VK_T);
88+
robot.delay(50);
89+
robot.keyRelease(KeyEvent.VK_T);
90+
robot.waitForIdle();
91+
robot.delay(2000);
92+
} finally {
93+
if (frame != null) {
94+
EventQueue.invokeAndWait(frame::dispose);
95+
}
96+
}
97+
if (failed) {
98+
throw new RuntimeException("No actioncommand");
99+
}
100+
}
101+
102+
// Since no ActionCommand is set, this should be the menuitem's label.
103+
public void actionPerformed(ActionEvent e) {
104+
event = true;
105+
String s = e.getActionCommand();
106+
if (s == null || !s.equals(ITEMTEXT)) {
107+
failed = true;
108+
}
109+
}
110+
111+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
/*
25+
* @test
26+
* @bug 4167811
27+
* @summary tests that shortcuts work for Checkbox menu items
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual CheckMenuShortcut
31+
*/
32+
33+
import java.awt.CheckboxMenuItem;
34+
import java.awt.Dimension;
35+
import java.awt.Frame;
36+
import java.awt.Insets;
37+
import java.awt.Menu;
38+
import java.awt.MenuBar;
39+
import java.awt.MenuItem;
40+
import java.awt.MenuShortcut;
41+
import java.awt.Panel;
42+
import java.awt.Rectangle;
43+
import java.awt.TextArea;
44+
import java.awt.event.ActionEvent;
45+
import java.awt.event.ActionListener;
46+
import java.awt.event.ItemEvent;
47+
import java.awt.event.ItemListener;
48+
import java.awt.event.KeyEvent;
49+
50+
public class CheckMenuShortcut implements ActionListener, ItemListener {
51+
52+
static final String INSTRUCTIONS = """
53+
A window that contains a text area will be displayed.
54+
The window will have a menu labeled 'Window Menu'. Click on the menu to see its items.
55+
56+
The two menu items should have shortcuts which in order are : Ctrl-A, Ctrl-I.
57+
On macOS these will be Command-A, Command-I.
58+
59+
If the second item only has the label 'checkbox item' and no shortcut
60+
ie none of Ctrl-I or Ctrl-i, or Command-I or Command-i on macOS painted on it, the test FAILS.
61+
62+
The same second item - labeled 'checkbox item' is in fact a Checkbox menu item.
63+
The menu item should NOT be checked (eg no tick mark).
64+
65+
Dismiss the menu by clicking inside the window, do not select any of menu items.
66+
After that press Ctrl-i, (Command-i on macOS).
67+
68+
After that click on the menu again. If the second menu item 'checkbox item' is now
69+
checked, the test PASSES, if it is not checked, the test FAILS.
70+
""";
71+
72+
public static void main(String[] args) throws Exception {
73+
PassFailJFrame.builder()
74+
.title("CheckboxMenuItem Shortcut Test Instructions")
75+
.instructions(INSTRUCTIONS)
76+
.columns(60)
77+
.logArea()
78+
.testUI(CheckMenuShortcut::createUI)
79+
.build()
80+
.awaitAndCheck();
81+
}
82+
83+
84+
static Frame createUI() {
85+
86+
MenuBar mainMenu;
87+
Menu menu;
88+
MenuItem action;
89+
CheckboxMenuItem item;
90+
TextArea pane;
91+
92+
boolean isMac = System.getProperty("os.name").startsWith("Mac");
93+
String ctrlA = (isMac) ? "Command-A" : "Ctrl-A";
94+
String ctrlI = (isMac) ? "Command-I" : "Ctrl-I";
95+
96+
CheckMenuShortcut cms = new CheckMenuShortcut();
97+
Frame frame = new Frame("CheckMenuShortcut");
98+
99+
mainMenu = new MenuBar();
100+
menu = new Menu("Window Menu");
101+
102+
action = new MenuItem("action");
103+
action.setShortcut(new MenuShortcut(KeyEvent.VK_A, false));
104+
action.addActionListener(cms);
105+
action.setActionCommand("action");
106+
menu.add(action);
107+
108+
item = new CheckboxMenuItem("checkbox item", false);
109+
item.setShortcut(new MenuShortcut(KeyEvent.VK_I,false));
110+
item.addItemListener(cms);
111+
item.addActionListener(cms);
112+
menu.add(item);
113+
114+
mainMenu.add(menu);
115+
116+
frame.setMenuBar(mainMenu);
117+
118+
pane = new TextArea(ctrlA + " -- action menu test\n", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);
119+
Dimension mySize = frame.getSize();
120+
Insets myIns = frame.getInsets();
121+
pane.setBounds(new Rectangle(mySize.width - myIns.left - myIns.right,
122+
mySize.height - myIns.top - myIns.bottom));
123+
pane.setLocation(myIns.left,myIns.top);
124+
frame.add(pane);
125+
126+
pane.append(ctrlI + " -- item menu test\n");
127+
128+
frame.pack();
129+
return frame;
130+
}
131+
132+
public void itemStateChanged(ItemEvent evt) {
133+
PassFailJFrame.log("Got item: " + evt.getItem() + "\n");
134+
}
135+
136+
public void actionPerformed(ActionEvent evt) {
137+
PassFailJFrame.log("Got action: " + evt.getActionCommand() + "\n");
138+
}
139+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (c) 1998, 2024, 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+
/*
25+
* @test
26+
* @bug 4034665
27+
* @key headful
28+
* @summary Function keys should work correctly as shortcuts
29+
*/
30+
31+
import java.awt.EventQueue;
32+
import java.awt.Frame;
33+
import java.awt.Menu;
34+
import java.awt.MenuBar;
35+
import java.awt.MenuItem;
36+
import java.awt.MenuShortcut;
37+
import java.awt.Point;
38+
import java.awt.Robot;
39+
import java.awt.event.ActionEvent;
40+
import java.awt.event.ActionListener;
41+
import java.awt.event.InputEvent;
42+
import java.awt.event.KeyEvent;
43+
44+
import static java.awt.event.KeyEvent.VK_CONTROL;
45+
import static java.awt.event.KeyEvent.VK_META;
46+
47+
public class FunctionKeyShortcut implements ActionListener {
48+
49+
static volatile Frame frame;
50+
static volatile boolean event = false;
51+
static volatile boolean failed = false;
52+
53+
static final boolean isMac = System.getProperty("os.name").contains("OS X");
54+
55+
static void createUI() {
56+
frame = new Frame("Function Key Menu Shortcut Test");
57+
MenuBar mb = new MenuBar();
58+
Menu m = new Menu("Test");
59+
MenuItem mi1 = new MenuItem("Function key 1", new MenuShortcut(KeyEvent.VK_F1));
60+
MenuItem mi2 = new MenuItem("Function key 2", new MenuShortcut(KeyEvent.VK_F2));
61+
MenuItem mi3 = new MenuItem("Function key 3", new MenuShortcut(KeyEvent.VK_F3));
62+
MenuItem mi4 = new MenuItem("Function key 4", new MenuShortcut(KeyEvent.VK_F4));
63+
MenuItem mi5 = new MenuItem("Function key 5", new MenuShortcut(KeyEvent.VK_F5));
64+
MenuItem mi6 = new MenuItem("Function key 6", new MenuShortcut(KeyEvent.VK_F6));
65+
MenuItem mi7 = new MenuItem("Function key 7", new MenuShortcut(KeyEvent.VK_F7));
66+
MenuItem mi8 = new MenuItem("Function key 8", new MenuShortcut(KeyEvent.VK_F8));
67+
MenuItem mi9 = new MenuItem("Function key 8", new MenuShortcut(KeyEvent.VK_F9));
68+
69+
FunctionKeyShortcut fks = new FunctionKeyShortcut();
70+
mi1.addActionListener(fks);
71+
mi2.addActionListener(fks);
72+
mi3.addActionListener(fks);
73+
mi4.addActionListener(fks);
74+
mi5.addActionListener(fks);
75+
mi6.addActionListener(fks);
76+
mi7.addActionListener(fks);
77+
mi8.addActionListener(fks);
78+
mi9.addActionListener(fks);
79+
80+
m.add(mi1);
81+
m.add(mi2);
82+
m.add(mi3);
83+
m.add(mi4);
84+
m.add(mi5);
85+
m.add(mi6);
86+
m.add(mi7);
87+
m.add(mi8);
88+
m.add(mi9);
89+
90+
mb.add(m);
91+
frame.setMenuBar(mb);
92+
frame.setBounds(50,400,200,200);
93+
frame.setVisible(true);
94+
}
95+
96+
public static void main(String[] args ) throws Exception {
97+
98+
EventQueue.invokeAndWait(FunctionKeyShortcut::createUI);
99+
try {
100+
Robot robot = new Robot();
101+
102+
robot.waitForIdle();
103+
robot.delay(2000);
104+
105+
// Ensure window has focus
106+
Point p = frame.getLocationOnScreen();
107+
robot.mouseMove(p.x + frame.getWidth() / 2, p.y + frame.getHeight() / 2);
108+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
109+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
110+
robot.waitForIdle();
111+
robot.delay(2000);
112+
113+
int mod = (isMac) ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
114+
robot.keyPress(mod);
115+
robot.keyPress(KeyEvent.VK_F1);
116+
robot.delay(50);
117+
robot.keyRelease(KeyEvent.VK_F1);
118+
robot.keyRelease(mod);
119+
robot.waitForIdle();
120+
robot.delay(2000);
121+
} finally {
122+
if (frame != null) {
123+
EventQueue.invokeAndWait(frame::dispose);
124+
}
125+
}
126+
if (!event || failed) {
127+
throw new RuntimeException("No actioncommand");
128+
}
129+
}
130+
131+
public void actionPerformed(ActionEvent e) {
132+
System.out.println("Got " + e);
133+
String s = e.getActionCommand();
134+
event = true;
135+
if (s == null || !s.equals("Function key 1")) {
136+
failed = true;
137+
}
138+
}
139+
140+
}

0 commit comments

Comments
 (0)