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 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 RestoreSceneSizeTest {
  47     static CountDownLatch startupLatch;
  48     static Stage stage;
  49     static double w;
  50     static double h;
  51 
  52     public static void main(String[] args) throws Exception {
  53         initFX();
  54         try {
  55             RestoreSceneSizeTest test = new RestoreSceneSizeTest();
  56             test.testUnfullscreenSize();
  57         } catch (Throwable e) {
  58             e.printStackTrace();
  59         } finally {
  60             teardown();
  61         }
  62     }
  63 
  64     public static class TestApp extends Application {
  65 
  66         @Override
  67         public void start(Stage primaryStage) throws Exception {
  68             primaryStage.setScene(new Scene(new VBox(), 234, 255));
  69             stage = primaryStage;
  70             w = stage.getScene().getWidth();
  71             h = stage.getScene().getHeight();
  72             stage.setFullScreen(true);
  73             stage.addEventHandler(WindowEvent.WINDOW_SHOWN, e ->
  74                     Platform.runLater(startupLatch::countDown));
  75             stage.show();
  76         }
  77     }
  78 
  79     @BeforeClass
  80     public static void initFX() {
  81         startupLatch = new CountDownLatch(1);
  82         new Thread(() -> Application.launch(TestApp.class, (String[])null)).start();
  83         try {
  84             if (!startupLatch.await(15, TimeUnit.SECONDS)) {
  85                 fail("Timeout waiting for FX runtime to start");
  86             }
  87         } catch (InterruptedException ex) {
  88             fail("Unexpected exception: " + ex);
  89         }
  90     }
  91 
  92     @Test
  93     public void testUnfullscreenSize() throws Exception {
  94         // Disable on Mac until JDK-8176813 is fixed
  95         assumeTrue(!PlatformUtil.isMac());
  96 
  97         Thread.sleep(200);
  98         Assert.assertTrue(stage.isShowing());
  99         Assert.assertTrue(stage.isFullScreen());
 100 
 101         CountDownLatch latch = new CountDownLatch(2);
 102         ChangeListener<Number> listenerW = (observable, oldValue, newValue) -> {
 103             if (Math.abs((Double) newValue - w) < 0.1) {
 104                 latch.countDown();
 105             };
 106         };
 107         ChangeListener<Number> listenerH = (observable, oldValue, newValue) -> {
 108             if (Math.abs((Double) newValue - h) < 0.1) {
 109                 latch.countDown();
 110             };
 111         };
 112         stage.getScene().widthProperty().addListener(listenerW);
 113         stage.getScene().heightProperty().addListener(listenerH);
 114         Platform.runLater(() -> stage.setFullScreen(false));
 115         latch.await(5, TimeUnit.SECONDS);
 116         Thread.sleep(200);
 117         Assert.assertFalse(stage.isFullScreen());
 118         stage.getScene().widthProperty().removeListener(listenerW);
 119         stage.getScene().heightProperty().removeListener(listenerH);
 120 
 121         Assert.assertEquals("Scene got wrong width", w, stage.getScene().getWidth(), 0.1);
 122         Assert.assertEquals("Scene got wrong height", h, stage.getScene().getHeight(), 0.1);
 123     }
 124 
 125     @AfterClass
 126     public static void teardown() {
 127         Platform.runLater(stage::hide);
 128         Platform.exit();
 129     }
 130 }