Skip to content

Commit bf8a881

Browse files
committed
8352733: Improve RotFontBoundsTest test
Use PassFailJFrame framework for creating manual test UI. Provide check boxes for each degree of rotation which allow hiding and showing the specified rotation. Provide 'Select All' and 'Clear All' buttons. Display Java version at the bottom of the test. Reviewed-by: prr, azvegint, abhiscxk, honkar
1 parent 3571664 commit bf8a881

File tree

1 file changed

+257
-0
lines changed

1 file changed

+257
-0
lines changed
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
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+
import java.awt.Color;
25+
import java.awt.Component;
26+
import java.awt.Dimension;
27+
import java.awt.FlowLayout;
28+
import java.awt.Font;
29+
import java.awt.Graphics;
30+
import java.awt.Graphics2D;
31+
import java.awt.event.ActionEvent;
32+
import java.awt.font.FontRenderContext;
33+
import java.awt.font.TextLayout;
34+
import java.awt.geom.AffineTransform;
35+
import java.awt.geom.Rectangle2D;
36+
import java.beans.PropertyChangeEvent;
37+
import java.beans.PropertyChangeListener;
38+
import java.util.Arrays;
39+
40+
import javax.swing.AbstractAction;
41+
import javax.swing.Box;
42+
import javax.swing.JButton;
43+
import javax.swing.JCheckBox;
44+
import javax.swing.JComponent;
45+
import javax.swing.JLabel;
46+
import javax.swing.JPanel;
47+
import javax.swing.UIManager;
48+
49+
import static javax.swing.BorderFactory.createEmptyBorder;
50+
51+
/*
52+
* @test
53+
* @bug 4650997
54+
* @summary rotate a TextLayout and verify that the bounds are correct
55+
* @library /java/awt/regtesthelpers
56+
* @build PassFailJFrame
57+
* @run main/manual RotFontBoundsTest
58+
*/
59+
public final class RotFontBoundsTest {
60+
private static final String TEXT = ".This is a STRINg.";
61+
62+
private static final String INSTRUCTIONS =
63+
"A string \u201C" + TEXT + "\u201D is drawn at eight different "
64+
+ "angles, and eight boxes that surround the bounds of the text "
65+
+ "layouts (give or take a pixel) are drawn in red. The boxes "
66+
+ "are always composed of horizontal and vertical lines \u2014 "
67+
+ "they are not rotated.\n"
68+
+ "\n"
69+
+ "By default, all the rotations are displayed. Select or clear "
70+
+ "a check box with an angle to show or hide a particular "
71+
+ "rotation. Click \"Select All\" or \"Clear All\" to show all "
72+
+ "the rotations or to hide them.\n"
73+
+ "\n"
74+
+ "Click the Pass button if each box encloses its corresponding "
75+
+ "text layout.\n"
76+
+ "Otherwise, click Screenshot to save a screenshot for failure "
77+
+ "analysis and then click Fail.";
78+
79+
private static boolean verbose;
80+
81+
public static void main(String[] args) throws Exception {
82+
verbose = (args.length > 0 && args[0].equalsIgnoreCase("verbose"));
83+
84+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
85+
86+
PassFailJFrame.builder()
87+
.instructions(INSTRUCTIONS)
88+
.rows(20)
89+
.columns(50)
90+
.testTimeOut(15)
91+
.screenCapture()
92+
.testUI(RotFontBoundsTest::createUI)
93+
.build()
94+
.awaitAndCheck();
95+
}
96+
97+
private static final int ROTATIONS = 8;
98+
99+
private static JComponent createUI() {
100+
final RotatedTextBounds rotatedText = new RotatedTextBounds();
101+
102+
final JPanel checkBoxes = new JPanel(new FlowLayout(FlowLayout.CENTER,
103+
4, 4));
104+
checkBoxes.setBorder(createEmptyBorder(0, 8, 8, 8));
105+
for (int i = 0; i < ROTATIONS; i++) {
106+
checkBoxes.add(new JCheckBox(new SelectRotationAction(i, rotatedText)));
107+
}
108+
109+
JButton selectAll = new JButton("Select All");
110+
selectAll.addActionListener(
111+
e -> selectAllCheckBoxes(checkBoxes.getComponents(), true));
112+
selectAll.setMnemonic('S');
113+
114+
JButton clearAll = new JButton("Clear All");
115+
clearAll.addActionListener(
116+
e -> selectAllCheckBoxes(checkBoxes.getComponents(), false));
117+
clearAll.setMnemonic('C');
118+
119+
Box controls = Box.createHorizontalBox();
120+
controls.add(new JLabel("Visible Rotations:"));
121+
controls.add(Box.createHorizontalGlue());
122+
controls.add(selectAll);
123+
controls.add(Box.createHorizontalStrut(4));
124+
controls.add(clearAll);
125+
controls.setBorder(createEmptyBorder(8, 8, 0, 8));
126+
127+
Box controlPanel = Box.createVerticalBox();
128+
controlPanel.add(controls);
129+
controlPanel.add(checkBoxes);
130+
131+
Box javaVersion = Box.createHorizontalBox();
132+
javaVersion.setBorder(createEmptyBorder(8, 8, 8, 8));
133+
javaVersion.add(new JLabel("Java version: "
134+
+ System.getProperty("java.runtime.version")));
135+
javaVersion.add(Box.createHorizontalGlue());
136+
137+
Box main = Box.createVerticalBox();
138+
main.setName("Rotated TextLayout Test");
139+
main.add(controlPanel);
140+
main.add(rotatedText);
141+
main.add(javaVersion);
142+
143+
return main;
144+
}
145+
146+
private static final class RotatedTextBounds extends JComponent {
147+
private final Font font = new Font(Font.DIALOG, Font.PLAIN, 24);
148+
149+
private final boolean[] rotationVisible = new boolean[ROTATIONS];
150+
151+
private RotatedTextBounds() {
152+
setBackground(Color.WHITE);
153+
setPreferredSize(new Dimension(400, 400));
154+
Arrays.fill(rotationVisible, true);
155+
}
156+
157+
public void setRotationVisible(int rotation, boolean visible) {
158+
rotationVisible[rotation] = visible;
159+
repaint();
160+
}
161+
162+
// Counts the number of paints
163+
private int counter = 0;
164+
165+
@Override
166+
public void paintComponent(Graphics _g) {
167+
Graphics2D g = (Graphics2D) _g;
168+
Dimension d = getSize();
169+
170+
g.setColor(getBackground());
171+
g.fillRect(0, 0, d.width, d.height);
172+
173+
counter++;
174+
int x = d.width / 2;
175+
int y = d.height / 2;
176+
FontRenderContext frc = g.getFontRenderContext();
177+
178+
for (int i = 0; i < ROTATIONS; i++) {
179+
if (!rotationVisible[i]) {
180+
continue;
181+
}
182+
183+
double angle = -Math.PI / 4.0 * i;
184+
AffineTransform flip = AffineTransform.getRotateInstance(angle);
185+
Font flippedFont = font.deriveFont(flip);
186+
TextLayout tl = new TextLayout(TEXT, flippedFont, frc);
187+
Rectangle2D bb = tl.getBounds();
188+
g.setPaint(Color.BLACK);
189+
tl.draw(g, x, y);
190+
g.setPaint(Color.RED);
191+
g.drawRect(x + (int) bb.getX(), y + (int) bb.getY(),
192+
(int) bb.getWidth(), (int) bb.getHeight());
193+
194+
if (verbose) {
195+
if (counter == 1) {
196+
printDetails(angle, tl);
197+
} else if (i == 0) {
198+
System.out.println("Paint, counter=" + counter);
199+
}
200+
}
201+
}
202+
}
203+
204+
private static void printDetails(double angle, TextLayout tl) {
205+
System.out.println("Angle: " + angle);
206+
System.out.println("getAscent: " + tl.getAscent());
207+
System.out.println("getAdvance: " + tl.getAdvance());
208+
System.out.println("getBaseline: " + tl.getBaseline());
209+
System.out.println("getBounds: " + tl.getBounds());
210+
System.out.println("getDescent: " + tl.getDescent());
211+
System.out.println("getLeading: " + tl.getLeading());
212+
System.out.println("getVisibleAdvance: " + tl.getVisibleAdvance());
213+
System.out.println(".");
214+
}
215+
}
216+
217+
private static final class SelectRotationAction
218+
extends AbstractAction
219+
implements PropertyChangeListener {
220+
private final int rotation;
221+
private final RotatedTextBounds rotatedText;
222+
223+
private SelectRotationAction(int rotation,
224+
RotatedTextBounds rotatedText) {
225+
super(rotation * (360 / ROTATIONS) + "\u00B0");
226+
this.rotation = rotation;
227+
this.rotatedText = rotatedText;
228+
229+
putValue(SELECTED_KEY, true);
230+
231+
addPropertyChangeListener(this);
232+
}
233+
234+
private void updateRotationVisible() {
235+
rotatedText.setRotationVisible(rotation,
236+
(Boolean) getValue(SELECTED_KEY));
237+
}
238+
239+
@Override
240+
public void actionPerformed(ActionEvent e) {
241+
updateRotationVisible();
242+
}
243+
244+
@Override
245+
public void propertyChange(PropertyChangeEvent evt) {
246+
if (evt.getPropertyName().equals(SELECTED_KEY)) {
247+
updateRotationVisible();
248+
}
249+
}
250+
}
251+
252+
private static void selectAllCheckBoxes(Component[] checkBoxes,
253+
boolean visible) {
254+
Arrays.stream(checkBoxes)
255+
.forEach(c -> ((JCheckBox) c).setSelected(visible));
256+
}
257+
}

0 commit comments

Comments
 (0)