Skip to content

Commit 3184912

Browse files
author
Harshitha Onkar
committed
8339962: Open source AWT TextField tests - Set1
Reviewed-by: jdv, dnguyen, prr
1 parent 9cfc03a commit 3184912

File tree

3 files changed

+437
-0
lines changed

3 files changed

+437
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
import java.awt.BorderLayout;
25+
import java.awt.Button;
26+
import java.awt.EventQueue;
27+
import java.awt.Frame;
28+
import java.awt.Label;
29+
import java.awt.Panel;
30+
import java.awt.Rectangle;
31+
import java.awt.Robot;
32+
import java.awt.TextField;
33+
import java.awt.event.InputEvent;
34+
import java.awt.event.MouseEvent;
35+
import java.awt.event.MouseListener;
36+
37+
/*
38+
* @test
39+
* @key headful
40+
* @bug 4247913
41+
* @summary Tests that Label repaints after call Container.validate()
42+
* @run main ContainerValidateTest
43+
*/
44+
45+
public class ContainerValidateTest extends Frame implements MouseListener {
46+
private static Robot robot;
47+
private static Panel currentPanel;
48+
private static Button currentBtn;
49+
private static Panel updatedPanel;
50+
private static Label updatedLabel;
51+
private static TextField updatedTxtField;
52+
private static Button updatedBtn;
53+
54+
private static volatile Rectangle btnBounds;
55+
56+
Panel pnl1 = new Panel();
57+
Panel pnl2 = new Panel();
58+
Label lbl1 = new Label("Label 1");
59+
Label lbl2 = new Label("Label 2");
60+
TextField txt1 = new TextField("field1", 20);
61+
TextField txt2 = new TextField("field2", 20);
62+
Button btn1 = new Button("Swap 1");
63+
Button btn2 = new Button("Swap 2");
64+
65+
public static void main(String[] args) throws Exception {
66+
robot = new Robot();
67+
68+
ContainerValidateTest containerValidate = new ContainerValidateTest();
69+
EventQueue.invokeAndWait(containerValidate::createAndShowUI);
70+
robot.waitForIdle();
71+
robot.delay(1000);
72+
73+
containerValidate.testUI();
74+
}
75+
76+
private void createAndShowUI() {
77+
this.setTitle("ContainerValidateTest Test");
78+
pnl1.add(lbl1);
79+
pnl1.add(txt1);
80+
pnl1.add(btn1);
81+
82+
pnl2.add(lbl2);
83+
pnl2.add(txt2);
84+
pnl2.add(btn2);
85+
86+
btn1.addMouseListener(this);
87+
btn2.addMouseListener(this);
88+
89+
this.add(pnl1, BorderLayout.CENTER);
90+
pack();
91+
setLocationRelativeTo(null);
92+
setVisible(true);
93+
}
94+
95+
private void testUI() throws Exception {
96+
EventQueue.invokeAndWait(() -> btnBounds
97+
= new Rectangle(btn1.getLocationOnScreen().x,
98+
btn1.getLocationOnScreen().y,
99+
btn1.getWidth(),
100+
btn1.getHeight()));
101+
for (int i= 1; i < 4 ; i++) {
102+
EventQueue.invokeAndWait(() -> {
103+
currentPanel = (Panel) this.getComponent(0);
104+
currentBtn = (Button) currentPanel.getComponent(2);
105+
});
106+
107+
robot.mouseMove(btnBounds.x + (int) btnBounds.getWidth() / 2,
108+
btnBounds.y + (int) btnBounds.getHeight() / 2);
109+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
110+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
111+
robot.waitForIdle();
112+
//large delay set for completion of UI validate()
113+
robot.delay(500);
114+
115+
EventQueue.invokeAndWait(() -> {
116+
updatedPanel = (Panel) this.getComponent(0);
117+
updatedLabel = (Label) updatedPanel.getComponent(0);
118+
updatedTxtField = (TextField) updatedPanel.getComponent(1);
119+
updatedBtn = (Button) updatedPanel.getComponent(2);
120+
});
121+
testPanelComponents(currentBtn.getLabel());
122+
}
123+
}
124+
125+
private void testPanelComponents(String btnLabel) {
126+
if (btnLabel.equals("Swap 1")) {
127+
if (!(updatedLabel.getText().equals(lbl2.getText())
128+
&& updatedTxtField.getText().equals(txt2.getText())
129+
&& updatedBtn.getLabel().equals(btn2.getLabel()))) {
130+
throw new RuntimeException("Test Failed!! Labels not repainted"
131+
+ " after Container.validate()");
132+
}
133+
} else {
134+
if (!(updatedLabel.getText().equals(lbl1.getText())
135+
&& updatedTxtField.getText().equals(txt1.getText())
136+
&& updatedBtn.getLabel().equals(btn1.getLabel()))) {
137+
throw new RuntimeException("Test Failed!! Labels not repainted"
138+
+ " after Container.validate()");
139+
}
140+
}
141+
}
142+
143+
@Override
144+
public void mousePressed(MouseEvent evt) {
145+
if (evt.getComponent() instanceof Button btn) {
146+
if (btn.equals(btn1)) {
147+
remove(pnl1);
148+
add(pnl2, BorderLayout.CENTER);
149+
} else {
150+
remove(pnl2);
151+
add(pnl1, BorderLayout.CENTER);
152+
}
153+
invalidate();
154+
validate();
155+
}
156+
}
157+
158+
@Override
159+
public void mouseReleased(MouseEvent e) {}
160+
161+
@Override
162+
public void mouseEntered(MouseEvent e) {}
163+
164+
@Override
165+
public void mouseExited(MouseEvent e) {}
166+
167+
@Override
168+
public void mouseClicked(MouseEvent e) {}
169+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
import java.awt.Button;
25+
import java.awt.EventQueue;
26+
import java.awt.FlowLayout;
27+
import java.awt.Frame;
28+
import java.awt.Label;
29+
import java.awt.Point;
30+
import java.awt.Robot;
31+
import java.awt.TextField;
32+
import java.awt.Toolkit;
33+
import java.awt.datatransfer.Clipboard;
34+
import java.awt.datatransfer.StringSelection;
35+
import java.awt.event.ActionEvent;
36+
import java.awt.event.ActionListener;
37+
import java.awt.event.InputEvent;
38+
import java.awt.event.KeyEvent;
39+
40+
import jdk.test.lib.Platform;
41+
42+
/*
43+
* @test
44+
* @bug 4124697
45+
* @key headful
46+
* @summary Make sure that after setting and then changing the echo
47+
* character again, the TextField continues to function as expected.
48+
* @library /test/lib
49+
* @build jdk.test.lib.Platform
50+
* @run main SetEchoCharTest
51+
*/
52+
53+
public class SetEchoCharTest {
54+
private static Frame frame;
55+
private static Robot robot;
56+
private static TextField tfPassword;
57+
private static Button btn1;
58+
private static Button btn2;
59+
private static volatile Point btn1Loc;
60+
private static volatile Point btn2Loc;
61+
62+
private static final String CHANGE = "Change echo char";
63+
private static final String PRINT = "Print text";
64+
private static final String INITIAL_TEXT = "DefaultPwd";
65+
private static final String CHANGED_TEXT = "NewPwd";
66+
private static final char NEW_ECHO_CHAR = '*';
67+
68+
public static void main(String[] args) throws Exception {
69+
try {
70+
robot = new Robot();
71+
robot.setAutoWaitForIdle(true);
72+
robot.setAutoDelay(50);
73+
74+
EventQueue.invokeAndWait(() -> createAndShowUI());
75+
robot.waitForIdle();
76+
robot.delay(1000);
77+
78+
testEchoChar();
79+
robot.waitForIdle();
80+
robot.delay(200);
81+
82+
testNewEchoChar();
83+
robot.waitForIdle();
84+
robot.delay(200);
85+
} finally {
86+
EventQueue.invokeAndWait(() -> {
87+
if (frame != null) {
88+
frame.dispose();
89+
}
90+
});
91+
}
92+
}
93+
94+
private static void createAndShowUI() {
95+
frame = new Frame("SetEchoCharTest");
96+
frame.setLayout(new FlowLayout());
97+
98+
Label label = new Label("Pwd:");
99+
tfPassword = new TextField(INITIAL_TEXT, 10);
100+
tfPassword.setEchoChar('X');
101+
tfPassword.addActionListener((ActionListener) e -> {
102+
if (e.getActionCommand().equals(CHANGED_TEXT)) {
103+
//check the 2nd condition only if ActionEvent
104+
//is triggered by changed text
105+
if (!(tfPassword.getText().equals(CHANGED_TEXT)
106+
&& tfPassword.getEchoChar() == NEW_ECHO_CHAR)) {
107+
throw new RuntimeException("Test Failed!!! TextField not working"
108+
+ " as expected after echo char change");
109+
}
110+
}
111+
});
112+
frame.add(label);
113+
frame.add(tfPassword);
114+
115+
btn1 = new Button(PRINT);
116+
btn1.addActionListener(new BtnActionListener());
117+
frame.add(btn1);
118+
119+
btn2 = new Button(CHANGE);
120+
btn2.addActionListener(new BtnActionListener());
121+
frame.add(btn2);
122+
frame.setSize(200,200);
123+
frame.setLocationRelativeTo(null);
124+
frame.setVisible(true);
125+
}
126+
127+
private static void testEchoChar() throws Exception {
128+
EventQueue.invokeAndWait(() -> {
129+
btn1Loc = btn1.getLocationOnScreen();
130+
btn2Loc = btn2.getLocationOnScreen();
131+
});
132+
133+
robot.mouseMove(btn1Loc.x + btn1.getWidth() / 2,
134+
btn1Loc.y + btn1.getHeight() / 2);
135+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
136+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
137+
robot.delay(1000);
138+
139+
robot.mouseMove(btn2Loc.x + btn2.getWidth() / 2,
140+
btn2Loc.y + btn2.getHeight() / 2);
141+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
142+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
143+
robot.delay(1000);
144+
}
145+
146+
private static void testNewEchoChar() {
147+
StringSelection stringSelection = new StringSelection(CHANGED_TEXT);
148+
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
149+
clipboard.setContents(stringSelection, stringSelection);
150+
151+
int ctrlKey = Platform.isOSX() ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
152+
robot.keyPress(ctrlKey);
153+
robot.keyPress(KeyEvent.VK_V);
154+
robot.keyRelease(KeyEvent.VK_V);
155+
robot.keyRelease(ctrlKey);
156+
157+
robot.keyPress(KeyEvent.VK_ENTER);
158+
robot.keyRelease(KeyEvent.VK_ENTER);
159+
}
160+
161+
private static class BtnActionListener implements ActionListener {
162+
public void actionPerformed(ActionEvent evt) {
163+
String ac = evt.getActionCommand();
164+
if (CHANGE.equals(ac)) {
165+
tfPassword.setText("");
166+
tfPassword.setEchoChar(NEW_ECHO_CHAR);
167+
tfPassword.requestFocus();
168+
}
169+
if (PRINT.equals(ac)) {
170+
if (!tfPassword.getText().equals(INITIAL_TEXT)) {
171+
throw new RuntimeException("Test Failed!!!"
172+
+ " Initial text not as expected");
173+
}
174+
}
175+
}
176+
}
177+
}
178+

0 commit comments

Comments
 (0)