Skip to content

Commit 9a68d1d

Browse files
committedApr 21, 2023
8306060: Open source few AWT Insets related tests
Reviewed-by: serb, prr
1 parent 2c70828 commit 9a68d1d

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 1999, 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+
@test
25+
@bug 4198994
26+
@summary getInsets should return Insets object that is safe to modify
27+
@key headful
28+
@run main ClobberSharedInsetsObjectTest
29+
*/
30+
31+
/**
32+
* ClobberSharedInsetsObjectTest.java
33+
*
34+
* summary: The bug is that getInsets directly returns Insets object
35+
* obtained from peer getInsets. The latter always return the
36+
* reference to the same object, so modifying this object will affect
37+
* other code that calls getInsets. The test checks that it's safe to
38+
* modify the Insets object returned by getInsets. If the change to
39+
* this object is not visible on the next invocation, the bug is
40+
* considered to be fixed.
41+
*/
42+
43+
import java.awt.BorderLayout;
44+
import java.awt.Color;
45+
import java.awt.EventQueue;
46+
import java.awt.Frame;
47+
import java.awt.Insets;
48+
import java.awt.Panel;
49+
50+
public class ClobberSharedInsetsObjectTest {
51+
static Panel p;
52+
53+
// Impossible inset value to use for the test
54+
final static int SENTINEL_INSET_VALUE = -10;
55+
static Frame f;
56+
57+
public static void main(String[] args) throws Exception {
58+
EventQueue.invokeAndWait(() -> {
59+
try {
60+
// Need a peer anyway, so let the bug manifest visuially, even
61+
// though we can detect it automatically.
62+
f = new Frame();
63+
p = new Panel();
64+
p.setBackground(Color.red);
65+
f.setLayout (new BorderLayout ());
66+
f.add(p, "Center");
67+
68+
Insets insetsBefore = p.getInsets();
69+
insetsBefore.top = SENTINEL_INSET_VALUE;
70+
71+
Insets insetsAfter = p.getInsets();
72+
if (insetsAfter.top == SENTINEL_INSET_VALUE) { // OOPS!
73+
throw new Error("4198994: getInsets returns the same object on subsequent invocations");
74+
}
75+
76+
f.setSize (200,200);
77+
f.setLocationRelativeTo(null);
78+
f.setVisible(true);
79+
80+
System.out.println("getInsets is ok. The object it returns is safe to modify.");
81+
} finally {
82+
if (f != null) {
83+
f.dispose();
84+
}
85+
}
86+
});
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2006, 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+
@test
25+
@bug 6353381
26+
@summary REG: Container.getInsets() returns an incorrect value after removal of menubar, Win32
27+
@key headful
28+
@run main RemoveMenuBarTest
29+
*/
30+
import java.awt.EventQueue;
31+
import java.awt.Frame;
32+
import java.awt.Insets;
33+
import java.awt.Menu;
34+
import java.awt.MenuBar;
35+
36+
public class RemoveMenuBarTest {
37+
static Frame frame;
38+
39+
public static void main(String[] args) throws Exception {
40+
EventQueue.invokeAndWait(() -> {
41+
try {
42+
// old insets: top>0 | left>0
43+
// new insets: top=0 & left=0
44+
// the bug is that updating doesn't happen
45+
frame = new Frame();
46+
MenuBar menubar = new MenuBar();
47+
frame.setBounds(100,100,100,100);
48+
frame.setUndecorated(true);
49+
frame.pack();
50+
menubar.add(new Menu());
51+
frame.setMenuBar(menubar);
52+
System.out.println(frame.getInsets());
53+
54+
frame.setMenuBar(null);
55+
Insets insets = frame.getInsets();
56+
System.out.println(insets);
57+
if (insets.top != 0 || insets.left != 0 ||
58+
insets.bottom !=0 || insets.right != 0) {
59+
throw new RuntimeException("Test failed: the incorrect insets");
60+
}
61+
} finally {
62+
if (frame != null) {
63+
frame.dispose();
64+
}
65+
}
66+
});
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
@test
25+
@bug 4704042
26+
@summary Unit tests for Insets.set()
27+
@run main SetInsetsTest
28+
*/
29+
import java.awt.Insets;
30+
import java.awt.EventQueue;
31+
32+
public class SetInsetsTest {
33+
public static void main(String[] args) throws Exception {
34+
EventQueue.invokeAndWait(() -> {
35+
Insets insets = new Insets(0,0,0,0);
36+
insets.set(100,100,100,100);
37+
if (insets.top != 100 ||
38+
insets.bottom != 100 ||
39+
insets.left != 100 ||
40+
insets.right != 100) {
41+
throw new RuntimeException("Test Failed! Insets=" + insets);
42+
}
43+
});
44+
}
45+
}// class SetInsetsTest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
@test
25+
@bug 5089312
26+
@summary Bottom inset must not change after a second pack call.
27+
@key headful
28+
@run main WindowInsetsTest
29+
*/
30+
import java.awt.EventQueue;
31+
import javax.swing.JButton;
32+
import javax.swing.JFrame;
33+
import javax.swing.JWindow;
34+
35+
public class WindowInsetsTest {
36+
static JFrame frame;
37+
static JWindow window;
38+
39+
public static void main(String[] args) throws Exception {
40+
EventQueue.invokeAndWait(() -> {
41+
try {
42+
frame = new JFrame("Window Test");
43+
frame.setBounds(100, 100, 400, 300);
44+
frame.setVisible(true);
45+
46+
JButton button = new JButton("A Button");
47+
window = new JWindow(frame);
48+
window.getContentPane().add(button);
49+
window.pack();
50+
window.setLocation(200, 200);
51+
window.show();
52+
double h0 = window.getSize().getHeight();
53+
window.pack();
54+
double h1 = window.getSize().getHeight();
55+
if( Math.abs(h1 - h0) > 0.5 ) {
56+
throw new RuntimeException("Test failed: Bad insets.");
57+
}
58+
System.out.println("Test Passed.");
59+
} finally {
60+
if (window != null) {
61+
window.dispose();
62+
}
63+
if (frame != null) {
64+
frame.dispose();
65+
}
66+
}
67+
});
68+
}
69+
}

0 commit comments

Comments
 (0)
Please sign in to comment.