test/jdk/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.java
author serb
Wed, 27 Feb 2019 18:46:55 -0800
changeset 54231 e4813eded7cb
parent 47216 71c04702a3d5
permissions -rw-r--r--
8213110: Remove the use of applets in automatic tests Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19175
478a81cf568d 8022247: java/awt/EventDispatchThread/LoopRobustness/LoopRobustness throws NPE
leonidr
parents: 7668
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2953
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2953
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2953
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 47216
diff changeset
    25
 * @test
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4023283
40128
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 19175
diff changeset
    27
 * @key headful
e635645d2a8a 8160974: [TESTBUG] Mark more headful tests with @key headful.
goetz
parents: 19175
diff changeset
    28
 * @summary Checks that an Error which propogates up to the EventDispatch
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * loop does not crash AWT.
2953
d8c81312ac1c 6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents: 2
diff changeset
    30
 * @author Andrei Dmitriev: area=awt.event
d8c81312ac1c 6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents: 2
diff changeset
    31
 * @library ../../regtesthelpers
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 47216
diff changeset
    32
 * @modules java.desktop/sun.awt
2953
d8c81312ac1c 6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents: 2
diff changeset
    33
 * @build Util
d8c81312ac1c 6847584: closed/java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.html fails
dcherepanov
parents: 2
diff changeset
    34
 * @run main LoopRobustness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.event.*;
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    39
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    40
import sun.awt.SunToolkit;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import test.java.awt.regtesthelpers.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class LoopRobustness {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    45
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    final static long TIMEOUT = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    final static Object LOCK = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    49
    public static int clicks = 0;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    50
    public static volatile boolean notifyOccured = false;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    51
    public static volatile boolean otherExceptionsCaught = false;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    52
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    53
    public static void main(String [] args) throws Exception {
19175
478a81cf568d 8022247: java/awt/EventDispatchThread/LoopRobustness/LoopRobustness throws NPE
leonidr
parents: 7668
diff changeset
    54
        SunToolkit.createNewAppContext();
478a81cf568d 8022247: java/awt/EventDispatchThread/LoopRobustness/LoopRobustness throws NPE
leonidr
parents: 7668
diff changeset
    55
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        long at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        //wait for a TIMEOUT giving a chance to a new Thread above to accomplish its stuff.
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    60
        synchronized (LoopRobustness.LOCK) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            new Thread(new TestThreadGroup(mainThreadGroup, "TestGroup"), new Impl()).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            at = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    64
                while (!notifyOccured && (System.currentTimeMillis() - at < TIMEOUT)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    LoopRobustness.LOCK.wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    67
            } catch (InterruptedException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                throw new RuntimeException("Test interrupted.", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    72
        if (!notifyOccured) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            //notify doesn't occur after a reasonable time.
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    74
            throw new RuntimeException("Test FAILED: second thread hasn't notified MainThread");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        //now wait for two clicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        at = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        while(System.currentTimeMillis() - at < TIMEOUT && clicks < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            } catch(InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw new RuntimeException("Test interrupted.", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (clicks != 2) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    87
            throw new RuntimeException("Test FAILED: robot should press button twice");
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    88
        }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    89
        if (otherExceptionsCaught) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    90
            throw new RuntimeException("Test FAILED: unexpected exceptions caught");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
class Impl implements Runnable{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static Robot robot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void run() {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    98
        SunToolkit.createNewAppContext();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    99
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Button b = new Button("Press me to test the AWT-Event Queue thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Frame lr = new Frame("ROBUST FRAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        lr.setBounds(100, 100, 300, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        b.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    LoopRobustness.clicks++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    //throwing an exception in Static Initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    System.out.println(HostileCrasher.aStaticMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        lr.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        lr.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            robot = new Robot();
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   115
        } catch (AWTException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new RuntimeException("Test interrupted.", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        synchronized (LoopRobustness.LOCK){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            LoopRobustness.LOCK.notify();
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   122
            LoopRobustness.notifyOccured = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int i = 0;
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   126
        while (i < 2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            robot.mouseMove(b.getLocationOnScreen().x + b.getWidth()/2,
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   128
                            b.getLocationOnScreen().y + b.getHeight()/2);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   129
            Util.waitForIdle(robot);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            robot.mousePress(InputEvent.BUTTON1_MASK);
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   131
            Util.waitForIdle(robot);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   133
            Util.waitForIdle(robot);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
class TestThreadGroup extends ThreadGroup {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   140
    TestThreadGroup(ThreadGroup threadGroup, String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super(threadGroup, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   144
    public void uncaughtException(Thread thread, Throwable e) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   145
        System.out.println("Exception caught: " + e);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   146
        e.printStackTrace(System.out);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   147
        System.out.flush();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   148
        if ((e instanceof ExceptionInInitializerError) ||
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   149
            (e instanceof NoClassDefFoundError))
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   150
        {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   151
            // These two are expected
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   152
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   154
        LoopRobustness.otherExceptionsCaught = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
class HostileCrasher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (Math.random() >= 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new RuntimeException("Die, AWT-Event Queue thread!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static String aStaticMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return "hello, world";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}