Skip to content

Commit e015e6c

Browse files
author
Alisen Chung
committedSep 22, 2023
8315825: Open some swing tests
Reviewed-by: abhiscxk, prr
1 parent 9aaac2e commit e015e6c

File tree

4 files changed

+449
-0
lines changed

4 files changed

+449
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (c) 2004, 2023, 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 4765272
27+
* @summary REGRESSION: IAE: focusCycleRoot not focus cyle root of a Component
28+
* @key headful
29+
*/
30+
31+
import java.awt.Component;
32+
import java.awt.Container;
33+
import java.awt.FlowLayout;
34+
import java.awt.FocusTraversalPolicy;
35+
import java.awt.Robot;
36+
import java.awt.event.FocusAdapter;
37+
import java.awt.event.FocusEvent;
38+
import javax.swing.JButton;
39+
import javax.swing.JFrame;
40+
import javax.swing.JPanel;
41+
import javax.swing.SwingUtilities;
42+
43+
public class bug4765272 {
44+
static boolean focusGained = false;
45+
static JFrame f;
46+
static JButton bt1;
47+
48+
public static void main(String[] args) throws Exception {
49+
try {
50+
SwingUtilities.invokeAndWait(() -> {
51+
f = new JFrame("bug4765272");
52+
bt1 = new JButton("Button 1");
53+
JButton bt2 = new JButton("Button 2");
54+
55+
JPanel p = new JPanel();
56+
p.setLayout(new FlowLayout());
57+
p.add(bt1);
58+
p.add(bt2);
59+
f.getContentPane().add(p);
60+
61+
FocusTraversalPolicy policy = new FocusTraversalPolicy() {
62+
@Override
63+
public Component getComponentAfter(Container aContainer, Component aComponent) {
64+
if (aComponent == bt1) {
65+
return bt2;
66+
}
67+
return bt1;
68+
}
69+
70+
@Override
71+
public Component getComponentBefore(Container aContainer, Component aComponent) {
72+
if (aComponent == bt1) {
73+
return bt2;
74+
}
75+
return bt1;
76+
}
77+
78+
@Override
79+
public Component getFirstComponent(Container aContainer) {
80+
return bt1;
81+
}
82+
83+
@Override
84+
public Component getLastComponent(Container aContainer) {
85+
return bt2;
86+
}
87+
88+
@Override
89+
public Component getDefaultComponent(Container aContainer) {
90+
return bt1;
91+
}
92+
};
93+
94+
bt1.addFocusListener(new FocusAdapter() {
95+
public void focusGained(FocusEvent e) {
96+
p.removeAll();
97+
synchronized (this) {
98+
focusGained = true;
99+
this.notifyAll();
100+
}
101+
}
102+
});
103+
104+
f.setLocationRelativeTo(null);
105+
f.setVisible(true);
106+
});
107+
108+
Robot r = new Robot();
109+
r.waitForIdle();
110+
r.delay(1000);
111+
112+
SwingUtilities.invokeAndWait(() -> {
113+
bt1.requestFocus();
114+
try {
115+
if (!focusGained) {
116+
Thread.sleep(5000);
117+
}
118+
} catch (Exception ex) {
119+
ex.printStackTrace();
120+
}
121+
});
122+
} finally {
123+
SwingUtilities.invokeAndWait(() -> {
124+
if (f != null) {
125+
f.dispose();
126+
}
127+
});
128+
}
129+
}
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2004, 2023, 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 4979794
27+
* @summary A component is sometimes the next component for itself in focus policy.
28+
* @key headful
29+
*/
30+
31+
import java.awt.Component;
32+
import java.awt.Container;
33+
import java.awt.FocusTraversalPolicy;
34+
import java.awt.Robot;
35+
import javax.swing.JButton;
36+
import javax.swing.JFrame;
37+
import javax.swing.JPanel;
38+
import javax.swing.SwingUtilities;
39+
40+
public class bug4979794 {
41+
static JFrame fr;
42+
static JButton btn1;
43+
static JButton btn2;
44+
45+
public static void main(String[] args) throws Exception {
46+
try {
47+
SwingUtilities.invokeAndWait(() -> {
48+
fr = new JFrame("bug4979794");
49+
fr.getContentPane().setLayout(null);
50+
51+
JPanel p = new JPanel();
52+
p.setLayout(null);
53+
fr.getContentPane().add(p);
54+
55+
btn1 = new JButton("Button 1");
56+
btn1.setBounds(0, 0, 200, 200);
57+
58+
btn2 = new JButton("Button 2");
59+
btn2.setBounds(0, 0, 200, 200);
60+
61+
p.add(btn1);
62+
p.add(btn2);
63+
p.setSize(200, 200);
64+
65+
fr.setLocationRelativeTo(null);
66+
fr.setSize(300, 300);
67+
fr.setVisible(true);
68+
});
69+
70+
Robot r = new Robot();
71+
r.waitForIdle();
72+
r.delay(1000);
73+
74+
SwingUtilities.invokeAndWait(() -> {
75+
Container root = btn1.getFocusCycleRootAncestor();
76+
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
77+
Component next1 = policy.getComponentAfter(fr, btn1);
78+
Component next2 = policy.getComponentAfter(fr, btn2);
79+
if (next1 == next2) {
80+
throw new RuntimeException("btn1 and btn2 have the same next Component.");
81+
}
82+
});
83+
} finally {
84+
SwingUtilities.invokeAndWait(() -> {
85+
if (fr != null) {
86+
fr.dispose();
87+
}
88+
});
89+
}
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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 4907772
27+
* @summary 1.4 REGRESSION: JPanel responds to mouse clicks on overlapping JPanel
28+
* @key headful
29+
*/
30+
31+
import java.awt.Component;
32+
import java.awt.Container;
33+
import java.awt.FocusTraversalPolicy;
34+
import java.awt.Robot;
35+
import javax.swing.JButton;
36+
import javax.swing.JFrame;
37+
import javax.swing.JPanel;
38+
import javax.swing.SwingUtilities;
39+
40+
public class bug4907772 {
41+
static JFrame fr;
42+
static JButton btn1;
43+
static JButton btn2;
44+
45+
public static void main(String[] args) throws Exception {
46+
try {
47+
SwingUtilities.invokeAndWait(() -> {
48+
fr = new JFrame("bug4907772");
49+
fr.getContentPane().setLayout(null);
50+
51+
JPanel p = new JPanel();
52+
p.setLayout(null);
53+
fr.getContentPane().add(p);
54+
55+
btn1 = new JButton("Button 1");
56+
btn1.setBounds(0, 0, 200, 200);
57+
58+
btn2 = new JButton("Button 2");
59+
btn2.setBounds(0, 0, 200, 200);
60+
61+
p.add(btn1);
62+
p.add(btn2);
63+
64+
fr.setLocationRelativeTo(null);
65+
fr.pack();
66+
fr.setVisible(true);
67+
});
68+
69+
Robot r = new Robot();
70+
r.waitForIdle();
71+
r.delay(1000);
72+
73+
SwingUtilities.invokeAndWait(() -> {
74+
Container root = btn1.getFocusCycleRootAncestor();
75+
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
76+
Component initial = policy.getInitialComponent(fr);
77+
if (initial == btn2) {
78+
throw new RuntimeException("The underlying button shouldn't be the initial component of FCR");
79+
}
80+
});
81+
} finally {
82+
SwingUtilities.invokeAndWait(() -> {
83+
if (fr != null) {
84+
fr.dispose();
85+
}
86+
});
87+
}
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2002, 2023, 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 4753342
27+
* @key headful
28+
* @summary Makes sure add/remove/setLayout redirect to the contentpane
29+
*/
30+
31+
import java.awt.Container;
32+
import java.awt.GridLayout;
33+
import java.awt.LayoutManager;
34+
import javax.swing.JButton;
35+
import javax.swing.JDialog;
36+
import javax.swing.JFrame;
37+
import javax.swing.JInternalFrame;
38+
import javax.swing.JWindow;
39+
import javax.swing.RootPaneContainer;
40+
import javax.swing.SwingUtilities;
41+
42+
public class RootPaneChecking {
43+
public static void main(String[] args) throws Exception {
44+
SwingUtilities.invokeAndWait(() -> {
45+
MyJFrame frame = new MyJFrame();
46+
frame.setTitle("RootPaneChecking");
47+
checkRootPaneCheckingEnabled(frame);
48+
frame.setRootPaneCheckingEnabled(false);
49+
checkRootPaneCheckingDisabled(frame);
50+
51+
MyJWindow window = new MyJWindow();
52+
checkRootPaneCheckingEnabled(window);
53+
window.setRootPaneCheckingEnabled(false);
54+
checkRootPaneCheckingDisabled(window);
55+
56+
MyJDialog dialog = new MyJDialog();
57+
checkRootPaneCheckingEnabled(dialog);
58+
dialog.setRootPaneCheckingEnabled(false);
59+
checkRootPaneCheckingDisabled(dialog);
60+
61+
MyJInternalFrame iframe = new MyJInternalFrame();
62+
checkRootPaneCheckingEnabled(iframe);
63+
iframe.setRootPaneCheckingEnabled(false);
64+
checkRootPaneCheckingDisabled(iframe);
65+
});
66+
}
67+
68+
private static void checkRootPaneCheckingEnabled(RootPaneContainer rpc) {
69+
Container parent = (Container) rpc;
70+
Container cp = rpc.getContentPane();
71+
// Test add
72+
JButton button = new JButton("RootPaneChecking");
73+
parent.add(button);
74+
if (button.getParent() != cp) {
75+
throw new RuntimeException("Add parent mismatch, want: " +
76+
cp + " got " + button.getParent());
77+
}
78+
79+
// Test remove
80+
parent.remove(button);
81+
if (button.getParent() != null) {
82+
throw new RuntimeException("Remove mismatch, want null got " +
83+
button.getParent());
84+
}
85+
86+
// Test setLayout
87+
LayoutManager manager = new GridLayout();
88+
parent.setLayout(manager);
89+
if (manager != cp.getLayout()) {
90+
throw new RuntimeException("LayoutManager mismatch, want: " +
91+
manager + " got " + cp.getLayout());
92+
}
93+
}
94+
95+
private static void checkRootPaneCheckingDisabled(RootPaneContainer rpc) {
96+
Container parent = (Container) rpc;
97+
Container cp = rpc.getContentPane();
98+
99+
// Test add
100+
JButton button = new JButton("RootPaneChecking");
101+
parent.add(button);
102+
if (button.getParent() != parent) {
103+
throw new RuntimeException("Add parent mismatch, want: " +
104+
parent + " got " + button.getParent());
105+
}
106+
107+
// Test setLayout
108+
LayoutManager manager = new GridLayout();
109+
parent.setLayout(manager);
110+
if (manager != parent.getLayout()) {
111+
throw new RuntimeException("LayoutManager mismatch, want: " +
112+
manager + " got " + cp.getLayout());
113+
}
114+
}
115+
116+
static class MyJFrame extends JFrame {
117+
public void setRootPaneCheckingEnabled(boolean x) {
118+
super.setRootPaneCheckingEnabled(x);
119+
}
120+
}
121+
122+
static class MyJWindow extends JWindow {
123+
public void setRootPaneCheckingEnabled(boolean x) {
124+
super.setRootPaneCheckingEnabled(x);
125+
}
126+
}
127+
128+
static class MyJDialog extends JDialog {
129+
public void setRootPaneCheckingEnabled(boolean x) {
130+
super.setRootPaneCheckingEnabled(x);
131+
}
132+
}
133+
134+
static class MyJInternalFrame extends JInternalFrame {
135+
public void setRootPaneCheckingEnabled(boolean x) {
136+
super.setRootPaneCheckingEnabled(x);
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)
Please sign in to comment.