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 7096375 27 * @summary Test that Swing does not ignore first click on a JButton after 28 * decreasing system's time 29 * @run main/manual TimeChangeButtonClickTest 30 */ 31 import java.awt.GridBagConstraints; 32 import java.awt.GridBagLayout; 33 import java.util.concurrent.CountDownLatch; 34 import javax.swing.BorderFactory; 35 import javax.swing.JPanel; 36 import javax.swing.JTextArea; 37 import javax.swing.SwingUtilities; 38 import javax.swing.JButton; 39 import javax.swing.JFrame; 40 import java.awt.event.ActionEvent; 41 import java.awt.event.ActionListener; 42 import java.util.concurrent.TimeUnit; 43 import javax.swing.Box; 44 import javax.swing.JLabel; 45 46 public class TimeChangeButtonClickTest { 47 main(String args[])48 public static void main(String args[]) throws Exception { 49 final CountDownLatch latch = new CountDownLatch(1); 50 TestUI test = new TestUI(latch); 51 52 SwingUtilities.invokeLater(new Runnable() { 53 @Override 54 public void run() { 55 try { 56 test.createUI(); 57 } catch (Exception ex) { 58 throw new RuntimeException("Exception while creating test UI"); 59 } 60 } 61 }); 62 63 boolean status = latch.await(5, TimeUnit.MINUTES); 64 65 if (!status) { 66 System.out.println("Test timed out."); 67 } 68 69 SwingUtilities.invokeAndWait(new Runnable() { 70 @Override 71 public void run() { 72 try { 73 test.disposeUI(); 74 } catch (Exception ex) { 75 throw new RuntimeException("Exception while disposing test UI"); 76 } 77 } 78 }); 79 80 if (test.testResult == false) { 81 throw new RuntimeException("Test Failed."); 82 } 83 } 84 } 85 86 class TestUI { 87 88 private static JFrame mainFrame; 89 private static JPanel mainControlPanel; 90 91 private static JTextArea instructionTextArea; 92 93 private static JPanel resultButtonPanel; 94 private static JButton passButton; 95 private static JButton failButton; 96 97 private static JPanel testPanel; 98 private static JButton testButton; 99 private static JLabel buttonPressCountLabel; 100 101 private static GridBagLayout layout; 102 private final CountDownLatch latch; 103 public boolean testResult = false; 104 private int buttonPressCount = 0; 105 TestUI(CountDownLatch latch)106 public TestUI(CountDownLatch latch) throws Exception { 107 this.latch = latch; 108 } 109 createUI()110 public final void createUI() throws Exception { 111 112 mainFrame = new JFrame("TimeChangeButtonClickTest"); 113 114 layout = new GridBagLayout(); 115 mainControlPanel = new JPanel(layout); 116 resultButtonPanel = new JPanel(layout); 117 testPanel = new JPanel(layout); 118 119 GridBagConstraints gbc = new GridBagConstraints(); 120 121 // Create Test instructions 122 String instructions 123 = "Test 1 : --------------------\n" 124 + "1. Click 'Test Button' with left mouse button\n" 125 + "Observe : Button Press count change to 1\n" 126 + "Test 2 : --------------------\n" 127 + "1. Change the system time to one hour less than current time\n" 128 + "2. Click 'Test Button' with left mouse button\n" 129 + "Observe : Button Press count change to 2\n" 130 + "Test 3 : --------------------\n" 131 + "1. Change the system time by adding two hours\n" 132 + "2. Click 'Test Button' with left mouse button\n" 133 + "Observe : Button Press count change to 3\n" 134 + "--------------------\n" 135 + "Restore the system time.\n" 136 + "--------------------\n" 137 + "Press 'Pass' if Button Press count is 3, 'Fail' otherwise"; 138 139 instructionTextArea = new JTextArea(); 140 instructionTextArea.setText(instructions); 141 instructionTextArea.setEditable(false); 142 instructionTextArea.setBorder(BorderFactory. 143 createTitledBorder("Test Instructions")); 144 145 gbc.gridx = 0; 146 gbc.gridy = 0; 147 gbc.fill = GridBagConstraints.HORIZONTAL; 148 mainControlPanel.add(instructionTextArea, gbc); 149 150 // Create Test Button and label 151 testButton = new JButton("Test Button"); 152 testButton.addActionListener(new ActionListener() { 153 @Override 154 public void actionPerformed(ActionEvent e) { 155 buttonPressCount++; 156 buttonPressCountLabel.setText( 157 "Button Press Count : " + buttonPressCount); 158 } 159 }); 160 161 gbc.gridx = 0; 162 gbc.gridy = 0; 163 testPanel.add(testButton, gbc); 164 165 gbc.gridx = 0; 166 gbc.gridy = 1; 167 testPanel.add(Box.createVerticalStrut(50)); 168 169 gbc.gridx = 0; 170 gbc.gridy = 2; 171 buttonPressCountLabel = new JLabel( 172 "Button Press Count : " + buttonPressCount); 173 testPanel.add(buttonPressCountLabel, gbc); 174 175 mainControlPanel.add(testPanel); 176 177 // Create resultButtonPanel with Pass, Fail buttons 178 passButton = new JButton("Pass"); 179 passButton.setActionCommand("Pass"); 180 passButton.addActionListener((ActionEvent e) -> { 181 System.out.println("Pass Button pressed!"); 182 testResult = true; 183 latch.countDown(); 184 185 }); 186 187 failButton = new JButton("Fail"); 188 failButton.setActionCommand("Fail"); 189 failButton.addActionListener(new ActionListener() { 190 @Override 191 public void actionPerformed(ActionEvent e) { 192 System.out.println("Fail Button pressed!"); 193 testResult = false; 194 latch.countDown(); 195 } 196 }); 197 198 gbc.gridx = 0; 199 gbc.gridy = 0; 200 resultButtonPanel.add(passButton, gbc); 201 gbc.gridx = 1; 202 gbc.gridy = 0; 203 resultButtonPanel.add(failButton, gbc); 204 205 gbc.gridx = 0; 206 gbc.gridy = 1; 207 mainControlPanel.add(resultButtonPanel, gbc); 208 209 mainFrame.add(mainControlPanel); 210 211 mainFrame.pack(); 212 mainFrame.setVisible(true); 213 } 214 disposeUI()215 public void disposeUI() { 216 mainFrame.setVisible(false); 217 mainFrame.dispose(); 218 } 219 } 220 221