Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/NullActiveWindowOnFocusLost/NullActiveWindowOnFocusLost.java
47497 views
/*1* Copyright (c) 2018, 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* @bug 821143532* @modules java.desktop/sun.awt33*/34public final class NullActiveWindowOnFocusLost {3536private static volatile long endtime;37private static Throwable failed;3839public static void main(final String[] args) throws Exception {40// Will run the test no more than 30 seconds41endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);42Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);4344final Thread[] threads = new Thread[20];45for (int i = 0; i < threads.length; i++) {46threads[i] = testThread(i);47}48for (final Thread thread : threads) {49thread.start();50}51for (final Thread thread : threads) {52thread.join();53}54if (failed != null) {55failed.printStackTrace();56throw new RuntimeException(failed);57}58}5960private static Thread testThread(int index) {61return new Thread(new ThreadGroup("TG " + index), () -> {62SunToolkit.createNewAppContext();63while (!isComplete()) {64final Frame frame = new Frame();65frame.setSize(300, 300);66frame.add(new Button("Button"));67frame.setLocationRelativeTo(null);68frame.setVisible(true);69try {70Thread.sleep(index); // increase probability of the failure71} catch (InterruptedException ignored) {72}73frame.dispose();74}75});76}7778private static boolean isComplete() {79return endtime - System.nanoTime() < 0 || failed != null;80}81}828384