Skip to content

Commit 16996c0

Browse files
author
Satyen Subramaniam
committed
8353309: Open source several Swing text tests
8359418: Test "javax/swing/text/GlyphView/bug4188841.java" failed because the phrase of text pane does not match the instructions Reviewed-by: serb Backport-of: 31a6de2e743923c92e976d5f5536120736d56029
1 parent 269a925 commit 16996c0

File tree

13 files changed

+577
-0
lines changed

13 files changed

+577
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
/* @test
25+
* @bug 4519537 4522866
26+
* @summary Tests that text and components in paragraph views line up at their baselines.
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @run main/manual BaselineTest
30+
*/
31+
32+
import java.awt.Color;
33+
import java.awt.Dimension;
34+
import java.awt.Font;
35+
import java.awt.Graphics;
36+
import java.awt.GridLayout;
37+
38+
import javax.swing.JFrame;
39+
import javax.swing.JLabel;
40+
import javax.swing.JScrollPane;
41+
import javax.swing.JTextPane;
42+
43+
import javax.swing.text.ComponentView;
44+
import javax.swing.text.BadLocationException;
45+
import javax.swing.text.Document;
46+
import javax.swing.text.Element;
47+
import javax.swing.text.SimpleAttributeSet;
48+
import javax.swing.text.StyleConstants;
49+
import javax.swing.text.StyledEditorKit;
50+
import javax.swing.text.View;
51+
import javax.swing.text.ViewFactory;
52+
53+
public class BaselineTest {
54+
55+
static final String INSTRUCTIONS = """
56+
Test that components displayed in a JTextPane properly respect their vertical alignment.
57+
There are two text panes, stacked vertically with similar content except the bottom components are taller.
58+
The content consists of a leading and trailing text string, with pink coloured components between.
59+
The text string content means the strings 'Default Size Text' and 'Large Size Text'.
60+
Text content baseline is at the bottom of CAPITAL letters in the text.
61+
Each pink component has a string displaying its alignment setting in the range 0.0 to 1.0
62+
NB: The position of the strings "align = 0.0" etc is not important, it is the component position that matters.
63+
0.0 means it should be aligned with its top at the text content baseline,
64+
1.0 means it should be aligned with its bottom at the text content baseline.
65+
A value in between will be a proportional alignment, eg 0.5 is centered on the text content baseline
66+
If the content displays as described, the test PASSES.
67+
""";
68+
69+
public static void main(String[] args) throws Exception {
70+
PassFailJFrame.builder()
71+
.title("BaselineTest Test Instructions")
72+
.instructions(INSTRUCTIONS)
73+
.columns(60)
74+
.rows(12)
75+
.testUI(BaselineTest::createUI)
76+
.positionTestUIBottomRowCentered()
77+
.build()
78+
.awaitAndCheck();
79+
}
80+
81+
public static JFrame createUI() {
82+
JFrame frame = new JFrame("BaselineTest");
83+
frame.setLayout(new GridLayout(2, 1));
84+
85+
JTextPane prefPane = new JTextPane();
86+
initJTextPane(prefPane);
87+
frame.add(new JScrollPane(prefPane));
88+
89+
JTextPane variablePane = new JTextPane();
90+
variablePane.setEditorKit(new CustomEditorKit());
91+
initJTextPane(variablePane);
92+
frame.add(new JScrollPane(variablePane));
93+
frame.setSize(800, 400);
94+
return frame;
95+
}
96+
97+
static void initJTextPane(JTextPane tp) {
98+
99+
try {
100+
Document doc = tp.getDocument();
101+
102+
doc.insertString(0, " Default Size Text ", null);
103+
tp.setCaretPosition(doc.getLength());
104+
tp.insertComponent(new PaintLabel(0.0f));
105+
tp.insertComponent(new PaintLabel(0.2f));
106+
tp.insertComponent(new PaintLabel(0.5f));
107+
tp.insertComponent(new PaintLabel(0.7f));
108+
tp.insertComponent(new PaintLabel(1.0f));
109+
SimpleAttributeSet set = new SimpleAttributeSet();
110+
StyleConstants.setFontSize(set, 20);
111+
tp.setCaretPosition(doc.getLength());
112+
doc.insertString(doc.getLength(), " Large Size Text ", set);
113+
} catch (BadLocationException ble) {
114+
throw new RuntimeException(ble);
115+
}
116+
}
117+
118+
static class PaintLabel extends JLabel {
119+
120+
private int pref = 40;
121+
private int min = pref - 30;
122+
private int max = pref + 30;
123+
124+
public PaintLabel(float align) {
125+
126+
setAlignmentY(align);
127+
String alignStr = String.valueOf(align);
128+
129+
setText("align = " + alignStr);
130+
setOpaque(true);
131+
setBackground(Color.PINK);
132+
}
133+
134+
public Dimension getMinimumSize() {
135+
return new Dimension(super.getMinimumSize().width, min);
136+
}
137+
138+
public Dimension getPreferredSize() {
139+
return new Dimension(super.getPreferredSize().width, pref);
140+
}
141+
142+
public Dimension getMaximumSize() {
143+
return new Dimension(super.getMaximumSize().width, max);
144+
}
145+
146+
public void paintComponent(Graphics g) {
147+
g.setColor(Color.PINK);
148+
g.fillRect(0, 0, getWidth(), getHeight());
149+
int y = (int)(getAlignmentY() * getHeight());
150+
g.setColor(Color.BLACK);
151+
g.drawLine(0, y, getWidth(), y);
152+
g.drawString(getText(), 0, 10);
153+
}
154+
}
155+
156+
static class CustomComponentView extends ComponentView {
157+
158+
public CustomComponentView(Element elem) {
159+
super(elem);
160+
}
161+
162+
public int getResizeWeight(int axis) {
163+
return 1;
164+
}
165+
}
166+
167+
static class CustomEditorKit extends StyledEditorKit implements ViewFactory {
168+
169+
public View create(Element elem) {
170+
if (StyleConstants.ComponentElementName.equals(elem.getName())) {
171+
return new CustomComponentView(elem);
172+
} else {
173+
return super.getViewFactory().create(elem);
174+
}
175+
}
176+
177+
public ViewFactory getViewFactory() {
178+
return this;
179+
}
180+
}
181+
182+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2003, 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+
/* @test
25+
* @bug 4188841
26+
* @summary Tests a JTextPane wrapping issue
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @run main/manual bug4188841
30+
*/
31+
32+
import java.awt.Dimension;
33+
import javax.swing.JFrame;
34+
import javax.swing.JScrollPane;
35+
import javax.swing.JTextPane;
36+
37+
public class bug4188841 {
38+
39+
static final String INSTRUCTIONS = """
40+
The text pane contains the phrase "the quick brown fox jumps over the lazy dog",
41+
all the words are separated by tabs. When the test starts, the whole phrase should
42+
appear on one line. If it is wrapped along two or more lines, the test FAILS.
43+
44+
Otherwise, place the text caret in the very end of the line (e.g. by clicking
45+
in the line and hitting End). Press Enter twice. The text should appear as one
46+
line at all times. If the text wraps when you press Enter, the test FAILS.
47+
""";
48+
49+
static class NoWrapTextPane extends JTextPane {
50+
51+
public boolean getScrollableTracksViewportWidth() {
52+
//should not allow text to be wrapped
53+
return false;
54+
}
55+
56+
public void setSize(Dimension d) {
57+
// don't let the Textpane get sized smaller than its parent
58+
if (d.width < getParent().getSize().width) {
59+
super.setSize(getParent().getSize());
60+
}
61+
else {
62+
super.setSize(d);
63+
}
64+
}
65+
}
66+
67+
static JFrame createUI() {
68+
69+
JFrame frame = new JFrame("bug4188841");
70+
71+
NoWrapTextPane nwp = new NoWrapTextPane();
72+
nwp.setText("the\tquick\tbrown\tfox\tjumps\tover\tthe\tlazy\tdog!");
73+
nwp.setCaretPosition(nwp.getText().length());
74+
75+
JScrollPane scrollPane = new JScrollPane(nwp,
76+
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
77+
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
78+
79+
frame.add(scrollPane);
80+
frame.setSize(400, 300);
81+
return frame;
82+
}
83+
84+
public static void main(String args[]) throws Exception {
85+
PassFailJFrame.builder()
86+
.title("Test Instructions")
87+
.instructions(INSTRUCTIONS)
88+
.columns(60)
89+
.testUI(bug4188841::createUI)
90+
.build()
91+
.awaitAndCheck();
92+
}
93+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2003, 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+
/* @test
25+
* @bug 4473401
26+
* @summary Tests if FormSubmitEvent is submitted correctly.
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @run main/manual bug4473401
30+
*/
31+
32+
import java.io.File;
33+
import javax.swing.JEditorPane;
34+
import javax.swing.JFrame;
35+
import javax.swing.event.HyperlinkEvent;
36+
import javax.swing.event.HyperlinkListener;
37+
import javax.swing.text.html.FormSubmitEvent;
38+
import javax.swing.text.html.HTMLEditorKit;
39+
40+
public class bug4473401 implements HyperlinkListener {
41+
42+
static final String INSTRUCTIONS = """
43+
The test window displays an HTML frameset with a frame
44+
on the left and another to the right.
45+
Push the 'Submit Query' button in the left frame to perform testing.
46+
The window should be updated to have only one frame with the
47+
message 'If you see this page the test PASSED'.
48+
If it appears, PASS the test, otherwise FAIL the test.
49+
""";
50+
51+
static JEditorPane jep;
52+
static JFrame createUI() {
53+
54+
JFrame frame = new JFrame("bug4473401");
55+
jep = new JEditorPane();
56+
jep.addHyperlinkListener(new bug4473401());
57+
HTMLEditorKit kit = new HTMLEditorKit();
58+
kit.setAutoFormSubmission(false);
59+
jep.setEditorKit(kit);
60+
jep.setEditable(false);
61+
62+
try {
63+
File file = new File(System.getProperty("test.src", "."), "frameset.html");
64+
System.out.println(file.toURI().toURL());
65+
jep.setPage(file.toURL());
66+
} catch (Exception e) {
67+
throw new RuntimeException(e);
68+
}
69+
70+
frame.add(jep);
71+
frame.setSize(500, 500);
72+
return frame;
73+
}
74+
75+
public void hyperlinkUpdate(HyperlinkEvent e) {
76+
if (e instanceof FormSubmitEvent) {
77+
jep.setText("If you see this page the test<font color=green> PASSED</font></CENTER>");
78+
}
79+
}
80+
81+
public static void main(String args[]) throws Exception {
82+
PassFailJFrame.builder()
83+
.title("Test Instructions")
84+
.instructions(INSTRUCTIONS)
85+
.columns(40)
86+
.testUI(bug4473401::createUI)
87+
.build()
88+
.awaitAndCheck();
89+
}
90+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<body>
3+
Push the <span style="color:blue">Submit query</span> button
4+
<FORM ACTION="./frameresult.html" METHOD=post TARGET="main" >
5+
<input type="text" name="Selection"><BR>
6+
<INPUT type="submit">
7+
</FORM>
8+
</body>
9+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<HTML>
2+
<BODY>
3+
</BODY>
4+
</HTML>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
If you see this page the test <font color=red>FAILED</font>.
4+
</body>
5+
</html>

0 commit comments

Comments
 (0)