|
1 | 1 | /* |
2 | | - * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
24 | 24 | /* |
25 | 25 | * @test |
26 | 26 | * @bug 4323121 |
27 | | - * @summary Tests whether any button that extends JButton always |
28 | | - returns true for isArmed() |
| 27 | + * @summary Tests whether a button model always returns true for isArmed() |
| 28 | + * when mouse hovers over the button |
29 | 29 | * @key headful |
30 | 30 | * @run main bug4323121 |
31 | 31 | */ |
32 | 32 |
|
33 | | -import java.awt.Graphics; |
34 | 33 | import java.awt.Point; |
35 | 34 | import java.awt.Robot; |
| 35 | +import java.awt.event.MouseAdapter; |
36 | 36 | import java.awt.event.MouseEvent; |
37 | | -import java.awt.event.MouseListener; |
38 | | -import java.awt.event.MouseMotionListener; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | + |
39 | 39 | import javax.swing.JButton; |
40 | 40 | import javax.swing.JFrame; |
41 | 41 | import javax.swing.SwingUtilities; |
42 | 42 |
|
43 | | -public class bug4323121 { |
| 43 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 44 | + |
| 45 | +public final class bug4323121 { |
44 | 46 |
|
45 | 47 | static JFrame frame; |
46 | | - static testButton button; |
47 | | - static volatile Point pt; |
48 | | - static volatile int buttonW; |
49 | | - static volatile int buttonH; |
50 | | - static volatile boolean failed = false; |
| 48 | + static JButton button; |
| 49 | + |
| 50 | + static volatile Point buttonCenter; |
| 51 | + |
| 52 | + private static final CountDownLatch mouseEntered = new CountDownLatch(1); |
| 53 | + |
| 54 | + // Usage of this flag is thread-safe because of using the mouseEntered latch |
| 55 | + private static boolean modelArmed; |
51 | 56 |
|
52 | 57 | public static void main(String[] args) throws Exception { |
53 | 58 | Robot robot = new Robot(); |
54 | 59 | robot.setAutoDelay(100); |
| 60 | + |
55 | 61 | try { |
56 | 62 | SwingUtilities.invokeAndWait(() -> { |
| 63 | + button = new JButton("gotcha"); |
| 64 | + button.addMouseListener(new MouseAdapter() { |
| 65 | + @Override |
| 66 | + public void mouseEntered(MouseEvent e) { |
| 67 | + if (button.getModel().isArmed()) { |
| 68 | + modelArmed = true; |
| 69 | + } |
| 70 | + mouseEntered.countDown(); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
57 | 74 | frame = new JFrame("bug4323121"); |
58 | | - button = new testButton("gotcha"); |
59 | 75 | frame.getContentPane().add(button); |
| 76 | + |
60 | 77 | frame.pack(); |
61 | 78 | frame.setLocationRelativeTo(null); |
62 | 79 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
63 | 80 | frame.setVisible(true); |
64 | 81 | }); |
| 82 | + |
65 | 83 | robot.waitForIdle(); |
66 | | - robot.delay(1000); |
| 84 | + |
67 | 85 | SwingUtilities.invokeAndWait(() -> { |
68 | | - pt = button.getLocationOnScreen(); |
69 | | - buttonW = button.getSize().width; |
70 | | - buttonH = button.getSize().height; |
| 86 | + Point location = button.getLocationOnScreen(); |
| 87 | + buttonCenter = new Point(location.x + button.getWidth() / 2, |
| 88 | + location.y + button.getHeight() / 2); |
71 | 89 | }); |
72 | | - robot.mouseMove(pt.x + buttonW / 2, pt.y + buttonH / 2); |
73 | | - robot.waitForIdle(); |
74 | | - if (failed) { |
75 | | - throw new RuntimeException("Any created button returns " + |
76 | | - "true for isArmed()"); |
| 90 | + |
| 91 | + robot.mouseMove(buttonCenter.x, buttonCenter.y); |
| 92 | + |
| 93 | + if (!mouseEntered.await(1, SECONDS)) { |
| 94 | + throw new RuntimeException("Mouse entered event wasn't received"); |
| 95 | + } |
| 96 | + if (modelArmed) { |
| 97 | + throw new RuntimeException("getModel().isArmed() returns true " |
| 98 | + + "when mouse hovers over the button"); |
77 | 99 | } |
78 | 100 | } finally { |
79 | | - SwingUtilities.invokeAndWait(() -> { |
| 101 | + SwingUtilities.invokeAndWait(() -> { |
80 | 102 | if (frame != null) { |
81 | 103 | frame.dispose(); |
82 | 104 | } |
83 | 105 | }); |
84 | 106 | } |
85 | 107 | } |
86 | 108 |
|
87 | | - static class testButton extends JButton implements MouseMotionListener, MouseListener { |
88 | | - public testButton(String label) { |
89 | | - super(label); |
90 | | - addMouseMotionListener(this); |
91 | | - addMouseListener(this); |
92 | | - } |
93 | | - |
94 | | - protected void paintComponent(Graphics g) { |
95 | | - super.paintComponent(g); |
96 | | - } |
97 | | - |
98 | | - protected void paintBorder(Graphics g) { |
99 | | - } |
100 | | - |
101 | | - public void mousePressed(MouseEvent e) { |
102 | | - } |
103 | | - |
104 | | - public void mouseDragged(MouseEvent e) { |
105 | | - } |
106 | | - |
107 | | - public void mouseMoved(MouseEvent e) { |
108 | | - } |
109 | | - |
110 | | - public void mouseReleased(MouseEvent e) { |
111 | | - } |
112 | | - |
113 | | - public void mouseEntered(MouseEvent e) { |
114 | | - if (getModel().isArmed()) { |
115 | | - failed = true; |
116 | | - } |
117 | | - } |
118 | | - |
119 | | - public void mouseExited(MouseEvent e) { |
120 | | - } |
121 | | - |
122 | | - public void mouseClicked(MouseEvent e) { |
123 | | - } |
124 | | - } |
125 | 109 | } |
0 commit comments