Skip to content

Commit

Permalink
8306640: Open source several AWT TextArea related tests
Browse files Browse the repository at this point in the history
Backport-of: 169a7c27a7c6521fbfb1e61fbe85e6d7b0470619
  • Loading branch information
Andrew Lu committed Feb 22, 2024
1 parent 9724132 commit aeb6381
Show file tree
Hide file tree
Showing 4 changed files with 437 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test/jdk/java/awt/TextArea/ReplaceRangeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.
*/
/*
@test
@bug 5025532
@requires (os.family == "windows")
@summary Tests that textarea replaces text correctly if the text contains
line separators
@key headful
*/

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextArea;

import java.lang.reflect.InvocationTargetException;

public class ReplaceRangeTest {
static Frame f;

public static void main(String[] args) throws InterruptedException, InvocationTargetException {
try {
EventQueue.invokeAndWait(() -> {
f = new Frame("Test frame");
f.setSize(400, 400);
f.setLayout(new GridLayout(3, 1));

TextArea textArea1 = new TextArea(5, 80);
TextArea textArea2 = new TextArea(5, 80);
TextArea textArea3 = new TextArea(5, 80);
f.add(textArea1);
f.add(textArea2);
f.add(textArea3);
f.setVisible(true);

textArea1.setText("01234");
textArea1.replaceRange("X", 3, 4);
textArea2.setText("0\r\n234");
textArea2.replaceRange("X", 3, 4);
textArea3.setText("0\n\n34");
textArea3.replaceRange("X", 3, 4);

if (textArea1.getText().equals("012X4") &&
textArea2.getText().equals("0\r\n2X4") &&
textArea3.getText().equals("0\n\nX4")) {
System.out.println("Test Pass");
return;
} else {
throw new RuntimeException("Test FAILED");
}
});
} finally {
EventQueue.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}
}
}
155 changes: 155 additions & 0 deletions test/jdk/java/awt/TextArea/TextAreaCRLFAutoDetectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2003, 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.
*/
/*
@test
@bug 4800187
@requires (os.family == "windows")
@summary REGRESSION:show the wrong selection when there are \r characters in the text
@key headful
*/

import java.awt.Button;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.IllegalComponentStateException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextArea;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;

import java.lang.reflect.InvocationTargetException;

public class TextAreaCRLFAutoDetectTest {
Frame f;
TextArea ta1;
TextArea ta2;
Button b;
boolean passed = true;
boolean crlf = true;

public static void main(String[] args) throws Exception {
TextAreaCRLFAutoDetectTest crlfAutoDetectTest = new TextAreaCRLFAutoDetectTest();
crlfAutoDetectTest.init();
crlfAutoDetectTest.start();
}

public void init() throws InterruptedException, InvocationTargetException {
EventQueue.invokeAndWait(() -> {
f = new Frame("TextAreaCRLFAutoDetectTest");
ta1 = new TextArea(5, 20);
ta2 = new TextArea(5, 20);
b = new Button("Click Me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ta1.setText("");
ta2.setText("");
System.out.println("--------------------------------");

String eoln = (crlf) ? "\r\n" : "\n";
String s = eoln + "123" + eoln + "567" + eoln + "90" + eoln;
printString(" s=", s);
ta1.setText(s);
printString("ta1.getText()=", ta1.getText());

s = "67" + eoln + "9";
ta1.select(6, 10);

String s1 = ta1.getSelectedText();
printString("ta1.getSelectedText()=", s1);
passed = passed && s.equals(s1);

ta2.setText(s1);
printString(" ta2.getText()=", s1);
passed = passed && s1.equals(ta2.getText());

crlf = false;
}
});

f.setLayout(new FlowLayout());
f.add(ta1);
f.add(ta2);
f.add(b);
f.setLocation(300, 50);
f.pack();
f.setVisible(true);
});
}

public void start() throws Exception {
try {
Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(50);
robot.waitForIdle();

Point pt = new Point(0, 0);

boolean drawn = false;
while (!drawn) {
try {
pt = b.getLocationOnScreen();
} catch (IllegalComponentStateException icse) {
Thread.sleep(50);
continue;
}
drawn = true;
}

for (int i = 0; i < 2; i++) {
pt = b.getLocationOnScreen();
robot.mouseMove(pt.x + b.getWidth() / 2,
pt.y + b.getHeight() / 2);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(250);
}
if (!passed) {
throw new RuntimeException("TextAreaCRLFAutoDetectTest FAILED.");
} else {
System.out.println("TextAreaCRLFAutoDetectTest PASSED");
}
} catch (Exception e) {
throw new RuntimeException("The test was not completed.\n\n" + e);
} finally {
EventQueue.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}
}

void printString(String t, String s) {
byte b[] = s.getBytes();
String o = t;
for (int i = 0; i < b.length; i++) {
o += Byte.toString(b[i]) + " ";
}
System.out.println(o);
}
}
116 changes: 116 additions & 0 deletions test/jdk/java/awt/TextArea/TextLengthTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 1998, 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.
*/

/*
@test
@bug 4072264
@summary REGRESSION:Test to verify getSelectedText,
getSelectedStart/End in TextArea class
@key headful
*/

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.TextArea;

public class TextLengthTest {
static final int MY_SIZE = 100;
static final int MY_START = 13;
static final int MY_END = 47;
TextArea ta;
Frame f;
int mySize;
int myStart;
int myEnd;

public static void main(String[] args) throws Exception {
TextLengthTest textLengthTest = new TextLengthTest();
textLengthTest.init();
textLengthTest.start();
}

public void init() throws Exception {
EventQueue.invokeAndWait(() -> {
f = new Frame("TextLengthTest");
ta = new TextArea(15, 30);
f.add(ta);
f.setSize(400, 400);
f.setVisible(true);
});
}

public void start() throws Exception {
try {
Robot r = new Robot();
r.delay(1000);
r.waitForIdle();
EventQueue.invokeAndWait(() -> {
StringBuffer bigStringBuffer = new StringBuffer();

for (int i = 1; i <= 10; i++) {
bigStringBuffer.append("abcdefghi\n");
}

ta.setText(bigStringBuffer.toString());

mySize = bigStringBuffer.toString().length();
System.out.println("String size = " + mySize);

if (mySize != MY_SIZE) {
throw new Error("The string size is " +
mySize + "but it should be " + MY_SIZE);
}

ta.select(MY_START, MY_END);

String str = new String(ta.getSelectedText());
str = str.toUpperCase();

myStart = ta.getSelectionStart();
myEnd = ta.getSelectionEnd();
System.out.println("Selected string start = " + myStart);
System.out.println("Selected string end = " + myEnd);

if (myStart != MY_START) {
throw new Error("The selected text starts at " +
mySize + "but it should start at " + MY_START);
}

if (myEnd != MY_END) {
throw new Error("The selected text ends at " +
myEnd + "but it should end at " + MY_END);
}

ta.replaceRange(str, myStart, myEnd);
});
} finally {
EventQueue.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}
System.out.println("Test Pass");
}
}

1 comment on commit aeb6381

@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.