Skip to content

Commit f37f64d

Browse files
author
Damon Nguyen
committedDec 3, 2024
8343736: Test java/awt/Focus/UnaccessibleChoice/AccessibleChoiceTest.java failed: Choice can't be controlled by keyboard
Reviewed-by: honkar, abhiscxk
1 parent 73b8b34 commit f37f64d

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed
 

‎test/jdk/java/awt/Focus/UnaccessibleChoice/AccessibleChoiceTest.java

+54-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,87 +23,93 @@
2323

2424
import java.awt.Button;
2525
import java.awt.Choice;
26+
import java.awt.EventQueue;
2627
import java.awt.FlowLayout;
2728
import java.awt.Frame;
2829
import java.awt.GraphicsConfiguration;
2930
import java.awt.GraphicsEnvironment;
3031
import java.awt.Point;
3132
import java.awt.Robot;
32-
import java.awt.Window;
3333
import java.awt.event.FocusAdapter;
3434
import java.awt.event.FocusEvent;
3535
import java.awt.event.InputEvent;
3636
import java.awt.event.KeyEvent;
3737
import java.awt.image.BufferedImage;
3838
import java.io.File;
39-
import java.io.IOException;
4039
import java.util.concurrent.CountDownLatch;
4140
import java.util.concurrent.TimeUnit;
4241

4342
import javax.imageio.ImageIO;
4443

45-
/**
44+
/*
4645
* @test
4746
* @bug 4478780
4847
* @key headful
4948
* @summary Tests that Choice can be accessed and controlled by keyboard.
5049
*/
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-
}
6350

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 {
6561
try {
66-
init();
67-
start();
62+
createAndShowUI();
63+
test();
6864
} finally {
69-
if (frame != null) frame.dispose();
70-
if (win != null) win.dispose();
65+
if (frame != null) {
66+
EventQueue.invokeAndWait(() -> frame.dispose());
67+
}
7168
}
7269
}
7370

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() {
7880
public void focusGained(FocusEvent e) {
7981
go.countDown();
8082
}
8183
});
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+
});
8591
}
8692

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;
9495
try {
9596
robot = new Robot();
9697
} catch (Exception ex) {
9798
throw new RuntimeException("Can't create robot");
9899
}
99100
robot.waitForIdle();
100101
robot.delay(1000);
101-
robot.setAutoDelay(150);
102102
robot.setAutoWaitForIdle(true);
103103

104104
// 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);
107113
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
108114
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
109115

@@ -113,25 +119,30 @@ public void start () throws IOException {
113119
throw new RuntimeException("Interrupted !!!");
114120
}
115121

116-
if (!def.isFocusOwner()) {
122+
if (!button.isFocusOwner()) {
117123
throw new RuntimeException("Button doesn't have focus");
118124
}
119125

126+
robot.delay(500);
127+
120128
// Press Tab key to move focus to Choice
121129
robot.keyPress(KeyEvent.VK_TAB);
122130
robot.keyRelease(KeyEvent.VK_TAB);
123131

124132
robot.delay(500);
125133

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
127139
// If bug exists we won't be able to do so
128140
robot.keyPress(KeyEvent.VK_DOWN);
129141
robot.keyRelease(KeyEvent.VK_DOWN);
130142

131-
robot.delay(500);
132-
133143
String osName = System.getProperty("os.name").toLowerCase();
134144
if (osName.startsWith("mac")) {
145+
robot.delay(500);
135146
robot.keyPress(KeyEvent.VK_DOWN);
136147
robot.keyRelease(KeyEvent.VK_DOWN);
137148
robot.delay(500);
@@ -142,7 +153,7 @@ public void start () throws IOException {
142153
robot.delay(1000);
143154

144155
// On success second item should be selected
145-
if (choice.getSelectedItem() != choice.getItem(1)) {
156+
if (!choice.getSelectedItem().equals(choice.getItem(1))) {
146157
// Print out os name to check if mac conditional is relevant
147158
System.err.println("Failed on os: " + osName);
148159

0 commit comments

Comments
 (0)
Please sign in to comment.