1
1
/*
2
- * Copyright (c) 1998, 2023 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1998, 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
31
31
import java .awt .Robot ;
32
32
import java .awt .TextArea ;
33
33
import java .awt .event .KeyEvent ;
34
+ import java .awt .event .WindowAdapter ;
35
+ import java .awt .event .WindowEvent ;
36
+ import java .util .ArrayList ;
37
+ import java .util .List ;
38
+ import java .util .concurrent .CountDownLatch ;
39
+ import java .util .concurrent .CyclicBarrier ;
40
+ import java .util .concurrent .TimeoutException ;
41
+ import java .util .concurrent .atomic .AtomicReference ;
42
+
43
+ import static java .awt .Event .KEY_ACTION ;
44
+ import static java .awt .Event .KEY_ACTION_RELEASE ;
45
+ import static java .util .concurrent .TimeUnit .SECONDS ;
34
46
35
47
/*
36
48
* @test
39
51
* @key headful
40
52
*/
41
53
42
- public class FunctionKeyTest {
43
- private static FunctionKeyTester frame ;
54
+ public final class FunctionKeyTest {
55
+ private static Frame frame ;
44
56
private static Robot robot ;
45
57
46
- static volatile boolean keyPressReceived ;
47
- static volatile boolean keyReleaseReceived ;
58
+ private static final CyclicBarrier keyPress = new CyclicBarrier ( 2 ) ;
59
+ private static final CyclicBarrier keyRelease = new CyclicBarrier ( 2 ) ;
48
60
49
- static final StringBuilder failures = new StringBuilder ( );
61
+ private static final CountDownLatch frameActivated = new CountDownLatch ( 1 );
50
62
51
- private static void testKey (int keyCode , String keyText ) {
52
- keyPressReceived = false ;
53
- keyReleaseReceived = false ;
63
+ private static final List <Error > failures = new ArrayList <>(4 );
64
+ private static final AtomicReference <Exception > edtException = new AtomicReference <>();
54
65
66
+ private static void testKey (int keyCode , String keyText ) throws Exception {
55
67
robot .keyPress (keyCode );
56
-
57
- if (!keyPressReceived ) {
58
- failures .append (keyText ).append (" key press is not received\n " );
68
+ try {
69
+ keyPress .await (2 , SECONDS );
70
+ } catch (TimeoutException e ) {
71
+ keyPress .reset ();
72
+ failures .add (new Error (keyText + " key press is not received" , e ));
59
73
}
60
74
61
75
robot .keyRelease (keyCode );
62
-
63
- if (!keyReleaseReceived ) {
64
- failures .append (keyText ).append (" key release is not received\n " );
76
+ try {
77
+ keyRelease .await (2 , SECONDS );
78
+ } catch (TimeoutException e ) {
79
+ keyRelease .reset ();
80
+ failures .add (new Error (keyText + " key release is not received" , e ));
65
81
}
66
82
}
67
83
68
84
public static void main (String [] args ) throws Exception {
69
85
robot = new Robot ();
70
- robot .setAutoWaitForIdle (true );
71
86
robot .setAutoDelay (150 );
72
87
73
88
try {
74
89
EventQueue .invokeAndWait (() -> {
75
90
frame = new FunctionKeyTester ();
76
91
frame .setSize (200 , 200 );
77
92
frame .setLocationRelativeTo (null );
93
+ frame .addWindowListener (new WindowAdapter () {
94
+ @ Override
95
+ public void windowActivated (WindowEvent e ) {
96
+ System .out .println ("frame.windowActivated" );
97
+ frameActivated .countDown ();
98
+ }
99
+ });
78
100
frame .setVisible (true );
79
101
});
80
102
81
- robot .waitForIdle ();
82
- robot .delay (1000 );
103
+ if (!frameActivated .await (2 , SECONDS )) {
104
+ throw new Error ("Frame wasn't activated" );
105
+ }
106
+ robot .delay (100 );
83
107
84
108
testKey (KeyEvent .VK_F11 , "F11" );
85
109
testKey (KeyEvent .VK_F12 , "F12" );
@@ -91,46 +115,69 @@ public static void main(String[] args) throws Exception {
91
115
});
92
116
}
93
117
94
- if (failures .isEmpty ()) {
95
- System .out .println ("Passed" );
96
- } else {
97
- throw new RuntimeException (failures .toString ());
118
+ if (!failures .isEmpty ()) {
119
+ System .err .println ("Failures detected:" );
120
+ failures .forEach (System .err ::println );
121
+ if (edtException .get () != null ) {
122
+ System .err .println ("\n Exception on EDT:" );
123
+ edtException .get ().printStackTrace ();
124
+ }
125
+ System .err .println ();
126
+ throw new RuntimeException ("Test failed: " + failures .get (0 ).getMessage (),
127
+ failures .get (0 ));
98
128
}
99
- }
100
- }
101
129
102
- class FunctionKeyTester extends Frame {
103
- Label l = new Label ("NULL" );
104
- Button b = new Button ();
105
- TextArea log = new TextArea ();
106
-
107
- FunctionKeyTester () {
108
- super ("Function Key Test" );
109
- this .setLayout (new BorderLayout ());
110
- this .add (BorderLayout .NORTH , l );
111
- this .add (BorderLayout .SOUTH , b );
112
- this .add (BorderLayout .CENTER , log );
113
- log .setFocusable (false );
114
- log .setEditable (false );
115
- l .setBackground (Color .red );
116
- setSize (200 , 200 );
130
+ if (edtException .get () != null ) {
131
+ throw new RuntimeException ("Test failed because of exception on EDT" ,
132
+ edtException .get ());
133
+ }
117
134
}
118
135
119
- public boolean handleEvent (Event e ) {
120
- String message = "e.id=" + e .id + "\n " ;
121
- System .out .print (message );
122
- log .append (message );
123
-
124
- switch (e .id ) {
125
- case 403 -> FunctionKeyTest .keyPressReceived = true ;
126
- case 404 -> FunctionKeyTest .keyReleaseReceived = true ;
136
+ private static final class FunctionKeyTester extends Frame {
137
+ Label l = new Label ("NULL" );
138
+ Button b = new Button ("button" );
139
+ TextArea log = new TextArea ();
140
+
141
+ FunctionKeyTester () {
142
+ super ("Function Key Test" );
143
+ this .setLayout (new BorderLayout ());
144
+ this .add (BorderLayout .NORTH , l );
145
+ this .add (BorderLayout .SOUTH , b );
146
+ this .add (BorderLayout .CENTER , log );
147
+ log .setFocusable (false );
148
+ log .setEditable (false );
149
+ l .setBackground (Color .red );
150
+ setSize (200 , 200 );
127
151
}
128
152
129
- return super .handleEvent (e );
130
- }
153
+ @ Override
154
+ @ SuppressWarnings ("deprecation" )
155
+ public boolean handleEvent (Event e ) {
156
+ String message = "e.id=" + e .id + "\n " ;
157
+ System .out .print (message );
158
+ log .append (message );
159
+
160
+ try {
161
+ switch (e .id ) {
162
+ case KEY_ACTION
163
+ -> keyPress .await ();
164
+ case KEY_ACTION_RELEASE
165
+ -> keyRelease .await ();
166
+ }
167
+ } catch (Exception ex ) {
168
+ if (!edtException .compareAndSet (null , ex )) {
169
+ edtException .get ().addSuppressed (ex );
170
+ }
171
+ }
131
172
132
- public boolean keyDown (Event e , int key ) {
133
- l .setText ("e.key=" + Integer .valueOf (e .key ).toString ());
134
- return false ;
173
+ return super .handleEvent (e );
174
+ }
175
+
176
+ @ Override
177
+ @ SuppressWarnings ("deprecation" )
178
+ public boolean keyDown (Event e , int key ) {
179
+ l .setText ("e.key=" + e .key );
180
+ return false ;
181
+ }
135
182
}
136
183
}
0 commit comments