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.  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.javafx.stage;
  27 
  28 import java.util.concurrent.CountDownLatch;
  29 import java.util.concurrent.TimeUnit;
  30 import javafx.application.Application;
  31 import javafx.application.Platform;
  32 import javafx.scene.Group;
  33 import javafx.scene.Scene;
  34 import javafx.stage.Stage;
  35 import javafx.stage.WindowEvent;
  36 import org.junit.Assert;
  37 import org.junit.BeforeClass;
  38 import org.junit.Test;
  39 import test.util.Util;
  40 
  41 public class NestedEventLoopPlatformExitTest {
  42 
  43     // Used to launch the application before running any test
  44     private static final CountDownLatch launchLatch = new CountDownLatch(1);
  45 
  46     public static class TestApp extends Application {
  47 
  48         @Override public void start(Stage primaryStage) throws Exception {
  49             primaryStage.setTitle("Primary stage");
  50             Group root = new Group();
  51             Scene scene = new Scene(root);
  52             primaryStage.setScene(scene);
  53             primaryStage.setWidth(100);
  54             primaryStage.setHeight(100);
  55             primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN,
  56                 e -> Platform.runLater(launchLatch::countDown));
  57             primaryStage.show();
  58         }
  59     }
  60 
  61     @BeforeClass
  62     public static void initFX() {
  63         // Start the Application
  64         new Thread(() -> Application.launch(TestApp.class, (String[])null)).start();
  65 
  66         try {
  67             if (!launchLatch.await(15, TimeUnit.SECONDS)) {
  68                 Assert.fail("Timeout waiting for FX runtime to start");
  69             }
  70         } catch (InterruptedException ex) {
  71             Assert.fail("Unexpected exception: " + ex);
  72         }
  73     }
  74 
  75     // Verify that Platform.exit can be called while the NestedEventLoop is
  76     // running
  77     @Test(timeout = 20000)
  78     public void testPlatformExitWithNestedEventLoop() {
  79         Util.runAndWait(
  80             () -> {
  81                 Assert.assertFalse(Platform.isNestedLoopRunning());
  82                 Platform.enterNestedEventLoop(1024L);
  83             },
  84             () -> {
  85                 Assert.assertTrue(Platform.isNestedLoopRunning());
  86                 Platform.exit();
  87             }
  88         );
  89     }
  90 }