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