< prev index next >

test/jdk/java/awt/event/SequencedEvent/SequencedEventTest.java

Print this page




   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 java.awt.Robot;
  35 import java.awt.Point;
  36 import java.awt.Dimension;
  37 import java.awt.FlowLayout;
  38 import java.awt.AWTEvent;
  39 import java.awt.Toolkit;
  40 import java.awt.event.ActionEvent;
  41 import java.awt.event.ActionListener;
  42 import java.awt.event.InputEvent;
  43 import java.lang.reflect.Constructor;
  44 import java.util.concurrent.CountDownLatch;
  45 
  46 import javax.swing.JFrame;
  47 import javax.swing.JButton;


  67         Dimension d = window.spamMeButton.getSize();
  68 
  69         robot.mouseMove(pt.x + d.width / 2, pt.y + d.height / 2);
  70         robot.waitForIdle();
  71         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  72         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  73         /*
  74          *Cannot have robot.waitForIdle() here since it will block the test forever,
  75          * in the case of failure and the test will timeout.
  76          */
  77 
  78         try {
  79             /*
  80              * Wait for 2 seconds, and then see if all the sequenced events are dispatched.
  81              */
  82             Thread.sleep(2000);
  83             AWTEvent ev = Toolkit.getDefaultToolkit().getSystemEventQueue().
  84                     peekEvent(java.awt.event.FocusEvent.FOCUS_LAST + 1);
  85 
  86             if (ev != null)
  87                 throw new RuntimeException("Test case failed!");

  88         } catch (InterruptedException e) {
  89             throw new RuntimeException("Test case failed." + e.getMessage());
  90         }
  91 
  92         /*
  93          * In the case of failure, the cleanup job cannot be executed, since it
  94          * will block the test.
  95          */
  96         System.out.println("Test case succeeded.");
  97         context.dispose();
  98         SwingUtilities.invokeAndWait(() -> window.dispose());
  99     }
 100 
 101     public SequencedEventTest() {
 102         super("Test Window");
 103 
 104         setLayout(new FlowLayout());
 105         JTextArea textBlock = new JTextArea("Lorem ipsum dolor sit amet...");
 106         add(textBlock);
 107 


 146 
 147             try {
 148                 t.join();
 149             } catch (InterruptedException ex) {
 150                 throw new RuntimeException("Test case failed.");
 151             }
 152         }
 153     }
 154 
 155     private AWTEvent getSequencedEvent()
 156     {
 157         AWTEvent wrapMe = new AWTEvent(this, AWTEvent.RESERVED_ID_MAX) {};
 158 
 159         try {
 160             /*
 161              * SequencedEvent is a package private class, which cannot be instantiated
 162              * by importing. So use reflection to create an instance.
 163              */
 164             Class<? extends AWTEvent> seqClass = (Class<? extends AWTEvent>) Class.forName("java.awt.SequencedEvent");
 165             Constructor<? extends AWTEvent> seqConst = seqClass.getConstructor(AWTEvent.class);
 166             seqConst.setAccessible(true);;
 167             return seqConst.newInstance(wrapMe);
 168         } catch (Throwable err) {
 169             throw new RuntimeException("Unable to instantiate SequencedEvent",err);
 170         }
 171     }
 172 }


   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;


  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 


 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 }
< prev index next >