Skip to content

Commit 4ca2d02

Browse files
author
Amos Shi
committed
8305874: Open source AWT Key, Text Event related tests
Backport-of: d6cf4aa1551df591c7bc75cb8c5e90d57630ca2a
1 parent 7491ed4 commit 4ca2d02

File tree

5 files changed

+547
-0
lines changed

5 files changed

+547
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (c) 2002, 2023, 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.EventQueue;
25+
import java.awt.Frame;
26+
import java.awt.Robot;
27+
import java.awt.TextField;
28+
import java.awt.event.KeyEvent;
29+
import java.awt.event.KeyListener;
30+
31+
/*
32+
* @test
33+
* @bug 4724007
34+
* @key headful
35+
* @summary Tests that KeyTyped events are fired for the Delete key
36+
* and that no extraneous characters are entered as a result.
37+
*/
38+
39+
public class DeleteKeyTyped {
40+
private static Frame frame;
41+
private static TextField tf;
42+
43+
private static boolean deleteKeyTypedReceived = false;
44+
private static final String ORIGINAL = "0123456789";
45+
private static final String SUCCESS = "123456789";
46+
47+
public static void main(String[] args) throws Exception {
48+
try {
49+
Robot robot = new Robot();
50+
robot.setAutoWaitForIdle(true);
51+
robot.setAutoDelay(100);
52+
53+
EventQueue.invokeAndWait(DeleteKeyTyped::createTestUI);
54+
robot.waitForIdle();
55+
robot.delay(1000);
56+
57+
// Move cursor to start of TextField
58+
robot.keyPress(KeyEvent.VK_HOME);
59+
robot.keyRelease(KeyEvent.VK_HOME);
60+
robot.waitForIdle();
61+
robot.delay(50);
62+
63+
// Press and release Delete
64+
robot.keyPress(KeyEvent.VK_DELETE);
65+
robot.keyRelease(KeyEvent.VK_DELETE);
66+
robot.waitForIdle();
67+
robot.delay(50);
68+
69+
EventQueue.invokeAndWait(DeleteKeyTyped::testDeleteKeyEvent);
70+
} finally {
71+
EventQueue.invokeAndWait(() -> {
72+
if (frame != null) {
73+
frame.dispose();
74+
}
75+
});
76+
}
77+
}
78+
79+
private static void createTestUI() {
80+
frame = new Frame();
81+
tf = new TextField(ORIGINAL, 20);
82+
frame.add(tf);
83+
frame.setSize(300, 100);
84+
frame.setVisible(true);
85+
tf.requestFocusInWindow();
86+
87+
tf.addKeyListener(new KeyListener() {
88+
@Override
89+
public void keyPressed(KeyEvent evt) {
90+
printKey(evt);
91+
}
92+
93+
@Override
94+
public void keyTyped(KeyEvent evt) {
95+
printKey(evt);
96+
int keychar = evt.getKeyChar();
97+
if (keychar == 127) { // Delete character is 127 or \u007F
98+
deleteKeyTypedReceived = true;
99+
}
100+
}
101+
102+
@Override
103+
public void keyReleased(KeyEvent evt) {
104+
printKey(evt);
105+
}
106+
107+
private void printKey(KeyEvent evt) {
108+
switch(evt.getID()) {
109+
case KeyEvent.KEY_TYPED:
110+
case KeyEvent.KEY_PRESSED:
111+
case KeyEvent.KEY_RELEASED:
112+
break;
113+
default:
114+
System.out.println("Other Event");
115+
return;
116+
}
117+
118+
System.out.println("params= " + evt.paramString() + " \n" +
119+
"KeyChar: " + evt.getKeyChar() + " = " + (int) evt.getKeyChar() +
120+
" KeyCode: " + evt.getKeyCode() +
121+
" Modifiers: " + evt.getModifiersEx());
122+
123+
if (evt.isActionKey()) {
124+
System.out.println("Action Key");
125+
}
126+
127+
System.out.println("keyText= " + KeyEvent.getKeyText(evt.getKeyCode()) + "\n");
128+
}
129+
});
130+
}
131+
132+
private static void testDeleteKeyEvent() {
133+
if (deleteKeyTypedReceived) {
134+
if (tf.getText().equals(SUCCESS)) {
135+
System.out.println("Test PASSED");
136+
} else {
137+
System.out.println("Test FAILED: wrong string");
138+
throw new RuntimeException("The test failed: wrong string: " +
139+
tf.getText());
140+
}
141+
}
142+
}
143+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 2002, 2023, 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.EventQueue;
25+
import java.awt.Frame;
26+
import java.awt.Robot;
27+
import java.awt.TextField;
28+
import java.awt.event.KeyEvent;
29+
import java.awt.event.KeyListener;
30+
31+
/*
32+
* @test
33+
* @key headful
34+
* @bug 4734408
35+
* @summary Tests that KeyTyped events are fired for the Escape key
36+
* and that no extraneous characters are entered as a result.
37+
*/
38+
39+
public class EscapeKeyTyped {
40+
private static Frame frame;
41+
private static TextField tf;
42+
43+
private static final String ORIGINAL = "0123456789";
44+
private static boolean escapeKeyTypedReceived = false;
45+
46+
public static void main(String[] args) throws Exception {
47+
try {
48+
Robot robot = new Robot();
49+
robot.setAutoWaitForIdle(true);
50+
robot.setAutoDelay(30);
51+
52+
EventQueue.invokeAndWait(EscapeKeyTyped::createAndShowUI);
53+
robot.waitForIdle();
54+
robot.delay(1000);
55+
56+
// Press and release Escape
57+
robot.keyPress(KeyEvent.VK_ESCAPE);
58+
robot.keyRelease(KeyEvent.VK_ESCAPE);
59+
robot.waitForIdle();
60+
robot.delay(20);
61+
62+
EventQueue.invokeAndWait(EscapeKeyTyped::testEscKeyEvent);
63+
} finally {
64+
EventQueue.invokeAndWait(() -> {
65+
if (frame != null) {
66+
frame.dispose();
67+
}
68+
});
69+
}
70+
}
71+
72+
private static void createAndShowUI() {
73+
frame = new Frame();
74+
tf = new TextField(ORIGINAL, 20);
75+
frame.add(tf);
76+
frame.setSize(300, 100);
77+
frame.setVisible(true);
78+
tf.requestFocusInWindow();
79+
80+
tf.addKeyListener(new KeyListener() {
81+
@Override
82+
public void keyTyped(KeyEvent e) {
83+
printKey(e);
84+
}
85+
86+
@Override
87+
public void keyPressed(KeyEvent e) {
88+
printKey(e);
89+
int keychar = e.getKeyChar();
90+
if (keychar == 27) { // Escape character is 27 or \u0021
91+
escapeKeyTypedReceived = true;
92+
}
93+
}
94+
95+
@Override
96+
public void keyReleased(KeyEvent e) {
97+
printKey(e);
98+
}
99+
100+
private void printKey(KeyEvent evt) {
101+
switch (evt.getID()) {
102+
case KeyEvent.KEY_TYPED:
103+
case KeyEvent.KEY_PRESSED:
104+
case KeyEvent.KEY_RELEASED:
105+
break;
106+
default:
107+
System.out.println("Other Event");
108+
return;
109+
}
110+
111+
System.out.println("params= " + evt.paramString() + " \n" +
112+
"KeyChar: " + evt.getKeyChar() + " = " + (int) evt.getKeyChar() +
113+
" KeyCode: " + evt.getKeyCode() +
114+
" Modifiers: " + evt.getModifiersEx());
115+
116+
if (evt.isActionKey()) {
117+
System.out.println("Action Key");
118+
}
119+
120+
System.out.println("keyText= " + KeyEvent.getKeyText(evt.getKeyCode()) + "\n");
121+
}
122+
});
123+
}
124+
125+
private static void testEscKeyEvent() {
126+
if (escapeKeyTypedReceived) {
127+
if (tf.getText().equals(ORIGINAL)) {
128+
System.out.println("Test PASSED");
129+
} else {
130+
System.out.println("Test FAILED: wrong string");
131+
throw new RuntimeException("The test failed: wrong string: " +
132+
tf.getText());
133+
}
134+
}
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2004, 2023, 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.EventQueue;
25+
import java.awt.Frame;
26+
import java.awt.Robot;
27+
import java.awt.event.KeyEvent;
28+
29+
/*
30+
* @test
31+
* @key headful
32+
* @bug 4965227
33+
* @requires (os.family == "linux")
34+
* @summary tests that Shift+F10 during Window show doesn't cause deadlock- Linux only
35+
*/
36+
37+
public class ShiftF10Test {
38+
private static Frame frame;
39+
40+
public static void main(String[] args) throws Exception {
41+
try {
42+
Robot robot = new Robot();
43+
robot.setAutoDelay(10);
44+
45+
EventQueue.invokeLater(() -> {
46+
frame = new Frame("Deadlocking one");
47+
frame.setSize(100, 100);
48+
frame.setVisible(true);
49+
});
50+
51+
for (int i = 0; i < 250; i++) {
52+
robot.keyPress(KeyEvent.VK_SHIFT);
53+
robot.keyPress(KeyEvent.VK_F10);
54+
robot.keyRelease(KeyEvent.VK_F10);
55+
robot.keyRelease(KeyEvent.VK_SHIFT);
56+
robot.delay(10);
57+
}
58+
} catch (Exception e) {
59+
throw new RuntimeException("Test Failed due to following error: ", e);
60+
} finally {
61+
EventQueue.invokeAndWait(() -> {
62+
if (frame != null) {
63+
frame.dispose();
64+
}
65+
});
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)