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.stage;
  26 
  27 import javafx.application.Application;
  28 import javafx.application.Platform;
  29 import javafx.scene.Scene;
  30 import javafx.scene.layout.VBox;
  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 
  38 import java.util.concurrent.CountDownLatch;
  39 import java.util.concurrent.TimeUnit;
  40 
  41 import static org.junit.Assert.fail;
  42 
  43 public class DeiconifiedWithChildTest {
  44     static CountDownLatch startupLatch;
  45     static Stage stage;
  46     static Stage childStage;
  47 
  48     public static void main(String[] args) throws Exception {
  49         initFX();
  50         try {
  51             new DeiconifiedWithChildTest().testDeiconifiedPosition();
  52         } finally {
  53             teardown();
  54         }
  55     }
  56 
  57     public static class TestApp extends Application {
  58 
  59         @Override
  60         public void start(Stage primaryStage) throws Exception {
  61             primaryStage.setScene(new Scene(new VBox(), 200, 200));
  62             stage = primaryStage;
  63             stage.show();
  64 
  65             childStage = new Stage();
  66             childStage.initOwner(stage);
  67             childStage.setScene(new Scene(new VBox(), 100, 100));
  68             childStage.addEventHandler(WindowEvent.WINDOW_SHOWN, e ->
  69                     Platform.runLater(startupLatch::countDown));
  70             childStage.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 testDeiconifiedPosition() throws Exception {
  89         Thread.sleep(200);
  90         Assert.assertTrue(stage.isShowing());
  91         Assert.assertTrue(childStage.isShowing());
  92         Assert.assertFalse(stage.isIconified());
  93 
  94         double x = childStage.getX();
  95         double y = childStage.getY();
  96 
  97         Platform.runLater(() -> stage.setIconified(true));
  98         Thread.sleep(200);
  99         Platform.runLater(() -> stage.setIconified(false));
 100         Thread.sleep(200);
 101         Assert.assertEquals("Child window was moved", x, childStage.getX(), 0.1);
 102         Assert.assertEquals("Child window was moved", y, childStage.getY(), 0.1);
 103     }
 104 
 105     @AfterClass
 106     public static void teardown() {
 107         Platform.runLater(childStage::hide);
 108         Platform.runLater(stage::hide);
 109         Platform.exit();
 110     }
 111 }
 112