Path: blob/master/test/jdk/java/awt/Focus/NullActiveWindowOnFocusLost/NullActiveWindowOnFocusLost.java
41152 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Button;24import java.awt.Frame;25import java.util.concurrent.TimeUnit;2627import sun.awt.SunToolkit;2829/**30* @test31* @key headful32* @bug 821143533* @modules java.desktop/sun.awt34*/35public final class NullActiveWindowOnFocusLost {3637private static volatile long endtime;38private static Throwable failed;3940public static void main(final String[] args) throws Exception {41// Will run the test no more than 30 seconds42endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);43Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);4445final Thread[] threads = new Thread[20];46for (int i = 0; i < threads.length; i++) {47threads[i] = testThread(i);48}49for (final Thread thread : threads) {50thread.start();51}52for (final Thread thread : threads) {53thread.join();54}55if (failed != null) {56failed.printStackTrace();57throw new RuntimeException(failed);58}59}6061private static Thread testThread(int index) {62return new Thread(new ThreadGroup("TG " + index), () -> {63SunToolkit.createNewAppContext();64while (!isComplete()) {65final Frame frame = new Frame();66frame.setSize(300, 300);67frame.add(new Button("Button"));68frame.setLocationRelativeTo(null);69frame.setVisible(true);70try {71Thread.sleep(index); // increase probability of the failure72} catch (InterruptedException ignored) {73}74frame.dispose();75}76});77}7879private static boolean isComplete() {80return endtime - System.nanoTime() < 0 || failed != null;81}82}838485