Skip to content

Commit 7557ec1

Browse files
Victor RudometovPaul Hohensee
Victor Rudometov
authored and
Paul Hohensee
committedMay 23, 2023
8306432: Open source several AWT Text Component related tests
Backport-of: 485a0691f4a762e9673967b7873356fb65afc5bc
1 parent 073d1a3 commit 7557ec1

5 files changed

+394
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2005, 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 5100200
27+
@summary IAE in X11 text field peer code
28+
@key headful
29+
*/
30+
31+
import java.awt.EventQueue;
32+
import java.awt.Frame;
33+
import java.awt.TextField;
34+
35+
public class CorrectSetCaretPositionDuringInitPeerTest
36+
{
37+
static TextField tf;
38+
static Frame frame;
39+
40+
public static void main(String[] args) throws Exception {
41+
try{
42+
EventQueue.invokeAndWait(() -> {
43+
frame = new Frame("Caret Position test");
44+
tf = new TextField("Very very very long string");
45+
tf.setSelectionStart(10);
46+
tf.setText("Short"); // now TextField.length() less than 10
47+
frame.add(tf);
48+
49+
frame.pack();
50+
frame.setVisible(true);
51+
});
52+
} finally {
53+
EventQueue.invokeAndWait(() -> {
54+
if (frame != null) {
55+
frame.dispose();
56+
}
57+
});
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2005, 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 5100950 8199723
27+
@summary textarea.getSelectedText() returns the de-selected text, on XToolkit
28+
@key headful
29+
@requires (os.family == "mac") | (os.family == "windows")
30+
*/
31+
32+
import java.awt.EventQueue;
33+
import java.awt.FlowLayout;
34+
import java.awt.Frame;
35+
import java.awt.KeyboardFocusManager;
36+
import java.awt.TextField;
37+
38+
public class DeselectionDuringDoSelectionNonVisibleTest {
39+
static TextField tf1;
40+
static TextField tf2;
41+
static Frame frame;
42+
43+
public static void main(String[] args) throws Exception {
44+
try {
45+
EventQueue.invokeAndWait(() -> {
46+
frame = new Frame("Deselection test");
47+
tf1 = new TextField("Text Field 1");
48+
tf2 = new TextField("Text Field 2");
49+
frame.add(tf1);
50+
frame.add(tf2);
51+
frame.setLayout(new FlowLayout());
52+
frame.setSize(200, 200);
53+
frame.setLocationRelativeTo(null);
54+
frame.pack();
55+
frame.setVisible(true);
56+
});
57+
58+
boolean isWin = System.getProperty("os.name").startsWith("Win");
59+
System.out.println("is Windows OS? " + isWin);
60+
61+
Thread.sleep(500);
62+
tf1.requestFocus();
63+
64+
Thread.sleep(500);
65+
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != tf1) {
66+
throw new RuntimeException("Test failed (TextField1 isn't focus owner).");
67+
}
68+
tf1.selectAll();
69+
Thread.sleep(500);
70+
71+
tf2.requestFocus();
72+
Thread.sleep(500);
73+
74+
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != tf2) {
75+
throw new RuntimeException("Test failed (TextField2 isn't focus owner).");
76+
}
77+
tf2.selectAll();
78+
Thread.sleep(500);
79+
80+
String selectedText = tf1.getSelectedText();
81+
String text = tf1.getText();
82+
83+
System.out.println("tf1.getText()=" + text);
84+
System.out.println("tf1.getSelectedText()=" + selectedText);
85+
86+
// Motif behaviour: After the selection of the second text, the first selected
87+
// text is unselected
88+
if (!selectedText.equals("") && !isWin) {
89+
throw new RuntimeException("Test failed (TextField1 isn't deselected).");
90+
}
91+
92+
// Windows behaviour: After the selection of the second text, the first selected
93+
// text is only not highlighted
94+
if (!selectedText.equals(text) && isWin) {
95+
throw new RuntimeException("Test failed (TextField1 is deselected).");
96+
}
97+
} finally {
98+
EventQueue.invokeAndWait(() -> {
99+
if (frame != null) {
100+
frame.dispose();
101+
}
102+
});
103+
}
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2006, 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+
@test
25+
@bug 4995931
26+
@summary java.awt.TextComponent caret position should be within the text bounds
27+
@key headful
28+
*/
29+
30+
import java.awt.EventQueue;
31+
import java.awt.TextField;
32+
33+
public class GetCaretPosOutOfBoundsTest {
34+
static TextField tf;
35+
public static void main(String[] args) throws Exception {
36+
EventQueue.invokeAndWait(() -> {
37+
tf = new TextField("1234567890");
38+
tf.setCaretPosition(100);
39+
int pos = tf.getCaretPosition();
40+
if (pos > 10) {
41+
throw new RuntimeException("Wrong caret position:" + pos + " instead of 10");
42+
}
43+
tf.setText("12345");
44+
if (tf.getCaretPosition() > 5) {
45+
throw new RuntimeException("Wrong caret position:" + pos + " instead of 5");
46+
}
47+
});
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 4185654
27+
@summary tests that the text insertion caret is positioned before the first character
28+
@key headful
29+
*/
30+
31+
import java.awt.EventQueue;
32+
import java.awt.Frame;
33+
import java.awt.TextField;
34+
import java.awt.TextArea;
35+
36+
public class InitialInsertionCaretPositionTest {
37+
static TextField textField;
38+
static TextArea textArea;
39+
static String failureMessage = "";
40+
41+
public static void main(String[] args) throws Exception {
42+
EventQueue.invokeAndWait(() -> {
43+
textArea = new TextArea("abcdefghij\nabcdefghij");
44+
textField = new TextField("abcdefghij");
45+
46+
boolean textFieldPassed = (textField.getCaretPosition() == 0);
47+
boolean textAreaPassed = (textArea.getCaretPosition() == 0);
48+
49+
if (!textFieldPassed) {
50+
failureMessage += " The text insertion caret for the text field is not\n";
51+
failureMessage += " initially set before the first character.\n";
52+
}
53+
if (!textAreaPassed) {
54+
failureMessage += " The text insertion caret for the text area is not\n";
55+
failureMessage += " initially set before the first character.\n";
56+
}
57+
if (textAreaPassed && textFieldPassed) {
58+
System.out.println("The test passed.");
59+
} else {
60+
System.out.println("The test failed:");
61+
System.out.println(failureMessage);
62+
}
63+
if (!textAreaPassed || !textFieldPassed) {
64+
throw new RuntimeException(failureMessage);
65+
}
66+
});
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 2001, 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+
@test
25+
@bug 4426750
26+
@requires (os.family == "linux")
27+
@key headful
28+
@summary tests that middle mouse button click pastes primary selection
29+
*/
30+
31+
import java.awt.BorderLayout;
32+
import java.awt.Dimension;
33+
import java.awt.EventQueue;
34+
import java.awt.Frame;
35+
import java.awt.Point;
36+
import java.awt.Robot;
37+
import java.awt.TextArea;
38+
import java.awt.TextComponent;
39+
import java.awt.TextField;
40+
41+
import java.awt.datatransfer.Clipboard;
42+
import java.awt.datatransfer.StringSelection;
43+
import java.awt.event.InputEvent;
44+
import java.awt.Toolkit;
45+
46+
public class MiddleMouseClickPasteTest {
47+
static final int FRAME_ACTIVATION_TIMEOUT = 1000;
48+
static final int SELECTION_PASTE_TIMEOUT = 1000;
49+
static final int CLICK_INTERVAL = 50;
50+
static final String TEST_TEXT = "TEST TEXT";
51+
static Frame frame;
52+
static TextField tf;
53+
static TextArea ta;
54+
static final Clipboard systemSelection = Toolkit.getDefaultToolkit().getSystemSelection();
55+
56+
57+
public static void main(String[] args) throws Exception {
58+
if (systemSelection != null) {
59+
try {
60+
EventQueue.invokeAndWait(MiddleMouseClickPasteTest::createAndShowGui);
61+
Thread.sleep(FRAME_ACTIVATION_TIMEOUT);
62+
63+
checkPaste(tf);
64+
checkPaste(ta);
65+
} catch (Exception e) {
66+
throw new RuntimeException(e);
67+
} finally {
68+
EventQueue.invokeAndWait(()-> {
69+
if (frame != null) {
70+
frame.dispose();
71+
}
72+
});
73+
}
74+
}
75+
}
76+
77+
public static void createAndShowGui() {
78+
frame = new Frame();
79+
tf = new TextField();
80+
ta = new TextArea();
81+
82+
frame.setLayout(new BorderLayout());
83+
frame.add(tf, BorderLayout.NORTH);
84+
frame.add(ta, BorderLayout.CENTER);
85+
frame.pack();
86+
frame.setLocationRelativeTo(null);
87+
frame.setVisible(true);
88+
}
89+
90+
public static void checkPaste(TextComponent textComponent) throws Exception {
91+
92+
final Point sourcePoint = textComponent.getLocationOnScreen();
93+
final Dimension d = textComponent.getSize();
94+
sourcePoint.translate(d.width / 2, d.height / 2);
95+
final Robot robot = new Robot();
96+
robot.setAutoWaitForIdle(true);
97+
robot.setAutoDelay(CLICK_INTERVAL);
98+
99+
textComponent.setText("");
100+
systemSelection.setContents(new StringSelection(TEST_TEXT), null);
101+
102+
robot.mouseMove(sourcePoint.x, sourcePoint.y);
103+
robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
104+
robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
105+
robot.delay(SELECTION_PASTE_TIMEOUT);
106+
107+
if (!TEST_TEXT.equals(textComponent.getText())) {
108+
throw new RuntimeException("Primary selection not pasted" +
109+
" into: " + textComponent);
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)
Please sign in to comment.