1/*
2 * Copyright (c) 2016, 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 /*
25 * @test
26 * @bug 8009477
27 * @summary Verify PageUp/PageDown key moves slider to Next/Previous minor tick.
28 * @run main/manual SliderTickTest
29 */
30import java.awt.Color;
31import java.awt.GridBagConstraints;
32import java.awt.GridBagLayout;
33import java.util.concurrent.CountDownLatch;
34import javax.swing.BorderFactory;
35import javax.swing.JPanel;
36import javax.swing.JTextArea;
37import javax.swing.SwingUtilities;
38import javax.swing.JButton;
39import javax.swing.JFrame;
40import java.awt.event.ActionEvent;
41import java.awt.event.ActionListener;
42import java.util.concurrent.TimeUnit;
43import javax.swing.JSlider;
44
45public class SliderTickTest {
46
47    public static void main(String args[]) throws Exception {
48        final CountDownLatch latch = new CountDownLatch(1);
49        TestUI test = new TestUI(latch);
50
51        SwingUtilities.invokeLater(new Runnable() {
52            @Override
53            public void run() {
54                try {
55                    test.createUI();
56                } catch (Exception ex) {
57                    throw new RuntimeException("Exception while creating UI");
58                }
59            }
60        });
61
62        boolean status = latch.await(5, TimeUnit.MINUTES);
63
64        if (!status) {
65            System.out.println("Test timed out.");
66        }
67
68        SwingUtilities.invokeAndWait(new Runnable() {
69            @Override
70            public void run() {
71                try {
72                    test.disposeUI();
73                } catch (Exception ex) {
74                    throw new RuntimeException("Exception while disposing UI");
75                }
76            }
77        });
78
79        if (test.testResult == false) {
80            throw new RuntimeException("Test Failed.");
81        }
82    }
83}
84
85class TestUI {
86
87    private static JFrame mainFrame;
88    private static JPanel mainControlPanel;
89
90    private static JTextArea instructionTextArea;
91
92    private static JPanel resultButtonPanel;
93    private static JButton passButton;
94    private static JButton failButton;
95
96    private static GridBagLayout layout;
97    private final CountDownLatch latch;
98    public boolean testResult = false;
99
100    public TestUI(CountDownLatch latch) throws Exception {
101        this.latch = latch;
102    }
103
104    public final void createUI() throws Exception {
105
106        mainFrame = new JFrame("SliderTickTest");
107
108        layout = new GridBagLayout();
109        mainControlPanel = new JPanel(layout);
110        resultButtonPanel = new JPanel(layout);
111
112        GridBagConstraints gbc = new GridBagConstraints();
113
114        // Create Test instructions
115        String instructions
116                = "INSTRUCTIONS:"
117                + "\n Click PageUp/PageDown key. If the slider indicator"
118                + "\n moves to Next/Previous immediate minor tick, then "
119                + "\n test passes else failed.";
120
121        instructionTextArea = new JTextArea();
122        instructionTextArea.setText(instructions);
123        instructionTextArea.setEnabled(false);
124        instructionTextArea.setDisabledTextColor(Color.black);
125        instructionTextArea.setBackground(Color.white);
126
127        gbc.gridx = 0;
128        gbc.gridy = 0;
129        gbc.fill = GridBagConstraints.HORIZONTAL;
130        mainControlPanel.add(instructionTextArea, gbc);
131
132        JSlider slider = new JSlider(0, 50);
133        slider.setMajorTickSpacing(10);
134        slider.setMinorTickSpacing(2);
135        slider.setPaintTicks(true);
136        slider.setPaintLabels(true);
137        slider.setValue(30);
138        slider.setBorder(BorderFactory.createTitledBorder("Ticks"));
139        gbc.gridx = 0;
140        gbc.gridy = 1;
141        mainControlPanel.add(slider, gbc);
142
143        passButton = new JButton("Pass");
144        passButton.setActionCommand("Pass");
145        passButton.addActionListener((ActionEvent e) -> {
146            testResult = true;
147            mainFrame.dispose();
148            latch.countDown();
149
150        });
151        failButton = new JButton("Fail");
152        failButton.setActionCommand("Fail");
153        failButton.addActionListener(new ActionListener() {
154            @Override
155            public void actionPerformed(ActionEvent e) {
156                testResult = false;
157                mainFrame.dispose();
158                latch.countDown();
159            }
160        });
161        gbc.gridx = 0;
162        gbc.gridy = 0;
163        resultButtonPanel.add(passButton, gbc);
164        gbc.gridx = 1;
165        gbc.gridy = 0;
166        resultButtonPanel.add(failButton, gbc);
167
168        gbc.gridx = 0;
169        gbc.gridy = 2;
170        mainControlPanel.add(resultButtonPanel, gbc);
171
172        mainFrame.add(mainControlPanel);
173        mainFrame.pack();
174        mainFrame.setVisible(true);
175    }
176
177    public void disposeUI() {
178        mainFrame.setVisible(false);
179        mainFrame.dispose();
180    }
181}
182