Path: blob/master/test/jdk/java/awt/EventDispatchThread/PreserveDispathThread/PreserveDispatchThread.java
41153 views
/*1* Copyright (c) 2010, 2016, 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@key headful26@bug 642415727@author Artem Ananiev: area=eventqueue28@run main PreserveDispatchThread29*/3031import java.awt.*;32import java.awt.event.*;3334public class PreserveDispatchThread {3536private static volatile Frame f;37private static volatile Dialog d;3839private static volatile boolean isEDT = true;4041public static void main(String[] args) throws Exception {42f = new Frame("F");43f.setSize(320, 340);44f.setLocationRelativeTo(null);45f.setVisible(true);4647try {48test1();49if (!isEDT) {50throw new RuntimeException("Test FAILED (test1): event dispatch thread is changed");51}5253test2();54if (!isEDT) {55throw new RuntimeException("Test FAILED (test2): event dispatch thread is changed");56}5758test3();59if (!isEDT) {60throw new RuntimeException("Test FAILED (test3): event dispatch thread is changed");61}62} finally {63if (d != null) {64d.dispose();65}66f.dispose();67}68}6970/*71* Tests that push/pop doesn't change the dispatch thread if72* called on EDT.73*/74private static void test1() throws Exception {75EventQueue.invokeAndWait(new Runnable() {76@Override77public void run() {78TestEventQueue teq = new TestEventQueue();79EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();80try {81seq.push(teq);82d = new TestDialog();83d.setVisible(true);84checkEDT();85} finally {86teq.pop();87}88checkEDT();89}90});91}9293/*94* Tests that push/pop doesn't change the dispatch thread if95* called on the main thread.96*/97private static void test2() throws Exception {98TestEventQueue teq = new TestEventQueue();99EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();100try {101seq.push(teq);102EventQueue.invokeAndWait(new Runnable() {103@Override104public void run() {105checkEDT();106d = new TestDialog();107d.setVisible(true);108checkEDT();109}110});111} finally {112teq.pop();113}114}115116private static final Object test3Lock = new Object();117private static boolean test3Sync = false;118119/*120* A complex test: several nested invokeLater() are called and121* in every runnable a check for EDT is performed. At the ent122* of the test we wait for all the runnables to be processed123* and the dialog is disposed; otherwise the last EDT check can124* be later than this method returns and the whole test is passed.125*/126private static void test3() throws Exception {127EventQueue.invokeLater(new Runnable() {128@Override129public void run() {130d = new Dialog(f, true);131d.setSize(240, 180);132d.setLocationRelativeTo(f);133EventQueue.invokeLater(new Runnable() {134@Override135public void run() {136d.setVisible(true);137checkEDT();138}139});140EventQueue.invokeLater(new Runnable() {141@Override142public void run() {143TestEventQueue teq = new TestEventQueue();144EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();145try {146seq.push(teq);147checkEDT();148EventQueue.invokeLater(new Runnable() {149@Override150public void run() {151d.dispose();152checkEDT();153synchronized (test3Lock) {154test3Sync = true;155test3Lock.notify();156}157}158});159} finally {160teq.pop();161}162checkEDT();163}164});165checkEDT();166}167});168synchronized (test3Lock) {169while (!test3Sync) {170try {171test3Lock.wait();172} catch (InterruptedException ie) {173break;174}175}176}177// Make sure all the nested invokeLater/invokeAndWait are processed178EventQueue.invokeAndWait(new Runnable() {179@Override180public void run() {181}182});183}184185private static void checkEDT() {186isEDT = isEDT && EventQueue.isDispatchThread();187}188189private static class TestEventQueue extends EventQueue {190public TestEventQueue() {191super();192}193public void pop() {194super.pop();195}196}197198private static class TestDialog extends Dialog {199private volatile boolean dialogShown = false;200private volatile boolean paintCalled = false;201public TestDialog() {202super(f, true);203setSize(240, 180);204setLocationRelativeTo(f);205addComponentListener(new ComponentAdapter() {206@Override207public void componentShown(ComponentEvent e) {208if (paintCalled) {209dispose();210}211dialogShown = true;212}213});214}215@Override216public void paint(Graphics g) {217if (dialogShown) {218dispose();219}220paintCalled = true;221}222}223224}225226227