Skip to content

Commit 9d332e6

Browse files
committed
8307193: Several Swing jtreg tests use class.forName on L&F classes
Reviewed-by: abhiscxk, prr
1 parent 98f6a80 commit 9d332e6

File tree

2 files changed

+326
-0
lines changed

2 files changed

+326
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2007, 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+
* @test
25+
* @bug 4211731 4214512
26+
* @summary
27+
* This test checks if menu bars lay out correctly when their
28+
* ComponentOrientation property is set to RIGHT_TO_LEFT. This test is
29+
* manual. The tester is asked to compare left-to-right and
30+
* right-to-left menu bars and judge whether they are mirror images of each
31+
* other.
32+
* @library /test/jdk/java/awt/regtesthelpers
33+
* @build PassFailJFrame
34+
* @run main/manual RightLeftOrientation
35+
*/
36+
37+
import java.awt.ComponentOrientation;
38+
import java.awt.Point;
39+
import java.awt.event.ActionEvent;
40+
import java.awt.event.ActionListener;
41+
import javax.swing.ButtonGroup;
42+
import javax.swing.JFrame;
43+
import javax.swing.JMenu;
44+
import javax.swing.JMenuBar;
45+
import javax.swing.JPanel;
46+
import javax.swing.JRadioButton;
47+
import javax.swing.SwingUtilities;
48+
import javax.swing.UIManager;
49+
50+
public class RightLeftOrientation {
51+
52+
static JFrame ltrFrame;
53+
static JFrame rtlFrame;
54+
55+
private static final String INSTRUCTIONS = """
56+
This test checks menu bars for correct Right-To-Left Component Orientation.
57+
58+
You should see two frames, each containing a menu bar.
59+
60+
One frame will be labelled "Left To Right" and will contain
61+
a menu bar with menus starting on its left side.
62+
The other frame will be labelled "Right To Left" and will
63+
contain a menu bar with menus starting on its right side.
64+
65+
The test will also contain radio buttons that can be used to set
66+
the look and feel of the menu bars.
67+
For each look and feel, you should compare the two menu
68+
bars and make sure they are mirror images of each other. """;
69+
70+
public static void main(String[] args) throws Exception {
71+
PassFailJFrame.builder()
72+
.title("RTL test Instructions")
73+
.instructions(INSTRUCTIONS)
74+
.rows((int) INSTRUCTIONS.lines().count() + 2)
75+
.columns(30)
76+
.testUI(RightLeftOrientation::createTestUI)
77+
.build()
78+
.awaitAndCheck();
79+
}
80+
81+
private static JFrame createTestUI() {
82+
JFrame frame = new JFrame("RightLeftOrientation");
83+
JPanel panel = new JPanel();
84+
85+
ButtonGroup group = new ButtonGroup();
86+
JRadioButton rb;
87+
ActionListener plafChanger = new PlafChanger();
88+
89+
UIManager.LookAndFeelInfo[] lafInfos = UIManager.getInstalledLookAndFeels();
90+
for (int i = 0; i < lafInfos.length; i++) {
91+
rb = new JRadioButton(lafInfos[i].getName());
92+
rb.setActionCommand(lafInfos[i].getClassName());
93+
rb.addActionListener(plafChanger);
94+
group.add(rb);
95+
panel.add(rb);
96+
if (i == 0) {
97+
rb.setSelected(true);
98+
}
99+
}
100+
101+
frame.add(panel);
102+
103+
ltrFrame = new JFrame("Left To Right");
104+
ltrFrame.setJMenuBar(createMenuBar(ComponentOrientation.LEFT_TO_RIGHT));
105+
ltrFrame.setSize(400, 100);
106+
ltrFrame.setLocation(new Point(10, 10));
107+
ltrFrame.setVisible(true);
108+
109+
rtlFrame = new JFrame("Right To Left");
110+
rtlFrame.setJMenuBar(createMenuBar(ComponentOrientation.RIGHT_TO_LEFT));
111+
rtlFrame.setSize(400, 100);
112+
rtlFrame.setLocation(new Point(10, 120));
113+
rtlFrame.setVisible(true);
114+
frame.pack();
115+
return frame;
116+
}
117+
118+
static class PlafChanger implements ActionListener {
119+
public void actionPerformed(ActionEvent e) {
120+
String lnfName = e.getActionCommand();
121+
122+
try {
123+
UIManager.setLookAndFeel(lnfName);
124+
SwingUtilities.updateComponentTreeUI(ltrFrame);
125+
SwingUtilities.updateComponentTreeUI(rtlFrame);
126+
}
127+
catch (Exception exc) {
128+
System.err.println("Could not load LookAndFeel: " + lnfName);
129+
}
130+
131+
}
132+
}
133+
134+
135+
static JMenuBar createMenuBar(ComponentOrientation o) {
136+
JMenuBar menuBar = new JMenuBar();
137+
menuBar.setComponentOrientation(o);
138+
139+
JMenu menu = new JMenu("One");
140+
menu.setComponentOrientation(o);
141+
menuBar.add(menu);
142+
143+
menu = new JMenu("Two");
144+
menu.setComponentOrientation(o);
145+
menuBar.add(menu);
146+
147+
menu = new JMenu("Three");
148+
menu.setComponentOrientation(o);
149+
menuBar.add(menu);
150+
151+
return menuBar;
152+
}
153+
154+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright (c) 2007, 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+
* @test
25+
* @bug 4214514
26+
* @summary
27+
* This test checks if tool bars lay out correctly when their
28+
* ComponentOrientation property is set to RIGHT_TO_LEFT. This test is
29+
* manual. The tester is asked to compare left-to-right and
30+
* right-to-left tool bars and judge whether they are mirror images of each
31+
* other.
32+
* @library /test/jdk/java/awt/regtesthelpers
33+
* @build PassFailJFrame
34+
* @run main/manual RightLeftOrientation
35+
*/
36+
37+
import java.awt.BorderLayout;
38+
import java.awt.Color;
39+
import java.awt.ComponentOrientation;
40+
import java.awt.Container;
41+
import java.awt.Point;
42+
import java.awt.event.ActionEvent;
43+
import java.awt.event.ActionListener;
44+
import javax.swing.ButtonGroup;
45+
import javax.swing.JButton;
46+
import javax.swing.JFrame;
47+
import javax.swing.JPanel;
48+
import javax.swing.JRadioButton;
49+
import javax.swing.JToolBar;
50+
import javax.swing.SwingUtilities;
51+
import javax.swing.UIManager;
52+
53+
public class RightLeftOrientation {
54+
55+
static JFrame ltrFrame;
56+
static JFrame rtlFrame;
57+
58+
private static final String INSTRUCTIONS = """
59+
This test checks tool bars for correct Right-To-Left Component Orientation.
60+
61+
You should see two frames, each containing a tool bar.
62+
63+
One frame will be labelled "Left To Right" and will contain
64+
a tool bar with buttons starting on its left side.
65+
The other frame will be labelled "Right To Left" and will
66+
contain a tool bar with buttons starting on its right side.
67+
68+
The test will also contain radio buttons that can be used to set
69+
the look and feel of the tool bars.
70+
For each look and feel, you should compare the two tool bars and
71+
make sure they are mirror images of each other.
72+
You should also drag the tool bars to each corner of the frame
73+
to make sure the docking behavior is consistent between the two frames.""";
74+
75+
public static void main(String[] args) throws Exception {
76+
PassFailJFrame.builder()
77+
.title("RTL test Instructions")
78+
.instructions(INSTRUCTIONS)
79+
.rows((int) INSTRUCTIONS.lines().count() + 2)
80+
.columns(35)
81+
.testUI(RightLeftOrientation::createTestUI)
82+
.build()
83+
.awaitAndCheck();
84+
}
85+
86+
private static JFrame createTestUI() {
87+
JFrame frame = new JFrame("RightLeftOrientation");
88+
JPanel panel = new JPanel();
89+
90+
ButtonGroup group = new ButtonGroup();
91+
JRadioButton rb;
92+
ActionListener plafChanger = new PlafChanger();
93+
94+
UIManager.LookAndFeelInfo[] lafInfos = UIManager.getInstalledLookAndFeels();
95+
for (int i = 0; i < lafInfos.length; i++) {
96+
rb = new JRadioButton(lafInfos[i].getName());
97+
rb.setActionCommand(lafInfos[i].getClassName());
98+
rb.addActionListener(plafChanger);
99+
group.add(rb);
100+
panel.add(rb);
101+
if (i == 0) {
102+
rb.setSelected(true);
103+
}
104+
}
105+
106+
frame.add(panel);
107+
108+
ltrFrame = new JFrame("Left To Right");
109+
Container contentPane = ltrFrame.getContentPane();
110+
contentPane.setLayout(new BorderLayout());
111+
panel = new JPanel();
112+
panel.setBackground(Color.white);
113+
contentPane.add("Center",panel);
114+
contentPane.add("North",
115+
createToolBar(ComponentOrientation.LEFT_TO_RIGHT));
116+
ltrFrame.setSize(400, 140);
117+
ltrFrame.setLocation(new Point(10, 10));
118+
ltrFrame.setVisible(true);
119+
120+
rtlFrame = new JFrame("Right To Left");
121+
contentPane = rtlFrame.getContentPane();
122+
contentPane.setLayout(new BorderLayout());
123+
panel = new JPanel();
124+
panel.setBackground(Color.white);
125+
contentPane.add("Center",panel);
126+
contentPane.add("North",
127+
createToolBar(ComponentOrientation.RIGHT_TO_LEFT));
128+
rtlFrame.setSize(400, 140);
129+
rtlFrame.setLocation(new Point(420, 10));
130+
rtlFrame.setVisible(true);
131+
132+
frame.pack();
133+
return frame;
134+
}
135+
136+
static class PlafChanger implements ActionListener {
137+
public void actionPerformed(ActionEvent e) {
138+
String lnfName = e.getActionCommand();
139+
140+
try {
141+
UIManager.setLookAndFeel(lnfName);
142+
SwingUtilities.updateComponentTreeUI(ltrFrame);
143+
SwingUtilities.updateComponentTreeUI(rtlFrame);
144+
}
145+
catch (Exception exc) {
146+
System.err.println("Could not load LookAndFeel: " + lnfName);
147+
}
148+
149+
}
150+
}
151+
152+
153+
static JToolBar createToolBar(ComponentOrientation o) {
154+
JToolBar toolBar = new JToolBar();
155+
toolBar.setComponentOrientation(o);
156+
157+
JButton button = new JButton("One");
158+
button.setComponentOrientation(o);
159+
toolBar.add(button);
160+
161+
button = new JButton("Two");
162+
button.setComponentOrientation(o);
163+
toolBar.add(button);
164+
165+
button = new JButton("Three");
166+
button.setComponentOrientation(o);
167+
toolBar.add(button);
168+
169+
return toolBar;
170+
}
171+
172+
}

0 commit comments

Comments
 (0)