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