Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JCheckBox/4449413/bug4449413.java
41153 views
1
/*
2
* Copyright (c) 2012, 2021, 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 4449413
26
* @summary Tests that checkbox and radiobuttons' check marks are visible when background is black
27
* @author Ilya Boyandin
28
* @run main/manual bug4449413
29
*/
30
31
import javax.swing.AbstractButton;
32
import javax.swing.BoxLayout;
33
import javax.swing.JButton;
34
import javax.swing.JCheckBox;
35
import javax.swing.JCheckBoxMenuItem;
36
import javax.swing.JFrame;
37
import javax.swing.JPanel;
38
import javax.swing.JRadioButton;
39
import javax.swing.JRadioButtonMenuItem;
40
import javax.swing.JTextArea;
41
import javax.swing.SwingUtilities;
42
import javax.swing.UIManager;
43
import javax.swing.plaf.metal.DefaultMetalTheme;
44
import javax.swing.plaf.metal.MetalLookAndFeel;
45
import javax.swing.plaf.metal.MetalTheme;
46
import javax.swing.plaf.metal.OceanTheme;
47
import java.awt.Color;
48
import java.awt.GridLayout;
49
import java.awt.Insets;
50
import java.awt.event.ActionListener;
51
import java.awt.event.ItemEvent;
52
import java.awt.event.WindowAdapter;
53
import java.awt.event.WindowEvent;
54
import java.util.concurrent.CountDownLatch;
55
import java.util.concurrent.TimeUnit;
56
57
public class bug4449413 extends JFrame {
58
59
private static final String INSTRUCTIONS =
60
"There are eight controls with black backgrounds.\n" +
61
"Four enabled (on the left side) and four disabled (on the right side)\n" +
62
"checkboxes and radiobuttons.\n\n" +
63
"1. If at least one of the controls' check marks is not visible:\n" +
64
" the test fails.\n";
65
66
private static final String INSTRUCTIONS_ADDITIONS_METAL =
67
"\n" +
68
"2. Uncheck the \"Use Ocean Theme\" check box.\n" +
69
" If now at least one of the controls' check marks is not visible:\n" +
70
" the test fails.\n";
71
72
private static final CountDownLatch latch = new CountDownLatch(1);
73
private static volatile boolean failed = true;
74
75
private final MetalTheme defaultMetalTheme = new DefaultMetalTheme();
76
private final MetalTheme oceanTheme = new OceanTheme();
77
78
private static bug4449413 instance;
79
80
boolean isMetalLookAndFeel() {
81
return UIManager.getLookAndFeel() instanceof MetalLookAndFeel;
82
}
83
84
public static void main(String[] args) throws Exception {
85
SwingUtilities.invokeLater(() -> {
86
instance = new bug4449413();
87
instance.createAndShowGUI();
88
});
89
90
boolean timeoutHappened = !latch.await(2, TimeUnit.MINUTES);
91
92
SwingUtilities.invokeAndWait(() -> {
93
if (instance != null) {
94
instance.dispose();
95
}
96
});
97
98
System.out.println("Passed: " + !failed);
99
100
if (timeoutHappened || failed) {
101
throw new RuntimeException("Test failed!");
102
}
103
}
104
105
private void createAndShowGUI() {
106
setTitle(UIManager.getLookAndFeel().getClass().getName());
107
108
addComponentsToPane();
109
110
addWindowListener(new WindowAdapter() {
111
@Override
112
public void windowClosing(WindowEvent e) {
113
latch.countDown();
114
}
115
});
116
117
setLocationRelativeTo(null);
118
pack();
119
setVisible(true);
120
}
121
122
public void addComponentsToPane() {
123
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
124
125
JPanel testedPanel = new JPanel();
126
testedPanel.setLayout(new GridLayout(4, 6, 10, 15));
127
for (int k = 0; k <= 3; k++) {
128
for (int j = 1; j >= 0; j--) {
129
AbstractButton b = createButton(j, k);
130
testedPanel.add(b);
131
}
132
}
133
134
add(testedPanel);
135
136
137
if (isMetalLookAndFeel()) {
138
JCheckBox oceanThemeSwitch = new JCheckBox("Use Ocean theme", true);
139
oceanThemeSwitch.addItemListener(e -> {
140
if (e.getStateChange() == ItemEvent.SELECTED) {
141
MetalLookAndFeel.setCurrentTheme(oceanTheme);
142
} else {
143
MetalLookAndFeel.setCurrentTheme(defaultMetalTheme);
144
}
145
SwingUtilities.updateComponentTreeUI(testedPanel);
146
});
147
148
add(oceanThemeSwitch);
149
}
150
151
JTextArea instructionArea = new JTextArea(
152
isMetalLookAndFeel()
153
? INSTRUCTIONS + INSTRUCTIONS_ADDITIONS_METAL
154
: INSTRUCTIONS
155
);
156
157
instructionArea.setEditable(false);
158
instructionArea.setFocusable(false);
159
instructionArea.setMargin(new Insets(10,10,10,10));
160
161
add(instructionArea);
162
163
164
JButton passButton = new JButton("Pass");
165
JButton failButton = new JButton("Fail");
166
167
ActionListener actionListener = e -> {
168
failed = e.getSource() == failButton;
169
latch.countDown();
170
};
171
172
passButton.addActionListener(actionListener);
173
failButton.addActionListener(actionListener);
174
175
JPanel passFailPanel = new JPanel();
176
passFailPanel.add(passButton);
177
passFailPanel.add(failButton);
178
179
add(passFailPanel);
180
}
181
182
static AbstractButton createButton(int enabled, int type) {
183
AbstractButton b = switch (type) {
184
case 0 -> new JRadioButton("RadioButton");
185
case 1 -> new JCheckBox("CheckBox");
186
case 2 -> new JRadioButtonMenuItem("RBMenuItem");
187
case 3 -> new JCheckBoxMenuItem("CBMenuItem");
188
default -> throw new IllegalArgumentException("type should be in range of 0..3");
189
};
190
191
b.setOpaque(true);
192
b.setBackground(Color.black);
193
b.setForeground(Color.white);
194
b.setEnabled(enabled == 1);
195
b.setSelected(true);
196
return b;
197
}
198
}
199
200