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.scene;
  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 RestoreSceneSizeTest {
  45     static CountDownLatch startupLatch;
  46     static Stage stage;
  47     static double w;
  48     static double h;
  49 
  50     public static void main(String[] args) throws Exception {
  51         initFX();
  52         try {
  53             RestoreSceneSizeTest test = new RestoreSceneSizeTest();
  54             test.testUnfullscreenSize();
  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(), 234, 255));
  67             stage = primaryStage;
  68             w = stage.getScene().getWidth();
  69             h = stage.getScene().getHeight();
  70             stage.setFullScreen(true);
  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     @Test
  91     public void testUnfullscreenSize() throws Exception {
  92         Thread.sleep(200);
  93         Assert.assertTrue(stage.isShowing());
  94         Assert.assertTrue(stage.isFullScreen());
  95 
  96         CountDownLatch latch = new CountDownLatch(2);
  97         ChangeListener<Number> listenerW = (observable, oldValue, newValue) -> {
  98             if (Math.abs((Double) newValue - w) < 0.1) {
  99                 latch.countDown();
 100             };
 101         };
 102         ChangeListener<Number> listenerH = (observable, oldValue, newValue) -> {
 103             if (Math.abs((Double) newValue - h) < 0.1) {
 104                 latch.countDown();
 105             };
 106         };
 107         stage.getScene().widthProperty().addListener(listenerW);
 108         stage.getScene().heightProperty().addListener(listenerH);
 109         Platform.runLater(() -> stage.setFullScreen(false));
 110         latch.await(5, TimeUnit.SECONDS);
 111         Thread.sleep(200);
 112         Assert.assertFalse(stage.isFullScreen());
 113         stage.getScene().widthProperty().removeListener(listenerW);
 114         stage.getScene().heightProperty().removeListener(listenerH);
 115 
 116         Assert.assertEquals("Scene got wrong width", w, stage.getScene().getWidth(), 0.1);
 117         Assert.assertEquals("Scene got wrong height", h, stage.getScene().getHeight(), 0.1);
 118     }
 119 
 120     @AfterClass
 121     public static void teardown() {
 122         Platform.runLater(stage::hide);
 123         Platform.exit();
 124     }
 125 }