Skip to content

Commit 073d1a3

Browse files
Victor RudometovPaul Hohensee
authored andcommitted
8306954: Open source five Focus related tests
Backport-of: 6d6d00b69cea47ccbe05a844db0fb6c384045caa
1 parent 2dc22d6 commit 073d1a3

File tree

5 files changed

+962
-0
lines changed

5 files changed

+962
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright (c) 2001, 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 4394789
27+
@summary KeyboardFocusManager.upFocusCycle is not working for Swing properly
28+
@key headful
29+
@run main AsyncUpFocusCycleTest
30+
*/
31+
32+
33+
import javax.swing.DefaultFocusManager;
34+
import javax.swing.JButton;
35+
import javax.swing.JFrame;
36+
import java.awt.AWTException;
37+
import java.awt.BorderLayout;
38+
import java.awt.Color;
39+
import java.awt.Container;
40+
import java.awt.DefaultKeyboardFocusManager;
41+
import java.awt.EventQueue;
42+
import java.awt.Insets;
43+
import java.awt.Point;
44+
import java.awt.Robot;
45+
import java.awt.event.FocusAdapter;
46+
import java.awt.event.FocusEvent;
47+
import java.awt.event.InputEvent;
48+
import java.lang.reflect.InvocationTargetException;
49+
50+
public class AsyncUpFocusCycleTest {
51+
volatile boolean isFailed = true;
52+
Object sema = new Object();
53+
JFrame frame;
54+
Point location;
55+
JButton button;
56+
Insets insets;
57+
int width;
58+
59+
public void start() throws InterruptedException,
60+
InvocationTargetException {
61+
try {
62+
Robot robot = new Robot();
63+
robot.mouseMove(100, 100);
64+
65+
EventQueue.invokeAndWait(() -> {
66+
frame = new JFrame("AsyncUpFocusCycleTest") {
67+
public void requestFocus() {
68+
boolean ret = super.requestFocus(false);
69+
System.err.println("requestFocus() on Frame " + ret);
70+
}
71+
72+
protected boolean requestFocus(boolean temporary) {
73+
boolean ret = super.requestFocus(temporary);
74+
System.err.println("requestFocus(" + temporary + ") on Frame " + ret);
75+
return ret;
76+
}
77+
78+
public boolean requestFocusInWindow() {
79+
boolean ret = super.requestFocusInWindow();
80+
System.err.println("requestFocusInWindow() on Frame " + ret);
81+
return ret;
82+
}
83+
84+
protected boolean requestFocusInWindow(boolean temporary) {
85+
boolean ret = super.requestFocusInWindow(temporary);
86+
System.err.println("requestFocusInWindow(" + temporary + ") on Frame " + ret);
87+
return ret;
88+
}
89+
};
90+
91+
Container container1 = frame.getContentPane();
92+
container1.setBackground(Color.yellow);
93+
94+
button = new JButton("Button") {
95+
public void requestFocus() {
96+
boolean ret = super.requestFocus(false);
97+
System.err.println("requestFocus() on Button " + ret);
98+
}
99+
100+
public boolean requestFocus(boolean temporary) {
101+
boolean ret = super.requestFocus(temporary);
102+
System.err.println("requestFocus(" + temporary + ") on Button " + ret);
103+
return ret;
104+
}
105+
106+
public boolean requestFocusInWindow() {
107+
boolean ret = super.requestFocusInWindow();
108+
System.err.println("requestFocusInWindow() on Button " + ret);
109+
return ret;
110+
}
111+
112+
protected boolean requestFocusInWindow(boolean temporary) {
113+
boolean ret = super.requestFocusInWindow(temporary);
114+
System.err.println("requestFocusInWindow(" + temporary + ") on Button " + ret);
115+
return ret;
116+
}
117+
};
118+
button.addFocusListener(new FocusAdapter() {
119+
public void focusGained(FocusEvent fe) {
120+
System.out.println("Button receive focus");
121+
frame.addFocusListener(new FocusAdapter() {
122+
public void focusGained(FocusEvent fe) {
123+
System.out.println("Frame receive focus");
124+
synchronized (sema) {
125+
isFailed = false;
126+
sema.notifyAll();
127+
}
128+
}
129+
});
130+
}
131+
});
132+
container1.add(new JButton("empty button"), BorderLayout.WEST);
133+
container1.add(button, BorderLayout.EAST);
134+
frame.setBounds(0, 0, 300, 300);
135+
frame.setVisible(true);
136+
});
137+
138+
robot.delay(2000);
139+
robot.waitForIdle();
140+
141+
EventQueue.invokeAndWait(() -> {
142+
location = frame.getLocationOnScreen();
143+
insets = frame.getInsets();
144+
width = frame.getWidth();
145+
});
146+
147+
robot.mouseMove(location.x + width / 2, location.y + insets.top / 2);
148+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
149+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
150+
DefaultKeyboardFocusManager manager = new DefaultFocusManager();
151+
robot.delay(1000);
152+
EventQueue.invokeAndWait(button::requestFocus);
153+
robot.delay(1000);
154+
EventQueue.invokeAndWait(() -> {
155+
manager.upFocusCycle(button);
156+
});
157+
158+
try {
159+
synchronized (sema) {
160+
sema.wait(5000);
161+
}
162+
163+
if (isFailed) {
164+
System.out.println("Test FAILED");
165+
throw new RuntimeException("Test FAILED");
166+
} else {
167+
System.out.println("Test PASSED");
168+
}
169+
} catch (InterruptedException ie) {
170+
throw new RuntimeException("Test was interrupted");
171+
}
172+
} catch (AWTException e) {
173+
System.out.println("Problem creating Robot.");
174+
} finally {
175+
if (frame != null) {
176+
EventQueue.invokeAndWait(frame::dispose);
177+
}
178+
}
179+
}
180+
181+
public static void main(String[] args) throws InterruptedException,
182+
InvocationTargetException {
183+
AsyncUpFocusCycleTest test = new AsyncUpFocusCycleTest();
184+
test.start();
185+
}
186+
}

0 commit comments

Comments
 (0)