1 /*
   2  * Copyright (c) 2008, 2009, 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 /* @test
  25  *
  26  * @bug 6638195 6844297
  27  * @author Igor Kushnirskiy
  28  * @summary tests if EventQueueDelegate.Delegate is invoked.
  29  */
  30 
  31 import sun.awt.EventQueueDelegate;
  32 import com.sun.java.swing.SwingUtilities3;
  33 
  34 import java.util.*;
  35 import java.util.concurrent.*;
  36 import java.awt.*;
  37 
  38 public class bug6638195 {
  39     public static void main(String[] args) throws Exception {
  40         MyEventQueueDelegate delegate = new MyEventQueueDelegate();
  41         EventQueueDelegate.setDelegate(delegate);
  42         runTest(delegate);
  43 
  44         delegate = new MyEventQueueDelegate();
  45         SwingUtilities3.setEventQueueDelegate(getObjectMap(delegate));
  46         runTest(delegate);
  47     }
  48 
  49     private static void runTest(MyEventQueueDelegate delegate) throws Exception {
  50         // We need an empty runnable here, so the next event is
  51         // processed with a new EventQueueDelegate. See 6844297
  52         // for details
  53         EventQueue.invokeLater(
  54             new Runnable() {
  55                 public void run() {
  56                 }
  57             });
  58         // The following event is expected to be processed by
  59         // the EventQueueDelegate instance
  60         EventQueue.invokeLater(
  61             new Runnable() {
  62                 public void run() {
  63                 }
  64             });
  65         // Finally, proceed on the main thread
  66         final CountDownLatch latch = new CountDownLatch(1);
  67         EventQueue.invokeLater(
  68             new Runnable() {
  69                 public void run() {
  70                     latch.countDown();
  71                 }
  72             });
  73         latch.await();
  74         if (!delegate.allInvoked()) {
  75             throw new RuntimeException("failed");
  76         }
  77     }
  78 
  79     static Map<String, Map<String, Object>> getObjectMap(
  80           final EventQueueDelegate.Delegate delegate) {
  81         Map<String, Map<String, Object>> objectMap =
  82             new HashMap<String, Map<String, Object>>();
  83         Map<String, Object> methodMap;
  84 
  85         final AWTEvent[] afterDispatchEventArgument = new AWTEvent[1];
  86         final Object[] afterDispatchHandleArgument = new Object[1];
  87         Callable<Void> afterDispatchCallable =
  88             new Callable<Void>() {
  89                 public Void call() {
  90                     try {
  91                         delegate.afterDispatch(afterDispatchEventArgument[0],
  92                                 afterDispatchHandleArgument[0]);
  93                     }
  94                     catch (InterruptedException e) {
  95                         throw new RuntimeException("afterDispatch interrupted", e);
  96                     }
  97                     return null;
  98                 }
  99             };
 100         methodMap = new HashMap<String, Object>();
 101         methodMap.put("event", afterDispatchEventArgument);
 102         methodMap.put("handle", afterDispatchHandleArgument);
 103         methodMap.put("method", afterDispatchCallable);
 104         objectMap.put("afterDispatch", methodMap);
 105 
 106         final AWTEvent[] beforeDispatchEventArgument = new AWTEvent[1];
 107         Callable<Object> beforeDispatchCallable =
 108             new Callable<Object>() {
 109                 public Object call() {
 110                     try {
 111                         return delegate.beforeDispatch(
 112                                 beforeDispatchEventArgument[0]);
 113                     }
 114                     catch (InterruptedException e) {
 115                         throw new RuntimeException("beforeDispatch interrupted", e);
 116                     }
 117                 }
 118             };
 119         methodMap = new HashMap<String, Object>();
 120         methodMap.put("event", beforeDispatchEventArgument);
 121         methodMap.put("method", beforeDispatchCallable);
 122         objectMap.put("beforeDispatch", methodMap);
 123 
 124         final EventQueue[] getNextEventEventQueueArgument = new EventQueue[1];
 125         Callable<AWTEvent> getNextEventCallable =
 126             new Callable<AWTEvent>() {
 127                 public AWTEvent call() throws Exception {
 128                     return delegate.getNextEvent(
 129                         getNextEventEventQueueArgument[0]);
 130                 }
 131             };
 132         methodMap = new HashMap<String, Object>();
 133         methodMap.put("eventQueue", getNextEventEventQueueArgument);
 134         methodMap.put("method", getNextEventCallable);
 135         objectMap.put("getNextEvent", methodMap);
 136 
 137         return objectMap;
 138     }
 139 
 140     static class MyEventQueueDelegate implements EventQueueDelegate.Delegate {
 141         private volatile boolean getNextEventInvoked = false;
 142         private volatile boolean beforeDispatchInvoked = false;
 143         private volatile boolean afterDispatchInvoked = false;
 144         public AWTEvent getNextEvent(EventQueue eventQueue)
 145               throws InterruptedException {
 146             getNextEventInvoked = true;
 147             return eventQueue.getNextEvent();
 148         }
 149         public Object beforeDispatch(AWTEvent event) {
 150             beforeDispatchInvoked = true;
 151             return null;
 152         }
 153         public void afterDispatch(AWTEvent event, Object handle) {
 154             afterDispatchInvoked = true;
 155         }
 156         private boolean allInvoked() {
 157             return getNextEventInvoked && beforeDispatchInvoked && afterDispatchInvoked;
 158         }
 159     }
 160 }