Path: blob/master/test/jdk/javax/swing/JSlider/SliderTick/SliderTickTest.java
41155 views
/*1* Copyright (c) 2016, 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 800947726* @summary Verify PageUp/PageDown key moves slider to Next/Previous minor tick.27* @run main/manual SliderTickTest28*/29import java.awt.Color;30import java.awt.GridBagConstraints;31import java.awt.GridBagLayout;32import java.util.concurrent.CountDownLatch;33import javax.swing.BorderFactory;34import javax.swing.JPanel;35import javax.swing.JTextArea;36import javax.swing.SwingUtilities;37import javax.swing.JButton;38import javax.swing.JFrame;39import java.awt.event.ActionEvent;40import java.awt.event.ActionListener;41import java.util.concurrent.TimeUnit;42import javax.swing.JSlider;4344public class SliderTickTest {4546public static void main(String args[]) throws Exception {47final CountDownLatch latch = new CountDownLatch(1);48TestUI test = new TestUI(latch);4950SwingUtilities.invokeLater(new Runnable() {51@Override52public void run() {53try {54test.createUI();55} catch (Exception ex) {56throw new RuntimeException("Exception while creating UI");57}58}59});6061boolean status = latch.await(5, TimeUnit.MINUTES);6263if (!status) {64System.out.println("Test timed out.");65}6667SwingUtilities.invokeAndWait(new Runnable() {68@Override69public void run() {70try {71test.disposeUI();72} catch (Exception ex) {73throw new RuntimeException("Exception while disposing UI");74}75}76});7778if (test.testResult == false) {79throw new RuntimeException("Test Failed.");80}81}82}8384class TestUI {8586private static JFrame mainFrame;87private static JPanel mainControlPanel;8889private static JTextArea instructionTextArea;9091private static JPanel resultButtonPanel;92private static JButton passButton;93private static JButton failButton;9495private static GridBagLayout layout;96private final CountDownLatch latch;97public boolean testResult = false;9899public TestUI(CountDownLatch latch) throws Exception {100this.latch = latch;101}102103public final void createUI() throws Exception {104105mainFrame = new JFrame("SliderTickTest");106107layout = new GridBagLayout();108mainControlPanel = new JPanel(layout);109resultButtonPanel = new JPanel(layout);110111GridBagConstraints gbc = new GridBagConstraints();112113// Create Test instructions114String instructions115= "INSTRUCTIONS:"116+ "\n Click PageUp/PageDown key. If the slider indicator"117+ "\n moves to Next/Previous immediate minor tick, then "118+ "\n test passes else failed.";119120instructionTextArea = new JTextArea();121instructionTextArea.setText(instructions);122instructionTextArea.setEnabled(false);123instructionTextArea.setDisabledTextColor(Color.black);124instructionTextArea.setBackground(Color.white);125126gbc.gridx = 0;127gbc.gridy = 0;128gbc.fill = GridBagConstraints.HORIZONTAL;129mainControlPanel.add(instructionTextArea, gbc);130131JSlider slider = new JSlider(0, 50);132slider.setMajorTickSpacing(10);133slider.setMinorTickSpacing(2);134slider.setPaintTicks(true);135slider.setPaintLabels(true);136slider.setValue(30);137slider.setBorder(BorderFactory.createTitledBorder("Ticks"));138gbc.gridx = 0;139gbc.gridy = 1;140mainControlPanel.add(slider, gbc);141142passButton = new JButton("Pass");143passButton.setActionCommand("Pass");144passButton.addActionListener((ActionEvent e) -> {145testResult = true;146mainFrame.dispose();147latch.countDown();148149});150failButton = new JButton("Fail");151failButton.setActionCommand("Fail");152failButton.addActionListener(new ActionListener() {153@Override154public void actionPerformed(ActionEvent e) {155testResult = false;156mainFrame.dispose();157latch.countDown();158}159});160gbc.gridx = 0;161gbc.gridy = 0;162resultButtonPanel.add(passButton, gbc);163gbc.gridx = 1;164gbc.gridy = 0;165resultButtonPanel.add(failButton, gbc);166167gbc.gridx = 0;168gbc.gridy = 2;169mainControlPanel.add(resultButtonPanel, gbc);170171mainFrame.add(mainControlPanel);172mainFrame.pack();173mainFrame.setVisible(true);174}175176public void disposeUI() {177mainFrame.setVisible(false);178mainFrame.dispose();179}180}181182183