Skip to content

Commit 094cd26

Browse files
Andrew LuTheRealMDoerr
Andrew Lu
authored andcommitted
8307078: Opensource and clean up five more AWT Focus related tests
Backport-of: 6d6f726b74f7fcd3e7c37d50bd2476b3e921662b
1 parent cde3cd2 commit 094cd26

5 files changed

+965
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2004, 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 4722671
27+
@summary Accessibility problem in JRE Finder
28+
@key headful
29+
@run main FocusForRemovedComponentTest
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.BorderLayout;
34+
import java.awt.Button;
35+
import java.awt.Dimension;
36+
import java.awt.EventQueue;
37+
import java.awt.Frame;
38+
import java.awt.Point;
39+
import java.awt.Robot;
40+
41+
import java.awt.event.ActionEvent;
42+
import java.awt.event.ActionListener;
43+
import java.awt.event.FocusAdapter;
44+
import java.awt.event.FocusEvent;
45+
import java.awt.event.InputEvent;
46+
import java.lang.reflect.InvocationTargetException;
47+
import java.util.concurrent.atomic.AtomicBoolean;
48+
49+
public class FocusForRemovedComponentTest
50+
implements ActionListener {
51+
static int ACTIVATION_TIMEOUT = 2000;
52+
static long WAIT_TIMEOUT = 3000;
53+
volatile Frame frame;
54+
volatile Button btnFirst;
55+
volatile Button btnSecond;
56+
volatile Button btnThird;
57+
58+
public void start() throws InterruptedException, InvocationTargetException {
59+
try {
60+
EventQueue.invokeAndWait(() -> {
61+
frame = new Frame("FocusForRemovedComponentTest");
62+
btnFirst = new Button("First Button");
63+
btnSecond = new Button("Second Button");
64+
btnThird = new Button("Third Button");
65+
frame.add(btnFirst, BorderLayout.NORTH);
66+
frame.add(btnSecond, BorderLayout.CENTER);
67+
btnFirst.addActionListener(this);
68+
btnFirst.requestFocusInWindow();
69+
frame.pack();
70+
frame.setVisible(true);
71+
});
72+
73+
try {
74+
Robot robot = new Robot();
75+
robot.delay(ACTIVATION_TIMEOUT);
76+
int[] location = new int[2];
77+
EventQueue.invokeAndWait(() -> {
78+
Point button_location = btnFirst.getLocationOnScreen();
79+
Dimension button_size = btnFirst.getSize();
80+
location[0] = button_location.x + button_size.width / 2;
81+
location[1] = button_location.y + button_size.height / 2;
82+
});
83+
robot.mouseMove(location[0], location[1]);
84+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
85+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
86+
87+
Object monitor = new Object();
88+
final MonitoredFocusListener monitorer = new MonitoredFocusListener(monitor);
89+
AtomicBoolean isFocused = new AtomicBoolean(false);
90+
synchronized (monitor) {
91+
EventQueue.invokeAndWait(() -> {
92+
btnThird.addFocusListener(monitorer);
93+
isFocused.set(btnThird.isFocusOwner());
94+
});
95+
96+
if (!isFocused.get()) {
97+
monitor.wait(WAIT_TIMEOUT);
98+
EventQueue.invokeAndWait(() -> {
99+
isFocused.set(btnThird.isFocusOwner());
100+
});
101+
}
102+
}
103+
104+
if (!isFocused.get()) {
105+
throw new RuntimeException("TEST FAILED. The third button is not focus owner.");
106+
} else {
107+
System.out.println("TEST PASSED.");
108+
}
109+
} catch (AWTException e) {
110+
e.printStackTrace();
111+
throw new RuntimeException("Some AWTException occurred.");
112+
} catch (InterruptedException e) {
113+
e.printStackTrace();
114+
throw new RuntimeException("Test was interrupted.");
115+
}
116+
} finally {
117+
if (frame != null) {
118+
EventQueue.invokeAndWait(frame::dispose);
119+
}
120+
}
121+
}
122+
123+
public void actionPerformed(ActionEvent e) {
124+
if (btnSecond.isVisible()) {
125+
btnFirst.setEnabled(false);
126+
frame.remove(btnSecond);
127+
frame.add(btnThird, BorderLayout.CENTER);
128+
btnThird.requestFocusInWindow();
129+
btnFirst.setEnabled(true);
130+
frame.validate();
131+
frame.repaint();
132+
}
133+
}
134+
135+
public static void main(String[] args) throws InterruptedException,
136+
InvocationTargetException {
137+
FocusForRemovedComponentTest test = new FocusForRemovedComponentTest();
138+
test.start();
139+
}
140+
}
141+
142+
class MonitoredFocusListener extends FocusAdapter {
143+
Object monitor;
144+
145+
public MonitoredFocusListener(Object monitor) {
146+
this.monitor = monitor;
147+
}
148+
149+
public void focusGained(FocusEvent fe) {
150+
synchronized (monitor) {
151+
monitor.notify();
152+
}
153+
}
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 6225100
27+
@summary FocusTraversalPolicy.getInitialComponent does not work as expected
28+
@run main FocusTraversalPolicyIAE
29+
*/
30+
31+
import java.awt.Component;
32+
import java.awt.Container;
33+
import java.awt.FocusTraversalPolicy;
34+
35+
public class FocusTraversalPolicyIAE {
36+
public static void main(String[] args) {
37+
CustomFocusTraversalPolicy cftp = new CustomFocusTraversalPolicy();
38+
try {
39+
cftp.getInitialComponent(null);
40+
throw new RuntimeException("Test failed. No exceptions thrown.");
41+
} catch (IllegalArgumentException iae) {
42+
System.out.println("Test passed.");
43+
} catch (NullPointerException npe) {
44+
throw new RuntimeException("Test failed. Unexpected NPE thrown: " + npe);
45+
} catch (Exception e) {
46+
throw new RuntimeException("Test failed. Unexpected exception thrown: " + e);
47+
}
48+
}
49+
}
50+
51+
class CustomFocusTraversalPolicy extends FocusTraversalPolicy {
52+
public Component getComponentAfter(Container focusCycleRoot,
53+
Component aComponent) {
54+
return null;
55+
}
56+
57+
public Component getComponentBefore(Container focusCycleRoot,
58+
Component aComponent) {
59+
return null;
60+
}
61+
62+
public Component getDefaultComponent(Container focusCycleRoot) {
63+
return null;
64+
}
65+
66+
public Component getFirstComponent(Container focusCycleRoot) {
67+
return null;
68+
}
69+
70+
public Component getLastComponent(Container focusCycleRoot) {
71+
return null;
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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 4150021
27+
@summary if user requests focus on some component, it must become a focus owner after activation
28+
@key headful
29+
@run main InitialFocusTest
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.Button;
34+
import java.awt.DefaultKeyboardFocusManager;
35+
import java.awt.Dimension;
36+
import java.awt.EventQueue;
37+
import java.awt.FlowLayout;
38+
import java.awt.Frame;
39+
import java.awt.Insets;
40+
import java.awt.Point;
41+
import java.awt.Robot;
42+
import java.awt.event.InputEvent;
43+
import java.beans.PropertyChangeEvent;
44+
import java.beans.PropertyChangeListener;
45+
import java.lang.reflect.InvocationTargetException;
46+
import java.util.concurrent.atomic.AtomicBoolean;
47+
48+
public class InitialFocusTest implements PropertyChangeListener {
49+
//Declare things used in the test, like buttons and labels here
50+
final static String FOCUSED_WINDOW_PROP = "focusedWindow";
51+
final static int ACTION_TIMEOUT = 2000;
52+
53+
volatile Frame frame;
54+
volatile Button btn1;
55+
volatile Button btn2;
56+
57+
public void start() throws InterruptedException, InvocationTargetException {
58+
DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().
59+
addPropertyChangeListener(FOCUSED_WINDOW_PROP, this);
60+
try {
61+
EventQueue.invokeAndWait(() -> {
62+
frame = new Frame("InitialFocusTest");
63+
frame.setLayout(new FlowLayout());
64+
btn1 = new Button("First Button");
65+
frame.add(btn1);
66+
btn2 = new Button("Second Button");
67+
frame.add(btn2);
68+
frame.setLocationRelativeTo(null);
69+
frame.pack();
70+
frame.setVisible(true);
71+
});
72+
try {
73+
Robot robot = new Robot();
74+
robot.delay(ACTION_TIMEOUT);
75+
if (!activateFrame(frame, robot, ACTION_TIMEOUT)) {
76+
throw new RuntimeException("Frame was not activated.");
77+
}
78+
robot.delay(ACTION_TIMEOUT);
79+
AtomicBoolean isFocused = new AtomicBoolean(false);
80+
EventQueue.invokeAndWait(() -> {
81+
isFocused.set(frame.isFocused());
82+
});
83+
if (!isFocused.get()) {
84+
throw new RuntimeException("Frame didn't become focused.");
85+
}
86+
EventQueue.invokeAndWait(() -> {
87+
isFocused.set(btn2.isFocusOwner());
88+
});
89+
if (!isFocused.get()) {
90+
throw new RuntimeException("Btn2 didn't receive focus.");
91+
}
92+
} catch (AWTException e) {
93+
e.printStackTrace();
94+
}
95+
System.out.printf("Test passed.");
96+
} finally {
97+
if (frame != null) {
98+
EventQueue.invokeAndWait(frame::dispose);
99+
}
100+
}
101+
}
102+
103+
public void propertyChange(PropertyChangeEvent pce) {
104+
if (FOCUSED_WINDOW_PROP.equals(pce.getPropertyName())) {
105+
if (pce.getNewValue() == frame) {
106+
System.out.println("requesting focus on btn2");
107+
btn2.requestFocusInWindow();
108+
}
109+
}
110+
}
111+
112+
boolean activateFrame(Frame frame, Robot robot, int timeout)
113+
throws InterruptedException, InvocationTargetException {
114+
AtomicBoolean isActive = new AtomicBoolean(false);
115+
EventQueue.invokeAndWait(() -> {
116+
isActive.set(frame.isActive());
117+
});
118+
if (!isActive.get()) {
119+
int[] point = new int[2];
120+
EventQueue.invokeAndWait(() -> {
121+
Point origin = frame.getLocationOnScreen();
122+
Dimension dim = frame.getSize();
123+
Insets insets = frame.getInsets();
124+
point[0] = origin.x + dim.width / 2;
125+
point[1] = origin.y + insets.top / 2;
126+
});
127+
robot.mouseMove(point[0], point[1]);
128+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
129+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
130+
robot.waitForIdle();
131+
robot.delay(timeout);
132+
EventQueue.invokeAndWait(() -> {
133+
isActive.set(frame.isActive());
134+
});
135+
}
136+
return frame.isActive();
137+
}
138+
139+
public static void main(String[] args) throws InterruptedException,
140+
InvocationTargetException {
141+
InitialFocusTest test = new InitialFocusTest();
142+
test.start();
143+
}
144+
}

0 commit comments

Comments
 (0)