test/jdk/java/awt/Focus/NullActiveWindowOnFocusLost/NullActiveWindowOnFocusLost.java
changeset 52549 50de7d1f6b64
child 54238 371d29d4add5
equal deleted inserted replaced
52548:472f10d8a501 52549:50de7d1f6b64
       
     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  * @requires (os.family == "mac")
       
    35  * @modules java.desktop/sun.awt
       
    36  */
       
    37 public final class NullActiveWindowOnFocusLost {
       
    38 
       
    39     private static volatile long endtime;
       
    40     private static Throwable failed;
       
    41 
       
    42     public static void main(final String[] args) throws Exception {
       
    43         // Will run the test no more than 30 seconds
       
    44         endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
       
    45         Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);
       
    46 
       
    47         final Thread[] threads = new Thread[20];
       
    48         for (int i = 0; i < threads.length; i++) {
       
    49             threads[i] = testThread(i);
       
    50         }
       
    51         for (final Thread thread : threads) {
       
    52             thread.start();
       
    53         }
       
    54         for (final Thread thread : threads) {
       
    55             thread.join();
       
    56         }
       
    57         if (failed != null) {
       
    58             failed.printStackTrace();
       
    59             throw new RuntimeException(failed);
       
    60         }
       
    61     }
       
    62 
       
    63     private static Thread testThread(int index) {
       
    64         return new Thread(new ThreadGroup("TG " + index), () -> {
       
    65             SunToolkit.createNewAppContext();
       
    66             while (!isComplete()) {
       
    67                 final Frame frame = new Frame();
       
    68                 frame.setSize(300, 300);
       
    69                 frame.add(new Button("Button"));
       
    70                 frame.setLocationRelativeTo(null);
       
    71                 frame.setVisible(true);
       
    72                 try {
       
    73                     Thread.sleep(index); // increase probability of the failure
       
    74                 } catch (InterruptedException ignored) {
       
    75                 }
       
    76                 frame.dispose();
       
    77             }
       
    78         });
       
    79     }
       
    80 
       
    81     private static boolean isComplete() {
       
    82         return endtime - System.nanoTime() < 0 || failed != null;
       
    83     }
       
    84 }