Skip to content

Commit 44a1813

Browse files
committed
8341258: Open source few various AWT tests - Set1
Backport-of: 86e3d52c70a611975da3abdebd2e1f14c7a1d019
1 parent 49900d8 commit 44a1813

File tree

3 files changed

+428
-0
lines changed

3 files changed

+428
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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 4546123
27+
* @summary CardLayout becomes unusable after deleting an element
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual RemoveComponentTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.CardLayout;
35+
import java.awt.Color;
36+
import java.awt.Frame;
37+
import java.awt.Insets;
38+
import java.awt.Menu;
39+
import java.awt.MenuBar;
40+
import java.awt.MenuItem;
41+
import java.awt.Panel;
42+
import java.awt.event.ActionEvent;
43+
import java.awt.event.ActionListener;
44+
45+
import javax.swing.JLabel;
46+
import javax.swing.JPanel;
47+
48+
public class RemoveComponentTest {
49+
public static void main(String[] args) throws Exception {
50+
String INSTRUCTIONS = """
51+
You should see a frame titled "Test Frame For
52+
RemoveComponentTest". Try to select a few different panels from
53+
the second menu. Make sure your last choice is the red panel.
54+
Then click close (in first menu). After that you should be able
55+
to select any panels except red one.
56+
If that is the case, the test passes. Otherwise, the test failed.
57+
""";
58+
59+
PassFailJFrame.builder()
60+
.title("Test Instructions")
61+
.instructions(INSTRUCTIONS)
62+
.columns(35)
63+
.testUI(RemoveComponentTest::createUI)
64+
.logArea(5)
65+
.build()
66+
.awaitAndCheck();
67+
}
68+
69+
public static Frame createUI() {
70+
TestFrame frame = new TestFrame();
71+
frame.setSize(200, 200);
72+
return frame;
73+
}
74+
}
75+
76+
class TestFrame extends Frame implements ActionListener {
77+
public Panel aPanel;
78+
public TestPanel pageRed;
79+
public TestPanel pageGreen;
80+
public TestPanel pageBlue;
81+
public String currentSelection = "";
82+
83+
public MenuItem mi;
84+
public CardLayout theCardLayout;
85+
86+
87+
public TestFrame() {
88+
super("Test Frame For RemoveComponentTest");
89+
90+
setBackground(Color.black);
91+
setLayout(new BorderLayout(5, 5));
92+
93+
MenuBar mb = new MenuBar();
94+
95+
Menu fileMenu = new Menu("File");
96+
Menu pageMenu = new Menu("Pages");
97+
98+
mi = new MenuItem("Close");
99+
mi.addActionListener(this);
100+
fileMenu.add(mi);
101+
102+
mi = new MenuItem("Red");
103+
mi.addActionListener(this);
104+
pageMenu.add(mi);
105+
106+
mi = new MenuItem("Green");
107+
mi.addActionListener(this);
108+
pageMenu.add(mi);
109+
110+
mi = new MenuItem("Blue");
111+
mi.addActionListener(this);
112+
pageMenu.add(mi);
113+
114+
mb.add(fileMenu);
115+
mb.add(pageMenu);
116+
117+
setMenuBar(mb);
118+
119+
aPanel = new Panel();
120+
theCardLayout = new CardLayout();
121+
122+
aPanel.setLayout(theCardLayout);
123+
124+
pageRed = new TestPanel("PageRed", Color.red);
125+
pageGreen = new TestPanel("PageGreen", Color.green);
126+
pageBlue = new TestPanel("PageBlue", Color.blue);
127+
128+
aPanel.add("PageRed", pageRed);
129+
aPanel.add("PageGreen", pageGreen);
130+
aPanel.add("PageBlue", pageBlue);
131+
132+
add("Center", aPanel);
133+
setSize(getPreferredSize());
134+
}
135+
136+
public Insets getInsets() {
137+
return new Insets(47, 9, 9, 9);
138+
}
139+
140+
public void actionPerformed(ActionEvent e) {
141+
if (e.getActionCommand().equals("Red")) {
142+
theCardLayout.show(aPanel, "PageRed");
143+
currentSelection = "PageRed";
144+
} else if (e.getActionCommand().equals("Green")) {
145+
theCardLayout.show(aPanel, "PageGreen");
146+
} else if (e.getActionCommand().equals("Blue")) {
147+
theCardLayout.show(aPanel, "PageBlue");
148+
} else if (e.getActionCommand().equals("Close")) {
149+
PassFailJFrame.log("Closing");
150+
151+
if (currentSelection.equals("PageRed")) {
152+
PassFailJFrame.log("Remove page red");
153+
theCardLayout.removeLayoutComponent(pageRed);
154+
}
155+
}
156+
}
157+
}
158+
159+
class TestPanel extends JPanel {
160+
private String pageName;
161+
162+
TestPanel(String pageName, Color color) {
163+
setBackground(color);
164+
add(new JLabel(pageName));
165+
}
166+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
/*
25+
* @test
26+
* @bug 4221201
27+
* @summary Test where the gradient drawn should remain in sync with the
28+
* rotating rectangle.
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual JerkyGradient
32+
*/
33+
34+
import java.awt.Color;
35+
import java.awt.Frame;
36+
import java.awt.GradientPaint;
37+
import java.awt.Graphics;
38+
import java.awt.Graphics2D;
39+
import java.awt.Paint;
40+
import java.awt.Panel;
41+
import java.awt.RenderingHints;
42+
import java.awt.Shape;
43+
import java.awt.geom.Rectangle2D;
44+
import java.awt.image.BufferedImage;
45+
46+
public class JerkyGradient extends Panel implements Runnable {
47+
protected static Shape mShape;
48+
protected static Paint mPaint;
49+
protected static double mTheta;
50+
static Thread animatorThread;
51+
static BufferedImage mImg;
52+
53+
public static void main(String[] args) throws Exception {
54+
String INSTRUCTIONS = """
55+
Watch at least one full rotation of the rectangle. Check that
56+
the gradient drawn remains in sync with the rotating
57+
rectangle. If so, pass this test. Otherwise, fail this test.
58+
""";
59+
60+
PassFailJFrame.builder()
61+
.title("Test Instructions")
62+
.instructions(INSTRUCTIONS)
63+
.columns(35)
64+
.testUI(JerkyGradient::createUI)
65+
.build()
66+
.awaitAndCheck();
67+
}
68+
69+
public static Frame createUI() {
70+
Frame f = new Frame("Rotating Gradient Test");
71+
JerkyGradient jg = new JerkyGradient();
72+
f.add(jg);
73+
f.setSize(200, 200);
74+
return f;
75+
}
76+
77+
public JerkyGradient() {
78+
mShape = new Rectangle2D.Double(60, 50, 80, 100);
79+
mPaint = new GradientPaint(0, 0, Color.red,
80+
25, 25, Color.yellow,
81+
true);
82+
mImg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
83+
84+
animatorThread = new Thread(this);
85+
animatorThread.setPriority(Thread.MIN_PRIORITY);
86+
animatorThread.start();
87+
}
88+
89+
public synchronized void run() {
90+
Thread me = Thread.currentThread();
91+
double increment = Math.PI / 36;
92+
double twoPI = Math.PI * 2;
93+
94+
while (animatorThread == me) {
95+
mTheta = (mTheta + increment) % twoPI;
96+
repaint();
97+
try {
98+
wait(50);
99+
} catch (InterruptedException ie) {
100+
break;
101+
}
102+
}
103+
}
104+
105+
public void update(Graphics g) {
106+
Graphics2D g2 = mImg.createGraphics();
107+
g2.setColor(getBackground());
108+
g2.fillRect(0, 0, 200, 200);
109+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
110+
RenderingHints.VALUE_ANTIALIAS_ON);
111+
g2.rotate(mTheta, 100, 100);
112+
g2.setPaint(Color.black);
113+
g2.drawLine(100, 30, 100, 55);
114+
g2.setPaint(mPaint);
115+
g2.fill(mShape);
116+
g2.setPaint(Color.black);
117+
g2.draw(mShape);
118+
paint(g);
119+
g2.dispose();
120+
}
121+
122+
public void paint(Graphics g) {
123+
g.drawImage(mImg, 0, 0, null);
124+
}
125+
}

0 commit comments

Comments
 (0)