1 /* 2 * Copyright (c) 2017, 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 package test.javafx.scene; 26 27 import java.util.concurrent.CountDownLatch; 28 import java.util.concurrent.TimeUnit; 29 30 import javafx.application.Application; 31 import javafx.application.Platform; 32 import javafx.beans.value.ChangeListener; 33 import javafx.scene.Scene; 34 import javafx.scene.layout.VBox; 35 import javafx.stage.Stage; 36 import javafx.stage.WindowEvent; 37 import junit.framework.Assert; 38 39 import org.junit.AfterClass; 40 import org.junit.BeforeClass; 41 import org.junit.Test; 42 43 import static org.junit.Assert.fail; 44 import static org.junit.Assume.assumeTrue; 45 46 public class NewSceneSizeTest { 47 static CountDownLatch startupLatch; 48 static volatile Stage stage; 49 50 public static void main(String[] args) throws Exception { 51 initFX(); 52 try { 53 NewSceneSizeTest test = new NewSceneSizeTest(); 54 test.testNewSceneSize(); 55 } catch (Throwable e) { 56 e.printStackTrace(); 57 } finally { 58 teardown(); 59 } 60 } 61 62 public static class TestApp extends Application { 63 64 @Override 65 public void start(Stage primaryStage) throws Exception { 66 primaryStage.setScene(new Scene(new VBox())); 67 stage = primaryStage; 68 stage.addEventHandler(WindowEvent.WINDOW_SHOWN, e -> 69 Platform.runLater(startupLatch::countDown)); 70 stage.show(); 71 } 72 } 73 74 @BeforeClass 75 public static void initFX() { 76 startupLatch = new CountDownLatch(1); 77 new Thread(() -> Application.launch(TestApp.class, (String[])null)).start(); 78 try { 79 if (!startupLatch.await(15, TimeUnit.SECONDS)) { 80 fail("Timeout waiting for FX runtime to start"); 81 } 82 } catch (InterruptedException ex) { 83 fail("Unexpected exception: " + ex); 84 } 85 } 86 87 @Test 88 public void testNewSceneSize() throws Exception { 89 assumeTrue(Boolean.getBoolean("unstable.test")); // JDK-8193185 90 Thread.sleep(200); 91 final int nTries = 100; 92 Stage childStage[] = new Stage[nTries]; 93 double w[] = new double[nTries]; 94 double h[] = new double[nTries]; 95 CountDownLatch latch = new CountDownLatch(2 * nTries); 96 for (int i = 0; i < nTries; i++) { 97 int fI = i; 98 Platform.runLater(new Runnable() { 99 ChangeListener<Number> listenerW; 100 ChangeListener<Number> listenerH; 101 102 @Override 103 public void run() { 104 Stage stage = new Stage(); 105 childStage[fI] = stage; 106 stage.setResizable(fI % 2 == 0); 107 Scene scene = new Scene(new VBox(), 300 - fI, 200 - fI); 108 stage.setScene(scene); 109 w[fI] = stage.getScene().getWidth(); 110 h[fI] = stage.getScene().getHeight(); 111 Assert.assertTrue(w[fI] > 1); 112 Assert.assertTrue(h[fI] > 1); 113 stage.widthProperty().addListener(listenerW = (v, o, n) -> { 114 if (Math.abs((Double) n - w[fI]) < 0.1) { 115 stage.widthProperty().removeListener(listenerW); 116 Platform.runLater(latch::countDown); 117 } 118 }); 119 stage.heightProperty().addListener(listenerH = (v, o, n) -> { 120 if (Math.abs((Double) n - h[fI]) < 0.1) { 121 stage.heightProperty().removeListener(listenerH); 122 Platform.runLater(latch::countDown); 123 } 124 }); 125 stage.show(); 126 } 127 }); 128 } 129 latch.await(5, TimeUnit.SECONDS); 130 Thread.sleep(200); 131 for (int i = 0; i < nTries; i++) { 132 Assert.assertEquals("Wrong scene " + i + " width", w[i], 133 childStage[i].getScene().getWidth()); 134 Assert.assertEquals("Wrong scene " + i + " height", h[i], 135 childStage[i].getScene().getHeight()); 136 } 137 } 138 139 @AfterClass 140 public static void teardown() { 141 Platform.runLater(stage::hide); 142 Platform.exit(); 143 } 144 }