Path: blob/master/test/jdk/javax/swing/JCheckBox/4449413/bug4449413.java
41153 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 444941325* @summary Tests that checkbox and radiobuttons' check marks are visible when background is black26* @author Ilya Boyandin27* @run main/manual bug444941328*/2930import javax.swing.AbstractButton;31import javax.swing.BoxLayout;32import javax.swing.JButton;33import javax.swing.JCheckBox;34import javax.swing.JCheckBoxMenuItem;35import javax.swing.JFrame;36import javax.swing.JPanel;37import javax.swing.JRadioButton;38import javax.swing.JRadioButtonMenuItem;39import javax.swing.JTextArea;40import javax.swing.SwingUtilities;41import javax.swing.UIManager;42import javax.swing.plaf.metal.DefaultMetalTheme;43import javax.swing.plaf.metal.MetalLookAndFeel;44import javax.swing.plaf.metal.MetalTheme;45import javax.swing.plaf.metal.OceanTheme;46import java.awt.Color;47import java.awt.GridLayout;48import java.awt.Insets;49import java.awt.event.ActionListener;50import java.awt.event.ItemEvent;51import java.awt.event.WindowAdapter;52import java.awt.event.WindowEvent;53import java.util.concurrent.CountDownLatch;54import java.util.concurrent.TimeUnit;5556public class bug4449413 extends JFrame {5758private static final String INSTRUCTIONS =59"There are eight controls with black backgrounds.\n" +60"Four enabled (on the left side) and four disabled (on the right side)\n" +61"checkboxes and radiobuttons.\n\n" +62"1. If at least one of the controls' check marks is not visible:\n" +63" the test fails.\n";6465private static final String INSTRUCTIONS_ADDITIONS_METAL =66"\n" +67"2. Uncheck the \"Use Ocean Theme\" check box.\n" +68" If now at least one of the controls' check marks is not visible:\n" +69" the test fails.\n";7071private static final CountDownLatch latch = new CountDownLatch(1);72private static volatile boolean failed = true;7374private final MetalTheme defaultMetalTheme = new DefaultMetalTheme();75private final MetalTheme oceanTheme = new OceanTheme();7677private static bug4449413 instance;7879boolean isMetalLookAndFeel() {80return UIManager.getLookAndFeel() instanceof MetalLookAndFeel;81}8283public static void main(String[] args) throws Exception {84SwingUtilities.invokeLater(() -> {85instance = new bug4449413();86instance.createAndShowGUI();87});8889boolean timeoutHappened = !latch.await(2, TimeUnit.MINUTES);9091SwingUtilities.invokeAndWait(() -> {92if (instance != null) {93instance.dispose();94}95});9697System.out.println("Passed: " + !failed);9899if (timeoutHappened || failed) {100throw new RuntimeException("Test failed!");101}102}103104private void createAndShowGUI() {105setTitle(UIManager.getLookAndFeel().getClass().getName());106107addComponentsToPane();108109addWindowListener(new WindowAdapter() {110@Override111public void windowClosing(WindowEvent e) {112latch.countDown();113}114});115116setLocationRelativeTo(null);117pack();118setVisible(true);119}120121public void addComponentsToPane() {122setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));123124JPanel testedPanel = new JPanel();125testedPanel.setLayout(new GridLayout(4, 6, 10, 15));126for (int k = 0; k <= 3; k++) {127for (int j = 1; j >= 0; j--) {128AbstractButton b = createButton(j, k);129testedPanel.add(b);130}131}132133add(testedPanel);134135136if (isMetalLookAndFeel()) {137JCheckBox oceanThemeSwitch = new JCheckBox("Use Ocean theme", true);138oceanThemeSwitch.addItemListener(e -> {139if (e.getStateChange() == ItemEvent.SELECTED) {140MetalLookAndFeel.setCurrentTheme(oceanTheme);141} else {142MetalLookAndFeel.setCurrentTheme(defaultMetalTheme);143}144SwingUtilities.updateComponentTreeUI(testedPanel);145});146147add(oceanThemeSwitch);148}149150JTextArea instructionArea = new JTextArea(151isMetalLookAndFeel()152? INSTRUCTIONS + INSTRUCTIONS_ADDITIONS_METAL153: INSTRUCTIONS154);155156instructionArea.setEditable(false);157instructionArea.setFocusable(false);158instructionArea.setMargin(new Insets(10,10,10,10));159160add(instructionArea);161162163JButton passButton = new JButton("Pass");164JButton failButton = new JButton("Fail");165166ActionListener actionListener = e -> {167failed = e.getSource() == failButton;168latch.countDown();169};170171passButton.addActionListener(actionListener);172failButton.addActionListener(actionListener);173174JPanel passFailPanel = new JPanel();175passFailPanel.add(passButton);176passFailPanel.add(failButton);177178add(passFailPanel);179}180181static AbstractButton createButton(int enabled, int type) {182AbstractButton b = switch (type) {183case 0 -> new JRadioButton("RadioButton");184case 1 -> new JCheckBox("CheckBox");185case 2 -> new JRadioButtonMenuItem("RBMenuItem");186case 3 -> new JCheckBoxMenuItem("CBMenuItem");187default -> throw new IllegalArgumentException("type should be in range of 0..3");188};189190b.setOpaque(true);191b.setBackground(Color.black);192b.setForeground(Color.white);193b.setEnabled(enabled == 1);194b.setSelected(true);195return b;196}197}198199200