1 /*
   2  * Copyright (c) 2009, 2015, 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 4913324
  27   @author Oleg Sukhodolsky: area=eventqueue
  28   @modules java.desktop/sun.awt
  29   @run main/timeout=30 PushPopTest
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.util.EmptyStackException;
  35 import sun.awt.SunToolkit;
  36 
  37 public class PushPopTest {
  38 
  39     public static Frame frame;
  40     public static void main(String[] args) {
  41         frame = new Frame("");
  42         frame.pack();
  43 
  44         Runnable dummy = new Runnable() {
  45                 public void run() {
  46                     System.err.println("Dummy is here.");
  47                     System.err.flush();
  48                 }
  49             };
  50         EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
  51         MyEventQueue1 eq1 = new MyEventQueue1();
  52         MyEventQueue2 eq2 = new MyEventQueue2();
  53         EventQueue.invokeLater(dummy);
  54 
  55         seq.push(eq1);
  56         EventQueue.invokeLater(dummy);
  57 
  58         eq1.push(eq2);
  59         EventQueue.invokeLater(dummy);
  60         Runnable runnable = new Runnable() {
  61                 public void run() {
  62                     System.err.println("Dummy from SunToolkit");
  63                     System.err.flush();
  64                 }
  65             };
  66         InvocationEvent ie = new InvocationEvent(eq2, runnable, null, false);
  67 //        System.err.println(ie);
  68         SunToolkit.postEvent(SunToolkit.targetToAppContext(frame), ie);
  69         eq1.pop();
  70         frame.dispose();
  71     }
  72 }
  73 
  74 class MyEventQueue1 extends EventQueue {
  75 
  76     public void pop() {
  77         super.pop();
  78     }
  79 }
  80 
  81 class MyEventQueue2 extends EventQueue {
  82 
  83     protected void pop() {
  84         System.err.println("pop2()");
  85         Thread.dumpStack();
  86         try {
  87             EventQueue.invokeAndWait(new Runnable() {
  88                     public void run() {
  89                         Runnable runnable = new Runnable() {
  90                                 public void run() {
  91                                     System.err.println("Dummy from pop");
  92                                     System.err.flush();
  93                                 }
  94                              };
  95                         InvocationEvent ie = new InvocationEvent(MyEventQueue2.this, runnable, null, false);
  96                         SunToolkit.postEvent(SunToolkit.targetToAppContext(PushPopTest.frame), ie);
  97                         postEvent(ie);
  98                     }
  99                 });
 100         } catch (InterruptedException ie) {
 101             ie.printStackTrace();
 102         } catch (java.lang.reflect.InvocationTargetException ie) {
 103             ie.printStackTrace();
 104         }
 105         super.pop();
 106     }
 107 }