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 test.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 WindowSceneInitDisposeTest {
  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                 } finally {
  91                     l.countDown();
  92                 }
  93             });
  94         });
  95         l.await();
  96         if (exception != null) {
  97             throw exception;
  98         }
  99     }
 100 
 101     @Test
 102     public void test2() throws Throwable {
 103         final CountDownLatch l = new CountDownLatch(1);
 104         Platform.runLater(() -> {
 105             final Stage t = new Stage();
 106             t.show();
 107             t.setScene(new Scene(new Group()));
 108             Platform.runLater(() -> {
 109                 try {
 110                     t.hide();
 111                 } finally {
 112                     l.countDown();
 113                 }
 114             });
 115         });
 116         l.await();
 117         if (exception != null) {
 118             throw exception;
 119         }
 120     }
 121 
 122     @Test
 123     public void test3() throws Throwable {
 124         final CountDownLatch l = new CountDownLatch(1);
 125         Platform.runLater(() -> {
 126             final Stage t = new Stage();
 127             t.show();
 128             Platform.runLater(() -> {
 129                 try {
 130                     t.hide();
 131                 } finally {
 132                     l.countDown();
 133                 }
 134             });
 135         });
 136         l.await();
 137         if (exception != null) {
 138             throw exception;
 139         }
 140     }
 141 
 142     @Test
 143     public void test4() throws Throwable {
 144         final CountDownLatch l = new CountDownLatch(1);
 145         Platform.runLater(() -> {
 146             final Stage t = new Stage();
 147             t.setScene(new Scene(new Group()));
 148             t.show();
 149             Platform.runLater(() -> {
 150                 try {
 151                     t.setScene(null);
 152                     t.hide();
 153                 } finally {
 154                     l.countDown();
 155                 }
 156             });
 157         });
 158         l.await();
 159         if (exception != null) {
 160             throw exception;
 161         }
 162     }
 163 
 164     @Test
 165     public void test5() throws Throwable {
 166         final CountDownLatch l = new CountDownLatch(1);
 167         Platform.runLater(() -> {
 168             final Stage t = new Stage();
 169             t.setScene(new Scene(new Group()));
 170             t.show();
 171             Platform.runLater(() -> {
 172                 try {
 173                     t.hide();
 174                     t.setScene(null);
 175                 } finally {
 176                     l.countDown();
 177                 }
 178             });
 179         });
 180         l.await();
 181         if (exception != null) {
 182             throw exception;
 183         }
 184     }
 185 
 186     @Test
 187     public void test6() throws Throwable {
 188         final CountDownLatch l = new CountDownLatch(1);
 189         Platform.runLater(() -> {
 190             final Stage t = new Stage();
 191             t.setScene(new Scene(new Group()));
 192             t.show();
 193             Platform.runLater(() -> {
 194                 try {
 195                     t.setScene(new Scene(new Group()));
 196                     t.hide();
 197                 } finally {
 198                     l.countDown();
 199                 }
 200             });
 201         });
 202         l.await();
 203         if (exception != null) {
 204             throw exception;
 205         }
 206     }
 207 
 208     @Test
 209     public void test7() throws Throwable {
 210         final CountDownLatch l = new CountDownLatch(1);
 211         Platform.runLater(() -> {
 212             final Stage t = new Stage();
 213             final Scene s = new Scene(new Group());
 214             t.setScene(s);
 215             t.show();
 216             final Stage p = new Stage();
 217             p.show();
 218             Platform.runLater(() -> {
 219                 try {
 220                     p.setScene(s);
 221                     p.hide();
 222                     t.hide();
 223                 } finally {
 224                     l.countDown();
 225                 }
 226             });
 227         });
 228         l.await();
 229         if (exception != null) {
 230             throw exception;
 231         }
 232     }
 233 
 234 }