Skip to content

Commit cea97c7

Browse files
committed
8354653: Clean up and open source KeyEvent related tests (Part 4)
Clean up four more KeyEvent related tests.
1 parent a875733 commit cea97c7

File tree

4 files changed

+409
-0
lines changed

4 files changed

+409
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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 4122687 4209844
27+
* @summary Characters typed with AltGr have Alt bit set on
28+
* KEY_TYPED events
29+
* @requires (os.family == "windows")
30+
* @library /java/awt/regtesthelpers
31+
* @build PassFailJFrame
32+
* @run main/manual AltGrTest
33+
*/
34+
35+
import java.awt.BorderLayout;
36+
import java.awt.Frame;
37+
import java.awt.TextField;
38+
import java.awt.event.KeyEvent;
39+
import java.awt.event.KeyListener;
40+
import java.lang.reflect.InvocationTargetException;
41+
42+
public class AltGrTest extends Frame implements KeyListener {
43+
static String INSTRUCTIONS = """
44+
Switch to German (Germany) keyboard layout and type
45+
few characters using <AltGr> key.
46+
Note: on windows keyboards without an AltGr key,
47+
you should use Ctrl-Alt to synthesize AltGr.
48+
For example, on German keyboards, `@' is AltGr-Q
49+
`{' is AltGr-7 and '[' is AltGr-8
50+
If you see the corresponding symbols appear in the text field
51+
and there are no entries in log area starting with word "FAIL:"
52+
press "Pass", otherwise press "Fail".
53+
""";
54+
55+
public AltGrTest() {
56+
setLayout(new BorderLayout());
57+
TextField entry = new TextField();
58+
entry.addKeyListener(this);
59+
add(entry, BorderLayout.CENTER);
60+
pack();
61+
}
62+
63+
public void keyTyped(KeyEvent e) {
64+
PassFailJFrame.log("----");
65+
PassFailJFrame.log("Got " + e);
66+
67+
if (e.isControlDown() || e.isAltDown()) {
68+
PassFailJFrame.log("FAIL: character typed has following modifiers bits set:");
69+
PassFailJFrame.log((e.isControlDown() ? " Control" : "")
70+
+ (e.isAltDown() ? " Alt" : ""));
71+
}
72+
73+
if (!(e.isAltGraphDown())) {
74+
PassFailJFrame.log("FAIL: AltGraph modifier is missing");
75+
}
76+
}
77+
78+
public void keyPressed(KeyEvent ignore) {}
79+
public void keyReleased(KeyEvent ignore) {}
80+
81+
public static void main(String[] args) throws InterruptedException,
82+
InvocationTargetException {
83+
PassFailJFrame.builder()
84+
.instructions(INSTRUCTIONS)
85+
.logArea(10)
86+
.testUI(AltGrTest::new)
87+
.build()
88+
.awaitAndCheck();
89+
}
90+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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 4257434
27+
* @summary Ensures that the right results are produced by the
28+
* carriage return keys.
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual CRTest
32+
*/
33+
34+
import java.awt.BorderLayout;
35+
import java.awt.Button;
36+
import java.awt.Frame;
37+
import java.awt.TextField;
38+
import java.awt.event.ActionEvent;
39+
import java.awt.event.ActionListener;
40+
import java.awt.event.KeyEvent;
41+
import java.awt.event.KeyListener;
42+
import java.lang.reflect.InvocationTargetException;
43+
import java.util.concurrent.atomic.AtomicBoolean;
44+
45+
public class CRTest extends Frame implements KeyListener, ActionListener {
46+
StringBuilder error = new StringBuilder();
47+
AtomicBoolean actionCompleted = new AtomicBoolean(false);
48+
static String INSTRUCTIONS = """
49+
This test requires keyboard with the numeric keypad (numpad).
50+
If your keyboard does not have numpad press "Pass" to skip testing.
51+
Click on the text field in window named "Check KeyChar values".
52+
Press Enter on keypad. Then press Return key on a standard keyboard.
53+
Then click on "Done" button. Test will pass or fail automatically.
54+
""";
55+
56+
public CRTest() {
57+
super("Check KeyChar values");
58+
setLayout(new BorderLayout());
59+
TextField tf = new TextField(30);
60+
61+
tf.addKeyListener(this);
62+
tf.addActionListener(this);
63+
64+
add(tf, BorderLayout.CENTER);
65+
66+
Button done = new Button("Done");
67+
done.addActionListener((event) -> {
68+
checkAndComplete();
69+
});
70+
add(done, BorderLayout.SOUTH);
71+
pack();
72+
}
73+
74+
public void checkAndComplete() {
75+
if (!actionCompleted.get()) {
76+
error.append("\nNo action received!");
77+
}
78+
79+
if (!error.isEmpty()) {
80+
PassFailJFrame.forceFail(error.toString());
81+
} else {
82+
PassFailJFrame.forcePass();
83+
}
84+
}
85+
86+
public void keyPressed(KeyEvent evt) {
87+
if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_ENTER)) {
88+
error.append("\nKeyPressed: Unexpected code " + evt.getKeyCode());
89+
} else {
90+
PassFailJFrame.log("KeyPressed Test PASSED");
91+
}
92+
}
93+
94+
public void keyTyped(KeyEvent evt) {
95+
if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_UNDEFINED)) {
96+
error.append("\nKeyTyped: Unexpected code " + evt.getKeyCode());
97+
} else {
98+
PassFailJFrame.log("KeyTyped Test PASSED");
99+
}
100+
}
101+
102+
public void keyReleased(KeyEvent evt) {
103+
if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_ENTER)) {
104+
error.append("\nKeyReleased: Unexpected code " + evt.getKeyCode());
105+
} else {
106+
PassFailJFrame.log("KeyReleased Test PASSED");
107+
}
108+
}
109+
110+
public void actionPerformed(ActionEvent evt) {
111+
PassFailJFrame.log("ActionPerformed Test PASSED");
112+
actionCompleted.set(true);
113+
}
114+
115+
public static void main(String[] args) throws InterruptedException,
116+
InvocationTargetException {
117+
PassFailJFrame.builder()
118+
.instructions(INSTRUCTIONS)
119+
.logArea(10)
120+
.testUI(CRTest::new)
121+
.build()
122+
.awaitAndCheck();
123+
}
124+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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 4279566
27+
* @summary Tests that numpad keys produce the correct key codes and
28+
* key chars when both the NumLock and CapsLock are on.
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual NumpadTest2
32+
*/
33+
34+
import java.awt.BorderLayout;
35+
import java.awt.Frame;
36+
import java.awt.TextField;
37+
import java.awt.event.KeyEvent;
38+
import java.awt.event.KeyListener;
39+
import java.lang.reflect.InvocationTargetException;
40+
41+
public class NumpadTest2 extends Frame implements KeyListener {
42+
static String INSTRUCTIONS = """
43+
Make sure that the NumLock and CapsLock are both ON.
44+
Click on the text field inside the window named "Check KeyChar values"
45+
Then, type the NumPad 7 key (not the regular 7 key).
46+
Verify that the keyChar and keyCode is correct for each key pressed.
47+
Remember that the keyCode for the KEY_TYPED event should be zero.
48+
If 7 appears in the text field and the key code printed is correct
49+
press "Pass", otherwise press "Fail".
50+
51+
Key Name keyChar Keycode
52+
-------------------------------------------------
53+
Numpad-7 Numpad-7 55 103
54+
""";
55+
56+
public NumpadTest2() {
57+
super("Check KeyChar values");
58+
setLayout(new BorderLayout());
59+
TextField tf = new TextField(30);
60+
tf.addKeyListener(this);
61+
add(tf, BorderLayout.CENTER);
62+
pack();
63+
}
64+
65+
public void keyPressed(KeyEvent evt) {
66+
printKey(evt);
67+
}
68+
69+
public void keyTyped(KeyEvent evt) {
70+
printKey(evt);
71+
}
72+
73+
public void keyReleased(KeyEvent evt) {
74+
printKey(evt);
75+
}
76+
77+
protected void printKey(KeyEvent evt) {
78+
switch (evt.getID()) {
79+
case KeyEvent.KEY_TYPED:
80+
break;
81+
case KeyEvent.KEY_PRESSED:
82+
break;
83+
case KeyEvent.KEY_RELEASED:
84+
break;
85+
default:
86+
System.out.println("Other Event ");
87+
return;
88+
}
89+
90+
if (evt.isActionKey()) {
91+
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
92+
(int) evt.getKeyChar() + " Action Key");
93+
} else {
94+
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
95+
(int) evt.getKeyChar());
96+
}
97+
}
98+
99+
public static void main(String[] args) throws InterruptedException,
100+
InvocationTargetException {
101+
PassFailJFrame.builder()
102+
.instructions(INSTRUCTIONS)
103+
.logArea(10)
104+
.testUI(NumpadTest2::new)
105+
.build()
106+
.awaitAndCheck();
107+
}
108+
}

0 commit comments

Comments
 (0)