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 }