1 /*
   2  * Copyright (c) 2010, 2016, 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   @key headful
  27   @bug 6304473 6727884
  28   @summary Tests that an exception on EDT is handled with ThreadGroup.uncaughtException()
  29   @author artem.ananiev: area=awt.eventdispatching
  30   @library ../../regtesthelpers
  31   @build Util
  32   @run main HandleExceptionOnEDT
  33 */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class HandleExceptionOnEDT
  41 {
  42     private final static String EXCEPTION_MESSAGE = "A1234567890";
  43 
  44     private static volatile boolean exceptionHandled = false;
  45     private static volatile boolean mousePressed = false;
  46 
  47     public static void main(String[] args)
  48     {
  49         final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()
  50         {
  51             @Override
  52             public void uncaughtException(Thread t, Throwable e)
  53             {
  54                 if (e.getMessage().equals(EXCEPTION_MESSAGE))
  55                 {
  56                     exceptionHandled = true;
  57                 }
  58             }
  59         };
  60 
  61         Frame f = new Frame("F");
  62         f.setBounds(100, 100, 400, 300);
  63         // set exception handler for EDT
  64         f.addWindowListener(new WindowAdapter()
  65         {
  66             @Override
  67             public void windowOpened(WindowEvent we)
  68             {
  69                 Thread edt = Thread.currentThread();
  70                 edt.setUncaughtExceptionHandler(eh);
  71             }
  72         });
  73         f.setVisible(true);
  74 
  75         Robot r = Util.createRobot();
  76         Util.waitForIdle(r);
  77 
  78         // check exception without modal dialog
  79         MouseListener exceptionListener = new MouseAdapter()
  80         {
  81             @Override
  82             public void mousePressed(MouseEvent me)
  83             {
  84                 throw new RuntimeException(EXCEPTION_MESSAGE);
  85             }
  86         };
  87         f.addMouseListener(exceptionListener);
  88 
  89         exceptionHandled = false;
  90         Point fp = f.getLocationOnScreen();
  91         r.mouseMove(fp.x + f.getWidth() / 2, fp.y + f.getHeight() / 2);
  92         Util.waitForIdle(r);
  93         r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  94         Util.waitForIdle(r);
  95         r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  96         f.removeMouseListener(exceptionListener);
  97 
  98         if (!exceptionHandled)
  99         {
 100             throw new RuntimeException("Test FAILED: exception is not handled for frame");
 101         }
 102 
 103         // check exception with modal dialog
 104         final Dialog d = new Dialog(f, "D", true);
 105         d.setBounds(fp.x + 100, fp.y + 100, 400, 300);
 106         d.addMouseListener(exceptionListener);
 107         EventQueue.invokeLater(new Runnable()
 108         {
 109             @Override
 110             public void run()
 111             {
 112                 d.setVisible(true);
 113             }
 114         });
 115         Util.waitForIdle(r);
 116 
 117         exceptionHandled = false;
 118         Point dp = d.getLocationOnScreen();
 119         r.mouseMove(dp.x + d.getWidth() / 2, dp.y + d.getHeight() / 2);
 120         Util.waitForIdle(r);
 121         r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 122         Util.waitForIdle(r);
 123         r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 124         d.removeMouseListener(exceptionListener);
 125 
 126         if (!exceptionHandled)
 127         {
 128             throw new RuntimeException("Test FAILED: exception is not handled for modal dialog");
 129         }
 130 
 131         // check the dialog is still modal
 132         MouseListener pressedListener = new MouseAdapter()
 133         {
 134             @Override
 135             public void mousePressed(MouseEvent me)
 136             {
 137                 mousePressed = true;
 138             }
 139         };
 140         f.addMouseListener(pressedListener);
 141 
 142         mousePressed = false;
 143         r.mouseMove(fp.x + 50, fp.y + 50);
 144         Util.waitForIdle(r);
 145         r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 146         Util.waitForIdle(r);
 147         r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 148         Util.waitForIdle(r);
 149         f.removeMouseListener(pressedListener);
 150 
 151         if (mousePressed)
 152         {
 153             throw new RuntimeException("Test FAILED: modal dialog is not modal or visible after exception");
 154         }
 155 
 156         // test is passed
 157         d.dispose();
 158         f.dispose();
 159     }
 160 }