Skip to content

Commit 2b9d54a

Browse files
Sonia Zaldana CallesGoeLin
Sonia Zaldana Calles
authored andcommittedNov 9, 2023
8306134: Open source some AWT tests relating to Button and a few other classes
Backport-of: fdb4bafa3142cedeb9eb3cb930890e97b35402de
1 parent a2ef248 commit 2b9d54a

File tree

6 files changed

+739
-0
lines changed

6 files changed

+739
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2005, 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 4513402
27+
@summary AWTEventMulticaster.getListeners throws unexpected ArrayStoreException
28+
*/
29+
30+
import java.awt.AWTEventMulticaster;
31+
import java.awt.event.ComponentAdapter;
32+
import java.awt.event.ComponentListener;
33+
import java.awt.event.FocusListener;
34+
35+
public class ArrayStoreException {
36+
37+
public static void main(String[] args) throws Exception {
38+
39+
ComponentListener mc =
40+
AWTEventMulticaster.add(
41+
new ComponentAdapter() {},
42+
new ComponentAdapter() {});
43+
44+
if (AWTEventMulticaster.getListeners(mc, FocusListener.class).length == 0) {
45+
System.out.println("OKAY");
46+
} else {
47+
System.out.println("empty array expected");
48+
throw new RuntimeException("Test failed");
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 4370733
27+
@summary AWTKeyStroke's getAWTKeyStroke(String) and toString() method aren't symmetric
28+
*/
29+
30+
import java.awt.AWTKeyStroke;
31+
import java.awt.event.InputEvent;
32+
import java.awt.event.KeyEvent;
33+
import javax.swing.KeyStroke;
34+
35+
public class ToStringTest {
36+
37+
/* Note this test is deliberately testing the deprecated constants
38+
* as well as their replacements.
39+
*/
40+
@SuppressWarnings("deprecation")
41+
public static final int[] modifiers = {
42+
0,
43+
InputEvent.SHIFT_MASK,
44+
InputEvent.CTRL_MASK,
45+
InputEvent.META_MASK,
46+
InputEvent.ALT_MASK,
47+
InputEvent.ALT_GRAPH_MASK,
48+
InputEvent.BUTTON1_MASK,
49+
InputEvent.BUTTON2_MASK,
50+
InputEvent.BUTTON3_MASK,
51+
InputEvent.SHIFT_DOWN_MASK,
52+
InputEvent.CTRL_DOWN_MASK,
53+
InputEvent.META_DOWN_MASK,
54+
InputEvent.ALT_DOWN_MASK,
55+
InputEvent.BUTTON1_DOWN_MASK,
56+
InputEvent.BUTTON2_DOWN_MASK,
57+
InputEvent.BUTTON3_DOWN_MASK,
58+
InputEvent.ALT_GRAPH_DOWN_MASK
59+
};
60+
61+
public static final int[] keys = {
62+
KeyEvent.VK_A,
63+
KeyEvent.VK_B,
64+
KeyEvent.VK_C,
65+
KeyEvent.VK_D,
66+
KeyEvent.VK_E,
67+
KeyEvent.VK_F,
68+
KeyEvent.VK_G,
69+
KeyEvent.VK_H,
70+
KeyEvent.VK_I,
71+
KeyEvent.VK_J,
72+
KeyEvent.VK_K,
73+
KeyEvent.VK_L,
74+
KeyEvent.VK_M,
75+
KeyEvent.VK_N,
76+
KeyEvent.VK_O,
77+
KeyEvent.VK_P,
78+
KeyEvent.VK_Q,
79+
KeyEvent.VK_R,
80+
KeyEvent.VK_S,
81+
KeyEvent.VK_T,
82+
KeyEvent.VK_U,
83+
KeyEvent.VK_V,
84+
KeyEvent.VK_W,
85+
KeyEvent.VK_X,
86+
KeyEvent.VK_Y,
87+
KeyEvent.VK_Z,
88+
KeyEvent.VK_0,
89+
KeyEvent.VK_1,
90+
KeyEvent.VK_2,
91+
KeyEvent.VK_3,
92+
KeyEvent.VK_4,
93+
KeyEvent.VK_5,
94+
KeyEvent.VK_6,
95+
KeyEvent.VK_7,
96+
KeyEvent.VK_8,
97+
KeyEvent.VK_9,
98+
99+
KeyEvent.VK_COMMA,
100+
KeyEvent.VK_PERIOD,
101+
KeyEvent.VK_SLASH,
102+
KeyEvent.VK_SEMICOLON,
103+
KeyEvent.VK_EQUALS,
104+
KeyEvent.VK_OPEN_BRACKET,
105+
KeyEvent.VK_BACK_SLASH,
106+
KeyEvent.VK_CLOSE_BRACKET,
107+
108+
KeyEvent.VK_ENTER,
109+
KeyEvent.VK_BACK_SPACE,
110+
KeyEvent.VK_TAB,
111+
KeyEvent.VK_CANCEL,
112+
KeyEvent.VK_CLEAR,
113+
KeyEvent.VK_SHIFT,
114+
KeyEvent.VK_CONTROL,
115+
KeyEvent.VK_ALT,
116+
KeyEvent.VK_PAUSE,
117+
KeyEvent.VK_CAPS_LOCK,
118+
KeyEvent.VK_ESCAPE,
119+
KeyEvent.VK_SPACE,
120+
KeyEvent.VK_PAGE_UP,
121+
KeyEvent.VK_PAGE_DOWN,
122+
KeyEvent.VK_END,
123+
KeyEvent.VK_HOME,
124+
KeyEvent.VK_LEFT,
125+
KeyEvent.VK_UP,
126+
KeyEvent.VK_RIGHT,
127+
KeyEvent.VK_DOWN,
128+
KeyEvent.VK_ADD,
129+
KeyEvent.VK_SEPARATOR,
130+
KeyEvent.VK_SUBTRACT,
131+
KeyEvent.VK_DECIMAL,
132+
KeyEvent.VK_DIVIDE,
133+
KeyEvent.VK_DELETE,
134+
KeyEvent.VK_NUM_LOCK,
135+
KeyEvent.VK_SCROLL_LOCK,
136+
137+
KeyEvent.VK_WINDOWS,
138+
KeyEvent.VK_CONTEXT_MENU,
139+
140+
KeyEvent.VK_F1,
141+
KeyEvent.VK_F2,
142+
KeyEvent.VK_F3,
143+
KeyEvent.VK_F4,
144+
KeyEvent.VK_F5,
145+
KeyEvent.VK_F6,
146+
KeyEvent.VK_F7,
147+
KeyEvent.VK_F8,
148+
KeyEvent.VK_F9,
149+
KeyEvent.VK_F10,
150+
KeyEvent.VK_F11,
151+
KeyEvent.VK_F12,
152+
KeyEvent.VK_F13,
153+
KeyEvent.VK_F14,
154+
KeyEvent.VK_F15,
155+
KeyEvent.VK_F16,
156+
KeyEvent.VK_F17,
157+
KeyEvent.VK_F18,
158+
KeyEvent.VK_F19,
159+
KeyEvent.VK_F20,
160+
KeyEvent.VK_F21,
161+
KeyEvent.VK_F22,
162+
KeyEvent.VK_F23,
163+
KeyEvent.VK_F24,
164+
165+
KeyEvent.VK_PRINTSCREEN,
166+
KeyEvent.VK_INSERT,
167+
KeyEvent.VK_HELP,
168+
KeyEvent.VK_META,
169+
KeyEvent.VK_BACK_QUOTE,
170+
KeyEvent.VK_QUOTE,
171+
172+
KeyEvent.VK_KP_UP,
173+
KeyEvent.VK_KP_DOWN,
174+
KeyEvent.VK_KP_LEFT,
175+
KeyEvent.VK_KP_RIGHT,
176+
177+
KeyEvent.VK_DEAD_GRAVE,
178+
KeyEvent.VK_DEAD_ACUTE,
179+
KeyEvent.VK_DEAD_CIRCUMFLEX,
180+
KeyEvent.VK_DEAD_TILDE,
181+
KeyEvent.VK_DEAD_MACRON,
182+
KeyEvent.VK_DEAD_BREVE,
183+
KeyEvent.VK_DEAD_ABOVEDOT,
184+
KeyEvent.VK_DEAD_DIAERESIS,
185+
KeyEvent.VK_DEAD_ABOVERING,
186+
KeyEvent.VK_DEAD_DOUBLEACUTE,
187+
KeyEvent.VK_DEAD_CARON,
188+
KeyEvent.VK_DEAD_CEDILLA,
189+
KeyEvent.VK_DEAD_OGONEK,
190+
KeyEvent.VK_DEAD_IOTA,
191+
KeyEvent.VK_DEAD_VOICED_SOUND,
192+
KeyEvent.VK_DEAD_SEMIVOICED_SOUND,
193+
194+
KeyEvent.VK_AMPERSAND,
195+
KeyEvent.VK_ASTERISK,
196+
KeyEvent.VK_QUOTEDBL,
197+
KeyEvent.VK_LESS,
198+
KeyEvent.VK_GREATER,
199+
KeyEvent.VK_BRACELEFT,
200+
KeyEvent.VK_BRACERIGHT,
201+
KeyEvent.VK_AT,
202+
KeyEvent.VK_COLON,
203+
KeyEvent.VK_CIRCUMFLEX,
204+
KeyEvent.VK_DOLLAR,
205+
KeyEvent.VK_EURO_SIGN,
206+
KeyEvent.VK_EXCLAMATION_MARK,
207+
KeyEvent.VK_INVERTED_EXCLAMATION_MARK,
208+
KeyEvent.VK_LEFT_PARENTHESIS,
209+
KeyEvent.VK_NUMBER_SIGN,
210+
KeyEvent.VK_MINUS,
211+
KeyEvent.VK_PLUS,
212+
KeyEvent.VK_RIGHT_PARENTHESIS,
213+
KeyEvent.VK_UNDERSCORE,
214+
215+
KeyEvent.VK_FINAL,
216+
KeyEvent.VK_CONVERT,
217+
KeyEvent.VK_NONCONVERT,
218+
KeyEvent.VK_ACCEPT,
219+
KeyEvent.VK_MODECHANGE,
220+
KeyEvent.VK_KANA,
221+
KeyEvent.VK_KANJI,
222+
KeyEvent.VK_ALPHANUMERIC,
223+
KeyEvent.VK_KATAKANA,
224+
KeyEvent.VK_HIRAGANA,
225+
KeyEvent.VK_FULL_WIDTH,
226+
KeyEvent.VK_HALF_WIDTH,
227+
KeyEvent.VK_ROMAN_CHARACTERS,
228+
KeyEvent.VK_ALL_CANDIDATES,
229+
KeyEvent.VK_PREVIOUS_CANDIDATE,
230+
KeyEvent.VK_CODE_INPUT,
231+
KeyEvent.VK_JAPANESE_KATAKANA,
232+
KeyEvent.VK_JAPANESE_HIRAGANA,
233+
KeyEvent.VK_JAPANESE_ROMAN,
234+
KeyEvent.VK_KANA_LOCK,
235+
KeyEvent.VK_INPUT_METHOD_ON_OFF,
236+
237+
KeyEvent.VK_AGAIN,
238+
KeyEvent.VK_UNDO,
239+
KeyEvent.VK_COPY,
240+
KeyEvent.VK_PASTE,
241+
KeyEvent.VK_CUT,
242+
KeyEvent.VK_FIND,
243+
KeyEvent.VK_PROPS,
244+
KeyEvent.VK_STOP,
245+
246+
KeyEvent.VK_COMPOSE,
247+
KeyEvent.VK_ALT_GRAPH,
248+
KeyEvent.VK_BEGIN,
249+
250+
KeyEvent.VK_NUMPAD0,
251+
KeyEvent.VK_NUMPAD1,
252+
KeyEvent.VK_NUMPAD2,
253+
KeyEvent.VK_NUMPAD3,
254+
KeyEvent.VK_NUMPAD4,
255+
KeyEvent.VK_NUMPAD5,
256+
KeyEvent.VK_NUMPAD6,
257+
KeyEvent.VK_NUMPAD7,
258+
KeyEvent.VK_NUMPAD8,
259+
KeyEvent.VK_NUMPAD9
260+
};
261+
262+
public static void main(String[] args) throws Exception {
263+
264+
System.err.println("**** Testing AWTKeyStrokes");
265+
for (int n_key=0; n_key < keys.length; n_key++) {
266+
for (int n_mod=0; n_mod < modifiers.length; n_mod++) {
267+
checkStroke(AWTKeyStroke.getAWTKeyStroke(keys[n_key],
268+
modifiers[n_mod],
269+
true));
270+
checkStroke(AWTKeyStroke.getAWTKeyStroke(keys[n_key],
271+
modifiers[n_mod],
272+
false));
273+
}
274+
}
275+
276+
System.err.println("**** Testing Swing KeyStrokes");
277+
for (int n_key=0; n_key < keys.length; n_key++) {
278+
for (int n_mod=0; n_mod < modifiers.length; n_mod++) {
279+
checkStroke(KeyStroke.getKeyStroke(keys[n_key],
280+
modifiers[n_mod],
281+
true));
282+
checkStroke(KeyStroke.getKeyStroke(keys[n_key],
283+
modifiers[n_mod],
284+
false));
285+
}
286+
}
287+
288+
Character a = Character.valueOf('a');
289+
System.err.println("**** Testing KEY_TYPED AWTKeyStrokes");
290+
for (int n_mod = 0; n_mod < modifiers.length; n_mod++) {
291+
checkStroke(AWTKeyStroke.getAWTKeyStroke(a, modifiers[n_mod]));
292+
}
293+
System.err.println("**** Testing KEY_TYPED Swing KeyStrokes");
294+
for (int n_mod = 0; n_mod < modifiers.length; n_mod++) {
295+
checkStroke(KeyStroke.getKeyStroke(a, modifiers[n_mod]));
296+
}
297+
298+
System.out.println("Test passed.");
299+
}
300+
301+
public static void checkStroke(AWTKeyStroke original) {
302+
System.err.println("AWT Original >> " + original);
303+
AWTKeyStroke copy = AWTKeyStroke.getAWTKeyStroke(original.toString());
304+
// System.err.println("AWT Copy >> " + copy);
305+
if (!original.equals(copy)) {
306+
System.out.println("AWT bad copy for VK= 0x" +
307+
Integer.toString(original.getKeyCode(), 16));
308+
throw new RuntimeException("Test Failed: for " + original);
309+
}
310+
}
311+
312+
public static void checkStroke(KeyStroke original) {
313+
System.err.println("Swing Original >> " + original);
314+
KeyStroke copy = KeyStroke.getKeyStroke(original.toString());
315+
// System.err.println("Swing Copy >> " + copy);
316+
if (!original.equals(copy)) {
317+
System.out.println("Swing bad copy for VK= 0x" +
318+
Integer.toString(original.getKeyCode(), 16));
319+
throw new RuntimeException("Test Failed: for " + original);
320+
}
321+
}
322+
323+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2005, 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 6242148
27+
@summary API method java.awt.BorderLayout.getConstraints(null) should return null
28+
*/
29+
30+
import java.awt.BorderLayout;
31+
32+
public class NullConstraintsReturns {
33+
34+
public static void main(String[] args) {
35+
BorderLayout bl = new BorderLayout();
36+
Object constraints = bl.getConstraints(null);
37+
if (constraints != null) {
38+
throw new RuntimeException("Test failed. Constraints is not null: " + constraints);
39+
}
40+
System.out.println("Test Passed.");
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 4245382
27+
@summary Tests that Button.setLabel(null) does not cause NPE in Java code or VM crash
28+
@key headful
29+
*/
30+
31+
import java.awt.Button;
32+
import java.awt.EventQueue;
33+
import java.awt.Frame;
34+
35+
public class ButtonNullLabelTest {
36+
37+
public static void main(String args[]) throws Exception {
38+
EventQueue.invokeAndWait(() -> runTest());
39+
}
40+
41+
static void runTest() {
42+
// Native code test
43+
Frame frame = new Frame("Test null in native");
44+
Button button = new Button();
45+
try {
46+
button.setLabel(null);
47+
System.out.println("Set to null - test native");
48+
frame.add(button);
49+
frame.pack();
50+
frame.setLocationRelativeTo(null);
51+
frame.setVisible(true);
52+
System.out.println("Test null in native **successful**");
53+
} catch (NullPointerException npe) {
54+
System.out.println("Test failed - test native");
55+
throw new RuntimeException("Test failed - test native");
56+
} finally {
57+
frame.dispose();
58+
}
59+
60+
// Peer code test
61+
frame = new Frame("Test null in peer before show");
62+
button = new Button();
63+
try {
64+
System.out.println("Set to null - test native before show");
65+
frame.add(button);
66+
frame.pack();
67+
button.setLabel(null);
68+
frame.setVisible(true);
69+
System.out.println("Set null in peer before show **successful**");
70+
} catch (NullPointerException npe) {
71+
System.out.println("Test failed - test peer before show");
72+
throw new RuntimeException("Test failed - test peer before show");
73+
} finally {
74+
frame.dispose();
75+
}
76+
77+
// Peer code test
78+
frame = new Frame("Test null in peer after show");
79+
button = new Button();
80+
try {
81+
System.out.println("Set to null - test peer after show");
82+
frame.add(button);
83+
frame.pack();
84+
frame.setVisible(true);
85+
button.setLabel(null);
86+
System.out.println("Test null in peer after show **successful**");
87+
} catch (NullPointerException npe) {
88+
System.out.println("Test failed - peer after show");
89+
throw new RuntimeException("Test failed - peer after show");
90+
} finally {
91+
frame.dispose();
92+
}
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2005, 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 5044469
27+
@summary REG: Disabled component gains focus and receives keyevents on win32
28+
@key headful
29+
*/
30+
31+
import java.awt.AWTException;
32+
import java.awt.Button;
33+
import java.awt.EventQueue;
34+
import java.awt.Frame;
35+
import java.awt.Robot;
36+
import java.awt.event.ActionEvent;
37+
import java.awt.event.ActionListener;
38+
import java.awt.event.FocusEvent;
39+
import java.awt.event.FocusListener;
40+
import java.awt.event.KeyEvent;
41+
42+
public class DisabledButtonPress implements ActionListener, FocusListener {
43+
44+
public static void main(String[] args) throws Exception {
45+
try {
46+
DisabledButtonPress test = new DisabledButtonPress();
47+
EventQueue.invokeAndWait(() -> test.createUI());
48+
runTest();
49+
} finally {
50+
if (f != null) {
51+
f.dispose();
52+
}
53+
}
54+
if (!testPassed) {
55+
throw new RuntimeException("Test Failed.");
56+
}
57+
}
58+
59+
final static Object FOCUS_LOCK = new Object();
60+
final static Object ACTION_LOCK = new Object();
61+
static volatile Frame f;
62+
static volatile Button b2;
63+
static volatile boolean testPassed = true;
64+
65+
public void createUI() {
66+
f = new Frame("DisabledButtonPress");
67+
b2 = new Button("Click Me");
68+
b2.addActionListener(this);
69+
b2.addFocusListener(this);
70+
f.add(b2);
71+
f.pack();
72+
f.setVisible(true);
73+
}
74+
75+
static void runTest() throws Exception {
76+
77+
Robot robot = new Robot();
78+
robot.delay(500);
79+
System.out.println("Requesting focus");
80+
System.out.println(" b2.requestFocusInWindow()="+ b2.requestFocusInWindow());
81+
b2.setEnabled(false);
82+
synchronized(FOCUS_LOCK) {
83+
FOCUS_LOCK.wait(3000);
84+
}
85+
if (!b2.isFocusOwner()) {
86+
throw new RuntimeException("Test failed. Button doesn't have a focus.");
87+
}
88+
System.out.println("Button disabling");
89+
robot.delay(1000);
90+
robot.keyPress(KeyEvent.VK_SPACE);
91+
robot.delay(10);
92+
robot.keyRelease(KeyEvent.VK_SPACE);
93+
synchronized(ACTION_LOCK) {
94+
ACTION_LOCK.wait(2000); //give time to handle
95+
// ACTION_PERFORMED event from the Button if it was generated
96+
}
97+
}
98+
99+
public void focusGained(FocusEvent ae) {
100+
System.out.println("Button got focus");
101+
synchronized(FOCUS_LOCK) {
102+
FOCUS_LOCK.notify();
103+
}
104+
}
105+
106+
public void focusLost(FocusEvent ae) {}
107+
108+
public void actionPerformed(ActionEvent evt) {
109+
System.out.println("Button: " + evt.getActionCommand() + " Clicked. Event is " +evt);
110+
if (evt.getSource() == b2) {
111+
testPassed = false;
112+
synchronized(ACTION_LOCK) {
113+
ACTION_LOCK.notify();
114+
}
115+
}
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 2002, 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 4531849
27+
@summary Test that double action event no longer sent
28+
@key headful
29+
*/
30+
31+
import java.awt.BorderLayout;
32+
import java.awt.Button;
33+
import java.awt.EventQueue;
34+
import java.awt.Frame;
35+
import java.awt.Robot;
36+
import java.awt.event.ActionEvent;
37+
import java.awt.event.ActionListener;
38+
import java.awt.event.InputEvent;
39+
import java.awt.event.WindowEvent;
40+
import java.awt.event.WindowListener;
41+
42+
public class DoubleActionEventTest implements ActionListener, WindowListener {
43+
44+
static class Lock {
45+
boolean go = false;
46+
public synchronized boolean getGo() {return go;}
47+
public synchronized void setGo(boolean newGo) {go = newGo;}
48+
}
49+
50+
static volatile Frame f;
51+
static volatile int numActionEvents = 0;
52+
static volatile Lock lock = new Lock();
53+
54+
public static void main(String[] args) throws Exception {
55+
try {
56+
DoubleActionEventTest test = new DoubleActionEventTest();
57+
EventQueue.invokeAndWait(() -> test.createUI());
58+
runTest();
59+
} finally {
60+
if (f != null) {
61+
f.dispose();
62+
}
63+
}
64+
}
65+
66+
public void createUI() {
67+
f = new Frame("DoubleActionEventTest");
68+
f.setLayout (new BorderLayout());
69+
f.addWindowListener(this);
70+
Button b = new Button("Action Listening Button");
71+
b.addActionListener(this);
72+
f.add(b);
73+
f.setBounds(100, 100, 200, 200);
74+
f.setVisible(true);
75+
}
76+
77+
static void runTest() throws Exception {
78+
79+
Robot robot = new Robot();
80+
robot.setAutoDelay(250);
81+
robot.setAutoWaitForIdle(true);
82+
robot.mouseMove(200, 200);
83+
84+
while (!lock.getGo()) {}
85+
86+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
87+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
88+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
89+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
90+
91+
if (numActionEvents != 1) {
92+
System.out.println("Wrong number of ActionEvents. Test FAILS.");
93+
throw new RuntimeException("TEST FAILS");
94+
}
95+
}
96+
97+
public void actionPerformed(ActionEvent e) {
98+
numActionEvents++;
99+
System.out.println("Number of ActionEvents: " + numActionEvents);
100+
}
101+
102+
public void windowActivated(WindowEvent e) {
103+
lock.setGo(true);
104+
}
105+
public void windowClosed(WindowEvent e) {}
106+
public void windowClosing(WindowEvent e) {}
107+
public void windowDeactivated(WindowEvent e) {}
108+
public void windowDeiconified(WindowEvent e) {}
109+
public void windowIconified(WindowEvent e) {}
110+
public void windowOpened(WindowEvent e) {}
111+
112+
}

0 commit comments

Comments
 (0)
Please sign in to comment.