1 /*
   2  * Copyright (c) 2017, 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.beans.value.ChangeListener;
  30 import javafx.scene.Scene;
  31 import javafx.scene.layout.VBox;
  32 import javafx.stage.Stage;
  33 import javafx.stage.WindowEvent;
  34 import org.junit.AfterClass;
  35 import org.junit.Assert;
  36 import org.junit.BeforeClass;
  37 import org.junit.Test;
  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 RestoreStagePositionTest {
  45     static CountDownLatch startupLatch;
  46     static Stage stage;
  47 
  48     public static void main(String[] args) throws Exception {
  49         initFX();
  50         try {
  51             RestoreStagePositionTest test = new RestoreStagePositionTest();
  52             test.testUfullscreenPosition();
  53             test.testDemaximizedPosition();
  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.setX(300);
  68             stage.setY(400);
  69             stage.addEventHandler(WindowEvent.WINDOW_SHOWN, e ->
  70                                     Platform.runLater(startupLatch::countDown));
  71             stage.show();
  72         }
  73     }
  74 
  75     @BeforeClass
  76     public static void initFX() {
  77         startupLatch = new CountDownLatch(1);
  78         new Thread(() -> Application.launch(TestApp.class, (String[])null)).start();
  79         try {
  80             if (!startupLatch.await(15, TimeUnit.SECONDS)) {
  81                 fail("Timeout waiting for FX runtime to start");
  82             }
  83         } catch (InterruptedException ex) {
  84             fail("Unexpected exception: " + ex);
  85         }
  86     }
  87 
  88 
  89     @Test
  90     public void testUfullscreenPosition() throws Exception {
  91         Thread.sleep(200);
  92         Assert.assertTrue(stage.isShowing());
  93         Assert.assertFalse(stage.isFullScreen());
  94 
  95         double x = stage.getX();
  96         double y = stage.getY();
  97 
  98         Platform.runLater(() -> stage.setFullScreen(true));
  99         Thread.sleep(400);
 100         Assert.assertTrue(stage.isFullScreen());
 101         CountDownLatch latch = new CountDownLatch(2);
 102 
 103         ChangeListener<Number> listenerX = (observable, oldValue, newValue) -> {
 104             if (Math.abs((Double) newValue - x) < 0.1) {
 105                 latch.countDown();
 106             };
 107         };
 108         ChangeListener<Number> listenerY = (observable, oldValue, newValue) -> {
 109             if (Math.abs((Double) newValue - y) < 0.1) {
 110                 latch.countDown();
 111             };
 112         };
 113         stage.xProperty().addListener(listenerX);
 114         stage.yProperty().addListener(listenerY);
 115         Platform.runLater(() -> stage.setFullScreen(false));
 116         latch.await(5, TimeUnit.SECONDS);
 117         stage.xProperty().removeListener(listenerX);
 118         stage.xProperty().removeListener(listenerY);
 119 
 120         Assert.assertEquals("Window was moved", x, stage.getX(), 0.1);
 121         Assert.assertEquals("Window was moved", y, stage.getY(), 0.1);
 122     }
 123 
 124     @Test
 125     public void testDemaximizedPosition() throws Exception {
 126         Thread.sleep(200);
 127         Assert.assertTrue(stage.isShowing());
 128         Assert.assertFalse(stage.isMaximized());
 129 
 130         double x = stage.getX();
 131         double y = stage.getY();
 132 
 133         Platform.runLater(() -> stage.setMaximized(true));
 134         Thread.sleep(200);
 135         Assert.assertTrue(stage.isMaximized());
 136         CountDownLatch latch = new CountDownLatch(2);
 137 
 138         ChangeListener<Number> listenerX = (observable, oldValue, newValue) -> {
 139             if (Math.abs((Double) newValue - x) < 0.1) {
 140                 latch.countDown();
 141             };
 142         };
 143         ChangeListener<Number> listenerY = (observable, oldValue, newValue) -> {
 144             if (Math.abs((Double) newValue - y) < 0.1) {
 145                 latch.countDown();
 146             };
 147         };
 148         stage.xProperty().addListener(listenerX);
 149         stage.yProperty().addListener(listenerY);
 150         Platform.runLater(() -> stage.setMaximized(false));
 151         latch.await(5, TimeUnit.SECONDS);
 152         stage.xProperty().removeListener(listenerX);
 153         stage.xProperty().removeListener(listenerY);
 154 
 155         Assert.assertEquals("Window was moved", x, stage.getX(), 0.1);
 156         Assert.assertEquals("Window was moved", y, stage.getY(), 0.1);
 157     }
 158 
 159     @AfterClass
 160     public static void teardown() {
 161         Platform.runLater(stage::hide);
 162         Platform.exit();
 163     }
 164 }