Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8200224: Multiple press event when JFXPanel gains focus #25

Closed
wants to merge 14 commits into from
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,6 @@
import org.junit.Test;
import junit.framework.AssertionFailedError;


import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
@@ -69,18 +68,12 @@ public void start(Stage primaryStage) throws Exception {
}

@BeforeClass
public static void doSetupOnce() {
public static void doSetupOnce() throws Exception {
// Start the Application
new Thread(() -> Application.launch(MyApp.class, (String[]) null)).start();

try {
if (!launchLatch.await(5000, TimeUnit.MILLISECONDS)) {
throw new AssertionFailedError("Timeout waiting for Application to launch");
}
} catch (InterruptedException ex) {
AssertionFailedError err = new AssertionFailedError("Unexpected exception");
err.initCause(ex);
throw err;
if (!launchLatch.await(5000, TimeUnit.MILLISECONDS)) {
throw new AssertionFailedError("Timeout waiting for Application to launch");
}

Assert.assertEquals(0, launchLatch.getCount());
@@ -91,7 +84,7 @@ public static void doTeardownOnce() {
Platform.exit();
}

class TestFXPanel extends JFXPanel {
static class TestFXPanel extends JFXPanel {
protected void processMouseEventPublic(MouseEvent e) {
processMouseEvent(e);
}
@@ -106,26 +99,29 @@ public void testNoDoubleClickOnFirstClick() throws Exception {
int[] pressedEventCounter = {0};

SwingUtilities.invokeLater(() -> {
This conversation was marked as resolved by FlorianKirmaier

This comment has been minimized.

@kevinrushforth

kevinrushforth Nov 25, 2019 Member

Add the following here:

            JFXPanel dummyFXPanel = new JFXPanel();
            dummyFXPanel.setPreferredSize(new Dimension(100, 100));

This comment has been minimized.

@FlorianKirmaier

FlorianKirmaier Nov 26, 2019 Author Member

done

TestFXPanel dummyFXPanel = new TestFXPanel();
dummyFXPanel.setPreferredSize(new Dimension(100, 100));
TestFXPanel fxPnl = new TestFXPanel();
fxPnl.setPreferredSize(new Dimension(100, 100));
JFrame jframe = new JFrame();
JPanel jpanel = new JPanel();
This conversation was marked as resolved by FlorianKirmaier

This comment has been minimized.

@kevinrushforth

kevinrushforth Nov 25, 2019 Member

Add the following here:

            jpanel.add(dummyFXPanel);
jpanel.add(dummyFXPanel);
jpanel.add(fxPnl);
jframe.setContentPane(jpanel);
jframe.pack();
jframe.setVisible(true);

Platform.runLater(() -> {
This conversation was marked as resolved by FlorianKirmaier

This comment has been minimized.

@kevinrushforth

kevinrushforth Nov 25, 2019 Member

Add the following here:

                Scene dummyScene = new Scene(new Group());
                dummyFXPanel.setScene(dummyScene);

This comment has been minimized.

@FlorianKirmaier

FlorianKirmaier Nov 26, 2019 Author Member

done

Group grp = new Group();
Scene dummyScene = new Scene(new Group());
dummyFXPanel.setScene(dummyScene);
Scene scene = new Scene(new Group());
fxPnl.setScene(scene);

scene.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_PRESSED, (event -> {
pressedEventCounter[0] += 1;
firstPressedEventLatch.countDown();
}));

fxPnl.setScene(scene);

SwingUtilities.invokeLater(() -> {
MouseEvent e = new MouseEvent(fxPnl, MouseEvent.MOUSE_PRESSED, 0, MouseEvent.BUTTON1_DOWN_MASK,
5, 5, 1, false, MouseEvent.BUTTON1);
@@ -135,9 +131,7 @@ public void testNoDoubleClickOnFirstClick() throws Exception {
});
});

if(!firstPressedEventLatch.await(5000, TimeUnit.MILLISECONDS)) {
throw new Exception();
};
Assert.assertTrue(firstPressedEventLatch.await(5000, TimeUnit.MILLISECONDS));

Thread.sleep(500); // there should be no pressed event after the initial one. Let's wait for 0.5s and check again.

@@ -0,0 +1,118 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package test.robot.javafx.embed.swing;
This conversation was marked as resolved by FlorianKirmaier

This comment has been minimized.

@kevinrushforth

kevinrushforth Nov 18, 2019 Member

Since you aren't using Robot I'll repeat my earlier recommendation to put your new test in test.javafx.embed.swing (as a new test class) rather than modifying this existing test class. This class isn't suitable anyway, since it is only run on Mac.


import com.sun.javafx.PlatformUtil;
import org.junit.Assume;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;

import javafx.embed.swing.JFXPanel;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class JFXPanelTest {
private static Robot robot;
private static JFrame frame;
private static volatile boolean stop;

public static void main(String[] args) throws Exception {
init();
try {
new JFXPanelTest().testJFXPanelNew();
teardown();
} catch (Throwable th) {
th.printStackTrace();
System.exit(1);
} finally {
System.exit(0);
}
}

@BeforeClass
public static void init() throws Exception {
Assume.assumeTrue(PlatformUtil.isMac());
System.setProperty("apple.laf.useScreenMenuBar", "true");
robot = new Robot();
robot.waitForIdle();
robot.setAutoDelay(10);
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame("JFXPanel init test");
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("te-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-e-st");
menu.add(new JMenuItem("1"));
menubar.add(menu);
frame.setJMenuBar(menubar);
frame.setSize(200, 200);
frame.setVisible(true);
});
robot.waitForIdle();
}

@Test
public void testJFXPanelNew() throws Exception {
CountDownLatch beginLatch = new CountDownLatch(1);
new Thread(() -> {
try {
beginLatch.await();
} catch (Exception e) {
e.printStackTrace();
}
while (!stop) {
robot.mouseMove(300, 10);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}).start();
beginLatch.countDown();
CountDownLatch endLatch = new CountDownLatch(1);
SwingUtilities.invokeLater(() -> {
new JFXPanel();
stop = true;
endLatch.countDown();
});
endLatch.await(5, TimeUnit.SECONDS);
Assert.assertTrue("It seems FX initialization is deadlocked", stop);
}

@AfterClass
public static void teardown() throws Exception {
stop = true;
if (frame != null) {
SwingUtilities.invokeLater(frame::dispose);
}
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.