1 /*
   2  * Copyright (c) 2013, 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.tk.quantum;
  27 
  28 import javafx.application.Application;
  29 import javafx.application.Platform;
  30 import javafx.scene.Group;
  31 import javafx.scene.Scene;
  32 import javafx.stage.Modality;
  33 import javafx.stage.Stage;
  34 
  35 import org.junit.AfterClass;
  36 import org.junit.Assert;
  37 import org.junit.BeforeClass;
  38 import org.junit.Test;
  39 
  40 import java.util.concurrent.CountDownLatch;
  41 
  42 public class CloseWindowTest {
  43 
  44     private static final CountDownLatch startupLatch = new CountDownLatch(1);
  45 
  46     private static Stage primaryStage;
  47 
  48     private static volatile Throwable exception;
  49 
  50     public static class TestApp extends Application {
  51         @Override
  52         public void start(Stage t) {
  53             primaryStage = t;
  54             t.setScene(new Scene(new Group()));
  55             t.setWidth(100);
  56             t.setHeight(100);
  57             t.show();
  58 
  59             Thread.currentThread().setUncaughtExceptionHandler((t2, e) -> {
  60                 System.err.println("Exception caught in thread: " + t2);
  61                 e.printStackTrace();
  62                 exception = e;
  63             });
  64 
  65             startupLatch.countDown();
  66         }
  67     }
  68 
  69     @BeforeClass
  70     public static void setup() throws Exception {
  71         new Thread(() -> Application.launch(TestApp.class)).start();
  72         startupLatch.await();
  73     }
  74 
  75     @AfterClass
  76     public static void shutdown() {
  77         Platform.runLater(primaryStage::hide);
  78     }
  79 
  80     @Test
  81     public void test1() throws Throwable {
  82         final CountDownLatch l = new CountDownLatch(1);
  83         Platform.runLater(() -> {
  84             final Stage t = new Stage();
  85             t.setScene(new Scene(new Group()));
  86             t.show();
  87             Platform.runLater(() -> {
  88                 try {
  89                     t.hide();
  90                     t.hide();
  91                 } catch (Throwable z) {
  92                     exception = z;
  93                 } finally {
  94                     l.countDown();
  95                 }
  96             });
  97         });
  98         l.await();
  99         if (exception != null) {
 100             throw exception;
 101         }
 102     }
 103 
 104     @Test
 105     public void test2() throws Throwable {
 106         final CountDownLatch l = new CountDownLatch(1);
 107         Platform.runLater(() -> {
 108             final Stage t = new Stage();
 109             t.setScene(new Scene(new Group()));
 110             t.show();
 111             final Stage p = new Stage();
 112             p.initOwner(t);
 113             p.setScene(new Scene(new Group()));
 114             p.show();
 115             Platform.runLater(() -> {
 116                 try {
 117                     t.hide();
 118                     p.hide();
 119                 } catch (Throwable z) {
 120                     exception = z;
 121                 } finally {
 122                     l.countDown();
 123                 }
 124             });
 125         });
 126         l.await();
 127         if (exception != null) {
 128             throw exception;
 129         }
 130     }
 131 
 132     @Test
 133     public void test3() throws Throwable {
 134         final CountDownLatch l = new CountDownLatch(1);
 135         Platform.runLater(() -> {
 136             final Stage t = new Stage();
 137             t.setScene(new Scene(new Group()));
 138             t.show();
 139             final Stage p = new Stage();
 140             p.initOwner(t);
 141             p.initModality(Modality.WINDOW_MODAL);
 142             p.setScene(new Scene(new Group()));
 143             p.show();
 144             Platform.runLater(() -> {
 145                 try {
 146                     t.hide();
 147                     p.hide();
 148                 } catch (Throwable z) {
 149                     exception = z;
 150                 } finally {
 151                     l.countDown();
 152                 }
 153             });
 154         });
 155         l.await();
 156         if (exception != null) {
 157             throw exception;
 158         }
 159     }
 160 
 161     @Test
 162     public void test4() throws Throwable {
 163         final CountDownLatch l = new CountDownLatch(1);
 164         Platform.runLater(() -> {
 165             final Stage t = new Stage();
 166             t.setScene(new Scene(new Group()));
 167             t.show();
 168             final Stage p = new Stage();
 169             p.initOwner(t);
 170             p.setScene(new Scene(new Group()));
 171             p.show();
 172             final Stage s = new Stage();
 173             s.initOwner(p);
 174             s.setScene(new Scene(new Group()));
 175             p.show();
 176             Platform.runLater(() -> {
 177                 try {
 178                     t.hide();
 179                     s.hide();
 180                     p.hide();
 181                 } catch (Throwable z) {
 182                     exception = z;
 183                 } finally {
 184                     l.countDown();
 185                 }
 186             });
 187         });
 188         l.await();
 189         if (exception != null) {
 190             throw exception;
 191         }
 192     }
 193 
 194 }