1/*
2 * Copyright (c) 2013, 2015, 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 8012933
27 * @summary Tests (although somewhat indirectly) that createNewAppContext()
28 * immediately followed by dispose() works correctly
29 * @author Leonid Romanov
30 * @modules java.desktop/sun.awt
31 */
32
33import sun.awt.SunToolkit;
34import sun.awt.AppContext;
35
36public class Test8012933 {
37    private AppContext appContext = null;
38    final ThreadGroup threadGroup = new ThreadGroup("test thread group");
39    final Object lock = new Object();
40    boolean isCreated = false;
41
42    public static void main(String[] args) throws Exception {
43        SunToolkit.createNewAppContext();
44        new Test8012933().test();
45    }
46
47    private void test() throws Exception {
48        createAppContext();
49        long startTime = System.currentTimeMillis();
50        appContext.dispose();
51        long endTime = System.currentTimeMillis();
52
53        // In case of the bug, calling dispose() when there is no EQ
54        // dispatch thread running fails to create it, so it takes
55        // almost 10 sec to return from dispose(), which is spent
56        // waiting on the notificationLock.
57        if ((endTime - startTime) > 9000) {
58            throw new RuntimeException("Returning from dispose() took too much time, probably a bug");
59        }
60    }
61
62    private void createAppContext() {
63        isCreated = false;
64        final Runnable runnable = new Runnable() {
65                public void run() {
66                    appContext = SunToolkit.createNewAppContext();
67                    synchronized (lock) {
68                        isCreated = true;
69                        lock.notifyAll();
70                    }
71                }
72            };
73
74        final Thread thread = new Thread(threadGroup, runnable, "creates app context");
75        synchronized (lock) {
76            thread.start();
77            while (!isCreated) {
78                try {
79                    lock.wait();
80                } catch (InterruptedException ie) {
81                    ie.printStackTrace();
82                }
83            }
84        }
85
86        if (appContext == null) {
87            throw new RuntimeException("failed to create app context.");
88        } else {
89            System.out.println("app context was created.");
90        }
91    }
92
93}
94