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