8341978: Improve JButton/bug4490179.java
Use latch to synchronize actions in the test. Verify mouseButton3Released does not trigger actionPerformed while mouse button 1 is pressed. Ensure mouse button 1 is released. Verify releasing mouse button 1 triggers actionPerformed. Reviewed-by: azvegint, achung, serb
This commit is contained in:
parent
9201e9fcc2
commit
8174cbd5cb
@ -31,61 +31,107 @@
|
||||
|
||||
import java.awt.Point;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class bug4490179 {
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
public final class bug4490179
|
||||
extends MouseAdapter
|
||||
implements ActionListener {
|
||||
static JFrame frame;
|
||||
static JButton button;
|
||||
static volatile Point pt;
|
||||
static volatile int buttonW;
|
||||
static volatile int buttonH;
|
||||
static volatile boolean passed = true;
|
||||
|
||||
private static volatile Point buttonCenter;
|
||||
|
||||
private static final CountDownLatch windowGainedFocus = new CountDownLatch(1);
|
||||
|
||||
private static final CountDownLatch mouseButton1Released = new CountDownLatch(1);
|
||||
private static final CountDownLatch mouseButton3Released = new CountDownLatch(2);
|
||||
|
||||
private static final CountDownLatch actionPerformed = new CountDownLatch(1);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(100);
|
||||
robot.setAutoWaitForIdle(true);
|
||||
|
||||
final bug4490179 eventHandler = new bug4490179();
|
||||
try {
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
frame = new JFrame("bug4490179");
|
||||
button = new JButton("Button");
|
||||
button.addActionListener(eventHandler);
|
||||
button.addMouseListener(eventHandler);
|
||||
|
||||
frame = new JFrame("bug4490179");
|
||||
frame.getContentPane().add(button);
|
||||
button.addActionListener(e -> {
|
||||
if ((e.getModifiers() & InputEvent.BUTTON1_MASK)
|
||||
!= InputEvent.BUTTON1_MASK) {
|
||||
System.out.println("Status: Failed");
|
||||
passed = false;
|
||||
|
||||
frame.addWindowFocusListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowGainedFocus(WindowEvent e) {
|
||||
windowGainedFocus.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
frame.pack();
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
});
|
||||
|
||||
if (!windowGainedFocus.await(1, SECONDS)) {
|
||||
throw new RuntimeException("Window didn't gain focus");
|
||||
}
|
||||
robot.waitForIdle();
|
||||
robot.delay(1000);
|
||||
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
pt = button.getLocationOnScreen();
|
||||
buttonW = button.getSize().width;
|
||||
buttonH = button.getSize().height;
|
||||
Point location = button.getLocationOnScreen();
|
||||
buttonCenter = new Point(location.x + button.getWidth() / 2,
|
||||
location.y + button.getHeight() / 2);
|
||||
});
|
||||
|
||||
robot.mouseMove(pt.x + buttonW / 2, pt.y + buttonH / 2);
|
||||
robot.waitForIdle();
|
||||
robot.mouseMove(buttonCenter.x, buttonCenter.y);
|
||||
System.out.println("Press / Release button 3");
|
||||
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
|
||||
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
|
||||
|
||||
System.out.println("Press button 1");
|
||||
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
||||
System.out.println("Press button 3");
|
||||
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
|
||||
System.out.println("Release button 3");
|
||||
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.delay(500);
|
||||
|
||||
if (!passed) {
|
||||
throw new RuntimeException("Test Failed");
|
||||
try {
|
||||
if (!mouseButton3Released.await(1, SECONDS)) {
|
||||
throw new RuntimeException("Mouse button 3 isn't released");
|
||||
}
|
||||
|
||||
robot.waitForIdle();
|
||||
|
||||
if (actionPerformed.await(100, MILLISECONDS)) {
|
||||
throw new RuntimeException("Action event triggered by releasing button 3");
|
||||
}
|
||||
} finally {
|
||||
System.out.println("Release button 1");
|
||||
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
||||
}
|
||||
|
||||
if (!mouseButton1Released.await(1, SECONDS)) {
|
||||
throw new RuntimeException("Mouse button 1 isn't released");
|
||||
}
|
||||
if (!actionPerformed.await(100, MILLISECONDS)) {
|
||||
throw new RuntimeException("Action event isn't triggered by releasing button 1");
|
||||
}
|
||||
} finally {
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
@ -95,4 +141,21 @@ public class bug4490179 {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println(" actionPerformed");
|
||||
actionPerformed.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
if (e.getButton() == MouseEvent.BUTTON1) {
|
||||
System.out.println(" mouseReleased: button 1");
|
||||
mouseButton1Released.countDown();
|
||||
} else if (e.getButton() == MouseEvent.BUTTON3) {
|
||||
System.out.println(" mouseReleased: button 3");
|
||||
mouseButton3Released.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user