Skip to content

Commit 2faf8b8

Browse files
committedSep 19, 2024
8340007: Refactor KeyEvent/FunctionKeyTest.java
Reviewed-by: azvegint
1 parent 8908812 commit 2faf8b8

File tree

1 file changed

+99
-52
lines changed

1 file changed

+99
-52
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,18 @@
3131
import java.awt.Robot;
3232
import java.awt.TextArea;
3333
import java.awt.event.KeyEvent;
34+
import java.awt.event.WindowAdapter;
35+
import java.awt.event.WindowEvent;
36+
import java.util.ArrayList;
37+
import java.util.List;
38+
import java.util.concurrent.CountDownLatch;
39+
import java.util.concurrent.CyclicBarrier;
40+
import java.util.concurrent.TimeoutException;
41+
import java.util.concurrent.atomic.AtomicReference;
42+
43+
import static java.awt.Event.KEY_ACTION;
44+
import static java.awt.Event.KEY_ACTION_RELEASE;
45+
import static java.util.concurrent.TimeUnit.SECONDS;
3446

3547
/*
3648
* @test
@@ -39,47 +51,59 @@
3951
* @key headful
4052
*/
4153

42-
public class FunctionKeyTest {
43-
private static FunctionKeyTester frame;
54+
public final class FunctionKeyTest {
55+
private static Frame frame;
4456
private static Robot robot;
4557

46-
static volatile boolean keyPressReceived;
47-
static volatile boolean keyReleaseReceived;
58+
private static final CyclicBarrier keyPress = new CyclicBarrier(2);
59+
private static final CyclicBarrier keyRelease = new CyclicBarrier(2);
4860

49-
static final StringBuilder failures = new StringBuilder();
61+
private static final CountDownLatch frameActivated = new CountDownLatch(1);
5062

51-
private static void testKey(int keyCode, String keyText) {
52-
keyPressReceived = false;
53-
keyReleaseReceived = false;
63+
private static final List<Error> failures = new ArrayList<>(4);
64+
private static final AtomicReference<Exception> edtException = new AtomicReference<>();
5465

66+
private static void testKey(int keyCode, String keyText) throws Exception {
5567
robot.keyPress(keyCode);
56-
57-
if (!keyPressReceived) {
58-
failures.append(keyText).append(" key press is not received\n");
68+
try {
69+
keyPress.await(2, SECONDS);
70+
} catch (TimeoutException e) {
71+
keyPress.reset();
72+
failures.add(new Error(keyText + " key press is not received", e));
5973
}
6074

6175
robot.keyRelease(keyCode);
62-
63-
if (!keyReleaseReceived) {
64-
failures.append(keyText).append(" key release is not received\n");
76+
try {
77+
keyRelease.await(2, SECONDS);
78+
} catch (TimeoutException e) {
79+
keyRelease.reset();
80+
failures.add(new Error(keyText + " key release is not received", e));
6581
}
6682
}
6783

6884
public static void main(String[] args) throws Exception {
6985
robot = new Robot();
70-
robot.setAutoWaitForIdle(true);
7186
robot.setAutoDelay(150);
7287

7388
try {
7489
EventQueue.invokeAndWait(() -> {
7590
frame = new FunctionKeyTester();
7691
frame.setSize(200, 200);
7792
frame.setLocationRelativeTo(null);
93+
frame.addWindowListener(new WindowAdapter() {
94+
@Override
95+
public void windowActivated(WindowEvent e) {
96+
System.out.println("frame.windowActivated");
97+
frameActivated.countDown();
98+
}
99+
});
78100
frame.setVisible(true);
79101
});
80102

81-
robot.waitForIdle();
82-
robot.delay(1000);
103+
if (!frameActivated.await(2, SECONDS)) {
104+
throw new Error("Frame wasn't activated");
105+
}
106+
robot.delay(100);
83107

84108
testKey(KeyEvent.VK_F11, "F11");
85109
testKey(KeyEvent.VK_F12, "F12");
@@ -91,46 +115,69 @@ public static void main(String[] args) throws Exception {
91115
});
92116
}
93117

94-
if (failures.isEmpty()) {
95-
System.out.println("Passed");
96-
} else {
97-
throw new RuntimeException(failures.toString());
118+
if (!failures.isEmpty()) {
119+
System.err.println("Failures detected:");
120+
failures.forEach(System.err::println);
121+
if (edtException.get() != null) {
122+
System.err.println("\nException on EDT:");
123+
edtException.get().printStackTrace();
124+
}
125+
System.err.println();
126+
throw new RuntimeException("Test failed: " + failures.get(0).getMessage(),
127+
failures.get(0));
98128
}
99-
}
100-
}
101129

102-
class FunctionKeyTester extends Frame {
103-
Label l = new Label ("NULL");
104-
Button b = new Button();
105-
TextArea log = new TextArea();
106-
107-
FunctionKeyTester() {
108-
super("Function Key Test");
109-
this.setLayout(new BorderLayout());
110-
this.add(BorderLayout.NORTH, l);
111-
this.add(BorderLayout.SOUTH, b);
112-
this.add(BorderLayout.CENTER, log);
113-
log.setFocusable(false);
114-
log.setEditable(false);
115-
l.setBackground(Color.red);
116-
setSize(200, 200);
130+
if (edtException.get() != null) {
131+
throw new RuntimeException("Test failed because of exception on EDT",
132+
edtException.get());
133+
}
117134
}
118135

119-
public boolean handleEvent(Event e) {
120-
String message = "e.id=" + e.id + "\n";
121-
System.out.print(message);
122-
log.append(message);
123-
124-
switch (e.id) {
125-
case 403 -> FunctionKeyTest.keyPressReceived = true;
126-
case 404 -> FunctionKeyTest.keyReleaseReceived = true;
136+
private static final class FunctionKeyTester extends Frame {
137+
Label l = new Label ("NULL");
138+
Button b = new Button("button");
139+
TextArea log = new TextArea();
140+
141+
FunctionKeyTester() {
142+
super("Function Key Test");
143+
this.setLayout(new BorderLayout());
144+
this.add(BorderLayout.NORTH, l);
145+
this.add(BorderLayout.SOUTH, b);
146+
this.add(BorderLayout.CENTER, log);
147+
log.setFocusable(false);
148+
log.setEditable(false);
149+
l.setBackground(Color.red);
150+
setSize(200, 200);
127151
}
128152

129-
return super.handleEvent(e);
130-
}
153+
@Override
154+
@SuppressWarnings("deprecation")
155+
public boolean handleEvent(Event e) {
156+
String message = "e.id=" + e.id + "\n";
157+
System.out.print(message);
158+
log.append(message);
159+
160+
try {
161+
switch (e.id) {
162+
case KEY_ACTION
163+
-> keyPress.await();
164+
case KEY_ACTION_RELEASE
165+
-> keyRelease.await();
166+
}
167+
} catch (Exception ex) {
168+
if (!edtException.compareAndSet(null, ex)) {
169+
edtException.get().addSuppressed(ex);
170+
}
171+
}
131172

132-
public boolean keyDown(Event e, int key) {
133-
l.setText("e.key=" + Integer.valueOf(e.key).toString());
134-
return false;
173+
return super.handleEvent(e);
174+
}
175+
176+
@Override
177+
@SuppressWarnings("deprecation")
178+
public boolean keyDown(Event e, int key) {
179+
l.setText("e.key=" + e.key);
180+
return false;
181+
}
135182
}
136183
}

0 commit comments

Comments
 (0)
Please sign in to comment.