Skip to content

Commit b1bdadd

Browse files
Amos ShiGoeLin
Amos Shi
authored andcommittedJan 30, 2024
8315600: Open source few more headless Swing misc tests
Backport-of: b05198a4f354934bc344fe9cbc19d98fd8bc3977
1 parent 34ea901 commit b1bdadd

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
/*
25+
* @test
26+
* @bug 4210354
27+
* @summary Tests whether method FixedHeightLayoutCache.getBounds returns bad Rectangle
28+
* @run main bug4210354
29+
*/
30+
31+
import java.awt.Rectangle;
32+
33+
import javax.swing.tree.AbstractLayoutCache;
34+
import javax.swing.tree.DefaultMutableTreeNode;
35+
import javax.swing.tree.DefaultTreeModel;
36+
import javax.swing.tree.FixedHeightLayoutCache;
37+
import javax.swing.tree.TreePath;
38+
39+
public class bug4210354 {
40+
static class DummyNodeDimensions extends AbstractLayoutCache.NodeDimensions {
41+
private final Rectangle rectangle;
42+
43+
public DummyNodeDimensions(Rectangle r) {
44+
rectangle = r;
45+
}
46+
public Rectangle getNodeDimensions(Object value, int row, int depth,
47+
boolean expanded, Rectangle bounds) {
48+
return rectangle;
49+
}
50+
51+
/* create the TreeModel of depth 1 with specified num of children */
52+
public DefaultTreeModel getTreeModelILike(int childrenCount) {
53+
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
54+
for (int i = 0; i < childrenCount; i++) {
55+
DefaultMutableTreeNode child =
56+
new DefaultMutableTreeNode("root.child" + i);
57+
root.insert(child, i);
58+
}
59+
return new DefaultTreeModel(root);
60+
}
61+
}
62+
63+
public void init() {
64+
int x = 1, y = 2, dx = 3, dy = 4, h = 3;
65+
DummyNodeDimensions dim = new DummyNodeDimensions(new Rectangle(x, y, dx, dy));
66+
FixedHeightLayoutCache fhlc = new FixedHeightLayoutCache();
67+
fhlc.setModel(dim.getTreeModelILike(3));
68+
fhlc.setRootVisible(true);
69+
fhlc.setNodeDimensions(dim);
70+
fhlc.setRowHeight(h);
71+
int row = 0;
72+
TreePath path = fhlc.getPathForRow(row);
73+
Rectangle r = fhlc.getBounds(path, new Rectangle());
74+
Rectangle r2 = new Rectangle(x, row * h, dx, h);
75+
if (r.width != r2.width) {
76+
throw new RuntimeException("FixedHeightLayoutCache.getBounds returns bad Rectangle");
77+
}
78+
}
79+
80+
public static void main(String[] args) throws Exception {
81+
bug4210354 b = new bug4210354();
82+
b.init();
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 4706533
27+
* @summary UndoManager.setLimit(0) doesn't correctly trim the UndoManager size
28+
* @run main bug4706533
29+
*/
30+
31+
import javax.swing.undo.AbstractUndoableEdit;
32+
import javax.swing.undo.CannotUndoException;
33+
import javax.swing.undo.CannotRedoException;
34+
import javax.swing.undo.UndoManager;
35+
36+
public class bug4706533 {
37+
38+
public static void main(String[] args) throws Exception {
39+
UndoManager manager = new UndoManager();
40+
manager.setLimit(1);
41+
AbstractUndoableEdit edit = new MyUndoableEdit();
42+
manager.addEdit(edit);
43+
manager.setLimit(0);
44+
try {
45+
manager.undo();
46+
throw new RuntimeException("The limit should be zero");
47+
} catch (CannotUndoException e) {
48+
//Expected to be thrown
49+
}
50+
}
51+
52+
static class MyUndoableEdit extends AbstractUndoableEdit {
53+
@Override
54+
public void undo() throws CannotUndoException {}
55+
@Override
56+
public void redo() throws CannotRedoException {}
57+
}
58+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 4992178
27+
* @summary REGRESSION: Allow unlimited number of edits in an UndoManager
28+
* @run main bug4992178
29+
*/
30+
31+
import javax.swing.undo.AbstractUndoableEdit;
32+
import javax.swing.undo.CannotRedoException;
33+
import javax.swing.undo.CannotUndoException;
34+
import javax.swing.undo.UndoManager;
35+
36+
public class bug4992178 {
37+
38+
public static void main(String[] argv) throws Exception {
39+
TestUndoManager manager = new TestUndoManager();
40+
manager.setLimit(1);
41+
AbstractUndoableEdit edit = new MyUndoableEdit();
42+
manager.addEdit(edit);
43+
44+
manager.setLimit(-1);
45+
46+
manager.discardAllEdits();
47+
48+
if (manager.getVectorSize() != 0) {
49+
throw new RuntimeException(
50+
"UndoManager's vector size should be 0 after discarding all changes");
51+
}
52+
}
53+
54+
static class TestUndoManager extends UndoManager {
55+
public int getVectorSize() {
56+
return edits.size();
57+
}
58+
}
59+
60+
static class MyUndoableEdit extends AbstractUndoableEdit {
61+
@Override
62+
public void undo() throws CannotUndoException {}
63+
@Override
64+
public void redo() throws CannotRedoException {}
65+
}
66+
67+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jan 30, 2024

@openjdk-notifier[bot]
Please sign in to comment.