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