Skip to content

Commit 1d9bf98

Browse files
committed
8339995: Open source several AWT focus tests - series 6
Backport-of: 3411f9dff79c2e7cb7ce8ebf036f8b3fd9bb647d
1 parent 337b601 commit 1d9bf98

File tree

5 files changed

+580
-0
lines changed

5 files changed

+580
-0
lines changed

test/jdk/ProblemList.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,4 +817,5 @@ java/awt/FullScreen/TranslucentWindow/TranslucentWindow.java 8258103 linux-all
817817
java/awt/Focus/FrameMinimizeTest/FrameMinimizeTest.java 8016266 linux-x64
818818
java/awt/Frame/SizeMinimizedTest.java 8305915 linux-x64
819819
java/awt/PopupMenu/PopupHangTest.java 8340022 windows-all
820+
java/awt/Focus/InactiveFocusRace.java 8023263 linux-all
820821
java/awt/dnd/WinMoveFileToShellTest.java 8341665 windows-all
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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 4700276
27+
* @summary Peers process KeyEvents before KeyEventPostProcessors
28+
* @requires (os.family == "windows")
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual ConsumedKeyEventTest
32+
*/
33+
34+
import java.awt.Canvas;
35+
import java.awt.Component;
36+
import java.awt.Color;
37+
import java.awt.Dimension;
38+
import java.awt.FlowLayout;
39+
import java.awt.Frame;
40+
import java.awt.Graphics;
41+
import java.awt.KeyboardFocusManager;
42+
import java.awt.KeyEventPostProcessor;
43+
import java.awt.event.KeyEvent;
44+
import java.awt.event.FocusAdapter;
45+
import java.awt.event.FocusEvent;
46+
import java.awt.event.FocusListener;
47+
import java.awt.event.MouseAdapter;
48+
import java.awt.event.MouseEvent;
49+
50+
public class ConsumedKeyEventTest implements KeyEventPostProcessor {
51+
52+
private static final String INSTRUCTIONS = """
53+
This is a Windows-only test.
54+
When the test starts, you will see a Frame with two components in it,
55+
components look like colored rectangles, one of them is lightweight, one is heavyweight.
56+
Do the following:
57+
1. Click the mouse on the left component.
58+
If it isn't yellow after the click (that means it doesn't have focus), the test fails.
59+
2. Press and release ALT key.
60+
In the output window, the text should appear stating that those key events were consumed.
61+
If no output appears, the test fails.
62+
3. Press space bar. If system menu drops down, the test fails.
63+
4. Click the right rectangle.
64+
It should become red after the click. If it doesn't, it means that it didn't get the focus, and the test fails.
65+
5. Repeat steps 2. and 3.
66+
6. If the test didn't fail on any of the previous steps, the test passes.""";
67+
68+
69+
public static void main(String[] args) throws Exception {
70+
PassFailJFrame.builder()
71+
.title("ConsumedKeyEventTest Instructions")
72+
.instructions(INSTRUCTIONS)
73+
.rows((int) INSTRUCTIONS.lines().count() + 5)
74+
.columns(35)
75+
.testUI(ConsumedKeyEventTest::createTestUI)
76+
.logArea()
77+
.build()
78+
.awaitAndCheck();
79+
}
80+
81+
private static Frame createTestUI() {
82+
KeyboardFocusManager.getCurrentKeyboardFocusManager().
83+
addKeyEventPostProcessor((e) -> {
84+
System.out.println("postProcessor(" + e + ")");
85+
// consumes all ALT-events
86+
if (e.getKeyCode() == KeyEvent.VK_ALT) {
87+
println("consumed " + e);
88+
e.consume();
89+
return true;
90+
}
91+
return false;
92+
});
93+
FocusRequestor requestor = new FocusRequestor();
94+
Frame frame = new Frame("Main Frame");
95+
frame.setLayout(new FlowLayout());
96+
97+
Canvas canvas = new CustomCanvas();
98+
canvas.addMouseListener(requestor);
99+
frame.add(canvas);
100+
canvas.requestFocus();
101+
102+
Component lwComp = new LWComponent();
103+
lwComp.addMouseListener(requestor);
104+
frame.add(lwComp);
105+
106+
frame.pack();
107+
108+
return frame;
109+
}
110+
111+
public boolean postProcessKeyEvent(KeyEvent e) {
112+
System.out.println("postProcessor(" + e + ")");
113+
// consumes all ALT-events
114+
if (e.getKeyCode() == KeyEvent.VK_ALT) {
115+
println("consumed " + e);
116+
e.consume();
117+
return true;
118+
}
119+
return false;
120+
}
121+
122+
static void println(String messageIn) {
123+
PassFailJFrame.log(messageIn);
124+
}
125+
}// class ConsumedKeyEventTest
126+
127+
class CustomCanvas extends Canvas {
128+
CustomCanvas() {
129+
super();
130+
setName("HWComponent");
131+
setSize(100, 100);
132+
addFocusListener(new FocusAdapter() {
133+
public void focusGained(FocusEvent fe) {
134+
repaint();
135+
}
136+
137+
public void focusLost(FocusEvent fe) {
138+
repaint();
139+
}
140+
});
141+
}
142+
143+
public void paint(Graphics g) {
144+
if (isFocusOwner()) {
145+
g.setColor(Color.YELLOW);
146+
} else {
147+
g.setColor(Color.GREEN);
148+
}
149+
g.fillRect(0, 0, 100, 100);
150+
}
151+
152+
}
153+
154+
class LWComponent extends Component {
155+
LWComponent() {
156+
super();
157+
setName("LWComponent");
158+
addFocusListener(new FocusAdapter() {
159+
public void focusGained(FocusEvent fe) {
160+
repaint();
161+
}
162+
163+
public void focusLost(FocusEvent fe) {
164+
repaint();
165+
}
166+
});
167+
}
168+
169+
public Dimension getPreferredSize() {
170+
return new Dimension(100, 100);
171+
}
172+
173+
public void paint(Graphics g) {
174+
if (isFocusOwner()) {
175+
g.setColor(Color.RED);
176+
} else {
177+
g.setColor(Color.BLACK);
178+
}
179+
g.fillRect(0, 0, 100, 100);
180+
}
181+
182+
}
183+
184+
class FocusRequestor extends MouseAdapter {
185+
static int counter = 0;
186+
public void mouseClicked(MouseEvent me) {
187+
System.out.println("mouseClicked on " + me.getComponent().getName());
188+
}
189+
public void mousePressed(MouseEvent me) {
190+
System.out.println("mousePressed on " + me.getComponent().getName());
191+
me.getComponent().requestFocus();
192+
}
193+
public void mouseReleased(MouseEvent me) {
194+
System.out.println("mouseReleased on " + me.getComponent().getName());
195+
}
196+
}
197+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2001, 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 4464723
27+
* @summary Tests simple KeyAdapter / KeyListener on an empty, focusable window
28+
* @key headful
29+
* @run main EmptyWindowKeyTest
30+
*/
31+
32+
import java.awt.AWTEvent;
33+
import java.awt.Frame;
34+
import java.awt.event.KeyAdapter;
35+
import java.awt.event.KeyEvent;
36+
import java.awt.Robot;
37+
38+
public class EmptyWindowKeyTest {
39+
40+
static volatile boolean passed1, passed2;
41+
42+
public static void main(String[] args) throws Exception {
43+
Robot robot = new Robot();
44+
robot.setAutoDelay(100);
45+
MainFrame mainFrame = new MainFrame();
46+
mainFrame.setSize(50,50);
47+
mainFrame.addKeyListener(new KeyboardTracker());
48+
robot.waitForIdle();
49+
robot.delay(1000);
50+
robot.keyPress(KeyEvent.VK_A);
51+
robot.keyRelease(KeyEvent.VK_A);
52+
robot.waitForIdle();
53+
robot.delay(1000);
54+
if (!passed1 || !passed2) {
55+
throw new RuntimeException("KeyPress/keyRelease not seen," +
56+
"passed1 " + passed1 + " passed2 " + passed2);
57+
}
58+
}
59+
60+
static public class KeyboardTracker extends KeyAdapter {
61+
public KeyboardTracker() { }
62+
public void keyTyped(KeyEvent e) {}
63+
64+
public void keyPressed(KeyEvent e) {
65+
if (e.getKeyText(e.getKeyCode()).equals("A")) {
66+
passed1 = true;
67+
}
68+
}
69+
public void keyReleased(KeyEvent e) {
70+
if (e.getKeyText(e.getKeyCode()).equals("A")) {
71+
passed2 = true;
72+
}
73+
}
74+
}
75+
76+
static public class MainFrame extends Frame {
77+
78+
public MainFrame() {
79+
super();
80+
enableEvents(AWTEvent.KEY_EVENT_MASK);
81+
setVisible(true);
82+
}
83+
84+
}
85+
86+
}
87+

0 commit comments

Comments
 (0)