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
869N/A/*
869N/A @test
869N/A @bug 6304473 6727884
869N/A @summary Tests that an exception on EDT is handled with ThreadGroup.uncaughtException()
869N/A @author artem.ananiev: area=awt.eventdispatching
869N/A @library ../../regtesthelpers
869N/A @build Util
869N/A @run main HandleExceptionOnEDT
869N/A*/
869N/A
869N/Aimport java.awt.*;
869N/Aimport java.awt.event.*;
869N/A
869N/Aimport test.java.awt.regtesthelpers.Util;
869N/A
869N/Apublic class HandleExceptionOnEDT
869N/A{
869N/A private final static String EXCEPTION_MESSAGE = "A1234567890";
869N/A
869N/A private static volatile boolean exceptionHandled = false;
869N/A private static volatile boolean mousePressed = false;
869N/A
869N/A public static void main(String[] args)
869N/A {
869N/A final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()
869N/A {
869N/A @Override
869N/A public void uncaughtException(Thread t, Throwable e)
869N/A {
869N/A if (e.getMessage().equals(EXCEPTION_MESSAGE))
869N/A {
869N/A exceptionHandled = true;
869N/A }
869N/A }
869N/A };
869N/A
869N/A Frame f = new Frame("F");
869N/A f.setBounds(100, 100, 400, 300);
869N/A // set exception handler for EDT
869N/A f.addWindowListener(new WindowAdapter()
869N/A {
869N/A @Override
869N/A public void windowOpened(WindowEvent we)
869N/A {
869N/A Thread edt = Thread.currentThread();
869N/A edt.setUncaughtExceptionHandler(eh);
869N/A }
869N/A });
869N/A f.setVisible(true);
869N/A
869N/A Robot r = Util.createRobot();
869N/A Util.waitForIdle(r);
869N/A
869N/A // check exception without modal dialog
869N/A MouseListener exceptionListener = new MouseAdapter()
869N/A {
869N/A @Override
869N/A public void mousePressed(MouseEvent me)
869N/A {
869N/A throw new RuntimeException(EXCEPTION_MESSAGE);
869N/A }
869N/A };
869N/A f.addMouseListener(exceptionListener);
869N/A
869N/A exceptionHandled = false;
869N/A Point fp = f.getLocationOnScreen();
869N/A r.mouseMove(fp.x + f.getWidth() / 2, fp.y + f.getHeight() / 2);
869N/A Util.waitForIdle(r);
869N/A r.mousePress(InputEvent.BUTTON1_MASK);
869N/A Util.waitForIdle(r);
869N/A r.mouseRelease(InputEvent.BUTTON2_MASK);
869N/A f.removeMouseListener(exceptionListener);
869N/A
869N/A if (!exceptionHandled)
869N/A {
869N/A throw new RuntimeException("Test FAILED: exception is not handled for frame");
869N/A }
869N/A
869N/A // check exception with modal dialog
869N/A final Dialog d = new Dialog(f, "D", true);
869N/A d.setBounds(fp.x + 100, fp.y + 100, 400, 300);
869N/A d.addMouseListener(exceptionListener);
869N/A EventQueue.invokeLater(new Runnable()
869N/A {
869N/A @Override
869N/A public void run()
869N/A {
869N/A d.setVisible(true);
869N/A }
869N/A });
869N/A Util.waitForIdle(r);
869N/A
869N/A exceptionHandled = false;
869N/A Point dp = d.getLocationOnScreen();
869N/A r.mouseMove(dp.x + d.getWidth() / 2, dp.y + d.getHeight() / 2);
869N/A Util.waitForIdle(r);
869N/A r.mousePress(InputEvent.BUTTON1_MASK);
869N/A Util.waitForIdle(r);
869N/A r.mouseRelease(InputEvent.BUTTON2_MASK);
869N/A d.removeMouseListener(exceptionListener);
869N/A
869N/A if (!exceptionHandled)
869N/A {
869N/A throw new RuntimeException("Test FAILED: exception is not handled for modal dialog");
869N/A }
869N/A
869N/A // check the dialog is still modal
869N/A MouseListener pressedListener = new MouseAdapter()
869N/A {
869N/A @Override
869N/A public void mousePressed(MouseEvent me)
869N/A {
869N/A mousePressed = true;
869N/A }
869N/A };
869N/A f.addMouseListener(pressedListener);
869N/A
869N/A mousePressed = false;
869N/A r.mouseMove(fp.x + 50, fp.y + 50);
869N/A Util.waitForIdle(r);
869N/A r.mousePress(InputEvent.BUTTON1_MASK);
869N/A Util.waitForIdle(r);
869N/A r.mouseRelease(InputEvent.BUTTON1_MASK);
869N/A Util.waitForIdle(r);
869N/A f.removeMouseListener(pressedListener);
869N/A
869N/A if (mousePressed)
869N/A {
869N/A throw new RuntimeException("Test FAILED: modal dialog is not modal or visible after exception");
869N/A }
869N/A
869N/A // test is passed
869N/A d.dispose();
869N/A f.dispose();
869N/A }
869N/A}