Path: blob/master/test/jdk/java/awt/EventQueue/SecondaryLoopTest/SecondaryLoopTest.java
41153 views
/*1* Copyright (c) 2010, 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/*24@test25@bug 694993626@author Artem Ananiev: area=eventqueue27@run main/timeout=30 SecondaryLoopTest28*/2930import java.awt.*;3132/**33* Unit test for java.awt.SecondaryLoop implementation34*/35public class SecondaryLoopTest {3637private static volatile boolean loopStarted;38private static volatile boolean doubleEntered;39private static volatile boolean loopActive;40private static volatile boolean eventDispatched;4142public static void main(String[] args) throws Exception {43test(true, true);44test(true, false);45test(false, true);46test(false, false);47}4849private static void test(final boolean enterEDT, final boolean exitEDT) throws Exception {50System.out.println("Running test(" + enterEDT + ", " + exitEDT + ")");51System.err.flush();52loopStarted = true;53Runnable enterRun = new Runnable() {54@Override55public void run() {56Toolkit tk = Toolkit.getDefaultToolkit();57EventQueue eq = tk.getSystemEventQueue();58final SecondaryLoop loop = eq.createSecondaryLoop();59doubleEntered = false;60eventDispatched = false;61Runnable eventRun = new Runnable() {62@Override63public void run() {64// Let the loop enter65sleep(1000);66if (loop.enter()) {67doubleEntered = true;68}69eventDispatched = true;70}71};72EventQueue.invokeLater(eventRun);73Runnable exitRun = new Runnable() {74@Override75public void run() {76// Let the loop enter and eventRun finish77sleep(2000);78if (doubleEntered) {79// Hopefully, we get here if the loop is entered twice80loop.exit();81}82loop.exit();83}84};85if (exitEDT) {86EventQueue.invokeLater(exitRun);87} else {88new Thread(exitRun).start();89}90if (!loop.enter()) {91loopStarted = false;92}93loopActive = eventDispatched;94}95};96if (enterEDT) {97EventQueue.invokeAndWait(enterRun);98} else {99enterRun.run();100}101// Print all the flags before we fail with exception102System.out.println(" loopStarted = " + loopStarted);103System.out.println(" doubleEntered = " + doubleEntered);104System.out.println(" loopActive = " + loopActive);105System.out.flush();106if (!loopStarted) {107throw new RuntimeException("Test FAILED: the secondary loop is not started");108}109if (doubleEntered) {110throw new RuntimeException("Test FAILED: the secondary loop is started twice");111}112if (!loopActive) {113throw new RuntimeException("Test FAILED: the secondary loop exited immediately");114}115}116117private static void sleep(long t) {118try {119Thread.sleep(t);120} catch (InterruptedException e) {121e.printStackTrace(System.err);122}123}124125}126127128