Skip to content

Commit 28df1d9

Browse files
committed
8273043: [TEST_BUG] Automate NimbusJTreeSelTextColor.java
Backport-of: 270a9d929307dce52e6961e57492c03a633ed044
1 parent fec638f commit 28df1d9

File tree

1 file changed

+91
-101
lines changed

1 file changed

+91
-101
lines changed

test/jdk/javax/swing/plaf/nimbus/NimbusJTreeSelTextColor.java

Lines changed: 91 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -22,140 +22,130 @@
2222
*/
2323
/*
2424
* @test
25-
* @bug 8266510 8271315
26-
* @summary Verifies Nimbus JTree default tree cell renderer use selected text color
27-
* @run main/manual NimbusJTreeSelTextColor
25+
* @bug 8266510 8271315 8273043
26+
* @summary Verifies Nimbus JTree default tree cell renderer uses selected text color
27+
* @requires os.family != "mac"
28+
* @run main/othervm -Dawt.useSystemAAFontSettings=off NimbusJTreeSelTextColor
2829
*/
29-
import java.util.concurrent.CountDownLatch;
30-
import java.util.concurrent.TimeUnit;
30+
3131
import java.awt.Color;
32-
import java.awt.GridBagLayout;
33-
import java.awt.Insets;
34-
import java.awt.GridBagConstraints;
35-
import java.awt.event.ActionEvent;
36-
import java.awt.event.ActionListener;
37-
import java.awt.event.WindowAdapter;
38-
import java.awt.event.WindowEvent;
39-
import javax.swing.JButton;
40-
import javax.swing.JComponent;
32+
import java.awt.Dimension;
33+
import java.awt.Graphics;
34+
import java.awt.image.BufferedImage;
35+
import java.io.File;
36+
import java.io.IOException;
37+
import javax.imageio.ImageIO;
4138
import javax.swing.JFrame;
42-
import javax.swing.JPanel;
43-
import javax.swing.JTextArea;
4439
import javax.swing.JTree;
4540
import javax.swing.SwingUtilities;
46-
import javax.swing.tree.DefaultTreeCellRenderer;
4741
import javax.swing.UIManager;
42+
import javax.swing.tree.DefaultTreeCellRenderer;
43+
44+
import static java.awt.image.BufferedImage.TYPE_INT_RGB;
4845

