< prev index next >

modules/javafx.swing/src/test/java/test/javafx/embed/swing/JFXPanelTest.java

Print this page

  1 /*
  2  * Copyright (c) 2014, 2016 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.javafx.embed.swing;
 27 
 28 import javafx.application.Application;
 29 import javafx.application.Platform;
 30 import javafx.embed.swing.JFXPanel;
 31 import javafx.scene.Group;
 32 import javafx.scene.shape.Circle;
 33 import javafx.stage.Stage;
 34 import jdk.swing.interop.SwingInterOpUtils;
 35 import junit.framework.AssertionFailedError;
 36 import org.junit.BeforeClass;
 37 import org.junit.Test;
 38 import javafx.scene.Scene;

 39 import javax.swing.JPanel;
 40 import javax.swing.JFrame;
 41 import javax.swing.SwingUtilities;



 42 
 43 import java.awt.Dimension;
 44 import java.awt.event.MouseEvent;
 45 import java.util.concurrent.CountDownLatch;
 46 import java.util.concurrent.TimeUnit;
 47 
 48 import static junit.framework.Assert.assertEquals;
 49 import static org.junit.Assert.assertNotNull;
 50 import static org.junit.Assert.assertTrue;
 51 

 52 public class JFXPanelTest {
 53 
 54     // Used to launch the application before running any test
 55     private static final CountDownLatch launchLatch = new CountDownLatch(1);
 56 
 57 
 58     @BeforeClass
 59     public static void doSetupOnce() {

 60         Platform.startup(() -> {
 61             Platform.setImplicitExit(false);
 62             launchLatch.countDown();
 63         });
 64 
 65 
 66         try {
 67             if (!launchLatch.await(5000, TimeUnit.MILLISECONDS)) {
 68                 throw new AssertionFailedError("Timeout waiting for Application to launch");
 69             }
 70         } catch (InterruptedException ex) {
 71             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
 72             err.initCause(ex);
 73             throw err;
 74         }
 75 
 76         assertEquals(0, launchLatch.getCount());

 77     }
 78 
 79     class TestFXPanel extends JFXPanel {
 80         protected void processMouseEventPublic(MouseEvent e) {
 81             processMouseEvent(e);
 82         }
 83     };
 84 
 85     @Test
 86     public void testNoDoubleClickOnFirstClick() throws Exception {
 87 
 88         CountDownLatch firstPressedEventLatch = new CountDownLatch(1);
 89 
 90         // It's an array, so we can mutate it inside of lambda statement
 91         int[] pressedEventCounter = {0};
 92 
 93         SwingUtilities.invokeLater(() -> {
 94             TestFXPanel fxPnl = new TestFXPanel();
 95             fxPnl.setPreferredSize(new Dimension(100, 100));
 96             JFrame jframe = new JFrame();
 97             JPanel jpanel = new JPanel();
 98             jpanel.add(fxPnl);
 99             jframe.setContentPane(jpanel);
100             jframe.setVisible(true);
101 
102             Platform.runLater(() -> {
103                 Group grp = new Group();
104                 Scene scene = new Scene(new Group());
105                 scene.getRoot().requestFocus();
106 
107                 scene.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_PRESSED, (event -> {
108                     pressedEventCounter[0] += 1;
109                     firstPressedEventLatch.countDown();
110                 }));
111 
112                 fxPnl.setScene(scene);
113 
114                 SwingUtilities.invokeLater(() -> {
115                     MouseEvent e = new MouseEvent(fxPnl, MouseEvent.MOUSE_PRESSED, 0, MouseEvent.BUTTON1_DOWN_MASK,
116                             5, 5, 1, false, MouseEvent.BUTTON1);
117 
118                     fxPnl.processMouseEventPublic(e);
119                 });
120             });
121         });
122 
123         if(!firstPressedEventLatch.await(5000, TimeUnit.MILLISECONDS)) {
124             throw new Exception();
125         };
126 
127         Thread.sleep(100); // there should be no pressed event after the initial one. Let's wait for 0.1s and check again.
128 
129         assertEquals(1, pressedEventCounter[0]);
130     }
131 
132 
133 }

  1 /*
  2  * Copyright (c) 2019 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.javafx.embed.swing;
 27 
 28 import javafx.application.Application;
 29 import javafx.application.Platform;
 30 import javafx.embed.swing.JFXPanel;
 31 import javafx.scene.Group;
 32 import javafx.scene.shape.Circle;





 33 import javafx.scene.Scene;
 34 import javafx.stage.Stage;
 35 import javax.swing.JPanel;
 36 import javax.swing.JFrame;
 37 import javax.swing.SwingUtilities;
 38 import junit.framework.AssertionFailedError;
 39 import org.junit.BeforeClass;
 40 import org.junit.Test;
 41 
 42 import java.awt.Dimension;
 43 import java.awt.event.MouseEvent;
 44 import java.util.concurrent.CountDownLatch;
 45 import java.util.concurrent.TimeUnit;
 46 
 47 import static org.junit.Assert.assertEquals;
 48 import static org.junit.Assert.assertNotNull;
 49 import static org.junit.Assert.assertTrue;
 50 
 51 
 52 public class JFXPanelTest {
 53 
 54     // Used to launch the application before running any test
 55     private static final CountDownLatch launchLatch = new CountDownLatch(1);
 56     private static boolean setupIsDone = false;
 57 
 58     @BeforeClass
 59     public static void doSetupOnce() {
 60         if (setupIsDone) return;
 61         Platform.startup(() -> {
 62             Platform.setImplicitExit(false);
 63             launchLatch.countDown();
 64         });
 65 

 66         try {
 67             if (!launchLatch.await(5000, TimeUnit.MILLISECONDS)) {
 68                 throw new AssertionFailedError("Timeout waiting for Application to launch");
 69             }
 70         } catch (InterruptedException ex) {
 71             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
 72             err.initCause(ex);
 73             throw err;
 74         }
 75 
 76         assertEquals(0, launchLatch.getCount());
 77         setupIsDone = true;
 78     }
 79 
 80     static class TestFXPanel extends JFXPanel {
 81         protected void processMouseEventPublic(MouseEvent e) {
 82             processMouseEvent(e);
 83         }
 84     };
 85 
 86     @Test
 87     public void testNoDoubleClickOnFirstClick() throws Exception {
 88 
 89         final CountDownLatch firstPressedEventLatch = new CountDownLatch(1);
 90 
 91         // It's an array, so we can mutate it inside of lambda statement
 92         final int[] pressedEventCounter = {0};
 93 
 94         SwingUtilities.invokeLater(() -> {
 95             TestFXPanel fxPnl = new TestFXPanel();
 96             fxPnl.setPreferredSize(new Dimension(100, 100));
 97             JFrame jframe = new JFrame();
 98             JPanel jpanel = new JPanel();
 99             jpanel.add(fxPnl);
100             jframe.setContentPane(jpanel);
101             jframe.setVisible(true);
102 
103             Platform.runLater(() -> {
104                 Group grp = new Group();
105                 Scene scene = new Scene(new Group());
106                 scene.getRoot().requestFocus();
107 
108                 scene.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_PRESSED, (event -> {
109                     pressedEventCounter[0] += 1;
110                     firstPressedEventLatch.countDown();
111                 }));
112 
113                 fxPnl.setScene(scene);
114 
115                 SwingUtilities.invokeLater(() -> {
116                     MouseEvent e = new MouseEvent(fxPnl, MouseEvent.MOUSE_PRESSED, 0, MouseEvent.BUTTON1_DOWN_MASK,
117                             5, 5, 1, false, MouseEvent.BUTTON1);
118 
119                     fxPnl.processMouseEventPublic(e);
120                 });
121             });
122         });
123 
124         if (!firstPressedEventLatch.await(5000, TimeUnit.MILLISECONDS)) {
125             throw new Exception("No first click detected.");
126         };
127 
128         Thread.sleep(100); // there should be no pressed event after the initial one. Let's wait for 0.1s and check again.

129         assertEquals(1, pressedEventCounter[0]);
130     }


131 }
< prev index next >