1
1
/*
2
- * Copyright (c) 2002, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2002, 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
23
23
24
24
import java .awt .Button ;
25
25
import java .awt .Choice ;
26
+ import java .awt .EventQueue ;
26
27
import java .awt .FlowLayout ;
27
28
import java .awt .Frame ;
28
29
import java .awt .GraphicsConfiguration ;
29
30
import java .awt .GraphicsEnvironment ;
30
31
import java .awt .Point ;
31
32
import java .awt .Robot ;
32
- import java .awt .Window ;
33
33
import java .awt .event .FocusAdapter ;
34
34
import java .awt .event .FocusEvent ;
35
35
import java .awt .event .InputEvent ;
36
36
import java .awt .event .KeyEvent ;
37
37
import java .awt .image .BufferedImage ;
38
38
import java .io .File ;
39
- import java .io .IOException ;
40
39
import java .util .concurrent .CountDownLatch ;
41
40
import java .util .concurrent .TimeUnit ;
42
41
43
42
import javax .imageio .ImageIO ;
44
43
45
- /**
44
+ /*
46
45
* @test
47
46
* @bug 4478780
48
47
* @key headful
49
48
* @summary Tests that Choice can be accessed and controlled by keyboard.
50
49
*/
51
- public class AccessibleChoiceTest {
52
- //Declare things used in the test, like buttons and labels here
53
- Frame frame = new Frame ("window owner" );
54
- Window win = new Window (frame );
55
- Choice choice = new Choice ();
56
- Button def = new Button ("default owner" );
57
- CountDownLatch go = new CountDownLatch (1 );
58
-
59
- public static void main (final String [] args ) throws IOException {
60
- AccessibleChoiceTest app = new AccessibleChoiceTest ();
61
- app .test ();
62
- }
63
50
64
- private void test () throws IOException {
51
+ public class AccessibleChoiceTest {
52
+ static Frame frame ;
53
+ static Choice choice ;
54
+ static Button button ;
55
+ static volatile CountDownLatch go ;
56
+ static volatile Point loc ;
57
+ static volatile int bWidth ;
58
+ static volatile int bHeight ;
59
+
60
+ public static void main (final String [] args ) throws Exception {
65
61
try {
66
- init ();
67
- start ();
62
+ createAndShowUI ();
63
+ test ();
68
64
} finally {
69
- if (frame != null ) frame .dispose ();
70
- if (win != null ) win .dispose ();
65
+ if (frame != null ) {
66
+ EventQueue .invokeAndWait (() -> frame .dispose ());
67
+ }
71
68
}
72
69
}
73
70
74
- public void init () {
75
- win .setLayout (new FlowLayout ());
76
- win .add (def );
77
- def .addFocusListener (new FocusAdapter () {
71
+ public static void createAndShowUI () throws Exception {
72
+ go = new CountDownLatch (1 );
73
+ EventQueue .invokeAndWait (() -> {
74
+ frame = new Frame ("Accessible Choice Test Frame" );
75
+ choice = new Choice ();
76
+ button = new Button ("default owner" );
77
+ frame .setLayout (new FlowLayout ());
78
+ frame .add (button );
79
+ button .addFocusListener (new FocusAdapter () {
78
80
public void focusGained (FocusEvent e ) {
79
81
go .countDown ();
80
82
}
81
83
});
82
- choice .add ("One" );
83
- choice .add ("Two" );
84
- win .add (choice );
84
+ choice .add ("One" );
85
+ choice .add ("Two" );
86
+ frame .add (choice );
87
+ frame .pack ();
88
+ frame .setLocationRelativeTo (null );
89
+ frame .setVisible (true );
90
+ });
85
91
}
86
92
87
- public void start () throws IOException {
88
- frame .setVisible (true );
89
- win .pack ();
90
- win .setLocation (100 , 200 );
91
- win .setVisible (true );
92
-
93
- Robot robot = null ;
93
+ public static void test () throws Exception {
94
+ Robot robot ;
94
95
try {
95
96
robot = new Robot ();
96
97
} catch (Exception ex ) {
97
98
throw new RuntimeException ("Can't create robot" );
98
99
}
99
100
robot .waitForIdle ();
100
101
robot .delay (1000 );
101
- robot .setAutoDelay (150 );
102
102
robot .setAutoWaitForIdle (true );
103
103
104
104
// Focus default button and wait till it gets focus
105
- Point loc = def .getLocationOnScreen ();
106
- robot .mouseMove (loc .x +2 , loc .y +2 );
105
+ EventQueue .invokeAndWait (() -> {
106
+ loc = button .getLocationOnScreen ();
107
+ bWidth = button .getWidth ();
108
+ bHeight = button .getHeight ();
109
+ });
110
+ robot .mouseMove (loc .x + bWidth / 2 ,
111
+ loc .y + bHeight / 2 );
112
+ robot .delay (500 );
107
113
robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
108
114
robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
109
115
@@ -113,25 +119,30 @@ public void start () throws IOException {
113
119
throw new RuntimeException ("Interrupted !!!" );
114
120
}
115
121
116
- if (!def .isFocusOwner ()) {
122
+ if (!button .isFocusOwner ()) {
117
123
throw new RuntimeException ("Button doesn't have focus" );
118
124
}
119
125
126
+ robot .delay (500 );
127
+
120
128
// Press Tab key to move focus to Choice
121
129
robot .keyPress (KeyEvent .VK_TAB );
122
130
robot .keyRelease (KeyEvent .VK_TAB );
123
131
124
132
robot .delay (500 );
125
133
126
- // Press Down key to select next item in the choice(Motif 2.1)
134
+ if (!choice .isFocusOwner ()) {
135
+ throw new RuntimeException ("Choice doesn't have focus" );
136
+ }
137
+
138
+ // Press Down key to select next item in the choice
127
139
// If bug exists we won't be able to do so
128
140
robot .keyPress (KeyEvent .VK_DOWN );
129
141
robot .keyRelease (KeyEvent .VK_DOWN );
130
142
131
- robot .delay (500 );
132
-
133
143
String osName = System .getProperty ("os.name" ).toLowerCase ();
134
144
if (osName .startsWith ("mac" )) {
145
+ robot .delay (500 );
135
146
robot .keyPress (KeyEvent .VK_DOWN );
136
147
robot .keyRelease (KeyEvent .VK_DOWN );
137
148
robot .delay (500 );
@@ -142,7 +153,7 @@ public void start () throws IOException {
142
153
robot .delay (1000 );
143
154
144
155
// On success second item should be selected
145
- if (choice .getSelectedItem () != choice .getItem (1 )) {
156
+ if (! choice .getSelectedItem (). equals ( choice .getItem (1 ) )) {
146
157
// Print out os name to check if mac conditional is relevant
147
158
System .err .println ("Failed on os: " + osName );
148
159
0 commit comments