Skip to content

Commit 32ae0fc

Browse files
committed
8284077: Create an automated test for JDK-4170173
Backport-of: 6db2e16b948ccb78839285051e136b8a023b2f7b
1 parent 2370a45 commit 32ae0fc

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2010, 2022, 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+
* @key headful
27+
* @bug 4170173
28+
* @summary AccessibleJTextComponent.getAfterIndex works incorrectly
29+
* @run main AccessibleJTextAfterIndexTest
30+
*/
31+
32+
import javax.accessibility.AccessibleText;
33+
import javax.swing.JEditorPane;
34+
import javax.swing.JTextArea;
35+
import javax.swing.JTextField;
36+
import javax.swing.SwingUtilities;
37+
38+
public class AccessibleJTextAfterIndexTest {
39+
40+
public static void doTest() {
41+
JTextField jTextField =
42+
new JTextField("Test1 Test2 Test3. Test4 Test5. Test6");
43+
JTextArea jTextArea = new JTextArea("Test1 Test2 Test3.\nTest4 Test5");
44+
JEditorPane jEditorPane =
45+
new JEditorPane("text/plain", "Test1 Test2 Test3.\nTest4 Test5");
46+
47+
String actualAccessibleText = jTextField.getAccessibleContext()
48+
.getAccessibleText().getAfterIndex(AccessibleText.CHARACTER, 5);
49+
if (!(actualAccessibleText.equals("T"))) {
50+
throw new RuntimeException(
51+
"JTextField -" + "getAfterIndex() CHARACTER parameter"
52+
+ " expected:--T--, actual:--" + actualAccessibleText + "--");
53+
}
54+
55+
actualAccessibleText = jTextField.getAccessibleContext()
56+
.getAccessibleText().getAfterIndex(AccessibleText.WORD, 5);
57+
if (!(actualAccessibleText.equals("Test2"))) {
58+
throw new RuntimeException(
59+
"JTextField - " + "getAfterIndex() WORD parameter"
60+
+ " expected:--Test2--, actual:--" + actualAccessibleText + "--");
61+
}
62+
63+
actualAccessibleText = jTextField.getAccessibleContext()
64+
.getAccessibleText().getAfterIndex(AccessibleText.SENTENCE, 5);
65+
if (!(actualAccessibleText.equals("Test4 Test5. "))) {
66+
throw new RuntimeException("JTextField - "
67+
+ "getAfterIndex() SENTENCE parameter"
68+
+ " expected:--Test4 Test5. --, actual:--" + actualAccessibleText + "--");
69+
}
70+
71+
actualAccessibleText = jTextArea.getAccessibleContext()
72+
.getAccessibleText().getAfterIndex(AccessibleText.CHARACTER, 5);
73+
if (!(actualAccessibleText.equals("T"))) {
74+
throw new RuntimeException(
75+
"JTextArea - " + "getAfterIndex() CHARACTER parameter"
76+
+ " expected:--T--, actual:--" + actualAccessibleText + "--");
77+
}
78+
79+
actualAccessibleText = jTextArea.getAccessibleContext()
80+
.getAccessibleText().getAfterIndex(AccessibleText.WORD, 5);
81+
if (!(actualAccessibleText.equals("Test2"))) {
82+
throw new RuntimeException(
83+
"JTextArea - " + "getAfterIndex() WORD parameter"
84+
+ " expected:--Test2--, actual:--" + actualAccessibleText + "--");
85+
}
86+
87+
actualAccessibleText = jTextArea.getAccessibleContext()
88+
.getAccessibleText().getAfterIndex(AccessibleText.SENTENCE, 5);
89+
if (!(actualAccessibleText.equals("Test4 Test5\n"))) {
90+
throw new RuntimeException("JTextArea - "
91+
+ "getAfterIndex() SENTENCE parameter"
92+
+ " expected:--Test4 Test5\n--, actual:--" + actualAccessibleText + "--");
93+
}
94+
95+
actualAccessibleText = jEditorPane.getAccessibleContext()
96+
.getAccessibleText().getAfterIndex(AccessibleText.CHARACTER, 5);
97+
if (!(actualAccessibleText.equals("T"))) {
98+
throw new RuntimeException(
99+
"JEditorPane - " + "getAfterIndex() CHARACTER parameter"
100+
+ " expected:--T--, actual:--" + actualAccessibleText +"--");
101+
}
102+
103+
actualAccessibleText = jEditorPane.getAccessibleContext()
104+
.getAccessibleText().getAfterIndex(AccessibleText.WORD, 5);
105+
if (!(actualAccessibleText.equals("Test2"))) {
106+
throw new RuntimeException(
107+
"JEditorPane - " + "getAfterIndex() WORD parameter"
108+
+ " expected:--Test2--, actual:--" + actualAccessibleText + "--");
109+
}
110+
111+
actualAccessibleText = jEditorPane.getAccessibleContext()
112+
.getAccessibleText().getAfterIndex(AccessibleText.SENTENCE, 5);
113+
if (!(actualAccessibleText.equals("Test4 Test5\n"))) {
114+
throw new RuntimeException("JEditorPane - "
115+
+ "getAfterIndex() Sentence parameter"
116+
+ " expected:--Test4 Test5\n--, actual:--" + actualAccessibleText +"--");
117+
}
118+
}
119+
120+
public static void main(String[] args) throws Exception {
121+
SwingUtilities.invokeAndWait(() -> doTest());
122+
System.out.println("Test Passed");
123+
}
124+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2010, 2022, 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+
* @key headful
27+
* @bug 4170173
28+
* @summary AccessibleJTextComponent.getBeforeIndex works incorrectly
29+
* @run main AccessibleJTextBeforeIndexTest
30+
*/
31+
32+
import javax.accessibility.AccessibleText;
33+
import javax.swing.JEditorPane;
34+
import javax.swing.JTextArea;
35+
import javax.swing.JTextField;
36+
import javax.swing.SwingUtilities;
37+
38+
public class AccessibleJTextBeforeIndexTest {
39+
40+
public static void doTest() {
41+
JTextField jTextField =
42+
new JTextField("Test1 Test2 Test3. Test4 Test5. Test6");
43+
JTextArea jTextArea = new JTextArea("Test1 Test2 Test3.\nTest4 Test5");
44+
JEditorPane jEditorPane =
45+
new JEditorPane("text/plain", "Test1 Test2 Test3.\nTest4 Test5");
46+
47+
String actualAccessibleText = jTextField.getAccessibleContext()
48+
.getAccessibleText().getBeforeIndex(AccessibleText.CHARACTER, 5);
49+
if (!(actualAccessibleText.equals("1"))) {
50+
throw new RuntimeException(
51+
"JTextField -" + "getBeforeIndex() CHARACTER parameter"
52+
+ " expected:--1--, actual:--" + actualAccessibleText + "--");
53+
}
54+
55+
actualAccessibleText = jTextField.getAccessibleContext()
56+
.getAccessibleText().getBeforeIndex(AccessibleText.WORD, 5);
57+
if (!(actualAccessibleText.equals("Test1"))) {
58+
throw new RuntimeException(
59+
"JTextField -" + "getBeforeIndex() WORD parameter"
60+
+ " expected:--Test1--, actual:--" + actualAccessibleText + "--");
61+
}
62+
63+
actualAccessibleText = jTextField.getAccessibleContext()
64+
.getAccessibleText().getBeforeIndex(AccessibleText.SENTENCE, 20);
65+
if (!(actualAccessibleText.equals("Test1 Test2 Test3. "))) {
66+
throw new RuntimeException(
67+
"JTextField -" + "getBeforeIndex() SENTENCE parameter"
68+
+ " expected:--Test1 Test2 Test3. --, actual:--"
69+
+ actualAccessibleText + "--");
70+
}
71+
72+
actualAccessibleText = jTextArea.getAccessibleContext()
73+
.getAccessibleText().getBeforeIndex(AccessibleText.CHARACTER, 5);
74+
if (!(actualAccessibleText.equals("1"))) {
75+
throw new RuntimeException(
76+
"JTextArea -" + "getBeforeIndex() CHARACTER parameter"
77+
+ " expected:--1--, actual:--" + actualAccessibleText + "--");
78+
}
79+
80+
actualAccessibleText = jTextArea.getAccessibleContext()
81+
.getAccessibleText().getBeforeIndex(AccessibleText.WORD, 5);
82+
if (!(actualAccessibleText.equals("Test1"))) {
83+
throw new RuntimeException("JTextArea -"
84+
+ "getBeforeIndex() WORD parameter"
85+
+ " expected:--Test1--, actual:--" + actualAccessibleText + "--");
86+
}
87+
88+
actualAccessibleText = jTextArea.getAccessibleContext()
89+
.getAccessibleText().getBeforeIndex(AccessibleText.SENTENCE, 20);
90+
if (!(actualAccessibleText.equals("Test1 Test2 Test3.\n"))) {
91+
throw new RuntimeException(
92+
"JTextArea -" + "getBeforeIndex() SENTENCE parameter"
93+
+ " expected: Test1 Test2 Test3.\n--, actual:--"
94+
+ actualAccessibleText + "--");
95+
}
96+
97+
actualAccessibleText = jEditorPane.getAccessibleContext()
98+
.getAccessibleText().getBeforeIndex(AccessibleText.CHARACTER, 5);
99+
if (!(actualAccessibleText.equals("1"))) {
100+
throw new RuntimeException(
101+
"JEditorPane -" + "getBeforeIndex() CHARACTER parameter"
102+
+ " expected:--1--, actual:--" + actualAccessibleText + "--");
103+
}
104+
105+
actualAccessibleText = jEditorPane.getAccessibleContext()
106+
.getAccessibleText().getBeforeIndex(AccessibleText.WORD, 5);
107+
if (!(actualAccessibleText.equals("Test1"))) {
108+
throw new RuntimeException(
109+
"JEditorPane -" + "getBeforeIndex() WORD parameter"
110+
+ " expected:--Test1--, actual:--" + actualAccessibleText + "--");
111+
}
112+
113+
actualAccessibleText = jEditorPane.getAccessibleContext()
114+
.getAccessibleText().getBeforeIndex(AccessibleText.SENTENCE, 20);
115+
if (!(actualAccessibleText.equals("Test1 Test2 Test3.\n"))) {
116+
throw new RuntimeException(
117+
"JEditorPane -" + "getBeforeIndex() SENTENCE parameter"
118+
+ " expected:--Test1 Test2 Test3.\n--, actual:--"
119+
+ actualAccessibleText + "--");
120+
}
121+
}
122+
123+
public static void main(String[] args) throws Exception {
124+
SwingUtilities.invokeAndWait(() -> doTest());
125+
System.out.println("Test Passed");
126+
}
127+
}

0 commit comments

Comments
 (0)