bug6638195.java 6.1 KB
Newer Older
I
idk 已提交
1
/*
2
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
I
idk 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/* @test
 *
26
 * @bug 6638195 6844297
I
idk 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 * @author Igor Kushnirskiy
 * @summary tests if EventQueueDelegate.Delegate is invoked.
 */

import sun.awt.EventQueueDelegate;
import com.sun.java.swing.SwingUtilities3;

import java.util.*;
import java.util.concurrent.*;
import java.awt.*;

public class bug6638195 {
    public static void main(String[] args) throws Exception {
        MyEventQueueDelegate delegate = new MyEventQueueDelegate();
        EventQueueDelegate.setDelegate(delegate);
        runTest(delegate);

        delegate = new MyEventQueueDelegate();
        SwingUtilities3.setEventQueueDelegate(getObjectMap(delegate));
        runTest(delegate);
    }

    private static void runTest(MyEventQueueDelegate delegate) throws Exception {
50 51 52
        // We need an empty runnable here, so the next event is
        // processed with a new EventQueueDelegate. See 6844297
        // for details
I
idk 已提交
53 54 55 56 57
        EventQueue.invokeLater(
            new Runnable() {
                public void run() {
                }
            });
58 59 60 61 62 63 64 65
        // The following event is expected to be processed by
        // the EventQueueDelegate instance
        EventQueue.invokeLater(
            new Runnable() {
                public void run() {
                }
            });
        // Finally, proceed on the main thread
I
idk 已提交
66 67 68 69 70 71 72 73
        final CountDownLatch latch = new CountDownLatch(1);
        EventQueue.invokeLater(
            new Runnable() {
                public void run() {
                    latch.countDown();
                }
            });
        latch.await();
74
        if (!delegate.allInvoked()) {
I
idk 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
            throw new RuntimeException("failed");
        }
    }

    static Map<String, Map<String, Object>> getObjectMap(
          final EventQueueDelegate.Delegate delegate) {
        Map<String, Map<String, Object>> objectMap =
            new HashMap<String, Map<String, Object>>();
        Map<String, Object> methodMap;

        final AWTEvent[] afterDispatchEventArgument = new AWTEvent[1];
        final Object[] afterDispatchHandleArgument = new Object[1];
        Callable<Void> afterDispatchCallable =
            new Callable<Void>() {
                public Void call() {
90 91 92 93 94 95 96
                    try {
                        delegate.afterDispatch(afterDispatchEventArgument[0],
                                afterDispatchHandleArgument[0]);
                    }
                    catch (InterruptedException e) {
                        throw new RuntimeException("afterDispatch interrupted", e);
                    }
I
idk 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
                    return null;
                }
            };
        methodMap = new HashMap<String, Object>();
        methodMap.put("event", afterDispatchEventArgument);
        methodMap.put("handle", afterDispatchHandleArgument);
        methodMap.put("method", afterDispatchCallable);
        objectMap.put("afterDispatch", methodMap);

        final AWTEvent[] beforeDispatchEventArgument = new AWTEvent[1];
        Callable<Object> beforeDispatchCallable =
            new Callable<Object>() {
                public Object call() {
110 111 112 113 114 115 116
                    try {
                        return delegate.beforeDispatch(
                                beforeDispatchEventArgument[0]);
                    }
                    catch (InterruptedException e) {
                        throw new RuntimeException("beforeDispatch interrupted", e);
                    }
I
idk 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                }
            };
        methodMap = new HashMap<String, Object>();
        methodMap.put("event", beforeDispatchEventArgument);
        methodMap.put("method", beforeDispatchCallable);
        objectMap.put("beforeDispatch", methodMap);

        final EventQueue[] getNextEventEventQueueArgument = new EventQueue[1];
        Callable<AWTEvent> getNextEventCallable =
            new Callable<AWTEvent>() {
                public AWTEvent call() throws Exception {
                    return delegate.getNextEvent(
                        getNextEventEventQueueArgument[0]);
                }
            };
        methodMap = new HashMap<String, Object>();
        methodMap.put("eventQueue", getNextEventEventQueueArgument);
        methodMap.put("method", getNextEventCallable);
        objectMap.put("getNextEvent", methodMap);

        return objectMap;
    }
139

I
idk 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    static class MyEventQueueDelegate implements EventQueueDelegate.Delegate {
        private volatile boolean getNextEventInvoked = false;
        private volatile boolean beforeDispatchInvoked = false;
        private volatile boolean afterDispatchInvoked = false;
        public AWTEvent getNextEvent(EventQueue eventQueue)
              throws InterruptedException {
            getNextEventInvoked = true;
            return eventQueue.getNextEvent();
        }
        public Object beforeDispatch(AWTEvent event) {
            beforeDispatchInvoked = true;
            return null;
        }
        public void afterDispatch(AWTEvent event, Object handle) {
            afterDispatchInvoked = true;
        }
        private boolean allInvoked() {
            return getNextEventInvoked && beforeDispatchInvoked && afterDispatchInvoked;
        }
    }
}