Skip to content

Commit 6033871

Browse files
Victor Rudometovshipilev
Victor Rudometov
authored andcommitted
8306640: Open source several AWT TextArea related tests
Backport-of: 169a7c27a7c6521fbfb1e61fbe85e6d7b0470619
1 parent 05b2ff4 commit 6033871

File tree

4 files changed

+437
-0
lines changed

4 files changed

+437
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
@test
25+
@bug 5025532
26+
@requires (os.family == "windows")
27+
@summary Tests that textarea replaces text correctly if the text contains
28+
line separators
29+
@key headful
30+
*/
31+
32+
import java.awt.EventQueue;
33+
import java.awt.Frame;
34+
import java.awt.GridLayout;
35+
import java.awt.TextArea;
36+
37+
import java.lang.reflect.InvocationTargetException;
38+
39+
public class ReplaceRangeTest {
40+
static Frame f;
41+
42+
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
43+
try {
44+
EventQueue.invokeAndWait(() -> {
45+
f = new Frame("Test frame");
46+
f.setSize(400, 400);
47+
f.setLayout(new GridLayout(3, 1));
48+
49+
TextArea textArea1 = new TextArea(5, 80);
50+
TextArea textArea2 = new TextArea(5, 80);
51+
TextArea textArea3 = new TextArea(5, 80);
52+
f.add(textArea1);
53+
f.add(textArea2);
54+
f.add(textArea3);
55+
f.setVisible(true);
56+
57+
textArea1.setText("01234");
58+
textArea1.replaceRange("X", 3, 4);
59+
textArea2.setText("0\r\n234");
60+
textArea2.replaceRange("X", 3, 4);
61+
textArea3.setText("0\n\n34");
62+
textArea3.replaceRange("X", 3, 4);
63+
64+
if (textArea1.getText().equals("012X4") &&
65+
textArea2.getText().equals("0\r\n2X4") &&
66+
textArea3.getText().equals("0\n\nX4")) {
67+
System.out.println("Test Pass");
68+
return;
69+
} else {
70+
throw new RuntimeException("Test FAILED");
71+
}
72+
});
73+
} finally {
74+
EventQueue.invokeAndWait(() -> {
75+
if (f != null) {
76+
f.dispose();
77+
}
78+
});
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
@test
25+
@bug 4800187
26+
@requires (os.family == "windows")
27+
@summary REGRESSION:show the wrong selection when there are \r characters in the text
28+
@key headful
29+
*/
30+
31+
import java.awt.Button;
32+
import java.awt.EventQueue;
33+
import java.awt.FlowLayout;
34+
import java.awt.Frame;
35+
import java.awt.IllegalComponentStateException;
36+
import java.awt.Point;
37+
import java.awt.Robot;
38+
import java.awt.TextArea;
39+
40+
import java.awt.event.ActionEvent;
41+
import java.awt.event.ActionListener;
42+
import java.awt.event.InputEvent;
43+
44+
import java.lang.reflect.InvocationTargetException;
45+
46+
public class TextAreaCRLFAutoDetectTest {
47+
Frame f;
48+
TextArea ta1;
49+
TextArea ta2;
50+
Button b;
51+
boolean passed = true;
52+
boolean crlf = true;
53+
54+
public static void main(String[] args) throws Exception {
55+
TextAreaCRLFAutoDetectTest crlfAutoDetectTest = new TextAreaCRLFAutoDetectTest();
56+
crlfAutoDetectTest.init();
57+
crlfAutoDetectTest.start();
58+
}
59+
60+
public void init() throws InterruptedException, InvocationTargetException {
61+
EventQueue.invokeAndWait(() -> {
62+
f = new Frame("TextAreaCRLFAutoDetectTest");
63+
ta1 = new TextArea(5, 20);
64+
ta2 = new TextArea(5, 20);
65+
b = new Button("Click Me");
66+
b.addActionListener(new ActionListener() {
67+
public void actionPerformed(ActionEvent evt) {
68+
ta1.setText("");
69+
ta2.setText("");
70+
System.out.println("--------------------------------");
71+
72+
String eoln = (crlf) ? "\r\n" : "\n";
73+
String s = eoln + "123" + eoln + "567" + eoln + "90" + eoln;
74+
printString(" s=", s);
75+
ta1.setText(s);
76+
printString("ta1.getText()=", ta1.getText());
77+
78+
s = "67" + eoln + "9";
79+
ta1.select(6, 10);
80+
81+
String s1 = ta1.getSelectedText();
82+
printString("ta1.getSelectedText()=", s1);
83+
passed = passed && s.equals(s1);
84+
85+
ta2.setText(s1);
86+
printString(" ta2.getText()=", s1);
87+
passed = passed && s1.equals(ta2.getText());
88+
89+
crlf = false;
90+
}
91+
});
92+
93+
f.setLayout(new FlowLayout());
94+
f.add(ta1);
95+
f.add(ta2);
96+
f.add(b);
97+
f.setLocation(300, 50);
98+
f.pack();
99+
f.setVisible(true);
100+
});
101+
}
102+
103+
public void start() throws Exception {
104+
try {
105+
Robot robot = new Robot();
106+
robot.setAutoWaitForIdle(true);
107+
robot.setAutoDelay(50);
108+
robot.waitForIdle();
109+
110+
Point pt = new Point(0, 0);
111+
112+
boolean drawn = false;
113+
while (!drawn) {
114+
try {
115+
pt = b.getLocationOnScreen();
116+
} catch (IllegalComponentStateException icse) {
117+
Thread.sleep(50);
118+
continue;
119+
}
120+
drawn = true;
121+
}
122+
123+
for (int i = 0; i < 2; i++) {
124+
pt = b.getLocationOnScreen();
125+
robot.mouseMove(pt.x + b.getWidth() / 2,
126+
pt.y + b.getHeight() / 2);
127+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
128+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
129+
Thread.sleep(250);
130+
}
131+
if (!passed) {
132+
throw new RuntimeException("TextAreaCRLFAutoDetectTest FAILED.");
133+
} else {
134+
System.out.println("TextAreaCRLFAutoDetectTest PASSED");
135+
}
136+
} catch (Exception e) {
137+
throw new RuntimeException("The test was not completed.\n\n" + e);
138+
} finally {
139+
EventQueue.invokeAndWait(() -> {
140+
if (f != null) {
141+
f.dispose();
142+
}
143+
});
144+
}
145+
}
146+
147+
void printString(String t, String s) {
148+
byte b[] = s.getBytes();
149+
String o = t;
150+
for (int i = 0; i < b.length; i++) {
151+
o += Byte.toString(b[i]) + " ";
152+
}
153+
System.out.println(o);
154+
}
155+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 4072264
27+
@summary REGRESSION:Test to verify getSelectedText,
28+
getSelectedStart/End in TextArea class
29+
@key headful
30+
*/
31+
32+
import java.awt.EventQueue;
33+
import java.awt.Frame;
34+
import java.awt.Robot;
35+
import java.awt.TextArea;
36+
37+
public class TextLengthTest {
38+
static final int MY_SIZE = 100;
39+
static final int MY_START = 13;
40+
static final int MY_END = 47;
41+
TextArea ta;
42+
Frame f;
43+
int mySize;
44+
int myStart;
45+
int myEnd;
46+
47+
public static void main(String[] args) throws Exception {
48+
TextLengthTest textLengthTest = new TextLengthTest();
49+
textLengthTest.init();
50+
textLengthTest.start();
51+
}
52+
53+
public void init() throws Exception {
54+
EventQueue.invokeAndWait(() -> {
55+
f = new Frame("TextLengthTest");
56+
ta = new TextArea(15, 30);
57+
f.add(ta);
58+
f.setSize(400, 400);
59+
f.setVisible(true);
60+
});
61+
}
62+
63+
public void start() throws Exception {
64+
try {
65+
Robot r = new Robot();
66+
r.delay(1000);
67+
r.waitForIdle();
68+
EventQueue.invokeAndWait(() -> {
69+
StringBuffer bigStringBuffer = new StringBuffer();
70+
71+
for (int i = 1; i <= 10; i++) {
72+
bigStringBuffer.append("abcdefghi\n");
73+
}
74+
75+
ta.setText(bigStringBuffer.toString());
76+
77+
mySize = bigStringBuffer.toString().length();
78+
System.out.println("String size = " + mySize);
79+
80+
if (mySize != MY_SIZE) {
81+
throw new Error("The string size is " +
82+
mySize + "but it should be " + MY_SIZE);
83+
}
84+
85+
ta.select(MY_START, MY_END);
86+
87+
String str = new String(ta.getSelectedText());
88+
str = str.toUpperCase();
89+
90+
myStart = ta.getSelectionStart();
91+
myEnd = ta.getSelectionEnd();
92+
System.out.println("Selected string start = " + myStart);
93+
System.out.println("Selected string end = " + myEnd);
94+
95+
if (myStart != MY_START) {
96+
throw new Error("The selected text starts at " +
97+
mySize + "but it should start at " + MY_START);
98+
}
99+
100+
if (myEnd != MY_END) {
101+
throw new Error("The selected text ends at " +
102+
myEnd + "but it should end at " + MY_END);
103+
}
104+
105+
ta.replaceRange(str, myStart, myEnd);
106+
});
107+
} finally {
108+
EventQueue.invokeAndWait(() -> {
109+
if (f != null) {
110+
f.dispose();
111+
}
112+
});
113+
}
114+
System.out.println("Test Pass");
115+
}
116+
}

0 commit comments

Comments
 (0)