1 /*
   2  * Copyright (c) 2017, 2019, 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.stage;
  26 
  27 import javafx.application.Application;
  28 import javafx.application.Platform;
  29 import javafx.scene.Group;
  30 import javafx.scene.Scene;
  31 import javafx.stage.Stage;
  32 import javafx.stage.WindowEvent;
  33 import org.junit.AfterClass;
  34 import org.junit.Assert;
  35 import org.junit.BeforeClass;
  36 import org.junit.Test;
  37 import test.util.Util;
  38 
  39 import java.util.concurrent.CountDownLatch;
  40 import java.util.concurrent.TimeUnit;
  41 
  42 import static org.junit.Assert.fail;
  43 
  44 public class InitialSizeTest {
  45     static CountDownLatch startupLatch;
  46     static Stage stage;
  47     private static final double INIT_SIZE = 200.d;
  48 
  49     public static class TestApp extends Application {
  50         @Override
  51         public void start(Stage primaryStage) throws Exception {
  52             primaryStage.setScene(new Scene(new Group()));
  53             stage = primaryStage;
  54             stage.setWidth(INIT_SIZE);
  55             stage.setHeight(INIT_SIZE);
  56             stage.addEventHandler(WindowEvent.WINDOW_SHOWN, e ->
  57                     Platform.runLater(startupLatch::countDown));
  58             stage.show();
  59         }
  60     }
  61 
  62     @BeforeClass
  63     public static void initFX() {
  64         startupLatch = new CountDownLatch(1);
  65         new Thread(() -> Application.launch(TestApp.class, (String[]) null)).start();
  66         try {
  67             if (!startupLatch.await(15, TimeUnit.SECONDS)) {
  68                 fail("Timeout waiting for FX runtime to start");
  69             }
  70         } catch (InterruptedException ex) {
  71             fail("Unexpected exception: " + ex);
  72         }
  73     }
  74 
  75     @Test
  76     public void testInitialSize() throws Exception {
  77         Util.sleep(200);
  78         Assert.assertTrue(stage.isShowing());
  79         Assert.assertEquals("Stage height", INIT_SIZE, stage.getHeight(), .1d);
  80         Assert.assertEquals("Stage width", INIT_SIZE, stage.getWidth(), .1d);
  81     }
  82 
  83     @AfterClass
  84     public static void teardown() {
  85         Platform.runLater(stage::hide);
  86         Platform.exit();
  87     }
  88 }