4946
public class NimbusJTreeSelTextColor {
5047

51-
private static JFrame frame;
52-
private static JTree tree;
53-
private static DefaultTreeCellRenderer treeCellRenderer;
54-
private static volatile CountDownLatch countDownLatch;
55-
private static volatile boolean testResult;
48+
private static int iconOffset;
49+
private static Color foregroundColor;
50+
private static Color backgroundColor;
51+
52+
private static volatile Exception testFailed;
5653

57-
private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n"
58-
+ "Verify selected text color is same as selected tree leaf icon color.\n "
59-
+ "If the color is same ie, white\n"
60-
+ "then press Pass otherwise press Fail.";
54+
private static final String FILENAME = "image.png";
6155

62-
public static void main(String args[]) throws Exception{
63-
countDownLatch = new CountDownLatch(1);
56+
public static void main(String[] args) throws Exception {
57+
final boolean showFrame = args.length >= 1 && args[0].equals("-show");
6458

65-
SwingUtilities.invokeAndWait(NimbusJTreeSelTextColor::createUI);
66-
countDownLatch.await(5, TimeUnit.MINUTES);
59+
// Disable text antialiasing
60+
System.setProperty("awt.useSystemAAFontSettings", "off");
6761

68-
if (!testResult) {
69-
throw new RuntimeException("Selected text color not same as selected tree leaf icon color!");
62+
SwingUtilities.invokeAndWait(() -> runTest(showFrame));
63+
64+
if (testFailed != null) {
65+
throw testFailed;
7066
}
7167
}
7268

73-
private static void createUI() {
69+
private static void runTest(final boolean showFrame) {
7470
try {
7571
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
7672
} catch (Exception e) {
7773
throw new RuntimeException(e);
7874
}
7975

80-
JFrame mainFrame = new JFrame();
81-
GridBagLayout layout = new GridBagLayout();
82-
JPanel mainControlPanel = new JPanel(layout);
83-
JPanel resultButtonPanel = new JPanel(layout);
84-
85-
GridBagConstraints gbc = new GridBagConstraints();
86-
87-
gbc.gridx = 0;
88-
gbc.gridy = 0;
89-
gbc.insets = new Insets(5, 15, 5, 15);
90-
gbc.fill = GridBagConstraints.HORIZONTAL;
91-
mainControlPanel.add(createComponent(), gbc);
92-
93-
JTextArea instructionTextArea = new JTextArea();
94-
instructionTextArea.setText(INSTRUCTIONS);
95-
instructionTextArea.setEditable(false);
96-
instructionTextArea.setBackground(Color.white);
97-
98-
gbc.gridx = 0;
99-
gbc.gridy = 1;
100-
gbc.fill = GridBagConstraints.HORIZONTAL;
101-
mainControlPanel.add(instructionTextArea, gbc);
102-
103-
JButton passButton = new JButton("Pass");
104-
passButton.setActionCommand("Pass");
105-
passButton.addActionListener((ActionEvent e) -> {
106-
testResult = true;
107-
mainFrame.dispose();
108-
countDownLatch.countDown();
109-
110-
});
111-
112-
JButton failButton = new JButton("Fail");
113-
failButton.setActionCommand("Fail");
114-
failButton.addActionListener(new ActionListener() {
115-
@Override
116-
public void actionPerformed(ActionEvent e) {
117-
mainFrame.dispose();
118-
countDownLatch.countDown();
119-
}
120-
});
76+
final JTree tree = createTree();
77+
Dimension size = tree.getPreferredSize();
78+
tree.setSize(size);
12179

122-
gbc.gridx = 0;
123-
gbc.gridy = 0;
80+
BufferedImage im = new BufferedImage(size.width, size.height,
81+
TYPE_INT_RGB);
82+
Graphics g = im.getGraphics();
83+
tree.paint(g);
84+
g.dispose();
12485

125-
resultButtonPanel.add(passButton, gbc);
126-
gbc.gridx = 1;
127-
gbc.gridy = 0;
128-
resultButtonPanel.add(failButton, gbc);
86+
if (showFrame) {
87+
showFrame(tree);
88+
}
89+
90+
size.height /= 4; // height of one row
91+
size.width -= iconOffset;
92+
size.width -= 2; // crop selection border on the right
93+
checkColors(im, iconOffset, size.height / 2, size.width);
94+
}
12995

130-
gbc.gridx = 0;
131-
gbc.gridy = 2;
132-
mainControlPanel.add(resultButtonPanel, gbc);
96+
private static void showFrame(final JTree tree) {
97+
JFrame frame = new JFrame("Nimbus Tree selected color");
98+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
13399

134-
mainFrame.add(mainControlPanel);
135-
mainFrame.pack();
100+
frame.getContentPane().add(tree);
136101

137-
mainFrame.addWindowListener(new WindowAdapter() {
102+
frame.pack();
103+
frame.setLocationRelativeTo(null);
104+
frame.setVisible(true);
105+
}
138106

139-
@Override
140-
public void windowClosing(WindowEvent e) {
141-
mainFrame.dispose();
142-
countDownLatch.countDown();
107+
private static void checkColors(final BufferedImage image,
108+
final int iconOffset,
109+
final int y,
110+
final int width) {
111+
final int foreground = foregroundColor.getRGB();
112+
final int background = backgroundColor.getRGB();
113+
114+
for (int x = iconOffset; x < width; x++) {
115+
int rgb = image.getRGB(x, y);
116+
if (rgb != foreground && rgb != background) {
117+
testFailed = new RuntimeException(
118+
"Unexpected color found: " + Integer.toHexString(rgb)
119+
+ " at (" + x + ", " + y + ");"
120+
+ " foreground: " + Integer.toHexString(foreground) + ";"
121+
+ " background: " + Integer.toHexString(background)
122+
+ " - check " + FILENAME);
123+
save(image);
143124
}
144-
});
145-
mainFrame.setLocationRelativeTo(null);
146-
mainFrame.setVisible(true);
125+
}
147126
}
148127

149-
private static JComponent createComponent() {
150-
tree = new JTree();
128+
private static JTree createTree() {
129+
DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer();
130+
iconOffset = cellRenderer.getOpenIcon().getIconWidth()
131+
+ cellRenderer.getIconTextGap();
132+
foregroundColor = (Color) UIManager.get("Tree.selectionForeground");
133+
backgroundColor = (Color) UIManager.get("Tree.selectionBackground");
151134

152-
treeCellRenderer = new DefaultTreeCellRenderer();
135+
JTree tree = new JTree();
153136
tree.setRootVisible(true);
154-
tree.setShowsRootHandles(true);
155-
156-
tree.setCellRenderer(treeCellRenderer);
157-
tree.setSelectionRow(1);
137+
tree.setShowsRootHandles(false);
138+
tree.setCellRenderer(cellRenderer);
139+
tree.setSelectionRow(0);
158140
return tree;
159141
}
160-
}
161142

143+
private static void save(final BufferedImage img) {
144+
try {
145+
ImageIO.write(img, "png", new File(FILENAME));
146+
} catch (IOException e) {
147+
throw new RuntimeException(e);
148+
}
149+
}
150+
151+
}

0 commit comments

Comments
 (0)