Skip to content

Commit 8a03f71

Browse files
committed
8340332: Open source mixed AWT tests - Set3
Backport-of: bfdeb33e6f1d4f9f0cc65925ea792be98b1f4d61
1 parent 0e663da commit 8a03f71

File tree

3 files changed

+364
-0
lines changed

3 files changed

+364
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright (c) 2003, 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.EventQueue;
27+
import java.awt.Frame;
28+
import java.awt.Point;
29+
import java.awt.Robot;
30+
import java.awt.event.ComponentAdapter;
31+
import java.awt.event.ComponentEvent;
32+
import java.util.concurrent.CountDownLatch;
33+
import java.util.concurrent.TimeUnit;
34+
35+
/*
36+
* @test
37+
* @key headful
38+
* @bug 4009555
39+
* @summary Unit test for a new method in Container class: getMousePosition(boolean)
40+
* while Container resized.
41+
*/
42+
43+
public class ContainerResizeMousePositionTest {
44+
private static Frame frame;
45+
private static Button button;
46+
private static Robot robot;
47+
private static volatile Point frameLocation;
48+
private static volatile Point newLoc;
49+
private static boolean testSucceeded = false;
50+
51+
private static final CountDownLatch eventCaught = new CountDownLatch(1);
52+
53+
public static void main(String[] args) throws Exception {
54+
try {
55+
robot = new Robot();
56+
EventQueue.invokeAndWait(() -> createAndShowUI());
57+
robot.waitForIdle();
58+
robot.delay(1000);
59+
testUI();
60+
} finally {
61+
EventQueue.invokeAndWait(() -> {
62+
if (frame != null) {
63+
frame.dispose();
64+
}
65+
});
66+
}
67+
}
68+
69+
private static void createAndShowUI() {
70+
frame = new Frame("Testing getMousePosition() after resize");
71+
button = new Button("Button");
72+
frame.setLayout(new BorderLayout());
73+
frame.add(button);
74+
frame.setSize(200, 200);
75+
frame.setLocationRelativeTo(null);
76+
frame.setVisible(true);
77+
}
78+
79+
private static void testUI() throws Exception {
80+
EventQueue.invokeAndWait(() -> {
81+
frameLocation = frame.getLocationOnScreen();
82+
newLoc = new Point(frame.getWidth() + 10, frame.getHeight() + 10);
83+
});
84+
85+
robot.mouseMove(frameLocation.x + newLoc.x, frameLocation.y + newLoc.y);
86+
EventQueue.invokeAndWait(() -> {
87+
button.addComponentListener(new ResizeAdapter());
88+
frame.setSize(frame.getWidth() * 2, frame.getHeight() * 2);
89+
frame.validate();
90+
});
91+
robot.waitForIdle();
92+
robot.delay(500);
93+
94+
if (!eventCaught.await(2, TimeUnit.SECONDS)) {
95+
throw new RuntimeException("componentResized Event isn't"
96+
+ " received within a timeout");
97+
}
98+
99+
if (!testSucceeded) {
100+
throw new RuntimeException("Container.getMousePosition(boolean)"
101+
+ " returned incorrect result while Container resized");
102+
}
103+
}
104+
105+
static class ResizeAdapter extends ComponentAdapter {
106+
int testStageCounter = 0;
107+
@Override
108+
public void componentResized(ComponentEvent e) {
109+
Point pTrue = frame.getMousePosition(true);
110+
if (frame.getMousePosition(false) == null) {
111+
testStageCounter++;
112+
System.out.println("""
113+
TEST STAGE 1 PASSED:
114+
Container.getMousePosition(false)
115+
returned NULL over Child Component
116+
during resize.
117+
""");
118+
}
119+
if (pTrue != null) {
120+
testStageCounter++;
121+
System.out.println("""
122+
TEST STAGE 2 PASSED:
123+
Container.getMousePosition(true)
124+
returned NON-NULL over Child Component
125+
during resize.
126+
""");
127+
}
128+
if (pTrue != null && pTrue.x == newLoc.x && pTrue.y == newLoc.y) {
129+
testStageCounter++;
130+
System.out.println("""
131+
TEST STAGE 3 PASSED:
132+
Container.getMousePosition(true)
133+
returned correct result over Child Component
134+
during resize.
135+
""");
136+
}
137+
testSucceeded = testStageCounter == 3;
138+
eventCaught.countDown();
139+
}
140+
}
141+
}
10.5 KB
Loading
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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.Button;
25+
import java.awt.Canvas;
26+
import java.awt.Checkbox;
27+
import java.awt.Choice;
28+
import java.awt.Component;
29+
import java.awt.Frame;
30+
import java.awt.GridLayout;
31+
import java.awt.Image;
32+
import java.awt.Label;
33+
import java.awt.Menu;
34+
import java.awt.MenuBar;
35+
import java.awt.MenuItem;
36+
import java.awt.Panel;
37+
import java.awt.PopupMenu;
38+
import java.awt.ScrollPane;
39+
import java.awt.Scrollbar;
40+
import java.awt.TextArea;
41+
import java.awt.TextField;
42+
import java.awt.Window;
43+
import java.awt.event.MouseAdapter;
44+
import java.awt.event.MouseEvent;
45+
import java.io.File;
46+
import java.util.ArrayList;
47+
import java.util.List;
48+
49+
import javax.swing.ImageIcon;
50+
import javax.swing.JLabel;
51+
52+
/*
53+
* @test
54+
* @bug 5092883 6513478 7154025
55+
* @requires (os.family == "linux")
56+
* @summary REGRESSION: SystemColor class gives back wrong values under Linux
57+
* @library /java/awt/regtesthelpers
58+
* @build PassFailJFrame
59+
* @run main/manual XAWTDifference
60+
*/
61+
62+
public class XAWTDifference {
63+
64+
private static final String INSTRUCTIONS = """
65+
You would see a frame with title "XAWTDifference Test Frame".
66+
67+
Test Frame (1)
68+
69+
a) It has three columns in it. The 1st one with ordinary components.
70+
The 2nd one with disabled components.
71+
The 3rd one with uneditable components (only text components
72+
are there). Verify that the difference between different states
73+
is visible.
74+
75+
Standard Frame (2)
76+
77+
b) You would also see a frame named StandardFrame (2)
78+
with a lot of components in it. Actually this is just a jpg-image
79+
in a frame. Verify that every component in the frame (1) looks
80+
similar to the same component in (2).
81+
82+
They might differ in colors and be darker or brighter but
83+
the whole picture should be the same.
84+
85+
c) Also check the color of the MenuBar Items in the MenuBar and
86+
the PopupMenu assigned to TextArea.
87+
As you can't compare the colors of menu items with the picture
88+
so just look if the are adequate enough.
89+
""";
90+
private static final int HGAP = 20;
91+
92+
public static void main(String[] args) throws Exception {
93+
PassFailJFrame.builder()
94+
.title("Test Instructions")
95+
.instructions(INSTRUCTIONS)
96+
.columns(40)
97+
.testUI(XAWTDifference::createAndShowUI)
98+
.positionTestUI(XAWTDifference::positionMultiTestUI)
99+
.build()
100+
.awaitAndCheck();
101+
}
102+
103+
private static Panel addComponentsIntoPanel(boolean enabled, boolean editable) {
104+
TextField tf = new TextField("TextField");
105+
TextArea ta = new TextArea("TextArea", 10, 10);
106+
107+
Choice levelChooser = new Choice();
108+
levelChooser.add("Item #1");
109+
levelChooser.add("Item #2");
110+
111+
Button b = new Button("BUTTON");
112+
Label label = new Label("LABEL");
113+
java.awt.List list = new java.awt.List(4, false);
114+
list.add("one");
115+
list.add("two");
116+
list.add("three");
117+
118+
Checkbox chb = new Checkbox();
119+
Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL);
120+
ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
121+
sp.add(new TextArea("this is a textarea in scrollpane"));
122+
sp.setSize(200, 200);
123+
Canvas canvas = new Canvas();
124+
canvas.setSize(100, 100);
125+
126+
//add popup menu to Button
127+
final PopupMenu pm = new PopupMenu();
128+
MenuItem i1 = new MenuItem("Item1");
129+
MenuItem i2 = new MenuItem("Item2");
130+
MenuItem i3 = new MenuItem("Item3");
131+
i3.setEnabled(false);
132+
pm.add(i1);
133+
pm.add(i2);
134+
pm.add(i3);
135+
canvas.add(pm);
136+
137+
ta.add(pm);
138+
ta.addMouseListener(new MouseAdapter() {
139+
public void mousePressed(MouseEvent me) {
140+
if (me.isPopupTrigger()) {
141+
pm.show(me.getComponent(), me.getX(), me.getY());
142+
}
143+
}
144+
});
145+
146+
ArrayList<Component> componentList = new ArrayList<>();
147+
148+
componentList.add(tf);
149+
componentList.add(ta);
150+
if (editable){
151+
componentList.add(levelChooser);
152+
componentList.add(b);
153+
componentList.add(label);
154+
componentList.add(list);
155+
componentList.add(chb);
156+
componentList.add(sb);
157+
componentList.add(sp);
158+
componentList.add(canvas);
159+
} else {
160+
tf.setEditable(false);
161+
ta.setEditable(false);
162+
}
163+
164+
Panel panel = new Panel();
165+
panel.setLayout(new GridLayout(0, 1));
166+
for (Component c : componentList) {
167+
if (!enabled) {
168+
c.setEnabled(false);
169+
}
170+
panel.add(c);
171+
}
172+
return panel;
173+
}
174+
175+
private static List<Window> createAndShowUI() {
176+
Frame testFrame = new Frame("XAWTDifference Test Frame");
177+
StandardFrame standardFrame = new StandardFrame("StandardFrame");
178+
standardFrame.pack();
179+
180+
testFrame.setLayout(new GridLayout(1, 3));
181+
testFrame.add(addComponentsIntoPanel(true, true));
182+
testFrame.add(addComponentsIntoPanel(false, true));
183+
testFrame.add(addComponentsIntoPanel(true, false));
184+
185+
MenuItem mi1 = new MenuItem("Item1");
186+
MenuItem mi2 = new MenuItem("Item2");
187+
MenuItem mi3 = new MenuItem("Disabled Item3");
188+
mi3.setEnabled(false);
189+
190+
MenuBar mb = new MenuBar();
191+
Menu enabledMenu = new Menu("Enabled Menu");
192+
Menu disabledMenu = new Menu("Disabled Menu");
193+
disabledMenu.setEnabled(false);
194+
mb.add(enabledMenu);
195+
mb.add(disabledMenu);
196+
enabledMenu.add(mi1);
197+
enabledMenu.add(mi2);
198+
enabledMenu.add(mi3);
199+
200+
testFrame.setMenuBar(mb);
201+
testFrame.setSize(standardFrame.getWidth(), standardFrame.getHeight());
202+
return List.of(testFrame, standardFrame);
203+
}
204+
205+
private static void positionMultiTestUI(List<? extends Window> windows,
206+
PassFailJFrame.InstructionUI instructionUI) {
207+
int x = instructionUI.getLocation().x + instructionUI.getSize().width + HGAP;
208+
for (Window w : windows) {
209+
w.setLocation(x, instructionUI.getLocation().y);
210+
x += w.getWidth() + HGAP;
211+
}
212+
}
213+
214+
private static class StandardFrame extends Frame {
215+
public StandardFrame(String name) {
216+
super(name);
217+
String testPath = System.getProperty("test.src", ".");
218+
Panel panel = new Panel();
219+
panel.add(new JLabel(new ImageIcon(testPath + File.separator + "XAWTColors.jpg")));
220+
add(panel);
221+
}
222+
}
223+
}

0 commit comments

Comments
 (0)