1 /* 2 * Copyright (c) 2018, 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 import java.awt.Button; 25 import java.awt.Frame; 26 import java.util.concurrent.TimeUnit; 27 28 import sun.awt.SunToolkit; 29 30 /** 31 * @test 32 * @key headful 33 * @bug 8211435 34 * @modules java.desktop/sun.awt 35 */ 36 public final class NullActiveWindowOnFocusLost { 37 38 private static volatile long endtime; 39 private static Throwable failed; 40 41 public static void main(final String[] args) throws Exception { 42 // Will run the test no more than 30 seconds 43 endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); 44 Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e); 45 46 final Thread[] threads = new Thread[20]; 47 for (int i = 0; i < threads.length; i++) { 48 threads[i] = testThread(i); 49 } 50 for (final Thread thread : threads) { 51 thread.start(); 52 } 53 for (final Thread thread : threads) { 54 thread.join(); 55 } 56 if (failed != null) { 57 failed.printStackTrace(); 58 throw new RuntimeException(failed); 59 } 60 } 61 62 private static Thread testThread(int index) { 63 return new Thread(new ThreadGroup("TG " + index), () -> { 64 SunToolkit.createNewAppContext(); 65 while (!isComplete()) { 66 final Frame frame = new Frame(); 67 frame.setSize(300, 300); 68 frame.add(new Button("Button")); 69 frame.setLocationRelativeTo(null); 70 frame.setVisible(true); 71 try { 72 Thread.sleep(index); // increase probability of the failure 73 } catch (InterruptedException ignored) { 74 } 75 frame.dispose(); 76 } 77 }); 78 } 79 80 private static boolean isComplete() { 81 return endtime - System.nanoTime() < 0 || failed != null; 82 } 83 }