Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/EventDispatchThread/PreserveDispathThread/PreserveDispatchThread.java
38828 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 642415726@author Artem Ananiev: area=eventqueue27@run main PreserveDispatchThread28*/2930import java.awt.*;31import java.awt.event.*;3233public class PreserveDispatchThread {3435private static volatile Frame f;36private static volatile Dialog d;3738private static volatile boolean isEDT = true;3940public static void main(String[] args) throws Exception {41f = new Frame("F");42f.setSize(320, 340);43f.setLocationRelativeTo(null);44f.setVisible(true);4546try {47test1();48if (!isEDT) {49throw new RuntimeException("Test FAILED (test1): event dispatch thread is changed");50}5152test2();53if (!isEDT) {54throw new RuntimeException("Test FAILED (test2): event dispatch thread is changed");55}5657test3();58if (!isEDT) {59throw new RuntimeException("Test FAILED (test3): event dispatch thread is changed");60}61} finally {62if (d != null) {63d.dispose();64}65f.dispose();66}67}6869/*70* Tests that push/pop doesn't change the dispatch thread if71* called on EDT.72*/73private static void test1() throws Exception {74EventQueue.invokeAndWait(new Runnable() {75@Override76public void run() {77TestEventQueue teq = new TestEventQueue();78EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();79try {80seq.push(teq);81d = new TestDialog();82d.setVisible(true);83checkEDT();84} finally {85teq.pop();86}87checkEDT();88}89});90}9192/*93* Tests that push/pop doesn't change the dispatch thread if94* called on the main thread.95*/96private static void test2() throws Exception {97TestEventQueue teq = new TestEventQueue();98EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();99try {100seq.push(teq);101EventQueue.invokeAndWait(new Runnable() {102@Override103public void run() {104checkEDT();105d = new TestDialog();106d.setVisible(true);107checkEDT();108}109});110} finally {111teq.pop();112}113}114115private static final Object test3Lock = new Object();116private static boolean test3Sync = false;117118/*119* A complex test: several nested invokeLater() are called and120* in every runnable a check for EDT is performed. At the ent121* of the test we wait for all the runnables to be processed122* and the dialog is disposed; otherwise the last EDT check can123* be later than this method returns and the whole test is passed.124*/125private static void test3() throws Exception {126EventQueue.invokeLater(new Runnable() {127@Override128public void run() {129d = new Dialog(f, true);130d.setSize(240, 180);131d.setLocationRelativeTo(f);132EventQueue.invokeLater(new Runnable() {133@Override134public void run() {135d.setVisible(true);136checkEDT();137}138});139EventQueue.invokeLater(new Runnable() {140@Override141public void run() {142TestEventQueue teq = new TestEventQueue();143EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();144try {145seq.push(teq);146checkEDT();147EventQueue.invokeLater(new Runnable() {148@Override149public void run() {150d.dispose();151checkEDT();152synchronized (test3Lock) {153test3Sync = true;154test3Lock.notify();155}156}157});158} finally {159teq.pop();160}161checkEDT();162}163});164checkEDT();165}166});167synchronized (test3Lock) {168while (!test3Sync) {169try {170test3Lock.wait();171} catch (InterruptedException ie) {172break;173}174}175}176// Make sure all the nested invokeLater/invokeAndWait are processed177EventQueue.invokeAndWait(new Runnable() {178@Override179public void run() {180}181});182}183184private static void checkEDT() {185isEDT = isEDT && EventQueue.isDispatchThread();186}187188private static class TestEventQueue extends EventQueue {189public TestEventQueue() {190super();191}192public void pop() {193super.pop();194}195}196197private static class TestDialog extends Dialog {198private volatile boolean dialogShown = false;199private volatile boolean paintCalled = false;200public TestDialog() {201super(f, true);202setSize(240, 180);203setLocationRelativeTo(f);204addComponentListener(new ComponentAdapter() {205@Override206public void componentShown(ComponentEvent e) {207if (paintCalled) {208dispose();209}210dialogShown = true;211}212});213}214@Override215public void paint(Graphics g) {216if (dialogShown) {217dispose();218}219paintCalled = true;220}221}222223}224225226