Skip to content

Commit 65ff527

Browse files
committed
8339906: Open source several AWT focus tests - series 4
Backport-of: 46b02f49bcc730d94e37cf17fa996fdd12bdb990
1 parent df6cdbe commit 65ff527

File tree

4 files changed

+526
-0
lines changed

4 files changed

+526
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 2002, 2024, 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 4524015
27+
* @summary Tests that when user switches between windows using Alt-tab then the appropriate events are generated
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual AltTabEventsTest
31+
*/
32+
33+
import java.awt.Button;
34+
import java.awt.Choice;
35+
import java.awt.Component;
36+
import java.awt.FlowLayout;
37+
import java.awt.Frame;
38+
import java.awt.Menu;
39+
import java.awt.MenuBar;
40+
import java.awt.MenuItem;
41+
import java.awt.PopupMenu;
42+
import java.awt.event.ActionListener;
43+
import java.awt.event.ActionEvent;
44+
import java.awt.event.WindowAdapter;
45+
import java.awt.event.WindowEvent;
46+
47+
public class AltTabEventsTest {
48+
49+
private static final String INSTRUCTIONS = """
50+
This test verifies that when user switches between windows using Alt-tab
51+
key combination then appropriate window events are generated. Also, when
52+
user interacts with Menu bar, Popup menu, Choice then no excessive window
53+
event is generated.
54+
55+
After test started you will see Frame('Test for 4524015')-F1 with some
56+
components and Frame('Another frame')-F2 with no components.
57+
1. Make F1 active by clicking on it.
58+
2. Press Alt-tab.
59+
In the messqge dialog area you should see that
60+
WINDOW_DEACTIVATED, WINDOW_LOST_FOCUS event were generated.
61+
If you switched to F2 then also WINDOW_ACTIVATED, WINDOW_GAINED_FOCUS
62+
were generated.
63+
If no events were generated the test FAILED.
64+
Repeat the 2) with different circumstances.
65+
66+
3. Make F1 active by clicking on it.
67+
4. Click on Menu bar/Button 'popup'/Choice and select some item from
68+
the list shown. If any of the window events appeared in the output then
69+
the test FAILED.
70+
71+
else the test PASSED.""";
72+
73+
public static void main(String[] args) throws Exception {
74+
PassFailJFrame.builder()
75+
.title("AltTabEventsTest Instructions")
76+
.instructions(INSTRUCTIONS)
77+
.rows((int) INSTRUCTIONS.lines().count() + 5)
78+
.columns(35)
79+
.testUI(Test::new)
80+
.logArea()
81+
.build()
82+
.awaitAndCheck();
83+
}
84+
85+
}
86+
87+
88+
class Test extends Frame {
89+
PopupMenu pop;
90+
Frame f;
91+
92+
void println(String messageIn) {
93+
PassFailJFrame.log(messageIn);
94+
}
95+
96+
public Test() {
97+
super("Test for 4524015");
98+
WindowAdapter wa = new WindowAdapter() {
99+
public void windowActivated(WindowEvent e) {
100+
println(e.toString());
101+
}
102+
public void windowDeactivated(WindowEvent e) {
103+
println(e.toString());
104+
}
105+
public void windowGainedFocus(WindowEvent e) {
106+
println(e.toString());
107+
}
108+
public void windowLostFocus(WindowEvent e) {
109+
println(e.toString());
110+
}
111+
};
112+
addWindowListener(wa);
113+
addWindowFocusListener(wa);
114+
115+
f = new Frame("Another frame");
116+
f.addWindowListener(wa);
117+
f.addWindowFocusListener(wa);
118+
f.setBounds(800, 300, 300, 100);
119+
f.setVisible(true);
120+
121+
setLayout(new FlowLayout());
122+
Button b = new Button("popup");
123+
add(b);
124+
b.addActionListener(new ActionListener() {
125+
public void actionPerformed(ActionEvent e) {
126+
pop.show((Component)e.getSource(), 10, 10);
127+
}
128+
});
129+
Choice cho = new Choice();
130+
add(cho);
131+
cho.addItem("1");
132+
cho.addItem("2");
133+
cho.addItem("3");
134+
135+
MenuBar bar = new MenuBar();
136+
Menu menu = new Menu("menu");
137+
MenuItem item = new MenuItem("first");
138+
menu.add(item);
139+
item = new MenuItem("second");
140+
menu.add(item);
141+
bar.add(menu);
142+
setMenuBar(bar);
143+
144+
pop = new PopupMenu();
145+
pop.add("1");
146+
pop.add("@");
147+
add(pop);
148+
setSize(300, 100);
149+
}
150+
}
151+
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2004, 2024, 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 4982943
27+
* @key headful
28+
* @summary focus lost in text fields or text areas, unable to enter characters from keyboard
29+
* @run main ComponentLostFocusTest
30+
*/
31+
32+
import java.awt.Dialog;
33+
import java.awt.EventQueue;
34+
import java.awt.FlowLayout;
35+
import java.awt.Frame;
36+
import java.awt.KeyboardFocusManager;
37+
import java.awt.Point;
38+
import java.awt.Robot;
39+
import java.awt.TextField;
40+
import java.awt.event.FocusAdapter;
41+
import java.awt.event.FocusEvent;
42+
import java.awt.event.InputEvent;
43+
import java.awt.event.WindowAdapter;
44+
import java.awt.event.WindowEvent;
45+
46+
public class ComponentLostFocusTest {
47+
48+
static Frame frame;
49+
static TextField tf;
50+
static Robot r;
51+
static Dialog dialog = null;
52+
static volatile boolean passed;
53+
static volatile Point loc;
54+
static volatile int width;
55+
static volatile int top;
56+
57+
private static void createTestUI() {
58+
59+
dialog = new Dialog(frame, "Dialog", true);
60+
61+
frame = new Frame("ComponentLostFocusTest Frame");
62+
frame.setLayout(new FlowLayout());
63+
frame.addWindowFocusListener(new WindowAdapter() {
64+
public void windowGainedFocus(WindowEvent e) {
65+
System.out.println("Frame gained focus: "+e);
66+
}
67+
});
68+
tf = new TextField("Text Field");
69+
frame.add(tf);
70+
frame.setSize(400,300);
71+
frame.setVisible(true);
72+
frame.setLocationRelativeTo(null);
73+
frame.validate();
74+
}
75+
76+
public static void doTest() {
77+
System.out.println("dialog.setVisible.... ");
78+
new Thread(new Runnable() {
79+
public void run() {
80+
dialog.setVisible(true);
81+
}
82+
}).start();
83+
84+
// The bug is that this construction leads to the redundant xRequestFocus
85+
// By the way, the requestFocusInWindow() works fine before the fix
86+
System.out.println("requesting.... ");
87+
frame.requestFocus();
88+
89+
r.delay(1000);
90+
91+
// Returning the focus to the initial frame will work correctly after the fix
92+
System.out.println("disposing.... ");
93+
dialog.dispose();
94+
95+
r.delay(1000);
96+
97+
// We want to track the GAIN_FOCUS from this time
98+
tf.addFocusListener(new FocusAdapter() {
99+
public void focusGained(FocusEvent e) {
100+
System.out.println("TextField gained focus: " + e);
101+
passed = true;
102+
}
103+
});
104+
105+
}
106+
107+
private static void doRequestFocusToTextField() {
108+
// do activation using press title
109+
r.mouseMove(loc.x + width / 2, loc.y + top / 2);
110+
r.waitForIdle();
111+
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
112+
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
113+
r.waitForIdle();
114+
115+
// request focus to the text field
116+
tf.requestFocus();
117+
}
118+
119+
public static final void main(String args[]) throws Exception {
120+
r = new Robot();
121+
r.setAutoDelay(100);
122+
123+
EventQueue.invokeAndWait(() -> createTestUI());
124+
r.waitForIdle();
125+
r.delay(1000);
126+
try {
127+
EventQueue.invokeAndWait(() -> {
128+
doTest();
129+
loc = frame.getLocationOnScreen();
130+
width = frame.getWidth();
131+
top = frame.getInsets().top;
132+
});
133+
doRequestFocusToTextField();
134+
135+
System.out.println("Focused window: " +
136+
KeyboardFocusManager.getCurrentKeyboardFocusManager().
137+
getFocusedWindow());
138+
System.out.println("Focus owner: " +
139+
KeyboardFocusManager.getCurrentKeyboardFocusManager().
140+
getFocusOwner());
141+
142+
if (!passed) {
143+
throw new RuntimeException("TextField got no focus! Test failed.");
144+
}
145+
} finally {
146+
EventQueue.invokeAndWait(() -> {
147+
if (frame != null) {
148+
frame.dispose();
149+
}
150+
});
151+
}
152+
}
153+
}
154+

0 commit comments

Comments
 (0)