Skip to content

Commit 337b601

Browse files
committed
8340084: Open source AWT Frame related tests
Backport-of: bc7c0dc45dcd66d24ece8ebbd5c1b25e131eae67
1 parent c37177c commit 337b601

File tree

5 files changed

+393
-0
lines changed

5 files changed

+393
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 1998, 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.EventQueue;
25+
import java.awt.Frame;
26+
import java.awt.Label;
27+
28+
/*
29+
* @test
30+
* @bug 4085599
31+
* @library /java/awt/regtesthelpers
32+
* @build PassFailJFrame
33+
* @summary Test default location for frame
34+
* @run main/manual DefaultLocationTest
35+
*/
36+
37+
public class DefaultLocationTest {
38+
private static Frame f;
39+
40+
public static void main(String[] args) throws Exception {
41+
String INSTRUCTIONS = """
42+
A small frame containing the label 'Hello World' should
43+
appear in the upper left hand corner of the screen. The
44+
exact location is dependent upon the window manager.
45+
46+
On Linux and Mac machines, the default location for frame
47+
is below the taskbar or close to top-left corner.
48+
49+
Upon test completion, click Pass or Fail appropriately.""";
50+
51+
PassFailJFrame passFailJFrame = new PassFailJFrame("DefaultLocationTest " +
52+
" Instructions", INSTRUCTIONS, 5, 10, 40);
53+
EventQueue.invokeAndWait(DefaultLocationTest::createAndShowUI);
54+
passFailJFrame.awaitAndCheck();
55+
}
56+
57+
private static void createAndShowUI() {
58+
f = new Frame("DefaultLocation");
59+
f.add("Center", new Label("Hello World"));
60+
f.pack();
61+
PassFailJFrame.addTestWindow(f);
62+
PassFailJFrame.positionTestWindow(
63+
null, PassFailJFrame.Position.HORIZONTAL);
64+
f.setVisible(true);
65+
}
66+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.Color;
25+
import java.awt.Dimension;
26+
import java.awt.EventQueue;
27+
import java.awt.Frame;
28+
import java.awt.Point;
29+
import java.awt.Rectangle;
30+
import java.awt.Robot;
31+
import java.awt.image.BufferedImage;
32+
import java.io.File;
33+
import java.io.IOException;
34+
import javax.imageio.ImageIO;
35+
36+
/*
37+
* @test
38+
* @bug 4237529
39+
* @key headful
40+
* @summary Test repainting of an empty frame
41+
* @run main EmptyFrameTest
42+
*/
43+
44+
public class EmptyFrameTest {
45+
private static Frame f;
46+
private static Robot robot;
47+
private static volatile Point p;
48+
private static volatile Dimension d;
49+
private static final int TOLERANCE = 5;
50+
public static void main(String[] args) throws Exception {
51+
robot = new Robot();
52+
robot.setAutoDelay(100);
53+
try {
54+
EventQueue.invokeAndWait(() -> {
55+
createAndShowUI();
56+
});
57+
robot.delay(1000);
58+
f.setSize(50, 50);
59+
robot.delay(500);
60+
EventQueue.invokeAndWait(() -> {
61+
p = f.getLocation();
62+
d = f.getSize();
63+
});
64+
Rectangle rect = new Rectangle(p, d);
65+
BufferedImage img = robot.createScreenCapture(rect);
66+
if (chkImgBackgroundColor(img)) {
67+
try {
68+
ImageIO.write(img, "png", new File("Frame.png"));
69+
} catch (IOException ignored) {}
70+
throw new RuntimeException("Frame doesn't repaint itself on resize");
71+
}
72+
} finally {
73+
EventQueue.invokeAndWait(() -> {
74+
if (f != null) {
75+
f.dispose();
76+
}
77+
});
78+
}
79+
}
80+
81+
private static void createAndShowUI() {
82+
f = new Frame("EmptyFrameTest");
83+
f.setUndecorated(true);
84+
f.setBackground(Color.RED);
85+
f.setVisible(true);
86+
}
87+
88+
private static boolean chkImgBackgroundColor(BufferedImage img) {
89+
for (int x = 1; x < img.getWidth() - 1; ++x) {
90+
for (int y = 1; y < img.getHeight() - 1; ++y) {
91+
Color c = new Color(img.getRGB(x, y));
92+
if ((c.getRed() - Color.RED.getRed()) > TOLERANCE) {
93+
return true;
94+
}
95+
}
96+
}
97+
return false;
98+
}
99+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.BorderLayout;
25+
import java.awt.Button;
26+
import java.awt.Frame;
27+
28+
/*
29+
* @test
30+
* @bug 4173503
31+
* @library /java/awt/regtesthelpers
32+
* @requires (os.family == "windows")
33+
* @build PassFailJFrame
34+
* @summary Tests that frame layout is performed when frame is maximized from taskbar
35+
* @run main/manual FrameLayoutTest
36+
*/
37+
38+
public class FrameLayoutTest {
39+
40+
public static void main(String[] args) throws Exception {
41+
String INSTRUCTIONS = """
42+
Right-click on the taskbar button for this test. In the menu appeared,
43+
choose Maximize. The frame will be maximized. Check if buttons inside
44+
the frame are laid out properly, i.e. they occupy the frame entirely.
45+
46+
If so, test passes. If buttons occupy small rectangle in the top left
47+
corner, test fails.""";
48+
49+
PassFailJFrame.builder()
50+
.title("Frame's Layout Test Instruction")
51+
.instructions(INSTRUCTIONS)
52+
.rows((int) INSTRUCTIONS.lines().count() + 2)
53+
.columns(40)
54+
.testUI(FrameLayoutTest::createUI)
55+
.build()
56+
.awaitAndCheck();
57+
}
58+
59+
private static Frame createUI() {
60+
Frame f = new Frame("Maximize Test");
61+
f.add(new Button("North"), BorderLayout.NORTH);
62+
f.add(new Button("South"), BorderLayout.SOUTH);
63+
f.add(new Button("East"), BorderLayout.EAST);
64+
f.add(new Button("West"), BorderLayout.WEST);
65+
f.add(new Button("Cent"), BorderLayout.CENTER);
66+
f.pack();
67+
f.setState(Frame.ICONIFIED);
68+
return f;
69+
}
70+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2005, 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.Dimension;
25+
import java.awt.EventQueue;
26+
import java.awt.Frame;
27+
28+
/*
29+
* @test
30+
* @bug 4320050
31+
* @key headful
32+
* @summary Minimum size for java.awt.Frame is not being enforced.
33+
* @run main FrameSetMinimumSizeTest
34+
*/
35+
36+
public class FrameSetMinimumSizeTest {
37+
private static Frame f;
38+
private static volatile boolean passed;
39+
40+
public static void main(String[] args) throws Exception {
41+
EventQueue.invokeAndWait(() -> {
42+
try {
43+
createAndShowUI();
44+
45+
f.setSize(200, 200);
46+
passed = verifyFrameSize(new Dimension(300, 300));
47+
isFrameSizeOk(passed);
48+
49+
f.setSize(200, 400);
50+
passed = verifyFrameSize(new Dimension(300, 400));
51+
isFrameSizeOk(passed);
52+
53+
f.setSize(400, 200);
54+
passed = verifyFrameSize(new Dimension(400, 300));
55+
isFrameSizeOk(passed);
56+
57+
f.setMinimumSize(null);
58+
59+
f.setSize(200, 200);
60+
passed = verifyFrameSize(new Dimension(200, 200));
61+
isFrameSizeOk(passed);
62+
} finally {
63+
if (f != null) {
64+
f.dispose();
65+
}
66+
}
67+
});
68+
}
69+
70+
private static void createAndShowUI() {
71+
f = new Frame("Minimum Size Test");
72+
f.setSize(300, 300);
73+
f.setMinimumSize(new Dimension(300, 300));
74+
f.setVisible(true);
75+
}
76+
77+
private static boolean verifyFrameSize(Dimension expected) {
78+
79+
if (f.getSize().width != expected.width || f.getSize().height != expected.height) {
80+
return false;
81+
}
82+
return true;
83+
}
84+
85+
private static void isFrameSizeOk(boolean passed) {
86+
if (!passed) {
87+
throw new RuntimeException("Frame's setMinimumSize not honoured for the" +
88+
" frame size: " + f.getSize());
89+
}
90+
}
91+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
import java.awt.Frame;
25+
import java.awt.TextField;
26+
27+
/*
28+
* @test
29+
* @bug 4097744
30+
* @library /java/awt/regtesthelpers
31+
* @build PassFailJFrame
32+
* @summary packing a frame twice stops it resizing
33+
* @run main/manual PackTwiceTest
34+
*/
35+
36+
public class PackTwiceTest {
37+
public static void main(String[] args) throws Exception {
38+
String INSTRUCTIONS = """
39+
1. You would see a Frame titled 'TestFrame'
40+
2. The Frame displays a text as below:
41+
'I am a lengthy sentence...can you see me?'
42+
3. If you can see the full text without resizing the frame
43+
using mouse, press 'Pass' else press 'Fail'.""";
44+
45+
PassFailJFrame.builder()
46+
.title("PackTwiceTest Instruction")
47+
.instructions(INSTRUCTIONS)
48+
.rows((int) INSTRUCTIONS.lines().count() + 2)
49+
.columns(40)
50+
.testUI(PackTwiceTest::createUI)
51+
.build()
52+
.awaitAndCheck();
53+
}
54+
55+
private static Frame createUI() {
56+
Frame f = new Frame("PackTwiceTest TestFrame");
57+
TextField tf = new TextField();
58+
f.add(tf, "Center");
59+
tf.setText("I am a short sentence");
60+
f.pack();
61+
f.pack();
62+
tf.setText("I am a lengthy sentence...can you see me?");
63+
f.pack();
64+
f.requestFocus();
65+
return f;
66+
}
67+
}

0 commit comments

Comments
 (0)