Skip to content

Commit

Permalink
8305874: Open source AWT Key, Text Event related tests
Browse files Browse the repository at this point in the history
Backport-of: d6cf4aa1551df591c7bc75cb8c5e90d57630ca2a
  • Loading branch information
Amos Shi committed Apr 23, 2024
1 parent 94caa64 commit c3438a5
Show file tree
Hide file tree
Showing 5 changed files with 547 additions and 0 deletions.
143 changes: 143 additions & 0 deletions test/jdk/java/awt/event/KeyEvent/KeyTyped/DeleteKeyTyped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/*
* @test
* @bug 4724007
* @key headful
* @summary Tests that KeyTyped events are fired for the Delete key
* and that no extraneous characters are entered as a result.
*/

public class DeleteKeyTyped {
private static Frame frame;
private static TextField tf;

private static boolean deleteKeyTypedReceived = false;
private static final String ORIGINAL = "0123456789";
private static final String SUCCESS = "123456789";

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

EventQueue.invokeAndWait(DeleteKeyTyped::createTestUI);
robot.waitForIdle();
robot.delay(1000);

// Move cursor to start of TextField
robot.keyPress(KeyEvent.VK_HOME);
robot.keyRelease(KeyEvent.VK_HOME);
robot.waitForIdle();
robot.delay(50);

// Press and release Delete
robot.keyPress(KeyEvent.VK_DELETE);
robot.keyRelease(KeyEvent.VK_DELETE);
robot.waitForIdle();
robot.delay(50);

EventQueue.invokeAndWait(DeleteKeyTyped::testDeleteKeyEvent);
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}

private static void createTestUI() {
frame = new Frame();
tf = new TextField(ORIGINAL, 20);
frame.add(tf);
frame.setSize(300, 100);
frame.setVisible(true);
tf.requestFocusInWindow();

tf.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent evt) {
printKey(evt);
}

@Override
public void keyTyped(KeyEvent evt) {
printKey(evt);
int keychar = evt.getKeyChar();
if (keychar == 127) { // Delete character is 127 or \u007F
deleteKeyTypedReceived = true;
}
}

@Override
public void keyReleased(KeyEvent evt) {
printKey(evt);
}

private void printKey(KeyEvent evt) {
switch(evt.getID()) {
case KeyEvent.KEY_TYPED:
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
break;
default:
System.out.println("Other Event");
return;
}

System.out.println("params= " + evt.paramString() + " \n" +
"KeyChar: " + evt.getKeyChar() + " = " + (int) evt.getKeyChar() +
" KeyCode: " + evt.getKeyCode() +
" Modifiers: " + evt.getModifiersEx());

if (evt.isActionKey()) {
System.out.println("Action Key");
}

System.out.println("keyText= " + KeyEvent.getKeyText(evt.getKeyCode()) + "\n");
}
});
}

private static void testDeleteKeyEvent() {
if (deleteKeyTypedReceived) {
if (tf.getText().equals(SUCCESS)) {
System.out.println("Test PASSED");
} else {
System.out.println("Test FAILED: wrong string");
throw new RuntimeException("The test failed: wrong string: " +
tf.getText());
}
}
}
}
136 changes: 136 additions & 0 deletions test/jdk/java/awt/event/KeyEvent/KeyTyped/EscapeKeyTyped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/*
* @test
* @key headful
* @bug 4734408
* @summary Tests that KeyTyped events are fired for the Escape key
* and that no extraneous characters are entered as a result.
*/

public class EscapeKeyTyped {
private static Frame frame;
private static TextField tf;

private static final String ORIGINAL = "0123456789";
private static boolean escapeKeyTypedReceived = false;

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

EventQueue.invokeAndWait(EscapeKeyTyped::createAndShowUI);
robot.waitForIdle();
robot.delay(1000);

// Press and release Escape
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
robot.waitForIdle();
robot.delay(20);

EventQueue.invokeAndWait(EscapeKeyTyped::testEscKeyEvent);
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}

private static void createAndShowUI() {
frame = new Frame();
tf = new TextField(ORIGINAL, 20);
frame.add(tf);
frame.setSize(300, 100);
frame.setVisible(true);
tf.requestFocusInWindow();

tf.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
printKey(e);
}

@Override
public void keyPressed(KeyEvent e) {
printKey(e);
int keychar = e.getKeyChar();
if (keychar == 27) { // Escape character is 27 or \u0021
escapeKeyTypedReceived = true;
}
}

@Override
public void keyReleased(KeyEvent e) {
printKey(e);
}

private void printKey(KeyEvent evt) {
switch (evt.getID()) {
case KeyEvent.KEY_TYPED:
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
break;
default:
System.out.println("Other Event");
return;
}

System.out.println("params= " + evt.paramString() + " \n" +
"KeyChar: " + evt.getKeyChar() + " = " + (int) evt.getKeyChar() +
" KeyCode: " + evt.getKeyCode() +
" Modifiers: " + evt.getModifiersEx());

if (evt.isActionKey()) {
System.out.println("Action Key");
}

System.out.println("keyText= " + KeyEvent.getKeyText(evt.getKeyCode()) + "\n");
}
});
}

private static void testEscKeyEvent() {
if (escapeKeyTypedReceived) {
if (tf.getText().equals(ORIGINAL)) {
System.out.println("Test PASSED");
} else {
System.out.println("Test FAILED: wrong string");
throw new RuntimeException("The test failed: wrong string: " +
tf.getText());
}
}
}
}
68 changes: 68 additions & 0 deletions test/jdk/java/awt/event/KeyEvent/ShiftF10Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.event.KeyEvent;

/*
* @test
* @key headful
* @bug 4965227
* @requires (os.family == "linux")
* @summary tests that Shift+F10 during Window show doesn't cause deadlock- Linux only
*/

public class ShiftF10Test {
private static Frame frame;

public static void main(String[] args) throws Exception {
try {
Robot robot = new Robot();
robot.setAutoDelay(10);

EventQueue.invokeLater(() -> {
frame = new Frame("Deadlocking one");
frame.setSize(100, 100);
frame.setVisible(true);
});

for (int i = 0; i < 250; i++) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_F10);
robot.keyRelease(KeyEvent.VK_F10);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.delay(10);
}
} catch (Exception e) {
throw new RuntimeException("Test Failed due to following error: ", e);
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
}
Loading

1 comment on commit c3438a5

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.