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.robot.javafx.scene;
  26 
  27 import com.sun.glass.ui.Robot;
  28 import com.sun.javafx.PlatformUtil;
  29 import javafx.application.Application;
  30 import javafx.application.Platform;
  31 import javafx.scene.Scene;
  32 import javafx.scene.layout.VBox;
  33 import javafx.stage.Modality;
  34 import javafx.stage.Stage;
  35 import javafx.stage.WindowEvent;
  36 import org.junit.AfterClass;
  37 import org.junit.Assert;
  38 import org.junit.BeforeClass;
  39 import org.junit.Test;
  40 
  41 import java.util.concurrent.CountDownLatch;
  42 import java.util.concurrent.TimeUnit;
  43 
  44 import static org.junit.Assert.fail;
  45 
  46 public class AfterModalClosedTest {
  47     static CountDownLatch startupLatch;
  48     static volatile Stage stage;
  49     private Robot robot;
  50     private int x, y;
  51     private boolean w, h;
  52 
  53     public static void main(String[] args) throws Exception {
  54         initFX();
  55         new AfterModalClosedTest().testResizability();
  56         teardown();
  57     }
  58 
  59     public static class TestApp extends Application {
  60         @Override
  61         public void start(Stage primaryStage) throws Exception {
  62             primaryStage.setScene(new Scene(new VBox()));
  63             primaryStage.setResizable(true);
  64             primaryStage.show();
  65             stage = primaryStage;
  66 
  67             Stage modalStage = new Stage();
  68             modalStage.setScene(new Scene(new VBox()));
  69             modalStage.initModality(Modality.APPLICATION_MODAL);
  70             modalStage.addEventHandler(WindowEvent.WINDOW_SHOWN, e ->
  71                     Platform.runLater(modalStage::hide));
  72             modalStage.addEventHandler(WindowEvent.WINDOW_HIDDEN, e ->
  73                     Platform.runLater(startupLatch::countDown));
  74             modalStage.show();
  75         }
  76     }
  77 
  78     @BeforeClass
  79     public static void initFX() {
  80         startupLatch = new CountDownLatch(1);
  81         new Thread(() -> Application.launch(TestApp.class, (String[])null)).start();
  82         try {
  83             if (!startupLatch.await(15, TimeUnit.SECONDS)) {
  84                 fail("Timeout waiting for FX runtime to start");
  85             }
  86         } catch (InterruptedException ex) {
  87             fail("Unexpected exception: " + ex);
  88         }
  89     }
  90 
  91     @Test
  92     public void testResizability() throws Exception {
  93         Assert.assertTrue(stage.isResizable());
  94         CountDownLatch resizeLatch = new CountDownLatch(2);
  95         Platform.runLater(() -> {
  96             stage.widthProperty().addListener((ov, o, n) -> {
  97                 if (!w && o != n) {
  98                     w = true;
  99                     resizeLatch.countDown();
 100                 }
 101             });
 102             stage.heightProperty().addListener((ov, o, n) -> {
 103                 if (!h && o != n) {
 104                     h = true;
 105                     resizeLatch.countDown();
 106                 }
 107             });
 108             robot = com.sun.glass.ui.Application.GetApplication().createRobot();
 109             x = (int) (stage.getX() + stage.getWidth());
 110             y = (int) (stage.getY() + stage.getHeight());
 111             int d = PlatformUtil.isLinux() ? -1 : 2;
 112             robot.mouseMove(x - d, y - d);
 113             robot.mousePress(Robot.MOUSE_LEFT_BTN);
 114         });
 115         Thread.sleep(100);
 116         Platform.runLater(() -> {
 117             robot.mouseMove(x + 20, y + 20);
 118             robot.mouseRelease(Robot.MOUSE_LEFT_BTN);
 119         });
 120         resizeLatch.await(5, TimeUnit.SECONDS);
 121         Assert.assertTrue("Window is not resized", w && h);
 122     }
 123 
 124     @AfterClass
 125     public static void teardown() {
 126         Platform.runLater(stage::hide);
 127         Platform.exit();
 128     }
 129 }
 130