Path: blob/master/test/jdk/javax/swing/JSpinner/TestJSpinnerPressUnpress.java
41152 views
/*1* Copyright (c) 2019, 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/*24* @test25* @bug 823473326* @summary Verify that JSpinner up/down button rendering is changed27* according to pressed state in GTKLookAndFeel28* @run main/manual TestJSpinnerPressUnpress29*/3031import java.awt.Color;32import java.awt.GridBagConstraints;33import java.awt.GridBagLayout;34import java.awt.Insets;35import java.awt.event.ActionEvent;36import java.awt.event.WindowAdapter;37import java.awt.event.WindowEvent;38import java.util.concurrent.CountDownLatch;39import java.util.concurrent.TimeUnit;40import javax.swing.JButton;41import javax.swing.JSpinner;42import javax.swing.JFrame;43import javax.swing.JPanel;44import javax.swing.JTextArea;45import javax.swing.UIManager;46import javax.swing.SwingUtilities;47import javax.swing.UnsupportedLookAndFeelException;4849public class TestJSpinnerPressUnpress {50private static JFrame mainFrame = new JFrame();51private static volatile boolean testResult = false;52private static volatile CountDownLatch countDownLatch;53private static final String GTK_LAF_CLASS = "GTKLookAndFeel";54private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n" +55"Verify that when spinner up/down button is pressed to change\n" +56"the spinner value, the button's rendering is changed to show\n" +57"the pressed state. If yes, Press Pass, Otherwise, Press Fail.";5859public static void main(String args[]) throws Exception {60if (!System.getProperty("os.name").startsWith("Linux")) {61System.out.println("This test is meant for Linux platform only");62return;63}64countDownLatch = new CountDownLatch(1);6566for (UIManager.LookAndFeelInfo lookAndFeelInfo :67UIManager.getInstalledLookAndFeels()) {68if (lookAndFeelInfo.getClassName().contains(GTK_LAF_CLASS)) {69try {70UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());71} catch (final UnsupportedLookAndFeelException ignored) {72System.out.println("GTK L&F could not be set, so this " +73"test can not be run in this scenario ");74return;75}76}77}78SwingUtilities.invokeLater(TestJSpinnerPressUnpress::createUI);79countDownLatch.await(15, TimeUnit.MINUTES);80SwingUtilities.invokeLater(mainFrame::dispose);8182if (!testResult) {83throw new RuntimeException("Test failed!");84}85}8687private static void createUI() {88GridBagLayout layout = new GridBagLayout();89JPanel mainControlPanel = new JPanel(layout);90JPanel resultButtonPanel = new JPanel(layout);9192GridBagConstraints gbc = new GridBagConstraints();9394gbc.gridx = 0;95gbc.gridy = 0;96gbc.insets = new Insets(5, 15, 5, 15);97gbc.fill = GridBagConstraints.HORIZONTAL;98mainControlPanel.add(new JSpinner(), gbc);99100JTextArea instructionTextArea = new JTextArea();101instructionTextArea.setText(INSTRUCTIONS);102instructionTextArea.setEditable(false);103104gbc.gridx = 0;105gbc.gridy = 1;106gbc.fill = GridBagConstraints.HORIZONTAL;107mainControlPanel.add(instructionTextArea, gbc);108109JButton passButton = new JButton("Pass");110passButton.setActionCommand("Pass");111passButton.addActionListener((ActionEvent e) -> {112testResult = true;113countDownLatch.countDown();114115});116117JButton failButton = new JButton("Fail");118failButton.setActionCommand("Fail");119failButton.addActionListener(e -> {120countDownLatch.countDown();121});122123gbc.gridx = 0;124gbc.gridy = 0;125126resultButtonPanel.add(passButton, gbc);127128gbc.gridx = 1;129gbc.gridy = 0;130resultButtonPanel.add(failButton, gbc);131132gbc.gridx = 0;133gbc.gridy = 2;134mainControlPanel.add(resultButtonPanel, gbc);135136mainFrame.add(mainControlPanel);137mainFrame.pack();138139mainFrame.addWindowListener(new WindowAdapter() {140@Override141public void windowClosing(WindowEvent e) {142mainFrame.dispose();143countDownLatch.countDown();144}145});146mainFrame.setVisible(true);147}148}149150151