1 /*
   2  * Copyright (c) 2012, 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.application;
  27 
  28 import com.sun.javafx.application.PlatformImplShim;
  29 import javafx.application.Platform;
  30 import javafx.embed.swing.JFXPanel;
  31 import javafx.scene.Group;
  32 import javafx.scene.Scene;
  33 import javafx.scene.paint.Color;
  34 import junit.framework.AssertionFailedError;
  35 import test.util.Util;
  36 
  37 import javax.swing.JFrame;
  38 import javax.swing.SwingUtilities;
  39 import java.awt.BorderLayout;
  40 import java.awt.Dimension;
  41 import java.lang.reflect.InvocationTargetException;
  42 import java.util.concurrent.CountDownLatch;
  43 import java.util.concurrent.TimeUnit;
  44 import java.util.concurrent.atomic.AtomicReference;
  45 
  46 import org.junit.Test;
  47 import org.junit.Assert;
  48 
  49 /**
  50  * Test program for Platform.exit() behavior with an embedded JFXPanel.
  51  */
  52 public class SwingNoExit {
  53 
  54     // Sleep time showing/hiding window in milliseconds
  55     private static final int SLEEP_TIME = 1000;
  56 
  57     private JFrame frame;
  58     private JFXPanel fxPanel;
  59 
  60     public void init() {
  61         // Create Swing window
  62         frame = new JFrame("JFXPanel 1");
  63         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  64         frame.setLayout(new BorderLayout());
  65 
  66         // Create javafx panel
  67         fxPanel = new JFXPanel();
  68         fxPanel.setPreferredSize(new Dimension(210, 180));
  69         frame.getContentPane().add(fxPanel, BorderLayout.CENTER);
  70 
  71         // Create scene and add it to the panel
  72         Util.runAndWait(() -> {
  73             Group root = new Group();
  74             Scene scene = new Scene(root);
  75             scene.setFill(Color.LIGHTYELLOW);
  76             fxPanel.setScene(scene);
  77         });
  78 
  79         // show frame
  80         frame.setLocationRelativeTo(null);
  81         frame.pack();
  82         frame.setVisible(true);
  83     }
  84 
  85     @Test
  86     public void doTestImplicitExit() throws Throwable {
  87         final AtomicReference<Throwable> error = new AtomicReference<>(null);
  88 
  89         final CountDownLatch initLatch = new CountDownLatch(1);
  90         SwingUtilities.invokeLater(() -> {
  91             try {
  92                 init();
  93                 initLatch.countDown();
  94             } catch (Throwable th) {
  95                 error.set(th);
  96             }
  97         });
  98         if (!initLatch.await(Util.TIMEOUT, TimeUnit.MILLISECONDS)) {
  99             throw new AssertionFailedError("Timeout waiting for JFXPanel to launch and initialize");
 100         }
 101         Throwable t = error.get();
 102         if (t != null) {
 103             throw t;
 104         }
 105 
 106         final CountDownLatch runAndWait = new CountDownLatch(1);
 107         Platform.runLater(() -> {
 108             Platform.exit();
 109             runAndWait.countDown();
 110         });
 111         if (!runAndWait.await(Util.TIMEOUT, TimeUnit.MILLISECONDS)) {
 112             throw new AssertionFailedError("Timeout waiting for Platform.exit()");
 113         }
 114 
 115         final CountDownLatch exitLatch = PlatformImplShim.test_getPlatformExitLatch();
 116         Thread.sleep(SLEEP_TIME);
 117         // Platform.exit() should not cause FX to exit, while JFXPanel is alive
 118         Assert.assertEquals("Platform.exit() caused FX to exit, while JFXPanel is alive",
 119                             1, exitLatch.getCount());
 120 
 121         try {
 122             SwingUtilities.invokeAndWait(() -> {
 123                 frame.setVisible(false);
 124                 frame.dispose();
 125             });
 126         }
 127         catch (InvocationTargetException ex) {
 128             throw new AssertionFailedError("Exception while disposing JFrame");
 129         }
 130 
 131         Thread.sleep(SLEEP_TIME);
 132         // JFXPanel is gone, implicit exit is false, so FX should have exited now
 133         Assert.assertEquals("FX is not exited, when the last JFXPanel is disposed",
 134                             0, exitLatch.getCount());
 135     }
 136 }