Skip to content

Commit

Permalink
JBR-6282: java/awt/TextArea/TextAreaEditing/TextAreaEditing.java inte…
Browse files Browse the repository at this point in the history
…rmittently fails due to deadlock.

Makes the test invoke any UI-operation on EDT only (since AWT doesn't guarantee thread-safety of UI operations, see more at https://mail.openjdk.org/pipermail/client-libs-dev/2023-November/016172.html).

(cherry picked from commit 8dbb889)
  • Loading branch information
NikitkoCent authored and vprovodin committed Mar 2, 2024
1 parent 37ee425 commit e92e4c3
Showing 1 changed file with 90 additions and 73 deletions.
163 changes: 90 additions & 73 deletions test/jdk/java/awt/TextArea/TextAreaEditing/TextAreaEditing.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,43 @@
import java.awt.Frame;
import java.awt.Robot;
import java.awt.TextArea;
import java.awt.AWTException;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;

import test.java.awt.regtesthelpers.Util;

import javax.swing.*;

public class TextAreaEditing {

final static Robot robot = Util.createRobot();
private int testFailCount;
private boolean isTestFail;
private StringBuilder testFailMessage;

private Frame mainFrame;
private TextArea textArea;

private TextAreaEditing() {
testFailMessage = new StringBuilder();
mainFrame = new Frame();
mainFrame.setSize(200, 200);

textArea = new TextArea();
mainFrame.add(textArea);
mainFrame.setVisible(true);
private volatile int testFailCount = 0;
private volatile boolean isTestFail = false;
private final StringBuilder testFailMessage = new StringBuilder();

private Frame mainFrame = null;
private TextArea textArea = null;

private TextAreaEditing() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
mainFrame = new Frame();
mainFrame.setSize(200, 200);

textArea = new TextArea();
mainFrame.add(textArea);
mainFrame.setVisible(true);
});
}

private void dispose() {
if (mainFrame != null) {
mainFrame.dispose();
}
private void dispose() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
if (mainFrame != null) {
mainFrame.dispose();
}
});
}

public static void main(String[] s) {
public static void main(String[] s) throws InterruptedException, InvocationTargetException {
TextAreaEditing textArea = new TextAreaEditing();
textArea.testReplaceRange();
textArea.testInsert();
Expand All @@ -74,79 +80,90 @@ public static void main(String[] s) {
textArea.dispose();
}

private void testReplaceRange() {
textArea.setText(null);
textArea.replaceRange("Replace", 0, 0);
textArea.setText(null);
checkTest("");
private void testReplaceRange() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
textArea.setText(null);
textArea.replaceRange("Replace", 0, 0);
textArea.setText(null);
checkTest("");

textArea.setText("SetText");
textArea.replaceRange("Replace", 0, 3);
checkTest("ReplaceText");
textArea.setText("SetText");
textArea.replaceRange("Replace", 0, 3);
checkTest("ReplaceText");

textArea.replaceRange("String", textArea.getText().length(),
textArea.getText().length());
checkTest("ReplaceTextString");
textArea.replaceRange("String", textArea.getText().length(),
textArea.getText().length());
checkTest("ReplaceTextString");

textArea.replaceRange("String", 0, 0);
checkTest("StringReplaceTextString");
textArea.replaceRange("String", 0, 0);
checkTest("StringReplaceTextString");

textArea.replaceRange("replaceRange", 0, textArea.getText().length());
checkTest("replaceRange");
textArea.replaceRange("replaceRange", 0, textArea.getText().length());
checkTest("replaceRange");
});
}

private void testInsert() {
textArea.setText(null);
textArea.insert("Insert", 0);
textArea.setText("");
checkTest("");
private void testInsert() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
textArea.setText(null);
textArea.insert("Insert", 0);
textArea.setText("");
checkTest("");

textArea.setText("SetText");
textArea.insert("Insert", 3);
checkTest("SetInsertText");
textArea.setText("SetText");
textArea.insert("Insert", 3);
checkTest("SetInsertText");

textArea.insert("Insert", 0);
checkTest("InsertSetInsertText");
textArea.insert("Insert", 0);
checkTest("InsertSetInsertText");

textArea.insert("Insert", textArea.getText().length());
checkTest("InsertSetInsertTextInsert");
textArea.insert("Insert", textArea.getText().length());
checkTest("InsertSetInsertTextInsert");
});
}

private void testAppend() {
textArea.setText(null);
textArea.append("Append");
textArea.setText(null);
checkTest("");

textArea.setText("SetText");
textArea.append("Append");
checkTest("SetTextAppend");

textArea.append("");
checkTest("SetTextAppend");
textArea.setText("");
checkTest("");
private void testAppend() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
textArea.setText(null);
textArea.append("Append");
textArea.setText(null);
checkTest("");

textArea.setText("SetText");
textArea.append("Append");
checkTest("SetTextAppend");

textArea.append("");
checkTest("SetTextAppend");
textArea.setText("");
checkTest("");
});
}

private void testSetText() {
textArea.setText(null);
textArea.requestFocus();
private void testSetText() throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(() -> {
textArea.setText(null);
textArea.requestFocus();
});
Util.clickOnComp(textArea, robot);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_A);
robot.delay(5);
robot.keyRelease(KeyEvent.VK_A);
Util.waitForIdle(robot);
textArea.setText(null);
checkTest("");
textArea.setText("CaseSensitive");
checkTest("CaseSensitive");
textArea.setText("caseSensitive");
checkTest("caseSensitive");

SwingUtilities.invokeAndWait(() -> {
textArea.setText(null);
checkTest("");
textArea.setText("CaseSensitive");
checkTest("CaseSensitive");
textArea.setText("caseSensitive");
checkTest("caseSensitive");
});
}

private void checkTest(String str) {
assert SwingUtilities.isEventDispatchThread();

if (str != null && !str.equals(textArea.getText())) {
testFailMessage.append("TestFail line : ");
testFailMessage.append(Thread.currentThread().getStackTrace()[2].
Expand All @@ -161,7 +178,7 @@ private void checkTest(String str) {
}
}

private void checkFailures() {
private void checkFailures() throws InterruptedException, InvocationTargetException {
if (isTestFail) {
testFailMessage.insert(0, "Test Fail count : " + testFailCount
+ System.getProperty("line.separator"));
Expand Down

0 comments on commit e92e4c3

Please sign in to comment.