1 /*
   2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test
  26   @bug 6304473 6727884
  27   @summary Tests that an exception on EDT is handled with ThreadGroup.uncaughtException()
  28   @author artem.ananiev: area=awt.eventdispatching
  29   @library ../../regtesthelpers
  30   @build Util
  31   @run main HandleExceptionOnEDT
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 
  37 import test.java.awt.regtesthelpers.Util;
  38 
  39 public class HandleExceptionOnEDT
  40 {
  41     private final static String EXCEPTION_MESSAGE = "A1234567890";
  42 
  43     private static volatile boolean exceptionHandled = false;
  44     private static volatile boolean mousePressed = false;
  45 
  46     public static void main(String[] args)
  47     {
  48         final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()
  49         {
  50             @Override
  51             public void uncaughtException(Thread t, Throwable e)
  52             {
  53                 if (e.getMessage().equals(EXCEPTION_MESSAGE))
  54                 {
  55                     exceptionHandled = true;
  56                 }
  57             }
  58         };
  59 
  60         Frame f = new Frame("F");
  61         f.setBounds(100, 100, 400, 300);
  62         // set exception handler for EDT
  63         f.addWindowListener(new WindowAdapter()
  64         {
  65             @Override
  66             public void windowOpened(WindowEvent we)
  67             {
  68                 Thread edt = Thread.currentThread();
  69                 edt.setUncaughtExceptionHandler(eh);
  70             }
  71         });
  72         f.setVisible(true);
  73 
  74         Robot r = Util.createRobot();
  75         Util.waitForIdle(r);
  76 
  77         // check exception without modal dialog
  78         MouseListener exceptionListener = new MouseAdapter()
  79         {
  80             @Override
  81             public void mousePressed(MouseEvent me)
  82             {
  83                 throw new RuntimeException(EXCEPTION_MESSAGE);
  84             }
  85         };
  86         f.addMouseListener(exceptionListener);
  87 
  88         exceptionHandled = false;
  89         Point fp = f.getLocationOnScreen();
  90         r.mouseMove(fp.x + f.getWidth() / 2, fp.y + f.getHeight() / 2);
  91         Util.waitForIdle(r);
  92         r.mousePress(InputEvent.BUTTON1_MASK);
  93         Util.waitForIdle(r);
  94         r.mouseRelease(InputEvent.BUTTON2_MASK);
  95         f.removeMouseListener(exceptionListener);
  96 
  97         if (!exceptionHandled)
  98         {
  99             throw new RuntimeException("Test FAILED: exception is not handled for frame");
 100         }
 101 
 102         // check exception with modal dialog
 103         final Dialog d = new Dialog(f, "D", true);
 104         d.setBounds(fp.x + 100, fp.y + 100, 400, 300);
 105         d.addMouseListener(exceptionListener);
 106         EventQueue.invokeLater(new Runnable()
 107         {
 108             @Override
 109             public void run()
 110             {
 111                 d.setVisible(true);
 112             }
 113         });
 114         Util.waitForIdle(r);
 115 
 116         exceptionHandled = false;
 117         Point dp = d.getLocationOnScreen();
 118         r.mouseMove(dp.x + d.getWidth() / 2, dp.y + d.getHeight() / 2);
 119         Util.waitForIdle(r);
 120         r.mousePress(InputEvent.BUTTON1_MASK);
 121         Util.waitForIdle(r);
 122         r.mouseRelease(InputEvent.BUTTON2_MASK);
 123         d.removeMouseListener(exceptionListener);
 124 
 125         if (!exceptionHandled)
 126         {
 127             throw new RuntimeException("Test FAILED: exception is not handled for modal dialog");
 128         }
 129 
 130         // check the dialog is still modal
 131         MouseListener pressedListener = new MouseAdapter()
 132         {
 133             @Override
 134             public void mousePressed(MouseEvent me)
 135             {
 136                 mousePressed = true;
 137             }
 138         };
 139         f.addMouseListener(pressedListener);
 140 
 141         mousePressed = false;
 142         r.mouseMove(fp.x + 50, fp.y + 50);
 143         Util.waitForIdle(r);
 144         r.mousePress(InputEvent.BUTTON1_MASK);
 145         Util.waitForIdle(r);
 146         r.mouseRelease(InputEvent.BUTTON1_MASK);
 147         Util.waitForIdle(r);
 148         f.removeMouseListener(pressedListener);
 149 
 150         if (mousePressed)
 151         {
 152             throw new RuntimeException("Test FAILED: modal dialog is not modal or visible after exception");
 153         }
 154 
 155         // test is passed
 156         d.dispose();
 157         f.dispose();
 158     }
 159 }