Skip to content

Commit 4031a88

Browse files
author
Andrew Lu
committed
8239801: [macos] java/awt/Focus/UnaccessibleChoice/AccessibleChoiceTest.java fails
8237222: [macos] java/awt/Focus/UnaccessibleChoice/AccessibleChoiceTest.java fails Reviewed-by: mdoerr Backport-of: b6dddf4ce6072416e17cadefbd8280f959fd93ca
1 parent 9a16bee commit 4031a88

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright (c) 2002, 2022, 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+
import java.awt.Button;
25+
import java.awt.Choice;
26+
import java.awt.FlowLayout;
27+
import java.awt.Frame;
28+
import java.awt.GraphicsConfiguration;
29+
import java.awt.GraphicsEnvironment;
30+
import java.awt.Point;
31+
import java.awt.Robot;
32+
import java.awt.Window;
33+
import java.awt.event.FocusAdapter;
34+
import java.awt.event.FocusEvent;
35+
import java.awt.event.InputEvent;
36+
import java.awt.event.KeyEvent;
37+
import java.awt.image.BufferedImage;
38+
import java.io.File;
39+
import java.io.IOException;
40+
import java.util.concurrent.CountDownLatch;
41+
import java.util.concurrent.TimeUnit;
42+
43+
import javax.imageio.ImageIO;
44+
45+
/**
46+
* @test
47+
* @bug 4478780
48+
* @key headful
49+
* @summary Tests that Choice can be accessed and controlled by keyboard.
50+
*/
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+
64+
private void test() throws IOException {
65+
try {
66+
init();
67+
start();
68+
} finally {
69+
if (frame != null) frame.dispose();
70+
if (win != null) win.dispose();
71+
}
72+
}
73+
74+
public void init() {
75+
win.setLayout (new FlowLayout ());
76+
win.add(def);
77+
def.addFocusListener(new FocusAdapter() {
78+
public void focusGained(FocusEvent e) {
79+
go.countDown();
80+
}
81+
});
82+
choice.add("One");
83+
choice.add("Two");
84+
win.add(choice);
85+
}
86+
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;
94+
try {
95+
robot = new Robot();
96+
} catch (Exception ex) {
97+
throw new RuntimeException("Can't create robot");
98+
}
99+
robot.waitForIdle();
100+
robot.delay(1000);
101+
robot.setAutoDelay(150);
102+
robot.setAutoWaitForIdle(true);
103+
104+
// Focus default button and wait till it gets focus
105+
Point loc = def.getLocationOnScreen();
106+
robot.mouseMove(loc.x+2, loc.y+2);
107+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
108+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
109+
110+
try {
111+
go.await(1, TimeUnit.SECONDS);
112+
} catch (InterruptedException ie) {
113+
throw new RuntimeException("Interrupted !!!");
114+
}
115+
116+
if (!def.isFocusOwner()) {
117+
throw new RuntimeException("Button doesn't have focus");
118+
}
119+
120+
// Press Tab key to move focus to Choice
121+
robot.keyPress(KeyEvent.VK_TAB);
122+
robot.keyRelease(KeyEvent.VK_TAB);
123+
124+
robot.delay(500);
125+
126+
// Press Down key to select next item in the choice(Motif 2.1)
127+
// If bug exists we won't be able to do so
128+
robot.keyPress(KeyEvent.VK_DOWN);
129+
robot.keyRelease(KeyEvent.VK_DOWN);
130+
131+
robot.delay(500);
132+
133+
String osName = System.getProperty("os.name").toLowerCase();
134+
if (osName.startsWith("mac")) {
135+
robot.keyPress(KeyEvent.VK_DOWN);
136+
robot.keyRelease(KeyEvent.VK_DOWN);
137+
robot.delay(500);
138+
robot.keyPress(KeyEvent.VK_ENTER);
139+
robot.keyRelease(KeyEvent.VK_ENTER);
140+
}
141+
142+
robot.delay(1000);
143+
144+
// On success second item should be selected
145+
if (choice.getSelectedItem() != choice.getItem(1)) {
146+
// Print out os name to check if mac conditional is relevant
147+
System.err.println("Failed on os: " + osName);
148+
149+
// Save image to better debug the status of test when failing
150+
GraphicsConfiguration ge = GraphicsEnvironment
151+
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
152+
.getDefaultConfiguration();
153+
BufferedImage failImage = robot.createScreenCapture(ge.getBounds());
154+
ImageIO.write(failImage, "png", new File("failImage.png"));
155+
156+
throw new RuntimeException("Choice can't be controlled by keyboard");
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)