Skip to content

Commit b734886

Browse files
committed
8315731: Open source several Swing Text related tests
Backport-of: 0dc17ca068b837606252e1db614fd005a2f2b942
1 parent 068ed1f commit b734886

File tree

5 files changed

+469
-0
lines changed

5 files changed

+469
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
import java.awt.Robot;
25+
import java.awt.Shape;
26+
import javax.swing.JEditorPane;
27+
import javax.swing.JFrame;
28+
import javax.swing.SwingUtilities;
29+
import javax.swing.text.BadLocationException;
30+
import javax.swing.text.Document;
31+
import javax.swing.text.Element;
32+
import javax.swing.text.Position;
33+
import javax.swing.text.StyleConstants;
34+
import javax.swing.text.View;
35+
import javax.swing.text.ViewFactory;
36+
import javax.swing.text.html.HTML;
37+
import javax.swing.text.html.HTMLEditorKit;
38+
import javax.swing.text.html.ParagraphView;
39+
40+
/*
41+
* @test
42+
* @bug 4398059
43+
* @key headful
44+
* @summary Tests that CompositeView doesn't throw NPE.
45+
*/
46+
47+
public class bug4398059 {
48+
private static JFrame jFrame;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
Robot robot = new Robot();
53+
SwingUtilities.invokeAndWait(bug4398059::createAndShowUI);
54+
robot.waitForIdle();
55+
robot.delay(1000);
56+
} finally {
57+
SwingUtilities.invokeAndWait(() -> {
58+
if (jFrame != null) {
59+
jFrame.dispose();
60+
}
61+
});
62+
}
63+
}
64+
65+
public static void createAndShowUI() {
66+
String text = "<H1>text";
67+
jFrame = new JFrame("CompositeView Test");
68+
JEditorPane jep = new JEditorPane();
69+
jep.setEditorKit(new MyHTMLEditorKit());
70+
jep.setText(text);
71+
72+
Document doc = jep.getDocument();
73+
jep.setCaretPosition(doc.getLength() - 1);
74+
75+
jFrame.getContentPane().add(jep);
76+
jFrame.setSize(200,200);
77+
jFrame.setVisible(true);
78+
}
79+
80+
static class MyHTMLEditorKit extends HTMLEditorKit {
81+
private static final ViewFactory defaultFactory = new MyHTMLFactory();
82+
83+
public ViewFactory getViewFactory() {
84+
return defaultFactory;
85+
}
86+
87+
static class MyHTMLFactory extends HTMLEditorKit.HTMLFactory {
88+
public View create(Element elem) {
89+
Object obj = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
90+
if (obj instanceof HTML.Tag) {
91+
HTML.Tag kind = (HTML.Tag) obj;
92+
if (kind == HTML.Tag.H1) {
93+
return new MyParagraphView(elem);
94+
}
95+
}
96+
return super.create(elem);
97+
}
98+
}
99+
100+
static class MyParagraphView extends ParagraphView {
101+
public MyParagraphView(Element elem) {
102+
super(elem);
103+
}
104+
105+
public Shape getChildAllocation(int index, Shape a) {
106+
return null;
107+
}
108+
109+
public Shape modelToView(int pos, Shape a, Position.Bias b)
110+
throws BadLocationException {
111+
return super.modelToView(pos, a, b);
112+
}
113+
}
114+
}
115+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
import java.awt.Point;
25+
import java.awt.Rectangle;
26+
import java.awt.Robot;
27+
import java.awt.event.InputEvent;
28+
import java.awt.event.KeyAdapter;
29+
import java.awt.event.KeyEvent;
30+
import javax.swing.JFrame;
31+
import javax.swing.JTextArea;
32+
import javax.swing.SwingUtilities;
33+
34+
/*
35+
* @test
36+
* @bug 4197894
37+
* @key headful
38+
* @summary Tests if shift-click adjusts selection in text areas.
39+
*/
40+
41+
public class bug4197894 {
42+
private static JFrame jFrame;
43+
private static JTextArea ta;
44+
45+
private static volatile Point point = null;
46+
private static volatile Rectangle bounds;
47+
48+
private static volatile boolean passed = true;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
Robot robot = new Robot();
53+
robot.setAutoDelay(50);
54+
robot.setAutoWaitForIdle(true);
55+
56+
SwingUtilities.invokeAndWait(bug4197894::createAndShowUI);
57+
robot.waitForIdle();
58+
robot.delay(1000);
59+
60+
SwingUtilities.invokeAndWait(() -> {
61+
point = ta.getLocationOnScreen();
62+
bounds = ta.getBounds();
63+
});
64+
robot.waitForIdle();
65+
robot.delay(300);
66+
67+
robot.mouseMove((point.x + bounds.width / 4),
68+
(point.y + bounds.height / 4));
69+
70+
robot.keyPress(KeyEvent.VK_SHIFT);
71+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
72+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
73+
robot.keyRelease(KeyEvent.VK_SHIFT);
74+
robot.delay(300);
75+
76+
if (!passed) {
77+
throw new RuntimeException("Test failed." +
78+
" Shift-Click Text Selection does not work!");
79+
}
80+
} finally {
81+
SwingUtilities.invokeAndWait(() -> {
82+
if (jFrame != null) {
83+
jFrame.dispose();
84+
}
85+
});
86+
}
87+
}
88+
89+
private static void createAndShowUI() {
90+
jFrame = new JFrame("Shift-Click Text Selection");
91+
ta = new JTextArea();
92+
ta.addKeyListener(new KeyAdapter() {
93+
public void keyReleased(KeyEvent e) {
94+
String selText = ta.getSelectedText();
95+
passed = !(selText == null || (selText.length() == 0));
96+
}
97+
});
98+
ta.setText("12345\n12345\n12345\n12345\n12345\n12345\n12345");
99+
ta.setCaretPosition(ta.getDocument().getLength());
100+
jFrame.getContentPane().add(ta);
101+
jFrame.pack();
102+
jFrame.setLocationRelativeTo(null);
103+
jFrame.setAlwaysOnTop(true);
104+
jFrame.setVisible(true);
105+
}
106+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2000, 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+
import java.awt.Container;
25+
import java.awt.Point;
26+
import java.awt.Robot;
27+
import java.awt.event.InputEvent;
28+
import java.awt.event.MouseAdapter;
29+
import java.awt.event.MouseEvent;
30+
import javax.swing.BoxLayout;
31+
import javax.swing.JButton;
32+
import javax.swing.JFrame;
33+
import javax.swing.JTextField;
34+
import javax.swing.SwingUtilities;
35+
36+
/*
37+
* @test
38+
* @bug 4203175
39+
* @key headful
40+
* @summary Tests that double-click on disabled JTextField doesn't
41+
* cause other text-field to select content.
42+
*/
43+
44+
public class bug4203175 {
45+
private static JFrame jFrame;
46+
private static JTextField tf1, tf2;
47+
private static JButton b;
48+
private static volatile Point point;
49+
private static volatile boolean passed = true;
50+
private static int clicks = 0;
51+
52+
public static void main(String[] args) throws Exception {
53+
try {
54+
Robot robot = new Robot();
55+
robot.setAutoDelay(50);
56+
robot.setAutoWaitForIdle(true);
57+
58+
SwingUtilities.invokeAndWait(bug4203175::createAndShowUI);
59+
robot.delay(1000);
60+
61+
SwingUtilities.invokeAndWait(() -> point = tf1.getLocationOnScreen());
62+
robot.mouseMove(point.x, point.y);
63+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
64+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
65+
robot.delay(200);
66+
67+
SwingUtilities.invokeAndWait(() -> point = b.getLocationOnScreen());
68+
robot.mouseMove(point.x, point.y);
69+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
70+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
71+
robot.delay(200);
72+
73+
SwingUtilities.invokeAndWait(() -> point = tf2.getLocationOnScreen());
74+
robot.mouseMove(point.x, point.y);
75+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
76+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
77+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
78+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
79+
robot.delay(200);
80+
81+
if (!passed) {
82+
throw new RuntimeException("Test failed!! Text selection on disabled" +
83+
" TextField does not work as expected!");
84+
}
85+
} finally {
86+
SwingUtilities.invokeAndWait(() -> {
87+
if (jFrame != null) {
88+
jFrame.dispose();
89+
}
90+
});
91+
}
92+
}
93+
94+
private static void createAndShowUI() {
95+
jFrame = new JFrame("JTextField Text Selection");
96+
Container cont = jFrame.getContentPane();
97+
cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
98+
99+
tf1 = new JTextField(20);
100+
tf1.setText("sometext");
101+
tf1.setName("Field 1");
102+
tf1.setCaretPosition(tf1.getDocument().getLength());
103+
cont.add(tf1);
104+
105+
tf2 = new JTextField(20);
106+
tf2.setName("Field 2");
107+
tf2.addMouseListener(new MouseAdapter() {
108+
@Override
109+
public void mouseClicked(MouseEvent e) {
110+
super.mouseClicked(e);
111+
clicks++;
112+
if (clicks == 2) {
113+
String selText = tf1.getSelectedText();
114+
passed = (selText == null || (selText.length() == 0));
115+
}
116+
}
117+
});
118+
cont.add(tf2);
119+
120+
b = new JButton("Toggle Enabled");
121+
cont.add(b);
122+
b.addActionListener(e -> {
123+
if (e.getSource() == b) {
124+
boolean b = tf1.isEnabled();
125+
tf1.setEnabled(!b);
126+
tf2.setEnabled(!b);
127+
}
128+
});
129+
130+
jFrame.pack();
131+
jFrame.setLocationRelativeTo(null);
132+
jFrame.setVisible(true);
133+
}
134+
}

0 commit comments

Comments
 (0)