Path: blob/master/test/jdk/java/awt/EventDispatchThread/HandleExceptionOnEDT/HandleExceptionOnEDT.java
41154 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 6304473 672788427@summary Tests that an exception on EDT is handled with ThreadGroup.uncaughtException()28@author artem.ananiev: area=awt.eventdispatching29@library ../../regtesthelpers30@build Util31@run main HandleExceptionOnEDT32*/3334import java.awt.*;35import java.awt.event.*;3637import test.java.awt.regtesthelpers.Util;3839public class HandleExceptionOnEDT40{41private final static String EXCEPTION_MESSAGE = "A1234567890";4243private static volatile boolean exceptionHandled = false;44private static volatile boolean mousePressed = false;4546public static void main(String[] args)47{48final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()49{50@Override51public void uncaughtException(Thread t, Throwable e)52{53if (e.getMessage().equals(EXCEPTION_MESSAGE))54{55exceptionHandled = true;56}57}58};5960Frame f = new Frame("F");61f.setBounds(100, 100, 400, 300);62// set exception handler for EDT63f.addWindowListener(new WindowAdapter()64{65@Override66public void windowOpened(WindowEvent we)67{68Thread edt = Thread.currentThread();69edt.setUncaughtExceptionHandler(eh);70}71});72f.setVisible(true);7374Robot r = Util.createRobot();75Util.waitForIdle(r);7677// check exception without modal dialog78MouseListener exceptionListener = new MouseAdapter()79{80@Override81public void mousePressed(MouseEvent me)82{83throw new RuntimeException(EXCEPTION_MESSAGE);84}85};86f.addMouseListener(exceptionListener);8788exceptionHandled = false;89Point fp = f.getLocationOnScreen();90r.mouseMove(fp.x + f.getWidth() / 2, fp.y + f.getHeight() / 2);91Util.waitForIdle(r);92r.mousePress(InputEvent.BUTTON1_DOWN_MASK);93Util.waitForIdle(r);94r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);95f.removeMouseListener(exceptionListener);9697if (!exceptionHandled)98{99throw new RuntimeException("Test FAILED: exception is not handled for frame");100}101102// check exception with modal dialog103final Dialog d = new Dialog(f, "D", true);104d.setBounds(fp.x + 100, fp.y + 100, 400, 300);105d.addMouseListener(exceptionListener);106EventQueue.invokeLater(new Runnable()107{108@Override109public void run()110{111d.setVisible(true);112}113});114Util.waitForIdle(r);115116exceptionHandled = false;117Point dp = d.getLocationOnScreen();118r.mouseMove(dp.x + d.getWidth() / 2, dp.y + d.getHeight() / 2);119Util.waitForIdle(r);120r.mousePress(InputEvent.BUTTON1_DOWN_MASK);121Util.waitForIdle(r);122r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);123d.removeMouseListener(exceptionListener);124125if (!exceptionHandled)126{127throw new RuntimeException("Test FAILED: exception is not handled for modal dialog");128}129130// check the dialog is still modal131MouseListener pressedListener = new MouseAdapter()132{133@Override134public void mousePressed(MouseEvent me)135{136mousePressed = true;137}138};139f.addMouseListener(pressedListener);140141mousePressed = false;142r.mouseMove(fp.x + 50, fp.y + 50);143Util.waitForIdle(r);144r.mousePress(InputEvent.BUTTON1_DOWN_MASK);145Util.waitForIdle(r);146r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);147Util.waitForIdle(r);148f.removeMouseListener(pressedListener);149150if (mousePressed)151{152throw new RuntimeException("Test FAILED: modal dialog is not modal or visible after exception");153}154155// test is passed156d.dispose();157f.dispose();158}159}160161162