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 
  26 package test.robot.javafx.embed.swing;
  27 
  28 import javafx.application.Application;
  29 import javafx.application.Platform;
  30 import javafx.embed.swing.SwingNode;
  31 import javafx.scene.Scene;
  32 import javafx.scene.layout.VBox;
  33 import javafx.stage.Stage;
  34 import junit.framework.AssertionFailedError;
  35 import org.junit.AfterClass;
  36 import org.junit.Assert;
  37 import org.junit.BeforeClass;
  38 import test.util.Util;
  39 
  40 import javax.swing.JDialog;
  41 import javax.swing.JOptionPane;
  42 import javax.swing.JPanel;
  43 import javax.swing.SwingUtilities;
  44 import java.awt.AWTException;
  45 import java.awt.Frame;
  46 import java.awt.Robot;
  47 import java.awt.event.InputEvent;
  48 import java.lang.reflect.InvocationTargetException;
  49 import java.util.concurrent.CountDownLatch;
  50 import java.util.concurrent.TimeUnit;
  51 
  52 import static test.util.Util.TIMEOUT;
  53 
  54 public class SwingNodeBase {
  55     public static final int BASE_LOCATION = 200;
  56     public static final int BASE_SIZE = 200;
  57     public static final int WAIT_TIME = 300;
  58     public static final int LONG_WAIT_TIME = 2500;
  59 
  60     protected static Robot robot;
  61 
  62     // Used to launch the application before running any test
  63     private static final CountDownLatch launchLatch = new CountDownLatch(1);
  64 
  65     // Singleton Application instance
  66     static MyApp myApp;
  67 
  68     @BeforeClass
  69     public static void setupOnce() throws AWTException, InvocationTargetException, InterruptedException {
  70         robot = new Robot();
  71         robot.setAutoDelay(100);
  72 
  73         // Start the Application
  74         new Thread(() -> Application.launch(MyApp.class, (String[])null)).start();
  75 
  76         try {
  77             if (!launchLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
  78                 throw new AssertionFailedError("Timeout waiting for Application to launch");
  79             }
  80         } catch (InterruptedException ex) {
  81             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
  82             err.initCause(ex);
  83             throw err;
  84         }
  85 
  86         CountDownLatch paintLatch = new CountDownLatch(1);
  87         SwingUtilities.invokeAndWait(()->{
  88             myApp.swingNode.setContent(new MyApp.ColorPanel(java.awt.Color.RED));
  89             myApp.swingNode.setVisible(true);
  90         });
  91         paintLatch.countDown();
  92     }
  93 
  94     @AfterClass
  95     public static void teardownOnce() {
  96         Platform.runLater(() -> {
  97             Platform.exit();
  98         });
  99     }
 100 
 101     public static void runWaitSleep(Runnable run) {
 102         Util.runAndWait(run);
 103         Util.sleep(WAIT_TIME);
 104     }
 105 
 106     public static void invokeWaitSleep(Runnable run) throws InvocationTargetException, InterruptedException {
 107         SwingUtilities.invokeAndWait(run);
 108         Util.sleep(WAIT_TIME);
 109     }
 110 
 111     // Application class. An instance is created and initialized before running
 112     // the first test, and it lives through the execution of all tests.
 113     public static class MyApp extends Application {
 114 
 115         private Stage stage;
 116 
 117         private SwingNode swingNode;
 118         private VBox vbox;
 119         private Scene scene;
 120 
 121         @Override public void init() {
 122             SwingNodeBase.myApp = this;
 123             Platform.setImplicitExit(false);
 124         }
 125 
 126         @Override public void start(Stage primaryStage) throws Exception {
 127             swingNode = new SwingNode();
 128             vbox = new VBox(swingNode);
 129             scene = new Scene(vbox, BASE_SIZE, BASE_SIZE,
 130                     javafx.scene.paint.Color.GREEN);
 131 
 132             launchLatch.countDown();
 133         }
 134 
 135         public void closeStage() {
 136             runWaitSleep(() -> {
 137                 stage.close();
 138                 stage = null;
 139             });
 140         }
 141 
 142         public void createAndShowStage() {
 143             runWaitSleep(() -> {
 144                 stage = new Stage();
 145                 stage.setScene(scene);
 146                 stage.setTitle("JFX toplevel");
 147                 stage.setX(BASE_LOCATION);
 148                 stage.setY(BASE_LOCATION);
 149                 stage.show();
 150                 stage.requestFocus();
 151             });
 152         }
 153 
 154         public void createStageAndDialog() throws InvocationTargetException, InterruptedException {
 155             createAndShowStage();
 156             createDialog();
 157         }
 158 
 159         public void attachSwingNode() {
 160             runWaitSleep(() -> vbox.getChildren().add(swingNode));
 161         }
 162 
 163         public void detachSwingNode() {
 164             runWaitSleep(() -> vbox.getChildren().remove(swingNode));
 165         }
 166 
 167         static JDialog dialog;
 168 
 169         public Runnable createDialogRunnable = () -> {
 170             Frame frame = JOptionPane.getFrameForComponent(myApp.swingNode.getContent());
 171             dialog = new JDialog(frame);
 172             dialog.setTitle("JDialog");
 173             dialog.getContentPane().add(new ColorPanel(java.awt.Color.BLUE));
 174             dialog.setLocationRelativeTo(dialog.getParent());
 175             dialog.setBounds(BASE_LOCATION + BASE_SIZE / 2,
 176                     BASE_LOCATION, BASE_SIZE,
 177                     BASE_SIZE * 2);
 178         };
 179 
 180         public void createDialog() throws InvocationTargetException, InterruptedException {
 181             dialog = null;
 182             invokeWaitSleep(createDialogRunnable);
 183         }
 184 
 185         public void disposeDialog() throws InvocationTargetException, InterruptedException {
 186             invokeWaitSleep(() -> dialog.dispose());
 187         }
 188 
 189         public void showDialog() throws InvocationTargetException, InterruptedException {
 190             invokeWaitSleep(() -> dialog.setVisible(true));
 191         }
 192 
 193         public void closeStageAndDialog() throws InvocationTargetException, InterruptedException {
 194             disposeDialog();
 195             closeStage();
 196         }
 197 
 198         public static class ColorPanel extends JPanel {
 199             private CountDownLatch latch;
 200             public ColorPanel(java.awt.Color color) {
 201                 super();
 202                 setBackground(color);
 203             }
 204         }
 205     }
 206 
 207     public void testAbove(boolean above) {
 208         int checkLoc = BASE_LOCATION + 3 * BASE_SIZE /4;
 209         int clickLoc = BASE_LOCATION + BASE_SIZE / 4;
 210 
 211         if (myApp.stage != null && myApp.stage.isShowing()) {
 212             robot.mouseMove(clickLoc, clickLoc);
 213             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 214             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 215         }
 216 
 217         if (above) {
 218             Assert.assertEquals("JDialog is not above JavaFX stage",
 219                     java.awt.Color.BLUE, robot.getPixelColor(checkLoc, checkLoc));
 220         } else {
 221             Assert.assertFalse("JDialog is above JavaFX stage",
 222                     java.awt.Color.BLUE.equals(robot.getPixelColor(checkLoc, checkLoc)));
 223         }
 224     }
 225 }