Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/EventQueue/6638195/bug6638195.java
38821 views
/*1* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24*25* @bug 6638195 684429726* @author Igor Kushnirskiy27* @summary tests if EventQueueDelegate.Delegate is invoked.28*/2930import sun.awt.EventQueueDelegate;31import com.sun.java.swing.SwingUtilities3;3233import java.util.*;34import java.util.concurrent.*;35import java.awt.*;3637public class bug6638195 {38public static void main(String[] args) throws Exception {39MyEventQueueDelegate delegate = new MyEventQueueDelegate();40EventQueueDelegate.setDelegate(delegate);41runTest(delegate);4243delegate = new MyEventQueueDelegate();44SwingUtilities3.setEventQueueDelegate(getObjectMap(delegate));45runTest(delegate);46}4748private static void runTest(MyEventQueueDelegate delegate) throws Exception {49// We need an empty runnable here, so the next event is50// processed with a new EventQueueDelegate. See 684429751// for details52EventQueue.invokeLater(53new Runnable() {54public void run() {55}56});57// The following event is expected to be processed by58// the EventQueueDelegate instance59EventQueue.invokeLater(60new Runnable() {61public void run() {62}63});64// Finally, proceed on the main thread65final CountDownLatch latch = new CountDownLatch(1);66EventQueue.invokeLater(67new Runnable() {68public void run() {69latch.countDown();70}71});72latch.await();73if (!delegate.allInvoked()) {74throw new RuntimeException("failed");75}76}7778static Map<String, Map<String, Object>> getObjectMap(79final EventQueueDelegate.Delegate delegate) {80Map<String, Map<String, Object>> objectMap =81new HashMap<String, Map<String, Object>>();82Map<String, Object> methodMap;8384final AWTEvent[] afterDispatchEventArgument = new AWTEvent[1];85final Object[] afterDispatchHandleArgument = new Object[1];86Callable<Void> afterDispatchCallable =87new Callable<Void>() {88public Void call() {89try {90delegate.afterDispatch(afterDispatchEventArgument[0],91afterDispatchHandleArgument[0]);92}93catch (InterruptedException e) {94throw new RuntimeException("afterDispatch interrupted", e);95}96return null;97}98};99methodMap = new HashMap<String, Object>();100methodMap.put("event", afterDispatchEventArgument);101methodMap.put("handle", afterDispatchHandleArgument);102methodMap.put("method", afterDispatchCallable);103objectMap.put("afterDispatch", methodMap);104105final AWTEvent[] beforeDispatchEventArgument = new AWTEvent[1];106Callable<Object> beforeDispatchCallable =107new Callable<Object>() {108public Object call() {109try {110return delegate.beforeDispatch(111beforeDispatchEventArgument[0]);112}113catch (InterruptedException e) {114throw new RuntimeException("beforeDispatch interrupted", e);115}116}117};118methodMap = new HashMap<String, Object>();119methodMap.put("event", beforeDispatchEventArgument);120methodMap.put("method", beforeDispatchCallable);121objectMap.put("beforeDispatch", methodMap);122123final EventQueue[] getNextEventEventQueueArgument = new EventQueue[1];124Callable<AWTEvent> getNextEventCallable =125new Callable<AWTEvent>() {126public AWTEvent call() throws Exception {127return delegate.getNextEvent(128getNextEventEventQueueArgument[0]);129}130};131methodMap = new HashMap<String, Object>();132methodMap.put("eventQueue", getNextEventEventQueueArgument);133methodMap.put("method", getNextEventCallable);134objectMap.put("getNextEvent", methodMap);135136return objectMap;137}138139static class MyEventQueueDelegate implements EventQueueDelegate.Delegate {140private volatile boolean getNextEventInvoked = false;141private volatile boolean beforeDispatchInvoked = false;142private volatile boolean afterDispatchInvoked = false;143public AWTEvent getNextEvent(EventQueue eventQueue)144throws InterruptedException {145getNextEventInvoked = true;146return eventQueue.getNextEvent();147}148public Object beforeDispatch(AWTEvent event) {149beforeDispatchInvoked = true;150return null;151}152public void afterDispatch(AWTEvent event, Object handle) {153afterDispatchInvoked = true;154}155private boolean allInvoked() {156return getNextEventInvoked && beforeDispatchInvoked && afterDispatchInvoked;157}158}159}160161162