2518N/A/*
2518N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2518N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2518N/A *
2518N/A * This code is free software; you can redistribute it and/or modify it
2518N/A * under the terms of the GNU General Public License version 2 only, as
2518N/A * published by the Free Software Foundation.
2518N/A *
2518N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2518N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2518N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2518N/A * version 2 for more details (a copy is included in the LICENSE file that
2518N/A * accompanied this code).
2518N/A *
2518N/A * You should have received a copy of the GNU General Public License version
2518N/A * 2 along with this work; if not, write to the Free Software Foundation,
2518N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2518N/A *
2518N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2518N/A * or visit www.oracle.com if you need additional information or have any
2518N/A * questions.
2518N/A */
2518N/A
2518N/A/*
2518N/A @test
2518N/A @bug 6424157
2518N/A @author Artem Ananiev: area=eventqueue
2518N/A @run main PreserveDispatchThread
2518N/A*/
2518N/A
2518N/Aimport java.awt.*;
2518N/Aimport java.awt.event.*;
2518N/A
2518N/Apublic class PreserveDispatchThread {
2518N/A
2518N/A private static volatile Frame f;
2518N/A private static volatile Dialog d;
2518N/A
2518N/A private static volatile boolean isEDT = true;
2518N/A
2518N/A public static void main(String[] args) throws Exception {
2518N/A f = new Frame("F");
2518N/A f.setSize(320, 340);
2518N/A f.setLocationRelativeTo(null);
2518N/A f.setVisible(true);
2518N/A
2518N/A try {
2518N/A test1();
2518N/A if (!isEDT) {
2518N/A throw new RuntimeException("Test FAILED (test1): event dispatch thread is changed");
2518N/A }
2518N/A
2518N/A test2();
2518N/A if (!isEDT) {
2518N/A throw new RuntimeException("Test FAILED (test2): event dispatch thread is changed");
2518N/A }
2518N/A
2518N/A test3();
2518N/A if (!isEDT) {
2518N/A throw new RuntimeException("Test FAILED (test3): event dispatch thread is changed");
2518N/A }
2518N/A } finally {
2518N/A if (d != null) {
2518N/A d.dispose();
2518N/A }
2518N/A f.dispose();
2518N/A }
2518N/A }
2518N/A
2518N/A /*
2518N/A * Tests that push/pop doesn't change the dispatch thread if
2518N/A * called on EDT.
2518N/A */
2518N/A private static void test1() throws Exception {
2518N/A EventQueue.invokeAndWait(new Runnable() {
2518N/A @Override
2518N/A public void run() {
2518N/A TestEventQueue teq = new TestEventQueue();
2518N/A EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
2518N/A try {
2518N/A seq.push(teq);
2518N/A d = new TestDialog();
2518N/A d.setVisible(true);
2518N/A checkEDT();
2518N/A } finally {
2518N/A teq.pop();
2518N/A }
2518N/A checkEDT();
2518N/A }
2518N/A });
2518N/A }
2518N/A
2518N/A /*
2518N/A * Tests that push/pop doesn't change the dispatch thread if
2518N/A * called on the main thread.
2518N/A */
2518N/A private static void test2() throws Exception {
2518N/A TestEventQueue teq = new TestEventQueue();
2518N/A EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
2518N/A try {
2518N/A seq.push(teq);
2518N/A EventQueue.invokeAndWait(new Runnable() {
2518N/A @Override
2518N/A public void run() {
2518N/A checkEDT();
2518N/A d = new TestDialog();
2518N/A d.setVisible(true);
2518N/A checkEDT();
2518N/A }
2518N/A });
2518N/A } finally {
2518N/A teq.pop();
2518N/A }
2518N/A }
2518N/A
2518N/A private static final Object test3Lock = new Object();
2518N/A private static boolean test3Sync = false;
2518N/A
2518N/A /*
2518N/A * A complex test: several nested invokeLater() are called and
2518N/A * in every runnable a check for EDT is performed. At the ent
2518N/A * of the test we wait for all the runnables to be processed
2518N/A * and the dialog is disposed; otherwise the last EDT check can
2518N/A * be later than this method returns and the whole test is passed.
2518N/A */
2518N/A private static void test3() throws Exception {
2518N/A EventQueue.invokeLater(new Runnable() {
2518N/A @Override
2518N/A public void run() {
2518N/A d = new Dialog(f, true);
2518N/A d.setSize(240, 180);
2518N/A d.setLocationRelativeTo(f);
2518N/A EventQueue.invokeLater(new Runnable() {
2518N/A @Override
2518N/A public void run() {
2518N/A d.setVisible(true);
2518N/A checkEDT();
2518N/A }
2518N/A });
2518N/A EventQueue.invokeLater(new Runnable() {
2518N/A @Override
2518N/A public void run() {
2518N/A TestEventQueue teq = new TestEventQueue();
2518N/A EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
2518N/A try {
2518N/A seq.push(teq);
2518N/A checkEDT();
2518N/A EventQueue.invokeLater(new Runnable() {
2518N/A @Override
2518N/A public void run() {
2518N/A d.dispose();
2518N/A checkEDT();
2518N/A synchronized (test3Lock) {
2518N/A test3Sync = true;
2518N/A test3Lock.notify();
2518N/A }
2518N/A }
2518N/A });
2518N/A } finally {
2518N/A teq.pop();
2518N/A }
2518N/A checkEDT();
2518N/A }
2518N/A });
2518N/A checkEDT();
2518N/A }
2518N/A });
2518N/A synchronized (test3Lock) {
2518N/A while (!test3Sync) {
2518N/A try {
2518N/A test3Lock.wait();
2518N/A } catch (InterruptedException ie) {
2518N/A break;
2518N/A }
2518N/A }
2518N/A }
2518N/A // Make sure all the nested invokeLater/invokeAndWait are processed
2518N/A EventQueue.invokeAndWait(new Runnable() {
2518N/A @Override
2518N/A public void run() {
2518N/A }
2518N/A });
2518N/A }
2518N/A
2518N/A private static void checkEDT() {
2518N/A isEDT = isEDT && EventQueue.isDispatchThread();
2518N/A }
2518N/A
2518N/A private static class TestEventQueue extends EventQueue {
2518N/A public TestEventQueue() {
2518N/A super();
2518N/A }
2518N/A public void pop() {
2518N/A super.pop();
2518N/A }
2518N/A }
2518N/A
2518N/A private static class TestDialog extends Dialog {
2518N/A private volatile boolean dialogShown = false;
2518N/A private volatile boolean paintCalled = false;
2518N/A public TestDialog() {
2518N/A super(f, true);
2518N/A setSize(240, 180);
2518N/A setLocationRelativeTo(f);
2518N/A addComponentListener(new ComponentAdapter() {
2518N/A @Override
2518N/A public void componentShown(ComponentEvent e) {
2518N/A if (paintCalled) {
2518N/A dispose();
2518N/A }
2518N/A dialogShown = true;
2518N/A }
2518N/A });
2518N/A }
2518N/A @Override
2518N/A public void paint(Graphics g) {
2518N/A if (dialogShown) {
2518N/A dispose();
2518N/A }
2518N/A paintCalled = true;
2518N/A }
2518N/A }
2518N/A
2518N/A}