Skip to content

Commit d3dda1c

Browse files
committed
8354472: Clean up and open source KeyEvent related tests (Part 3)
Clean up and publish five more KeyEvent related tests.
1 parent a875733 commit d3dda1c

File tree

5 files changed

+530
-0
lines changed

5 files changed

+530
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 4115484 4164672 4167893
27+
* @summary Ensures that KeyEvent has right keyChar for modifier and action keys.
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual CharUndefinedTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Frame;
35+
import java.awt.TextField;
36+
import java.awt.event.KeyEvent;
37+
import java.awt.event.KeyListener;
38+
import java.lang.reflect.InvocationTargetException;
39+
40+
public class CharUndefinedTest extends Frame implements KeyListener {
41+
42+
static String INSTRUCTIONS = """
43+
Click on the text field inside the window named "Check KeyChar values".
44+
Of any of the keys mentioned in this list that exist on your keyboard
45+
press once every of them and also press them in two-key combinations such as
46+
Control-Shift or Alt-Control.
47+
The list of keys is: "Control, Shift, Meta, Alt, Command, Option".
48+
After that press once all function keys from F1 to F12,
49+
Insert, Home, End, PageUp, PageDown and four arrow keys.
50+
Check the log area below. If there are no messages starting with word "ERROR"
51+
press "Pass" otherwise press "Fail".
52+
""";
53+
54+
public CharUndefinedTest() {
55+
super("Check KeyChar values");
56+
setLayout(new BorderLayout());
57+
TextField tf = new TextField(30);
58+
tf.addKeyListener(this);
59+
add(tf, BorderLayout.CENTER);
60+
pack();
61+
tf.requestFocus();
62+
}
63+
64+
public void keyPressed(KeyEvent e) {
65+
if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
66+
PassFailJFrame.log("ERROR: KeyPressed: keyChar = " + e.getKeyChar() +
67+
" keyCode = " + e.getKeyCode() + " " + e.getKeyText(e.getKeyCode()));
68+
}
69+
}
70+
71+
public void keyTyped(KeyEvent e) {
72+
if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
73+
PassFailJFrame.log("ERROR: KeyTyped: keyChar = " + e.getKeyChar() +
74+
" keyCode = " + e.getKeyCode() + " " + e.getKeyText(e.getKeyCode()));
75+
}
76+
}
77+
78+
public void keyReleased(KeyEvent e) {
79+
if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
80+
PassFailJFrame.log("ERROR: KeyReleased: keyChar = " + e.getKeyChar() +
81+
" keyCode = " + e.getKeyCode() + " " + e.getKeyText(e.getKeyCode()));
82+
}
83+
}
84+
85+
public static void main(String[] args) throws InterruptedException,
86+
InvocationTargetException {
87+
PassFailJFrame.builder()
88+
.instructions(INSTRUCTIONS)
89+
.columns(45)
90+
.logArea(10)
91+
.testUI(CharUndefinedTest::new)
92+
.build()
93+
.awaitAndCheck();
94+
}
95+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 4218892 4191924 4199284
27+
* @summary Unable to enter some chars via european keyboard layout(s)
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual ExtendedKeysTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Frame;
35+
import java.awt.TextArea;
36+
import java.lang.reflect.InvocationTargetException;
37+
38+
public class ExtendedKeysTest extends Frame {
39+
static String INSTRUCTIONS = """
40+
This test requires Swiss German input. If the Swiss German input
41+
can not be installed or configured press "Pass" to skip testing.
42+
Click on the text area inside the window named "Check input".
43+
Switch to Swiss German input and press key with "\\" on it
44+
(usually this key is above or to the left of the main "Enter" key).
45+
If you see a dollar sign press "Pass".
46+
If you see any other character or question mark press "Fail".
47+
""";
48+
49+
public ExtendedKeysTest() {
50+
super("Check input");
51+
setLayout(new BorderLayout());
52+
add(new TextArea(20, 20), "Center");
53+
pack();
54+
}
55+
56+
public static void main(String[] args) throws InterruptedException,
57+
InvocationTargetException {
58+
PassFailJFrame.builder()
59+
.instructions(INSTRUCTIONS)
60+
.columns(45)
61+
.logArea(10)
62+
.testUI(ExtendedKeysTest::new)
63+
.build()
64+
.awaitAndCheck();
65+
}
66+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2001, 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 4093998
27+
* @summary keyDown not called on subclasses of Component
28+
* @key headful
29+
* @run main KeyDownCaptureTest
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.BorderLayout;
34+
import java.awt.Canvas;
35+
import java.awt.Color;
36+
import java.awt.Dimension;
37+
import java.awt.EventQueue;
38+
import java.awt.Frame;
39+
import java.awt.Point;
40+
import java.awt.Robot;
41+
import java.awt.event.InputEvent;
42+
import java.awt.event.KeyEvent;
43+
import java.awt.event.KeyListener;
44+
import java.lang.reflect.InvocationTargetException;
45+
import java.util.concurrent.atomic.AtomicBoolean;
46+
47+
public class KeyDownCaptureTest extends Frame implements KeyListener {
48+
static AtomicBoolean passed = new AtomicBoolean(false);
49+
50+
public KeyDownCaptureTest() {
51+
super("Key Down Capture Test");
52+
}
53+
54+
public void initUI() {
55+
setLayout (new BorderLayout());
56+
setSize(200, 200);
57+
setLocationRelativeTo(null);
58+
Canvas canvas = new Canvas();
59+
canvas.setBackground(Color.RED);
60+
canvas.addKeyListener(this);
61+
add(canvas, BorderLayout.CENTER);
62+
setVisible(true);
63+
}
64+
65+
public void middle(Point p) {
66+
Point loc = getLocationOnScreen();
67+
Dimension size = getSize();
68+
p.setLocation(loc.x + (size.width / 2), loc.y + (size.height / 2));
69+
}
70+
71+
@Override
72+
public void keyTyped(KeyEvent ignore) {}
73+
74+
@Override
75+
public void keyPressed(KeyEvent e) {
76+
passed.set(true);
77+
}
78+
79+
@Override
80+
public void keyReleased(KeyEvent ignore) {}
81+
82+
public static void main(String[] args) throws InterruptedException,
83+
InvocationTargetException, AWTException {
84+
KeyDownCaptureTest test = new KeyDownCaptureTest();
85+
try {
86+
EventQueue.invokeAndWait((test::initUI));
87+
Robot robot = new Robot();
88+
robot.setAutoDelay(50);
89+
robot.delay(500);
90+
robot.waitForIdle();
91+
Point target = new Point();
92+
EventQueue.invokeAndWait(() -> {
93+
test.middle(target);
94+
});
95+
robot.mouseMove(target.x, target.y);
96+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
97+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
98+
robot.keyPress(KeyEvent.VK_SPACE);
99+
robot.keyRelease(KeyEvent.VK_SPACE);
100+
robot.delay(100);
101+
robot.waitForIdle();
102+
if (!passed.get()) {
103+
throw new RuntimeException("KeyPressed has not arrived to canvas");
104+
}
105+
} finally {
106+
if (test != null) {
107+
EventQueue.invokeAndWait(test::dispose);
108+
}
109+
}
110+
}
111+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2001, 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 4397557
27+
* @summary Check that focused lightweight component gets key events
28+
* even if mouse is outside of it or on top of heavyweight component
29+
* @key headful
30+
* @run main KeyEventToLightweight
31+
*/
32+
33+
import java.awt.AWTException;
34+
import java.awt.Button;
35+
import java.awt.Component;
36+
import java.awt.Dimension;
37+
import java.awt.EventQueue;
38+
import java.awt.FlowLayout;
39+
import java.awt.Frame;
40+
import java.awt.Point;
41+
import java.awt.Robot;
42+
import java.awt.event.InputEvent;
43+
import java.awt.event.KeyAdapter;
44+
import java.awt.event.KeyEvent;
45+
import java.lang.reflect.InvocationTargetException;
46+
import java.util.concurrent.atomic.AtomicBoolean;
47+
import javax.swing.JButton;
48+
49+
public class KeyEventToLightweight extends Frame {
50+
JButton lwbutton = new JButton("Select Me");
51+
Button hwbutton = new Button("Heavyweight");
52+
53+
AtomicBoolean aTyped = new AtomicBoolean(false);
54+
AtomicBoolean bTyped = new AtomicBoolean(false);
55+
AtomicBoolean cTyped = new AtomicBoolean(false);
56+
57+
public static void main(String[] args) throws InterruptedException,
58+
InvocationTargetException, AWTException {
59+
KeyEventToLightweight test = new KeyEventToLightweight();
60+
try {
61+
EventQueue.invokeAndWait(test::initUI);
62+
test.performTest();
63+
} finally {
64+
EventQueue.invokeAndWait(test::dispose);
65+
}
66+
}
67+
68+
public void initUI() {
69+
this.setLayout(new FlowLayout());
70+
add(lwbutton);
71+
add(hwbutton);
72+
setSize(200, 200);
73+
setLocationRelativeTo(null);
74+
lwbutton.addKeyListener(new KeyAdapter() {
75+
public void keyPressed(KeyEvent e) {
76+
if (e.getKeyCode() == KeyEvent.VK_A) {
77+
aTyped.set(true);
78+
} else if (e.getKeyCode() == KeyEvent.VK_B) {
79+
bTyped.set(true);
80+
} else if (e.getKeyCode() == KeyEvent.VK_C) {
81+
cTyped.set(true);
82+
}
83+
}
84+
});
85+
setVisible(true);
86+
}
87+
88+
public void middleOf(Component c, Point p) {
89+
Point loc = c.getLocationOnScreen();
90+
Dimension size = c.getSize();
91+
p.setLocation(loc.x + (size.width / 2), loc.y + (size.height / 2));
92+
}
93+
94+
public void performTest() throws AWTException {
95+
Robot robot = new Robot();
96+
robot.setAutoDelay(50);
97+
robot.delay(500);
98+
robot.waitForIdle();
99+
Point target = new Point();
100+
middleOf(lwbutton, target);
101+
robot.mouseMove(target.x, target.y);
102+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
103+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
104+
robot.waitForIdle();
105+
robot.delay(500);
106+
robot.keyPress(KeyEvent.VK_A);
107+
robot.keyRelease(KeyEvent.VK_A);
108+
robot.waitForIdle();
109+
robot.mouseMove(target.x - 200, target.y);
110+
robot.keyPress(KeyEvent.VK_B);
111+
robot.keyRelease(KeyEvent.VK_B);
112+
robot.waitForIdle();
113+
robot.delay(500);
114+
middleOf(hwbutton, target);
115+
robot.mouseMove(target.x, target.y);
116+
robot.keyPress(KeyEvent.VK_C);
117+
robot.keyRelease(KeyEvent.VK_C);
118+
if (!aTyped.get() || !bTyped.get() || !cTyped.get()) {
119+
throw new RuntimeException("Key event was not delivered, case 1: "
120+
+ aTyped.get() + ", case 2: " + bTyped.get() + ", case 3: "
121+
+ cTyped.get());
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)