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
Reviewed-by: azvegint
  • Loading branch information
Harshitha Onkar committed Apr 20, 2023
1 parent 9412c0a commit d6cf4aa
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

3 comments on commit d6cf4aa

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@Rudometov
Copy link
Member

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d6cf4aa May 18, 2023

Choose a reason for hiding this comment

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

@Rudometov the backport was successfully created on the branch Rudometov-backport-d6cf4aa1 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d6cf4aa1 from the openjdk/jdk repository.

The commit being backported was authored by Harshitha Onkar on 20 Apr 2023 and was reviewed by Alexander Zvegintsev.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git Rudometov-backport-d6cf4aa1:Rudometov-backport-d6cf4aa1
$ git checkout Rudometov-backport-d6cf4aa1
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git Rudometov-backport-d6cf4aa1

Please sign in to comment.