1 /* 2 * Copyright (c) 2018, 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 8152974 27 * @summary AWT hang occurrs when sequenced events arrive out of sequence 28 * @run main SequencedEventTest 29 */ 30 import sun.awt.AppContext; 31 import sun.awt.SunToolkit; 32 33 import java.awt.Robot; 34 import java.awt.Point; 35 import java.awt.Dimension; 36 import java.awt.FlowLayout; 37 import java.awt.AWTEvent; 38 import java.awt.Toolkit; 39 import java.awt.event.ActionEvent; 40 import java.awt.event.ActionListener; 41 import java.awt.event.InputEvent; 42 import java.lang.reflect.Constructor; 43 import java.util.concurrent.CountDownLatch; 44 45 import javax.swing.JFrame; 46 import javax.swing.JButton; 47 import javax.swing.SwingUtilities; 48 import javax.swing.JTextArea; 49 50 public class SequencedEventTest extends JFrame implements ActionListener { 51 private JButton spamMeButton; 52 private static Robot robot; 53 private static SequencedEventTest window; 54 private static AppContext context; 55 56 public static void main(String[] args) throws Exception { 57 SwingUtilities.invokeAndWait(() -> { 58 window = new SequencedEventTest(); 59 window.setVisible(true); 60 }); 61 62 robot = new Robot(); 63 robot.waitForIdle(); 64 65 Point pt = window.spamMeButton.getLocationOnScreen(); 66 Dimension d = window.spamMeButton.getSize(); 67 68 robot.mouseMove(pt.x + d.width / 2, pt.y + d.height / 2); 69 robot.waitForIdle(); 70 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 71 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 72 /* 73 *Cannot have robot.waitForIdle() here since it will block the test forever, 74 * in the case of failure and the test will timeout. 75 */ 76 77 try { 78 /* 79 * Wait for 2 seconds, and then see if all the sequenced events are dispatched. 80 */ 81 Thread.sleep(2000); 82 AWTEvent ev = Toolkit.getDefaultToolkit().getSystemEventQueue(). 83 peekEvent(java.awt.event.FocusEvent.FOCUS_LAST + 1); 84 85 if (ev != null) 86 throw new RuntimeException("Test case failed!"); 87 } catch (InterruptedException e) { 88 throw new RuntimeException("Test case failed." + e.getMessage()); 89 } 90 91 /* 92 * In the case of failure, the cleanup job cannot be executed, since it 93 * will block the test. 94 */ 95 System.out.println("Test case succeeded."); 96 context.dispose(); 97 SwingUtilities.invokeAndWait(() -> window.dispose()); 98 } 99 100 public SequencedEventTest() { 101 super("Test Window"); 102 103 setLayout(new FlowLayout()); 104 JTextArea textBlock = new JTextArea("Lorem ipsum dolor sit amet..."); 105 add(textBlock); 106 107 spamMeButton = new JButton("Press me!"); 108 spamMeButton.addActionListener(this); 109 add(spamMeButton); 110 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 111 pack(); 112 } 113 114 @Override 115 public void actionPerformed(ActionEvent e) { 116 if(e.getSource() == spamMeButton) { 117 AWTEvent eventOne = getSequencedEvent(); 118 AWTEvent eventFour = getSequencedEvent(); 119 ThreadGroup tg = new ThreadGroup("TestThreadGroup" ); 120 CountDownLatch latch = new CountDownLatch(1); 121 Thread t = new Thread(tg, () -> { 122 context = SunToolkit.createNewAppContext(); 123 AWTEvent eventTwo = getSequencedEvent(); 124 AWTEvent eventThree = getSequencedEvent(); 125 126 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(eventThree); 127 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new ActionEvent(this, 0, null)); 128 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new ActionEvent(this, 1, null)); 129 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(eventTwo); 130 131 latch.countDown(); 132 }); 133 134 t.start(); 135 try { 136 latch.await(); 137 }catch (InterruptedException ex) { 138 throw new RuntimeException("Test case failed."); 139 } 140 141 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(eventFour); 142 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new ActionEvent(this, 2, null)); 143 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new ActionEvent(this, 3, null)); 144 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(eventOne); 145 146 try { 147 t.join(); 148 } catch (InterruptedException ex) { 149 throw new RuntimeException("Test case failed."); 150 } 151 } 152 } 153 154 private AWTEvent getSequencedEvent() 155 { 156 AWTEvent wrapMe = new AWTEvent(this, AWTEvent.RESERVED_ID_MAX) {}; 157 158 try { 159 /* 160 * SequencedEvent is a package private class, which cannot be instantiated 161 * by importing. So use reflection to create an instance. 162 */ 163 Class<? extends AWTEvent> seqClass = (Class<? extends AWTEvent>) Class.forName("java.awt.SequencedEvent"); 164 Constructor<? extends AWTEvent> seqConst = seqClass.getConstructor(AWTEvent.class); 165 seqConst.setAccessible(true);; 166 return seqConst.newInstance(wrapMe); 167 } catch (Throwable err) { 168 throw new RuntimeException("Unable to instantiate SequencedEvent",err); 169 } 170 } 171 }