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