Skip to content

Commit 966eb72

Browse files
author
Alexander Zvegintsev
committedOct 8, 2024
8341447: Open source closed frame tests # 5
Reviewed-by: honkar
1 parent b9db74a commit 966eb72

File tree

3 files changed

+200
-8
lines changed

3 files changed

+200
-8
lines changed
 

‎test/jdk/ProblemList.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ java/awt/Focus/AutoRequestFocusTest/AutoRequestFocusToFrontTest.java 6848406 gen
121121
java/awt/Focus/AutoRequestFocusTest/AutoRequestFocusSetVisibleTest.java 6848407 generic-all
122122
java/awt/Frame/MaximizedUndecorated/MaximizedUndecorated.java 8022302 generic-all
123123
java/awt/Frame/RestoreToOppositeScreen/RestoreToOppositeScreen.java 8286840 linux-all
124-
java/awt/Frame/InitialIconifiedTest.java 8203920 macosx-all,linux-all
124+
java/awt/Frame/InitialIconifiedTest.java 7144049,8203920 macosx-all,linux-all
125125
java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java 8341370 macosx-all
126+
java/awt/Frame/FocusTest.java 8341480 macosx-all
126127
java/awt/FileDialog/FileDialogIconTest/FileDialogIconTest.java 8160558 windows-all
127128
java/awt/event/MouseWheelEvent/InfiniteRecursion/InfiniteRecursion.java 8060176 windows-all,macosx-all
128129
java/awt/event/MouseWheelEvent/InfiniteRecursion/InfiniteRecursion_1.java 8060176 windows-all,macosx-all
+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 1999, 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+
import java.awt.Canvas;
25+
import java.awt.Color;
26+
import java.awt.Dimension;
27+
import java.awt.Frame;
28+
import java.awt.Graphics;
29+
import java.awt.GridLayout;
30+
import java.awt.Panel;
31+
import java.awt.Window;
32+
import java.awt.event.FocusEvent;
33+
import java.awt.event.FocusListener;
34+
import java.awt.event.MouseAdapter;
35+
import java.awt.event.MouseEvent;
36+
import java.awt.event.WindowAdapter;
37+
import java.awt.event.WindowEvent;
38+
39+
/*
40+
* @test
41+
* @bug 4140293
42+
* @summary Tests that focus is returned to the correct Component when a Frame
43+
* is reactivated.
44+
* @library /java/awt/regtesthelpers
45+
* @build PassFailJFrame
46+
* @run main/manual FocusTest
47+
*/
48+
49+
public class FocusTest {
50+
private static final String INSTRUCTIONS = """
51+
Click on the bottom rectangle. Move the mouse slightly.
52+
A focus rectangle should appear around the bottom rectangle.
53+
54+
Now, deactivate the window and then reactivate it.
55+
(You would click on the caption bar of another window,
56+
and then on the caption bar of the FocusTest Frame.)
57+
58+
If the focus rectangle appears again, the test passes.
59+
If it does not appear, or appears around the top rectangle,
60+
the test fails.
61+
""";
62+
63+
public static void main(String[] args) throws Exception {
64+
PassFailJFrame.builder()
65+
.title("FocusTest Instructions")
66+
.instructions(INSTRUCTIONS)
67+
.columns(45)
68+
.logArea(6)
69+
.testUI(FocusTest::createAndShowUI)
70+
.build()
71+
.awaitAndCheck();
72+
}
73+
74+
private static Window createAndShowUI() {
75+
Frame frame = new Frame("FocusTest");
76+
77+
frame.add(new FocusTestPanel());
78+
frame.setSize(400, 400);
79+
80+
frame.addWindowListener(new WindowAdapter() {
81+
public void windowClosing(WindowEvent e) {
82+
frame.dispose();
83+
}
84+
});
85+
86+
frame.validate();
87+
return frame;
88+
}
89+
90+
private static class FocusTestPanel extends Panel {
91+
PassiveClient pc1 = new PassiveClient("pc1");
92+
PassiveClient pc2 = new PassiveClient("pc2");
93+
94+
public FocusTestPanel() {
95+
super();
96+
setLayout(new GridLayout(2, 1, 10, 10));
97+
add(pc1);
98+
add(pc2);
99+
}
100+
}
101+
102+
private static class PassiveClient extends Canvas implements FocusListener {
103+
boolean haveFocus = false;
104+
final String name;
105+
106+
PassiveClient(String name) {
107+
super();
108+
this.name = name;
109+
setSize(400, 100);
110+
setBackground(Color.cyan);
111+
setVisible(true);
112+
setEnabled(true);
113+
addMouseListener(new MouseAdapter() {
114+
@Override
115+
public void mouseClicked(MouseEvent e) {
116+
requestFocus();
117+
}
118+
});
119+
addFocusListener(this);
120+
}
121+
122+
public void paint(Graphics g) {
123+
g.setColor(getBackground());
124+
Dimension size = getSize();
125+
g.fillRect(0, 0, size.width, size.height);
126+
if (haveFocus) {
127+
g.setColor(Color.black);
128+
g.drawRect(0, 0, size.width - 1, size.height - 1);
129+
g.drawRect(1, 1, size.width - 3, size.height - 3);
130+
}
131+
g.setColor(getForeground());
132+
}
133+
134+
public void focusGained(FocusEvent event) {
135+
haveFocus = true;
136+
paint(getGraphics());
137+
PassFailJFrame.log("<<<< %s Got focus!! %s>>>>".formatted(this, event));
138+
}
139+
140+
public void focusLost(FocusEvent event) {
141+
haveFocus = false;
142+
paint(getGraphics());
143+
PassFailJFrame.log("<<<< %s Lost focus!! %s>>>>".formatted(this, event));
144+
}
145+
146+
@Override
147+
public String toString() {
148+
return "PassiveClient " + name;
149+
}
150+
}
151+
}

