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 6949936
27  @author Artem Ananiev: area=eventqueue
28  @run main/timeout=30 SecondaryLoopTest
29*/
30
31import java.awt.*;
32
33/**
34 * Unit test for java.awt.SecondaryLoop implementation
35 */
36public class SecondaryLoopTest {
37
38    private static volatile boolean loopStarted;
39    private static volatile boolean doubleEntered;
40    private static volatile boolean loopActive;
41    private static volatile boolean eventDispatched;
42
43    public static void main(String[] args) throws Exception {
44        test(true, true);
45        test(true, false);
46        test(false, true);
47        test(false, false);
48    }
49
50    private static void test(final boolean enterEDT, final boolean exitEDT) throws Exception {
51        System.out.println("Running test(" + enterEDT + ", " + exitEDT + ")");
52        System.err.flush();
53        loopStarted = true;
54        Runnable enterRun = new Runnable() {
55            @Override
56            public void run() {
57                Toolkit tk = Toolkit.getDefaultToolkit();
58                EventQueue eq = tk.getSystemEventQueue();
59                final SecondaryLoop loop = eq.createSecondaryLoop();
60                doubleEntered = false;
61                eventDispatched = false;
62                Runnable eventRun = new Runnable() {
63                    @Override
64                    public void run() {
65                        // Let the loop enter
66                        sleep(1000);
67                        if (loop.enter()) {
68                            doubleEntered = true;
69                        }
70                        eventDispatched = true;
71                    }
72                };
73                EventQueue.invokeLater(eventRun);
74                Runnable exitRun = new Runnable() {
75                    @Override
76                    public void run() {
77                        // Let the loop enter and eventRun finish
78                        sleep(2000);
79                        if (doubleEntered) {
80                            // Hopefully, we get here if the loop is entered twice
81                            loop.exit();
82                        }
83                        loop.exit();
84                    }
85                };
86                if (exitEDT) {
87                    EventQueue.invokeLater(exitRun);
88                } else {
89                    new Thread(exitRun).start();
90                }
91                if (!loop.enter()) {
92                    loopStarted = false;
93                }
94                loopActive = eventDispatched;
95            }
96        };
97        if (enterEDT) {
98            EventQueue.invokeAndWait(enterRun);
99        } else {
100            enterRun.run();
101        }
102        // Print all the flags before we fail with exception
103        System.out.println("    loopStarted = " + loopStarted);
104        System.out.println("    doubleEntered = " + doubleEntered);
105        System.out.println("    loopActive = " + loopActive);
106        System.out.flush();
107        if (!loopStarted) {
108            throw new RuntimeException("Test FAILED: the secondary loop is not started");
109        }
110        if (doubleEntered) {
111            throw new RuntimeException("Test FAILED: the secondary loop is started twice");
112        }
113        if (!loopActive) {
114            throw new RuntimeException("Test FAILED: the secondary loop exited immediately");
115        }
116    }
117
118    private static void sleep(long t) {
119        try {
120            Thread.sleep(t);
121        } catch (InterruptedException e) {
122            e.printStackTrace(System.err);
123        }
124    }
125
126}
127