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 com.sun.javafx.application;
  27 
  28 import java.awt.BorderLayout;
  29 import javax.swing.SwingUtilities;
  30 import java.awt.Dimension;
  31 import java.lang.reflect.InvocationTargetException;
  32 import java.util.concurrent.CountDownLatch;
  33 import java.util.concurrent.TimeUnit;
  34 import javafx.application.Platform;
  35 import javafx.embed.swing.JFXPanel;
  36 import javafx.scene.Group;
  37 import javafx.scene.Scene;
  38 import javafx.scene.paint.Color;
  39 import javax.swing.JFrame;
  40 import junit.framework.AssertionFailedError;
  41 import util.Util;
  42 
  43 import static org.junit.Assert.*;
  44 import static util.Util.TIMEOUT;
  45 
  46 /**
  47  * Test program for Platform implicit exit behavior using an embedded JFXPanel.
  48  * Each of the tests must be run in a separate JVM which is why each
  49  * is in its own subclass.
  50  */
  51 public class SwingExitCommon {
  52 
  53     // Sleep time showing/hiding window in milliseconds
  54     private static final int SLEEP_TIME = 1000;
  55 
  56     // Used to launch the application before running any test
  57     private static final CountDownLatch initialized = new CountDownLatch(1);
  58 
  59     // Value of the implicit exit flag for the given test
  60     private static volatile boolean implicitExit;
  61 
  62     private JFrame frame;
  63     private JFXPanel fxPanel;
  64 
  65     public void init() {
  66         assertTrue(SwingUtilities.isEventDispatchThread());
  67         assertEquals(1, initialized.getCount());
  68         assertTrue(Platform.isImplicitExit());
  69         if (!implicitExit) {
  70             Platform.setImplicitExit(false);
  71             assertFalse(Platform.isImplicitExit());
  72         }
  73 
  74         frame = new JFrame("JFXPanel 1");
  75         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  76         frame.setLayout(new BorderLayout());
  77 
  78         // Create javafx panel
  79         fxPanel = new JFXPanel();
  80         fxPanel.setPreferredSize(new Dimension(210, 180));
  81         frame.getContentPane().add(fxPanel, BorderLayout.CENTER);
  82 
  83         // Create scene and add it to the panel
  84         Util.runAndWait(() -> {
  85             Group root = new Group();
  86             Scene scene = new Scene(root);
  87             scene.setFill(Color.LIGHTYELLOW);
  88             fxPanel.setScene(scene);
  89         });
  90 
  91         // show frame
  92         frame.setLocationRelativeTo(null);
  93         frame.pack();
  94         frame.setVisible(true);
  95 
  96         initialized.countDown();
  97         assertEquals(0, initialized.getCount());
  98     }
  99 
 100     private void doTestCommon(boolean implicitExit,
 101             boolean reEnableImplicitExit, boolean appShouldExit) {
 102 
 103         SwingExitCommon.implicitExit = implicitExit;
 104 
 105         final Throwable[] testError = new Throwable[1];
 106         final Thread testThread = Thread.currentThread();
 107 
 108         // Start the Application
 109         SwingUtilities.invokeLater(() -> {
 110             try {
 111                 init();
 112             } catch (Throwable th) {
 113                 testError[0] = th;
 114                 testThread.interrupt();
 115             }
 116         });
 117 
 118         try {
 119             if (!initialized.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
 120                 throw new AssertionFailedError("Timeout waiting for JFXPanel to launch and initialize");
 121             }
 122 
 123             Thread.sleep(SLEEP_TIME);
 124             try {
 125                 SwingUtilities.invokeAndWait(() -> {
 126                     frame.setVisible(false);
 127                     frame.dispose();
 128                 });
 129             }
 130             catch (InvocationTargetException ex) {
 131                 AssertionFailedError err = new AssertionFailedError("Exception while disposing JFrame");
 132                 err.initCause(ex.getCause());
 133                 throw err;
 134             }
 135 
 136             final CountDownLatch exitLatch = PlatformImpl.test_getPlatformExitLatch();
 137 
 138             if (reEnableImplicitExit) {
 139                 Thread.sleep(SLEEP_TIME);
 140                 assertEquals(1, exitLatch.getCount());
 141                 assertFalse(Platform.isImplicitExit());
 142                 Platform.setImplicitExit(true);
 143                 assertTrue(Platform.isImplicitExit());
 144             }
 145 
 146             if (!appShouldExit) {
 147                 Thread.sleep(SLEEP_TIME);
 148                 assertEquals(1, exitLatch.getCount());
 149                 Platform.exit();
 150             }
 151 
 152             if (!exitLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
 153                 throw new AssertionFailedError("Timeout waiting for Platform to exit");
 154             }
 155         } catch (InterruptedException ex) {
 156             Util.throwError(testError[0]);
 157         }
 158     }
 159 
 160     // ========================== TEST CASES ==========================
 161 
 162     // Implementation of SingleImplicitTest.testImplicitExit
 163     public void doTestImplicitExit() {
 164         // implicitExit, no re-enable, should exit
 165         doTestCommon(true, false, true);
 166     }
 167 
 168     // Implementation of testExplicitExit
 169     public void doTestExplicitExit() {
 170         // no implicitExit, no re-enable, should not exit
 171         doTestCommon(false, false, false);
 172     }
 173 
 174     // Implementation of testExplicitExitReEnable
 175     public void doTestExplicitExitReEnable() {
 176         // no implicitExit, re-enable, should exit
 177         doTestCommon(false, true, true);
 178     }
 179 
 180 }