‎test/jdk/java/awt/Frame/InitialIconifiedTest.java

+47-7
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,75 @@ public class InitialIconifiedTest {
5050

5151
private static Robot robot;
5252

53+
private static final StringBuilder FAILURES = new StringBuilder();
54+
5355
public static void main(String[] args) throws Exception {
5456
robot = new Robot();
55-
5657
try {
57-
EventQueue.invokeAndWait(InitialIconifiedTest::initAndShowGui);
58+
EventQueue.invokeAndWait(InitialIconifiedTest::initAndShowBackground);
5859
robot.waitForIdle();
5960
robot.delay(500);
60-
test();
61+
62+
test(false);
63+
test(true);
6164
} finally {
6265
EventQueue.invokeAndWait(() -> {
6366
backgroundFrame.dispose();
6467
testedFrame.dispose();
6568
});
6669
}
70+
71+
if (!FAILURES.isEmpty()) {
72+
throw new RuntimeException(FAILURES.toString());
73+
}
74+
}
75+
76+
private static void test(boolean isUndecorated) throws Exception {
77+
String prefix = isUndecorated ? "undecorated" : "decorated";
78+
79+
EventQueue.invokeAndWait(() -> initAndShowTestedFrame(isUndecorated));
80+
// On macos, we can observe the animation of the window from the initial
81+
// NORMAL state to the ICONIFIED state,
82+
// even if the window was created in the ICONIFIED state.
83+
// The following delay is commented out to capture this animation
84+
// robot.waitForIdle();
85+
// robot.delay(500);
86+
if (!testIfIconified(prefix + "_no_extra_delay")) {
87+
FAILURES.append("Case %s frame with no extra delay failed\n"
88+
.formatted(isUndecorated ? "undecorated" : "decorated"));
89+
}
90+
91+
EventQueue.invokeAndWait(() -> initAndShowTestedFrame(isUndecorated));
92+
robot.waitForIdle();
93+
robot.delay(500);
94+
if (!testIfIconified(prefix + "_with_extra_delay")) {
95+
FAILURES.append("Case %s frame with extra delay failed\n"
96+
.formatted(isUndecorated ? "undecorated" : "decorated"));
97+
}
6798
}
6899

69-
private static void initAndShowGui() {
100+
private static void initAndShowBackground() {
70101
backgroundFrame = new Frame("DisposeTest background");
71102
backgroundFrame.setUndecorated(true);
72103
backgroundFrame.setBackground(Color.RED);
73104
backgroundFrame.setBounds(backgroundFrameBounds);
74105
backgroundFrame.setVisible(true);
106+
}
75107

108+
private static void initAndShowTestedFrame(boolean isUndecorated) {
109+
if (testedFrame != null) {
110+
testedFrame.dispose();
111+
}
76112
testedFrame = new Frame("Should have started ICONIC");
113+
if (isUndecorated) {
114+
testedFrame.setUndecorated(true);
115+
}
77116
testedFrame.setExtendedState(Frame.ICONIFIED);
78117
testedFrame.setBounds(testedFrameBounds);
79118
testedFrame.setVisible(true);
80119
}
81120

82-
private static void test() {
121+
private static boolean testIfIconified(String prefix) {
83122
BufferedImage bi = robot.createScreenCapture(backgroundFrameBounds);
84123
int redPix = Color.RED.getRGB();
85124

@@ -88,11 +127,12 @@ private static void test() {
88127
if (bi.getRGB(x, y) != redPix) {
89128
try {
90129
ImageIO.write(bi, "png",
91-
new File("failure.png"));
130+
new File(prefix + "_failure.png"));
92131
} catch (IOException ignored) {}
93-
throw new RuntimeException("Test failed");
132+
return false;
94133
}
95134
}
96135
}
136+
return true;
97137
}
98138
}

0 commit comments

Comments
 (0)
Please sign in to comment.