Skip to content

Commit 815f5dd

Browse files
author
Satyen Subramaniam
committed
8352865: Open source several AWT TextComponent tests - Batch 2
Backport-of: 3d0feba00a1c1ef7627880859a093bb00eb8fc4c
1 parent ec6528e commit 815f5dd

File tree

5 files changed

+450
-0
lines changed

5 files changed

+450
-0
lines changed

test/jdk/ProblemList.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ java/awt/TrayIcon/DragEventSource/DragEventSource.java 8252242 macosx-all
840840
java/awt/FileDialog/DefaultFocusOwner/DefaultFocusOwner.java 7187728 macosx-all,linux-all
841841
java/awt/print/PageFormat/Orient.java 8016055 macosx-all
842842
java/awt/TextArea/TextAreaCursorTest/HoveringAndDraggingTest.java 8024986 macosx-all,linux-all
843+
java/awt/TextComponent/CorrectTextComponentSelectionTest.java 8237220 macosx-all
844+
java/awt/TextComponent/SelectionAndCaretColor.java 7017622 linux-all
843845
java/awt/event/MouseEvent/SpuriousExitEnter/SpuriousExitEnter.java 8254841 macosx-all
844846
java/awt/Focus/AppletInitialFocusTest/AppletInitialFocusTest1.java 8256289 windows-x64
845847
java/awt/FullScreen/TranslucentWindow/TranslucentWindow.java 8258103 linux-all
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2002, 2025, 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 4737679 4623376 4501485 4740906 4708221
27+
* @requires (os.family == "windows")
28+
* @summary Alt+Left/right/up/down generate characters in JTextArea
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual AltPlusNumberKeyCombinationsTest
32+
*/
33+
34+
import java.awt.FlowLayout;
35+
import java.awt.Frame;
36+
import java.awt.TextArea;
37+
import java.awt.TextField;
38+
39+
public class AltPlusNumberKeyCombinationsTest {
40+
public static void main(String[] args) throws Exception {
41+
String INSTRUCTIONS = """
42+
[WINDOWS PLATFORM ONLY]
43+
Please do the following steps for both TextField and TextArea:
44+
1. Hold down ALT and press a NON-NUMPAD right arrow, then release
45+
ALT key. If any symbol is typed the test failed.
46+
2. Hold down ALT and press one after another the following
47+
NUMPAD keys: 0, 1, 2, 8. Release ALT key. If the Euro symbol
48+
is not typed the test failed
49+
3. Hold down ALT and press one after another the following
50+
NUMPAD keys: 0, 2, 2, 7. Release ALT key. If nothing or
51+
the blank symbol is typed the test failed
52+
If all the steps are done successfully the test PASSed,
53+
else test FAILS.
54+
""";
55+
PassFailJFrame.builder()
56+
.title("Test Instructions")
57+
.instructions(INSTRUCTIONS)
58+
.columns(35)
59+
.testUI(initialize())
60+
.build()
61+
.awaitAndCheck();
62+
}
63+
64+
public static Frame initialize() {
65+
Frame f = new Frame("key combination test");
66+
f.setLayout(new FlowLayout());
67+
TextField tf = new TextField("TextField");
68+
f.add(tf);
69+
TextArea ta = new TextArea("TextArea");
70+
f.add(ta);
71+
f.setSize(200, 200);
72+
return f;
73+
}
74+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2005, 2025, 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 5100806
27+
* @summary TextArea.select(0,0) does not de-select the selected text properly
28+
* @key headful
29+
* @run main CorrectTextComponentSelectionTest
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Color;
34+
import java.awt.EventQueue;
35+
import java.awt.Frame;
36+
import java.awt.Point;
37+
import java.awt.Robot;
38+
import java.awt.TextArea;
39+
import java.awt.TextComponent;
40+
import java.awt.TextField;
41+
import java.lang.reflect.InvocationTargetException;
42+
43+
public class CorrectTextComponentSelectionTest {
44+
static TextField tf = new TextField("TextField");
45+
static TextArea ta = new TextArea("TextArea");
46+
static Robot r;
47+
static Frame frame;
48+
static volatile Color color_center;
49+
static volatile Point loc;
50+
51+
public static void main(String[] args) throws Exception {
52+
try {
53+
r = new Robot();
54+
EventQueue.invokeAndWait(() -> {
55+
initialize();
56+
});
57+
r.waitForIdle();
58+
r.delay(1000);
59+
60+
test(tf);
61+
test(ta);
62+
System.out.println("Test Passed!");
63+
} finally {
64+
EventQueue.invokeAndWait(() -> {
65+
if (frame != null) {
66+
frame.dispose();
67+
}
68+
});
69+
}
70+
}
71+
72+
public static void initialize() {
73+
frame = new Frame("TextComponent Selection Test");
74+
frame.setLayout(new BorderLayout());
75+
76+
// We should place to the text components the long strings in order to
77+
// cover the components by the selection completely
78+
String sf = "";
79+
for (int i = 0; i < 50; i++) {
80+
sf = sf + " ";
81+
}
82+
tf.setText(sf);
83+
// We check the color of the text component in order to find out the
84+
// bug reproducible situation
85+
tf.setForeground(Color.WHITE);
86+
tf.setBackground(Color.WHITE);
87+
88+
String sa = "";
89+
for (int i = 0; i < 50; i++) {
90+
for (int j = 0; j < 50; j++) {
91+
sa = sa + " ";
92+
}
93+
sa = sa + "\n";
94+
}
95+
ta.setText(sa);
96+
ta.setForeground(Color.WHITE);
97+
ta.setBackground(Color.WHITE);
98+
99+
frame.add(tf, "North");
100+
frame.add(ta, "South");
101+
frame.setSize(200, 200);
102+
frame.setVisible(true);
103+
}
104+
105+
private static void test(TextComponent tc) throws Exception {
106+
if (tc instanceof TextField) {
107+
System.out.println("TextField testing ...");
108+
} else if (tc instanceof TextArea) {
109+
System.out.println("TextArea testing ...");
110+
}
111+
112+
r.waitForIdle();
113+
r.delay(100);
114+
EventQueue.invokeAndWait(() -> {
115+
tc.requestFocus();
116+
tc.selectAll();
117+
tc.select(0, 0);
118+
});
119+
120+
r.waitForIdle();
121+
r.delay(100);
122+
EventQueue.invokeAndWait(() -> {
123+
loc = tc.getLocationOnScreen();
124+
});
125+
r.waitForIdle();
126+
r.delay(100);
127+
128+
EventQueue.invokeAndWait(() -> {
129+
color_center = r.getPixelColor(loc.x + tc.getWidth() / 2, loc.y + tc.getHeight() / 2);
130+
});
131+
132+
System.out.println("Color of the text component (CENTER) =" + color_center);
133+
System.out.println("White color=" + Color.WHITE);
134+
135+
if (color_center.getRGB() != Color.WHITE.getRGB()) {
136+
throw new RuntimeException("Test Failed");
137+
}
138+
}
139+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (c) 2006, 2025, 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 6287895
27+
* @requires (os.family == "linux")
28+
* @summary Test cursor and selected text incorrectly colored in TextField
29+
* @key headful
30+
* @run main SelectionAndCaretColor
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Color;
35+
import java.awt.EventQueue;
36+
import java.awt.Font;
37+
import java.awt.Frame;
38+
import java.awt.Rectangle;
39+
import java.awt.Robot;
40+
import java.awt.TextArea;
41+
import java.awt.TextComponent;
42+
import java.awt.TextField;
43+
import java.awt.image.BufferedImage;
44+
45+
public class SelectionAndCaretColor {
46+
static TextField tf = new TextField(20);
47+
static TextArea ta = new TextArea("", 1, 20, TextArea.SCROLLBARS_NONE);
48+
static Robot r;
49+
static Frame frame;
50+
static volatile int flips;
51+
52+
public static void main(String[] args) throws Exception {
53+
try {
54+
frame = new Frame("Selection and Caret color test");
55+
r = new Robot();
56+
57+
EventQueue.invokeAndWait(() -> {
58+
frame.setLayout(new BorderLayout());
59+
tf.setFont(new Font("Monospaced", Font.PLAIN, 15));
60+
ta.setFont(new Font("Monospaced", Font.PLAIN, 15));
61+
62+
frame.add(tf, BorderLayout.NORTH);
63+
frame.add(ta, BorderLayout.SOUTH);
64+
frame.setSize(200, 200);
65+
frame.setVisible(true);
66+
});
67+
r.waitForIdle();
68+
r.delay(1000);
69+
test(tf);
70+
test(ta);
71+
} finally {
72+
EventQueue.invokeAndWait(() -> {
73+
if (frame != null) {
74+
frame.dispose();
75+
}
76+
});
77+
}
78+
}
79+
80+
private static int countFlips(TextComponent tc) {
81+
int y = tc.getLocationOnScreen().y + tc.getHeight() / 2;
82+
int x1 = tc.getLocationOnScreen().x + 5;
83+
int x2 = tc.getLocationOnScreen().x + tc.getWidth() - 5;
84+
85+
int[] fb = {tc.getBackground().getRGB(), tc.getForeground().getRGB()};
86+
int i = 0;
87+
int flips = 0;
88+
89+
BufferedImage img = r.createScreenCapture(new Rectangle(x1, y, x2 - x1, 1));
90+
for (int x = 0; x < x2 - x1; x++) {
91+
int c = img.getRGB(x, 0);
92+
if (c == fb[i]) {
93+
;
94+
} else if (c == fb[1 - i]) {
95+
flips++;
96+
i = 1 - i;
97+
} else {
98+
throw new RuntimeException("Invalid color detected: " +
99+
Integer.toString(c, 16) + " instead of " +
100+
Integer.toString(fb[i], 16));
101+
}
102+
}
103+
return flips;
104+
}
105+
106+
private static void test(TextComponent tc) throws Exception {
107+
if (tc instanceof TextField) {
108+
System.out.println("TextField testing ...");
109+
} else if (tc instanceof TextArea) {
110+
System.out.println("TextArea testing ...");
111+
}
112+
113+
// now passing along the component's vertical center,
114+
// skipping 5px from both sides,
115+
// we should see bg - textcolor - bg - selcolor -
116+
// seltextcolor - selcolor - bg
117+
// that is bg-fg-bg-fg-bg-fg-bg, 6 flips
118+
119+
EventQueue.invokeAndWait(() -> {
120+
tc.setForeground(Color.green);
121+
tc.setBackground(Color.magenta);
122+
123+
tc.setText(" I I ");
124+
tc.select(5, 10);
125+
tc.requestFocus();
126+
});
127+
r.waitForIdle();
128+
r.delay(200);
129+
EventQueue.invokeAndWait(() -> {
130+
flips = countFlips(tc);
131+
});
132+
if (flips != 6) {
133+
throw new RuntimeException("Invalid number of flips: "
134+
+ flips + " instead of 6");
135+
}
136+
EventQueue.invokeAndWait(() -> {
137+
// same for caret: spaces in the tc, caret in the middle
138+
// bg-fg-bg - 2 flips
139+
140+
tc.select(0, 0);
141+
tc.setText(" ");
142+
tc.setCaretPosition(5);
143+
});
144+
r.waitForIdle();
145+
r.delay(200);
146+
147+
for (int i = 0; i < 10; i++) {
148+
EventQueue.invokeAndWait(() -> {
149+
flips = countFlips(tc);
150+
});
151+
152+
if (flips == 2) {
153+
break;
154+
}
155+
if (flips == 0) {
156+
continue;
157+
}
158+
throw new RuntimeException("Invalid number of flips: "
159+
+ flips + " instead of 2");
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)