Skip to content

Commit f880fa9

Browse files
author
Tejesh R
committed
8352793: Open source several AWT TextComponent tests - Batch 1
Reviewed-by: prr, serb
1 parent ade67df commit f880fa9

File tree

4 files changed

+377
-0
lines changed

4 files changed

+377
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 1999, 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 4258667 4405602
27+
* @summary Make sure TextComponents are grayed out when non-editable
28+
* if the background color has not been set by client code.
29+
* Make sure TextComponents are not grayed out when non-editable
30+
* if the background color has been set by client code.
31+
* @library /java/awt/regtesthelpers
32+
* @build PassFailJFrame
33+
* @run main/manual BackgroundTest
34+
*/
35+
36+
import java.awt.Button;
37+
import java.awt.Color;
38+
import java.awt.FlowLayout;
39+
import java.awt.Frame;
40+
import java.awt.TextArea;
41+
import java.awt.TextField;
42+
43+
public class BackgroundTest {
44+
private static final String enableString = "EnableText";
45+
private static final String disableString = "DisableText";
46+
47+
public static void main(String[] args) throws Exception {
48+
String INSTRUCTIONS = """
49+
1. When the frame appears, it should have a blue background.
50+
2. The first TextField and TextArea will be the default color.
51+
The second TextField and TextArea will be green.
52+
3. Press the "DisableText" button.
53+
The first TextField and TextArea should change colors to the
54+
default disabled color. On Windows, this is usually gray.
55+
On linux and macos it will match the environment settings.
56+
If the TextField or the TextArea do not change colors as described,
57+
the test FAILS.
58+
4. The second TextField and TextArea should still be green.
59+
If either of them are not green, the test FAILS.
60+
Press the "EnableText" button (same button as before).
61+
The first TextField and TextArea should return to their
62+
original colors as described in the first paragraph. If they
63+
do not, the test FAILS.
64+
5. The second TextField and TextArea should still be green.
65+
If either of them are not green, the test FAILS.
66+
Otherwise, the test PASSES.
67+
""";
68+
69+
PassFailJFrame.builder()
70+
.instructions(INSTRUCTIONS)
71+
.columns(35)
72+
.testUI(BackgroundTest::initialize)
73+
.build()
74+
.awaitAndCheck();
75+
}
76+
77+
public static Frame initialize() {
78+
Frame frame = new Frame("Background Test");
79+
frame.setLayout(new FlowLayout());
80+
TextField tf = new TextField(30);
81+
TextArea ta = new TextArea(4, 30);
82+
TextField setTf = new TextField(30);
83+
TextArea setTa = new TextArea(4, 30);
84+
Button enableButton = new Button(disableString);
85+
86+
enableButton.setBackground(Color.red);
87+
frame.setSize(500, 250);
88+
89+
frame.setBackground(Color.blue);
90+
91+
tf.setText("Background not set - should be default");
92+
tf.setEditable(true);
93+
frame.add(tf);
94+
ta.setText("Background not set - should be default");
95+
ta.setEditable(true);
96+
frame.add(ta);
97+
98+
setTf.setText("Background is set - should be Green");
99+
setTf.setBackground(Color.green);
100+
setTf.setEditable(true);
101+
frame.add(setTf);
102+
setTa.setText("Background is set - should be Green");
103+
setTa.setBackground(Color.green);
104+
setTa.setEditable(true);
105+
frame.add(setTa);
106+
107+
enableButton.addActionListener(e -> {
108+
boolean currentlyEditable = tf.isEditable();
109+
110+
if (currentlyEditable) {
111+
tf.setEditable(false);
112+
ta.setEditable(false);
113+
setTf.setEditable(false);
114+
setTa.setEditable(false);
115+
enableButton.setLabel(enableString);
116+
} else {
117+
tf.setEditable(true);
118+
ta.setEditable(true);
119+
setTf.setEditable(true);
120+
setTa.setEditable(true);
121+
enableButton.setLabel(disableString);
122+
}
123+
});
124+
frame.add(enableButton);
125+
return frame;
126+
}
127+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 5042122
27+
* @summary Verifies the TextComponent is grayed out when disabled
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual DisableTest
31+
*/
32+
33+
import javax.swing.BoxLayout;
34+
import java.awt.BorderLayout;
35+
import java.awt.Button;
36+
import java.awt.Component;
37+
import java.awt.Frame;
38+
import java.awt.Panel;
39+
import java.awt.TextArea;
40+
import java.awt.TextField;
41+
import java.awt.event.ActionListener;
42+
import java.util.Vector;
43+
import java.util.Iterator;
44+
45+
public class DisableTest {
46+
public static void main(String[] args) throws Exception {
47+
String INSTRUCTIONS = """
48+
1. Click "Enable" and "Disable" buttons and verify the text
49+
components are disabled and enabled correctly.
50+
2. Verify that the disabled text components are grayed
51+
out and are uneditable.
52+
3. Click PASS or FAIL accordingly.
53+
""";
54+
55+
PassFailJFrame.builder()
56+
.instructions(INSTRUCTIONS)
57+
.columns(35)
58+
.testUI(DisableTest::initialize)
59+
.build()
60+
.awaitAndCheck();
61+
}
62+
63+
public static Frame initialize() {
64+
Frame frame = new Frame("TextComponent Disabled test");
65+
frame.setLayout(new BorderLayout());
66+
frame.setSize(200, 200);
67+
final Vector comps = new Vector();
68+
comps.add(new TextField("TextField"));
69+
TextArea ta = new TextArea("TextArea", 2, 100, TextArea.SCROLLBARS_NONE);
70+
comps.add(ta);
71+
Panel pc = new Panel();
72+
pc.setLayout(new BoxLayout(pc, BoxLayout.Y_AXIS));
73+
Iterator iter = comps.iterator();
74+
while (iter.hasNext()) {
75+
Component c = (Component) iter.next();
76+
c.setEnabled(false);
77+
pc.add(c);
78+
}
79+
frame.add(pc, BorderLayout.CENTER);
80+
Panel p = new Panel();
81+
final Button be = new Button("Enable");
82+
final Button bd = new Button("Disable");
83+
p.add(be);
84+
p.add(bd);
85+
ActionListener al = ev -> {
86+
boolean enable = (ev.getSource() == be);
87+
Iterator iterator = comps.iterator();
88+
while (iterator.hasNext()) {
89+
Component c = (Component) iterator.next();
90+
c.setEnabled(enable);
91+
}
92+
};
93+
be.addActionListener(al);
94+
bd.addActionListener(al);
95+
frame.add(p, BorderLayout.SOUTH);
96+
return frame;
97+
}
98+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 1999, 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 4035364
27+
* @summary Checks that Caps Lock key works
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual ModifiersTest
31+
*/
32+
33+
import java.awt.Frame;
34+
import java.awt.GridLayout;
35+
import java.awt.Label;
36+
import java.awt.TextArea;
37+
import java.awt.event.KeyAdapter;
38+
import java.awt.event.KeyEvent;
39+
40+
public class ModifiersTest {
41+
public static void main(String[] args) throws Exception {
42+
String INSTRUCTIONS = """
43+
1. Type some text in the TextArea in upper and lowercase,
44+
using the Caps Lock ON/OFF.
45+
2. If Caps Lock toggles correctly and you are able to type in
46+
both cases, the test PASS. Else Test FAILS.
47+
""";
48+
PassFailJFrame.builder()
49+
.instructions(INSTRUCTIONS)
50+
.columns(35)
51+
.testUI(ModifiersTest::initialize)
52+
.build()
53+
.awaitAndCheck();
54+
}
55+
56+
public static Frame initialize() {
57+
Frame frame = new Frame("Modifiers Test");
58+
frame.setLayout(new GridLayout(1, 1));
59+
frame.addKeyListener(new KeyChecker());
60+
frame.setLayout(new GridLayout(2, 1));
61+
Label label = new Label("See if you can type in upper and lowercase using Caps Lock:");
62+
frame.add(label);
63+
TextArea ta = new TextArea();
64+
frame.add(ta);
65+
ta.addKeyListener(new KeyChecker());
66+
ta.requestFocus();
67+
frame.setSize(400, 300);
68+
return frame;
69+
}
70+
}
71+
72+
// a KeyListener for debugging purposes
73+
class KeyChecker extends KeyAdapter {
74+
public void keyPressed(KeyEvent ev) {
75+
System.out.println(ev);
76+
}
77+
78+
public void keyReleased(KeyEvent ev) {
79+
System.out.println(ev);
80+
}
81+
82+
public void keyTyped(KeyEvent ev) {
83+
System.out.println(ev);
84+
}
85+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 1999, 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 4129511
27+
* @summary Tests that TextField margins are not exceedingly wide
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual TextFieldMargin
31+
*/
32+
33+
import java.awt.Frame;
34+
import java.awt.GridLayout;
35+
import java.awt.Label;
36+
import java.awt.TextArea;
37+
import java.awt.TextField;
38+
39+
public class TextFieldMargin {
40+
public static void main(String[] args) throws Exception {
41+
String INSTRUCTIONS = """
42+
1. Examine the TextField, Label, and TextArea to see
43+
that the text is vertically aligned along the left
44+
2. If all are aligned along the left, then test PASS,
45+
else test FAILS.
46+
""";
47+
PassFailJFrame.builder()
48+
.instructions(INSTRUCTIONS)
49+
.columns(35)
50+
.testUI(TextFieldMargin::initialize)
51+
.build()
52+
.awaitAndCheck();
53+
}
54+
55+
public static Frame initialize() {
56+
Frame frame = new Frame("Frame with a text field & a label");
57+
frame.setLayout(new GridLayout(5, 1));
58+
TextField text_field = new TextField("Left Textfield");
59+
frame.add(text_field);
60+
Label label = new Label("Left Label");
61+
frame.add(label);
62+
TextArea text_area = new TextArea("Left Textfield");
63+
frame.add(text_area);
64+
frame.setBounds(50, 50, 300, 300);
65+
return frame;
66+
}
67+
}

0 commit comments

Comments
 (0)