Skip to content

Commit

Permalink
FunctionKeyTest converted to automated
Browse files Browse the repository at this point in the history
  • Loading branch information
azvegint committed Sep 20, 2023
1 parent a9345d8 commit 69a125f
Showing 1 changed file with 56 additions and 31 deletions.
87 changes: 56 additions & 31 deletions test/jdk/java/awt/event/KeyEvent/FunctionKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,81 @@
import java.awt.Button;
import java.awt.Color;
import java.awt.Event;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Robot;
import java.awt.TextArea;
import javax.swing.SwingUtilities;
import java.awt.event.KeyEvent;

/*
* @test
* @bug 4011219
* @summary Test for function key press/release received by Java client.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual FunctionKeyTest
* @key headful
*/

public class FunctionKeyTest {
private static final String INSTRUCTIONS = """
Press and release function keys F11 and F12.
Look at the test window:
On KeyPress: 'e.id=403' is printed
On KeyRelease: 'e.id=404' is printed
If the above is true, then click Pass, else click Fail.
""";

private static FunctionKeyTester frame;
private static Robot robot;

public static void main(String[] args) throws Exception {
static volatile boolean keyPressReceived;
static volatile boolean keyReleaseReceived;

PassFailJFrame passFailJFrame = new PassFailJFrame.Builder()
.title("FunctionKeyTest Instructions Frame")
.instructions(INSTRUCTIONS)
.testTimeOut(5)
.rows(10)
.columns(45)
.build();
static final StringBuilder failures = new StringBuilder();

SwingUtilities.invokeAndWait(() -> {
frame = new FunctionKeyTester();
private static void testKey(int keyCode, String keyText) {
keyPressReceived = false;
keyReleaseReceived = false;

PassFailJFrame.addTestWindow(frame);
PassFailJFrame
.positionTestWindow(frame, PassFailJFrame.Position.HORIZONTAL);
robot.keyPress(keyCode);

frame.setVisible(true);
});
if (!keyPressReceived) {
failures.append(keyText).append(" key press is not received\n");
}

Thread.sleep(500);
robot.keyRelease(keyCode);

SwingUtilities.invokeAndWait(() -> frame.requestFocus());
if (!keyReleaseReceived) {
failures.append(keyText).append(" key release is not received\n");
}
}

passFailJFrame.awaitAndCheck();
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(150);

try {
EventQueue.invokeAndWait(() -> {
frame = new FunctionKeyTester();
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});

robot.waitForIdle();
robot.delay(1000);

testKey(KeyEvent.VK_F11, "F11");
testKey(KeyEvent.VK_F12, "F12");
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}

if (failures.isEmpty()) {
System.out.println("Passed");
} else {
throw new RuntimeException(failures.toString());
}
}
}

class FunctionKeyTester extends Frame {

Label l = new Label ("NULL");
Button b = new Button ();
TextArea log = new TextArea();
Expand All @@ -101,6 +121,11 @@ public boolean handleEvent(Event e) {
System.out.print(message);
log.append(message);

switch (e.id) {
case 403 -> FunctionKeyTest.keyPressReceived = true;
case 404 -> FunctionKeyTest.keyReleaseReceived = true;
}

return super.handleEvent(e);
}

Expand Down

0 comments on commit 69a125f

Please sign in to comment.