8075915: The eight controls without black backgrounds with WinLAF & GTK LAF & Nimbus LAF

Reviewed-by: psadhukhan, kizune, aivanov
This commit is contained in:
Alexander Zvegintsev 2021-04-16 14:32:12 +00:00
parent 714298a58e
commit 6946d91d7c
2 changed files with 151 additions and 108 deletions
test/jdk/javax/swing/JCheckBox/4449413

@ -1,40 +0,0 @@
<!--
Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
This code is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 only, as
published by the Free Software Foundation.
This code is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
version 2 for more details (a copy is included in the LICENSE file that
accompanied this code).
You should have received a copy of the GNU General Public License version
2 along with this work; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
or visit www.oracle.com if you need additional information or have any
questions.
-->
<html>
<body>
When the applet starts, you'll see eight controls with black backgrounds.
Four enabled (on the left side) and four disabled (on the right side)
checkboxes and radiobuttons.
1. If at least one of the controls' check marks is not visible:
the test fails.
2. Uncheck the "Use Ocean Theme" check box.
If now at least one of the controls' check marks is not visible:
the test fails.
<applet code="bug4449413.class" width=250 height=190></applet>
</body>
</html>

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,87 +25,170 @@
* @bug 4449413
* @summary Tests that checkbox and radiobuttons' check marks are visible when background is black
* @author Ilya Boyandin
* @library /test/lib
* @modules java.desktop/sun.awt
* @build jdk.test.lib.Platform
* @run applet/manual=yesno bug4449413.html
* @run main/manual bug4449413
*/
import javax.swing.*;
import javax.swing.plaf.metal.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTheme;
import javax.swing.plaf.metal.OceanTheme;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import jdk.test.lib.Platform;
public class bug4449413 extends JFrame {
public class bug4449413 extends JApplet {
private static final String INSTRUCTIONS =
"There are eight controls with black backgrounds.\n" +
"Four enabled (on the left side) and four disabled (on the right side)\n" +
"checkboxes and radiobuttons.\n\n" +
"1. If at least one of the controls' check marks is not visible:\n" +
" the test fails.\n";
@Override
public void init() {
private static final String INSTRUCTIONS_ADDITIONS_METAL =
"\n" +
"2. Uncheck the \"Use Ocean Theme\" check box.\n" +
" If now at least one of the controls' check marks is not visible:\n" +
" the test fails.\n";
try {
private static final CountDownLatch latch = new CountDownLatch(1);
private static volatile boolean failed = true;
if (Platform.isOSX()) {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
private final MetalTheme defaultMetalTheme = new DefaultMetalTheme();
private final MetalTheme oceanTheme = new OceanTheme();
private static bug4449413 instance;
boolean isMetalLookAndFeel() {
return UIManager.getLookAndFeel() instanceof MetalLookAndFeel;
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(() -> {
instance = new bug4449413();
instance.createAndShowGUI();
});
boolean timeoutHappened = !latch.await(2, TimeUnit.MINUTES);
SwingUtilities.invokeAndWait(() -> {
if (instance != null) {
instance.dispose();
}
});
final MetalTheme oceanTheme = (MetalTheme) sun.awt.AppContext.getAppContext().get("currentMetalTheme");
System.out.println("Passed: " + !failed);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
getContentPane().setLayout(new FlowLayout());
final JPanel panel = new JPanel();
JCheckBox box = new JCheckBox("Use Ocean theme", true);
getContentPane().add(box);
box.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
MetalLookAndFeel.setCurrentTheme(oceanTheme);
} else {
MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
}
SwingUtilities.updateComponentTreeUI(panel);
}
});
getContentPane().add(panel);
panel.setLayout(new GridLayout(4, 6, 10, 15));
for (int k = 0; k <= 3; k++) {
for (int j = 1; j >= 0; j--) {
AbstractButton b = createButton(j, k);
panel.add(b);
}
}
}
});
} catch (Exception e) {
throw new RuntimeException(e);
if (timeoutHappened || failed) {
throw new RuntimeException("Test failed!");
}
}
static AbstractButton createButton(int enabled, int type) {
AbstractButton b = null;
switch (type) {
case 0:
b = new JRadioButton("RadioButton");
break;
case 1:
b = new JCheckBox("CheckBox");
break;
case 2:
b = new JRadioButtonMenuItem("RBMenuItem");
break;
case 3:
b = new JCheckBoxMenuItem("CBMenuItem");
break;
private void createAndShowGUI() {
setTitle(UIManager.getLookAndFeel().getClass().getName());
addComponentsToPane();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
latch.countDown();
}
});
setLocationRelativeTo(null);
pack();
setVisible(true);
}
public void addComponentsToPane() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JPanel testedPanel = new JPanel();
testedPanel.setLayout(new GridLayout(4, 6, 10, 15));
for (int k = 0; k <= 3; k++) {
for (int j = 1; j >= 0; j--) {
AbstractButton b = createButton(j, k);
testedPanel.add(b);
}
}
add(testedPanel);
if (isMetalLookAndFeel()) {
JCheckBox oceanThemeSwitch = new JCheckBox("Use Ocean theme", true);
oceanThemeSwitch.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
MetalLookAndFeel.setCurrentTheme(oceanTheme);
} else {
MetalLookAndFeel.setCurrentTheme(defaultMetalTheme);
}
SwingUtilities.updateComponentTreeUI(testedPanel);
});
add(oceanThemeSwitch);
}
JTextArea instructionArea = new JTextArea(
isMetalLookAndFeel()
? INSTRUCTIONS + INSTRUCTIONS_ADDITIONS_METAL
: INSTRUCTIONS
);
instructionArea.setEditable(false);
instructionArea.setFocusable(false);
instructionArea.setMargin(new Insets(10,10,10,10));
add(instructionArea);
JButton passButton = new JButton("Pass");
JButton failButton = new JButton("Fail");
ActionListener actionListener = e -> {
failed = e.getSource() == failButton;
latch.countDown();
};
passButton.addActionListener(actionListener);
failButton.addActionListener(actionListener);
JPanel passFailPanel = new JPanel();
passFailPanel.add(passButton);
passFailPanel.add(failButton);
add(passFailPanel);
}
static AbstractButton createButton(int enabled, int type) {
AbstractButton b = switch (type) {
case 0 -> new JRadioButton("RadioButton");
case 1 -> new JCheckBox("CheckBox");
case 2 -> new JRadioButtonMenuItem("RBMenuItem");
case 3 -> new JCheckBoxMenuItem("CBMenuItem");
default -> throw new IllegalArgumentException("type should be in range of 0..3");
};
b.setOpaque(true);
b.setBackground(Color.black);
b.setForeground(Color.white);
b.setEnabled(enabled == 1);