Skip to content

Commit cd3c64f

Browse files
Andrew LuGoeLin
authored andcommitted
8306430: Open source some AWT tests related to TextComponent and Toolkit
Backport-of: 36ec05d52a79185d8c6669713fd17933128c032a
1 parent ce1602d commit cd3c64f

File tree

5 files changed

+562
-0
lines changed

5 files changed

+562
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
/*
25+
@test
26+
@bug 4257143
27+
@summary RFE: Cannot set some AWT properties until peer has been created
28+
@key headful
29+
*/
30+
31+
import java.awt.EventQueue;
32+
import java.awt.TextArea;
33+
import java.awt.TextField;
34+
35+
public class PeerlessSetCaret {
36+
public static void main(String[] args) throws Exception {
37+
EventQueue.invokeAndWait(() -> {
38+
TextField tf = new TextField("Hello, World!");
39+
TextArea ta = new TextArea("Hello, World!");
40+
41+
// without the fix these will throw IllegalComponentStateException
42+
tf.setCaretPosition(1);
43+
ta.setCaretPosition(1);
44+
});
45+
}
46+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 1999, 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 4118247
27+
@summary Make sure bounds are enforced correctly on
28+
TextComponent.Select(int, int)
29+
@key headful
30+
*/
31+
32+
import java.awt.EventQueue;
33+
import java.awt.TextArea;
34+
import java.awt.TextComponent;
35+
36+
public class SelectionBounds {
37+
public static TextComponent tc;
38+
39+
public static int[][] index = {
40+
{0, 0}, // 0 = selectionStart = selectionEnd
41+
{5, 5}, // selectionStart = selectionEnd
42+
{5, 7}, // 0 < selectionStart < selectionEnd < textLength
43+
{-50, 7}, // selectionStart < 0 < selectionEnd < textLength
44+
{-50, 50}, // selectionStart < 0 < textLength < selectionEnd
45+
{5, 50}, // 0 < selectionStart < textLength < selectionEnd
46+
{40, 50}, // 0 < textLength < selectionStart < selectionEnd
47+
{-50, -40}, // selectionStart < selectionEnd < 0 < textLength
48+
{7, 5}, // 0 < selectionEnd < selectionStart < textLength
49+
{7, -50}, // selectionEnd < 0 < selectionStart < textLength
50+
{50, -50}, // selectionEnd < 0 < textLength < selectionStart
51+
{50, 5}, // 0 < selectionEnd < textLength < selectionStart
52+
{50, 40}, // 0 < textLength < selectionEnd < selectionStart
53+
{-40, -50} // selectionEnd < selectionStart < 0 < textLength
54+
};
55+
56+
public static String[] selections = {
57+
"",
58+
"",
59+
"56",
60+
"0123456",
61+
"0123456789",
62+
"56789",
63+
"",
64+
"",
65+
"",
66+
"",
67+
"",
68+
"",
69+
"",
70+
""
71+
};
72+
73+
74+
public static void main(String[] args) throws Exception {
75+
EventQueue.invokeAndWait(() -> {
76+
tc = new TextArea("0123456789");
77+
runTheTest();
78+
});
79+
}
80+
81+
private static void runTheTest() {
82+
int i;
83+
String str1;
84+
85+
for (i=0; i<index.length; i++) {
86+
tc.select(index[i][0], index[i][1]);
87+
str1 = tc.getSelectedText();
88+
89+
if (!str1.equals(selections[i])) {
90+
System.out.println("Test " + i + " FAILED: " + str1 +
91+
" != " + selections[i]);
92+
System.out.println("Test " + i + " FAILED: " + str1 +
93+
" != " + selections[i]);
94+
throw new RuntimeException("Test " + i + " FAILED: " + str1 +
95+
" != " + selections[i]);
96+
}
97+
else {
98+
System.out.println("Test " + i + " PASSED: " + str1 +
99+
" = " + selections[i]);
100+
System.out.println("Test " + i + " PASSED: " + str1 +
101+
" = " + selections[i]);
102+
}
103+
}
104+
105+
System.out.println("\nAll tests PASSED.");
106+
}
107+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright (c) 2002, 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 4701398 4652358 4659958 4697796 4666876
27+
@summary REGRESSION: TextArea.append does not work consistently with \r.
28+
@key headful
29+
*/
30+
31+
import java.awt.BorderLayout;
32+
import java.awt.Color;
33+
import java.awt.Dialog;
34+
import java.awt.EventQueue;
35+
import java.awt.Font;
36+
import java.awt.Frame;
37+
import java.awt.Panel;
38+
import java.awt.TextArea;
39+
40+
public class TextAreaCRLFTest {
41+
private static final char[] DIGITS = {
42+
'0', '1', '2', '3', '4', '5', '6', '7',
43+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
44+
};
45+
46+
public static Dialog aDialog;
47+
public static TextArea area;
48+
public static boolean passed = true;
49+
public static boolean res;
50+
public static String atext = "";
51+
52+
public static void main(String[] args) throws Exception {
53+
String atextCRLF = "row1\r\nrow2\r\nrow3";
54+
55+
try {
56+
EventQueue.invokeAndWait(() -> {
57+
aDialog = new Dialog(new Frame());
58+
aDialog.setTitle("ADialog");
59+
aDialog.setBackground(Color.lightGray);
60+
aDialog.setLayout(new BorderLayout());
61+
Panel mainPanel = new Panel();
62+
mainPanel.setLayout(new BorderLayout(6, 6));
63+
area = new TextArea(atextCRLF, 25, 68,
64+
TextArea.SCROLLBARS_VERTICAL_ONLY);
65+
area.setFont(new Font("Monospaced", Font.PLAIN, 11));
66+
mainPanel.add(area, "Center");
67+
aDialog.add(mainPanel, "Center");
68+
aDialog.pack();
69+
System.out.println("before: "+hexEncode(atextCRLF));
70+
System.out.println(" after: "+hexEncode(area.getText()));
71+
res = area.getText().equals(atextCRLF);
72+
System.out.println("01: " + res + "\n");
73+
passed = passed && res;
74+
area.setText(atextCRLF);
75+
System.out.println("before: "+hexEncode(atextCRLF));
76+
System.out.println(" after: "+hexEncode(area.getText()));
77+
res = area.getText().equals(atextCRLF);
78+
System.out.println("02: " + res + "\n");
79+
passed = passed && res;
80+
81+
area.setText("");
82+
atext = "row1";
83+
area.append(atext+"\r");
84+
area.append(atext+"\r");
85+
System.out.println("before: "
86+
+hexEncode(atext+"\r" + atext+"\r"));
87+
System.out.println(" after: "+hexEncode(area.getText()));
88+
res = area.getText().equals(atext + atext);
89+
System.out.println("03: " + res + "\n");
90+
passed = passed && res;
91+
92+
area.setText("");
93+
String atext1 = "fine.";
94+
String atext2 = "messed up.";
95+
atext = atext1 +"\r\n"+ atext2;
96+
for (int i = 0; i < atext.length(); i++) {
97+
area.append(atext.substring(i, i+1));
98+
}
99+
System.out.println("before: "
100+
+hexEncode(atext1 +"\r\n"+ atext2));
101+
System.out.println(" after: "+hexEncode(area.getText()));
102+
String s = area.getText();
103+
String t = s.substring(s.length()-atext2.length());
104+
res = t.equals(atext2);
105+
System.out.println("04: " + res);
106+
passed = passed && res;
107+
108+
area.setText("");
109+
atext = "\r";
110+
area.append(atext);
111+
System.out.println("before: "+hexEncode(atext));
112+
System.out.println(" after: "+hexEncode(area.getText()));
113+
res = area.getText().equals("");
114+
System.out.println("05: " + res + "\n");
115+
passed = passed && res;
116+
117+
if (System.getProperty("os.name").toUpperCase().
118+
startsWith("WIN")) {
119+
if (!passed) {
120+
throw new RuntimeException("TextAreaCRLFTest FAILED.");
121+
} else {
122+
System.out.println("TextAreaCRLFTest PASSED");
123+
}
124+
} else {
125+
System.out.println("This is a Windows oriented testcase.");
126+
}
127+
});
128+
} finally {
129+
EventQueue.invokeAndWait(() -> {
130+
if (aDialog != null) {
131+
aDialog.dispose();
132+
}
133+
});
134+
}
135+
}
136+
137+
private static String hexEncode(String str) {
138+
return hexEncode(str.getBytes());
139+
}
140+
141+
private static String hexEncode(byte[] bytes) {
142+
StringBuffer buffer = new StringBuffer(bytes.length * 2);
143+
for (int i = 0; i < bytes.length; i++) {
144+
byte b = bytes[i];
145+
buffer.append(DIGITS[(b & 0xF0) >> 4]);
146+
buffer.append(DIGITS[b & 0x0F]);
147+
}
148+
return buffer.toString();
149+
}
150+
}

0 commit comments

Comments
 (0)