Path: blob/master/test/jdk/javax/swing/JButton/TimeChangeButtonClickTest.java
41149 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 709637526* @summary Test that Swing does not ignore first click on a JButton after27* decreasing system's time28* @run main/manual TimeChangeButtonClickTest29*/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.Box;43import javax.swing.JLabel;4445public class TimeChangeButtonClickTest {4647public static void main(String args[]) throws Exception {48final CountDownLatch latch = new CountDownLatch(1);49TestUI test = new TestUI(latch);5051SwingUtilities.invokeLater(new Runnable() {52@Override53public void run() {54try {55test.createUI();56} catch (Exception ex) {57throw new RuntimeException("Exception while creating test UI");58}59}60});6162boolean status = latch.await(5, TimeUnit.MINUTES);6364if (!status) {65System.out.println("Test timed out.");66}6768SwingUtilities.invokeAndWait(new Runnable() {69@Override70public void run() {71try {72test.disposeUI();73} catch (Exception ex) {74throw new RuntimeException("Exception while disposing test UI");75}76}77});7879if (test.testResult == false) {80throw new RuntimeException("Test Failed.");81}82}83}8485class TestUI {8687private static JFrame mainFrame;88private static JPanel mainControlPanel;8990private static JTextArea instructionTextArea;9192private static JPanel resultButtonPanel;93private static JButton passButton;94private static JButton failButton;9596private static JPanel testPanel;97private static JButton testButton;98private static JLabel buttonPressCountLabel;99100private static GridBagLayout layout;101private final CountDownLatch latch;102public boolean testResult = false;103private int buttonPressCount = 0;104105public TestUI(CountDownLatch latch) throws Exception {106this.latch = latch;107}108109public final void createUI() throws Exception {110111mainFrame = new JFrame("TimeChangeButtonClickTest");112113layout = new GridBagLayout();114mainControlPanel = new JPanel(layout);115resultButtonPanel = new JPanel(layout);116testPanel = new JPanel(layout);117118GridBagConstraints gbc = new GridBagConstraints();119120// Create Test instructions121String instructions122= "Test 1 : --------------------\n"123+ "1. Click 'Test Button' with left mouse button\n"124+ "Observe : Button Press count change to 1\n"125+ "Test 2 : --------------------\n"126+ "1. Change the system time to one hour less than current time\n"127+ "2. Click 'Test Button' with left mouse button\n"128+ "Observe : Button Press count change to 2\n"129+ "Test 3 : --------------------\n"130+ "1. Change the system time by adding two hours\n"131+ "2. Click 'Test Button' with left mouse button\n"132+ "Observe : Button Press count change to 3\n"133+ "--------------------\n"134+ "Restore the system time.\n"135+ "--------------------\n"136+ "Press 'Pass' if Button Press count is 3, 'Fail' otherwise";137138instructionTextArea = new JTextArea();139instructionTextArea.setText(instructions);140instructionTextArea.setEditable(false);141instructionTextArea.setBorder(BorderFactory.142createTitledBorder("Test Instructions"));143144gbc.gridx = 0;145gbc.gridy = 0;146gbc.fill = GridBagConstraints.HORIZONTAL;147mainControlPanel.add(instructionTextArea, gbc);148149// Create Test Button and label150testButton = new JButton("Test Button");151testButton.addActionListener(new ActionListener() {152@Override153public void actionPerformed(ActionEvent e) {154buttonPressCount++;155buttonPressCountLabel.setText(156"Button Press Count : " + buttonPressCount);157}158});159160gbc.gridx = 0;161gbc.gridy = 0;162testPanel.add(testButton, gbc);163164gbc.gridx = 0;165gbc.gridy = 1;166testPanel.add(Box.createVerticalStrut(50));167168gbc.gridx = 0;169gbc.gridy = 2;170buttonPressCountLabel = new JLabel(171"Button Press Count : " + buttonPressCount);172testPanel.add(buttonPressCountLabel, gbc);173174mainControlPanel.add(testPanel);175176// Create resultButtonPanel with Pass, Fail buttons177passButton = new JButton("Pass");178passButton.setActionCommand("Pass");179passButton.addActionListener((ActionEvent e) -> {180System.out.println("Pass Button pressed!");181testResult = true;182latch.countDown();183184});185186failButton = new JButton("Fail");187failButton.setActionCommand("Fail");188failButton.addActionListener(new ActionListener() {189@Override190public void actionPerformed(ActionEvent e) {191System.out.println("Fail Button pressed!");192testResult = false;193latch.countDown();194}195});196197gbc.gridx = 0;198gbc.gridy = 0;199resultButtonPanel.add(passButton, gbc);200gbc.gridx = 1;201gbc.gridy = 0;202resultButtonPanel.add(failButton, gbc);203204gbc.gridx = 0;205gbc.gridy = 1;206mainControlPanel.add(resultButtonPanel, gbc);207208mainFrame.add(mainControlPanel);209210mainFrame.pack();211mainFrame.setVisible(true);212}213214public void disposeUI() {215mainFrame.setVisible(false);216mainFrame.dispose();217}218}219220221222