|
22 | 22 | */ |
23 | 23 | /* |
24 | 24 | * @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 |
28 | 29 | */ |
29 | | -import java.util.concurrent.CountDownLatch; |
30 | | -import java.util.concurrent.TimeUnit; |
| 30 | + |
31 | 31 | 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; |
41 | 38 | import javax.swing.JFrame; |
42 | | -import javax.swing.JPanel; |
43 | | -import javax.swing.JTextArea; |
44 | 39 | import javax.swing.JTree; |
45 | 40 | import javax.swing.SwingUtilities; |
46 | | -import javax.swing.tree.DefaultTreeCellRenderer; |
47 | 41 | import javax.swing.UIManager; |
| 42 | +import javax.swing.tree.DefaultTreeCellRenderer; |
| 43 | + |
| 44 | +import static java.awt.image.BufferedImage.TYPE_INT_RGB; |
48 | 45 |
|
49 | 46 | public class NimbusJTreeSelTextColor { |
50 | 47 |
|
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; |
56 | 53 |
|
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"; |
61 | 55 |
|
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"); |
64 | 58 |
|
65 | | - SwingUtilities.invokeAndWait(NimbusJTreeSelTextColor::createUI); |
66 | | - countDownLatch.await(5, TimeUnit.MINUTES); |
| 59 | + // Disable text antialiasing |
| 60 | + System.setProperty("awt.useSystemAAFontSettings", "off"); |
67 | 61 |
|
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; |
70 | 66 | } |
71 | 67 | } |
72 | 68 |
|
73 | | - private static void createUI() { |
| 69 | + private static void runTest(final boolean showFrame) { |
74 | 70 | try { |
75 | 71 | UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); |
76 | 72 | } catch (Exception e) { |
77 | 73 | throw new RuntimeException(e); |
78 | 74 | } |
79 | 75 |
|
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); |
121 | 79 |
|
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(); |
124 | 85 |
|
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 | + } |
129 | 95 |
|
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); |
133 | 99 |
|
134 | | - mainFrame.add(mainControlPanel); |
135 | | - mainFrame.pack(); |
| 100 | + frame.getContentPane().add(tree); |
136 | 101 |
|
137 | | - mainFrame.addWindowListener(new WindowAdapter() { |
| 102 | + frame.pack(); |
| 103 | + frame.setLocationRelativeTo(null); |
| 104 | + frame.setVisible(true); |
| 105 | + } |
138 | 106 |
|
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); |
143 | 124 | } |
144 | | - }); |
145 | | - mainFrame.setLocationRelativeTo(null); |
146 | | - mainFrame.setVisible(true); |
| 125 | + } |
147 | 126 | } |
148 | 127 |
|
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"); |
151 | 134 |
|
152 | | - treeCellRenderer = new DefaultTreeCellRenderer(); |
| 135 | + JTree tree = new JTree(); |
153 | 136 | 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); |
158 | 140 | return tree; |
159 | 141 | } |
160 | | -} |
161 | 142 |
|
| 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