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