Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/EventDispatchThread/HandleExceptionOnEDT/HandleExceptionOnEDT.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 6304473 672788426@summary Tests that an exception on EDT is handled with ThreadGroup.uncaughtException()27@author artem.ananiev: area=awt.eventdispatching28@library ../../regtesthelpers29@build Util30@run main HandleExceptionOnEDT31*/3233import java.awt.*;34import java.awt.event.*;3536import test.java.awt.regtesthelpers.Util;3738public class HandleExceptionOnEDT39{40private final static String EXCEPTION_MESSAGE = "A1234567890";4142private static volatile boolean exceptionHandled = false;43private static volatile boolean mousePressed = false;4445public static void main(String[] args)46{47final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()48{49@Override50public void uncaughtException(Thread t, Throwable e)51{52if (e.getMessage().equals(EXCEPTION_MESSAGE))53{54exceptionHandled = true;55}56}57};5859Frame f = new Frame("F");60f.setBounds(100, 100, 400, 300);61// set exception handler for EDT62f.addWindowListener(new WindowAdapter()63{64@Override65public void windowOpened(WindowEvent we)66{67Thread edt = Thread.currentThread();68edt.setUncaughtExceptionHandler(eh);69}70});71f.setVisible(true);7273Robot r = Util.createRobot();74Util.waitForIdle(r);7576// check exception without modal dialog77MouseListener exceptionListener = new MouseAdapter()78{79@Override80public void mousePressed(MouseEvent me)81{82throw new RuntimeException(EXCEPTION_MESSAGE);83}84};85f.addMouseListener(exceptionListener);8687exceptionHandled = false;88Point fp = f.getLocationOnScreen();89r.mouseMove(fp.x + f.getWidth() / 2, fp.y + f.getHeight() / 2);90Util.waitForIdle(r);91r.mousePress(InputEvent.BUTTON1_MASK);92Util.waitForIdle(r);93r.mouseRelease(InputEvent.BUTTON2_MASK);94f.removeMouseListener(exceptionListener);9596if (!exceptionHandled)97{98throw new RuntimeException("Test FAILED: exception is not handled for frame");99}100101// check exception with modal dialog102final Dialog d = new Dialog(f, "D", true);103d.setBounds(fp.x + 100, fp.y + 100, 400, 300);104d.addMouseListener(exceptionListener);105EventQueue.invokeLater(new Runnable()106{107@Override108public void run()109{110d.setVisible(true);111}112});113Util.waitForIdle(r);114115exceptionHandled = false;116Point dp = d.getLocationOnScreen();117r.mouseMove(dp.x + d.getWidth() / 2, dp.y + d.getHeight() / 2);118Util.waitForIdle(r);119r.mousePress(InputEvent.BUTTON1_MASK);120Util.waitForIdle(r);121r.mouseRelease(InputEvent.BUTTON2_MASK);122d.removeMouseListener(exceptionListener);123124if (!exceptionHandled)125{126throw new RuntimeException("Test FAILED: exception is not handled for modal dialog");127}128129// check the dialog is still modal130MouseListener pressedListener = new MouseAdapter()131{132@Override133public void mousePressed(MouseEvent me)134{135mousePressed = true;136}137};138f.addMouseListener(pressedListener);139140mousePressed = false;141r.mouseMove(fp.x + 50, fp.y + 50);142Util.waitForIdle(r);143r.mousePress(InputEvent.BUTTON1_MASK);144Util.waitForIdle(r);145r.mouseRelease(InputEvent.BUTTON1_MASK);146Util.waitForIdle(r);147f.removeMouseListener(pressedListener);148149if (mousePressed)150{151throw new RuntimeException("Test FAILED: modal dialog is not modal or visible after exception");152}153154// test is passed155d.dispose();156f.dispose();157}158}159160161