Skip to content

Commit cce9336

Browse files
author
Sonia Zaldana Calles
committed
8316371: Open some swing tests 6
Backport-of: d3a79b5861be27227b8c28cb3acdce089b74c50b
1 parent 0cdb868 commit cce9336

File tree

5 files changed

+569
-0
lines changed

5 files changed

+569
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2001, 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 4549069
27+
* @summary Tests if javax.swing.text.AbstractDocument.BranchElement.getEndOffset() throws AIOOBE
28+
* @key headful
29+
*/
30+
31+
import java.awt.Point;
32+
import java.awt.Robot;
33+
import java.awt.event.InputEvent;
34+
import java.awt.event.KeyAdapter;
35+
import java.awt.event.KeyEvent;
36+
import java.awt.event.WindowAdapter;
37+
import java.awt.event.WindowEvent;
38+
import java.util.Timer;
39+
import java.util.TimerTask;
40+
import javax.swing.JFrame;
41+
import javax.swing.JTextArea;
42+
import javax.swing.SwingUtilities;
43+
import javax.swing.text.DefaultStyledDocument;
44+
import javax.swing.undo.UndoManager;
45+
46+
public class bug4549069 {
47+
static Timer timer;
48+
static volatile Point p;
49+
50+
static JFrame f;
51+
static JTextArea jta;
52+
static UndoManager um;
53+
static Robot robot;
54+
55+
public static void main(String[] argv) throws Exception {
56+
robot = new Robot();
57+
try {
58+
SwingUtilities.invokeAndWait(() -> {
59+
f = new JFrame("bug4549069");
60+
f.addWindowListener(new TestStateListener());
61+
62+
jta = new JTextArea();
63+
um = new UndoManager();
64+
jta.setDocument(new DefaultStyledDocument());
65+
jta.getDocument().addUndoableEditListener(um);
66+
67+
String text = "Press Ctrl-Z (undo) to get\n" +
68+
"a stacktrace U shouldn't XX\n";
69+
jta.setText(text);
70+
71+
jta.addKeyListener(new KeyAdapter() {
72+
public void keyPressed(KeyEvent e) {
73+
if (um.canUndo()) {
74+
um.undo();
75+
}
76+
}
77+
});
78+
79+
f.getContentPane().add(jta);
80+
f.pack();
81+
f.setVisible(true);
82+
});
83+
84+
robot.waitForIdle();
85+
robot.delay(1000);
86+
} finally {
87+
if (f != null) {
88+
SwingUtilities.invokeAndWait(() -> {
89+
f.dispose();
90+
});
91+
}
92+
}
93+
}
94+
95+
static class TestStateListener extends WindowAdapter {
96+
public void windowOpened(WindowEvent ev) {
97+
timer = new Timer();
98+
timer.schedule(new RobotTask(), 1000);
99+
}
100+
}
101+
102+
static class RobotTask extends TimerTask {
103+
public void run() {
104+
try {
105+
SwingUtilities.invokeAndWait(() -> {
106+
p = jta.getLocationOnScreen();
107+
});
108+
} catch (Exception e) {
109+
throw new RuntimeException("Could not get location");
110+
111+
}
112+
robot.mouseMove(p.x, p.y);
113+
robot.waitForIdle();
114+
115+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
116+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
117+
robot.waitForIdle();
118+
119+
robot.keyPress(KeyEvent.VK_SPACE);
120+
robot.keyRelease(KeyEvent.VK_SPACE);
121+
robot.waitForIdle();
122+
}
123+
}
124+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (c) 1998, 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 4185537
27+
* @summary javax.swing.text.AbstractWriter: TOTAL REWRITE COMPLETE.
28+
*/
29+
30+
import java.io.IOException;
31+
import java.io.StringWriter;
32+
import java.io.Writer;
33+
import javax.swing.text.AbstractWriter;
34+
import javax.swing.text.BadLocationException;
35+
import javax.swing.text.DefaultStyledDocument;
36+
import javax.swing.text.Document;
37+
38+
public class bug4185537 {
39+
static char[] chars = {'a', 'b', 'c', 'd', 'e'};
40+
static StringWriter wr = new StringWriter();
41+
42+
public static void main(String[] argv) {
43+
DefaultStyledDocument doc = new DefaultStyledDocument();
44+
45+
SimpleWriter sw = new SimpleWriter(wr, doc, 5, 200);
46+
sw.test_getWriter();
47+
48+
if (sw.getStartOffset() != 5) {
49+
throw new RuntimeException("getStartOffset fails...");
50+
}
51+
if (sw.getEndOffset() != 205) {
52+
throw new RuntimeException("getEndOffset fails...");
53+
}
54+
55+
sw.setLineSeparator("+");
56+
if (!sw.getLineSeparator().equals("+")) {
57+
throw new RuntimeException("Doesn't set line separator correctly...");
58+
}
59+
sw.test_writeLineSeparator();
60+
61+
sw.test_write_output();
62+
sw.test_CurrentLineLength();
63+
sw.test_getLineLength();
64+
sw.test_getIndentLevel();
65+
sw.test_CanWrapLines();
66+
if (!wr.toString().equals("+abcde")) {
67+
throw new RuntimeException("Test fails...");
68+
}
69+
try {
70+
wr.close();
71+
} catch (Exception e) {
72+
System.out.println("Exception...");
73+
}
74+
}
75+
76+
static class SimpleWriter extends AbstractWriter {
77+
78+
public SimpleWriter(Writer w, Document d, int pos, int len) {
79+
super(w, d, pos, len);
80+
}
81+
82+
void test_writeLineSeparator() {
83+
try {
84+
writeLineSeparator();
85+
} catch (IOException e) {
86+
e.printStackTrace();
87+
throw new RuntimeException("writeLineSeparator fails...");
88+
}
89+
}
90+
91+
void test_getWriter() {
92+
if (getWriter() != wr) throw new RuntimeException("Writer gets incorrect...");
93+
}
94+
95+
void test_CurrentLineLength() {
96+
setCurrentLineLength(0);
97+
if (getCurrentLineLength() != 0) throw new RuntimeException("Doesn't set CurrentLineLength...");
98+
if (!isLineEmpty()) {
99+
throw new RuntimeException("isLineEmpty() should return false...");
100+
}
101+
}
102+
103+
void test_getLineLength() {
104+
setLineLength(80);
105+
if (getLineLength() != 80) {
106+
throw new RuntimeException("Default line length doesn't set...");
107+
}
108+
}
109+
110+
void test_CanWrapLines() {
111+
setCanWrapLines(true);
112+
if (!getCanWrapLines()) {
113+
throw new RuntimeException("Doesn't set wrapping lines correctly");
114+
}
115+
}
116+
117+
void test_getIndentLevel() {
118+
incrIndent();
119+
if (getIndentLevel() != 1) {
120+
throw new RuntimeException("getIndentLevel() fails...");
121+
}
122+
}
123+
124+
void test_write_output() {
125+
try {
126+
write(chars, 0, 3);
127+
} catch (IOException e) {
128+
throw new RuntimeException("write(char[], int, int): unexpected IOException...");
129+
}
130+
try {
131+
output(chars, 3, 2);
132+
} catch (IOException e) {
133+
throw new RuntimeException("output(char[], int, int): unexpected IOException...");
134+
}
135+
}
136+
137+
public void write() throws IOException, BadLocationException {
138+
}
139+
}
140+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 4240870 4240855
27+
* @summary Tests that DefaultTableCellRenderer overrides following methods:
28+
* validate()
29+
* revalidate()
30+
* repaint(long, int, int, int, int)
31+
* repaint(Rectangle)
32+
* firePropertyChange(String, Object, Object)
33+
* firePropertyChange(String, boolean, boolean)
34+
*/
35+
36+
import java.beans.PropertyChangeEvent;
37+
import java.beans.PropertyChangeListener;
38+
import javax.swing.table.DefaultTableCellRenderer;
39+
40+
public class bug4240870 {
41+
public static void main(String[] argv) {
42+
// Test overridden public methods using reflection
43+
String methodName = null;
44+
try {
45+
Class clazz = Class.forName(
46+
"javax.swing.table.DefaultTableCellRenderer");
47+
Class[] noArgs = {};
48+
methodName = "validate";
49+
clazz.getDeclaredMethod(methodName, noArgs);
50+
methodName = "revalidate";
51+
clazz.getDeclaredMethod(methodName, noArgs);
52+
53+
Class[] args1 = {long.class, int.class, int.class,
54+
int.class, int.class};
55+
methodName = "repaint";
56+
clazz.getDeclaredMethod(methodName, args1);
57+
Class[] args2 = {Class.forName("java.awt.Rectangle")};
58+
methodName = "repaint";
59+
clazz.getDeclaredMethod(methodName, args2);
60+
61+
Class objectClass = Class.forName("java.lang.Object");
62+
Class stringClass = Class.forName("java.lang.String");
63+
Class[] args3 = {stringClass, objectClass, objectClass};
64+
methodName = "firePropertyChange";
65+
clazz.getDeclaredMethod(methodName, args3);
66+
Class[] args4 = {stringClass, boolean.class, boolean.class};
67+
clazz.getDeclaredMethod(methodName, args4);
68+
} catch (NoSuchMethodException e) {
69+
throw new RuntimeException("Failed: " + methodName + " not overridden");
70+
} catch (ClassNotFoundException e) {
71+
}
72+
73+
// test protected firePropertyChange(String, Object, Object)
74+
Renderer r = new Renderer();
75+
r.addPropertyChangeListener(new Listener());
76+
r.test();
77+
}
78+
79+
static class Renderer extends DefaultTableCellRenderer {
80+
public void test() {
81+
super.firePropertyChange("text", "old_text", "new_text");
82+
super.firePropertyChange("stuff", "old_stuff", "new_stuff");
83+
}
84+
}
85+
86+
static class Listener implements PropertyChangeListener {
87+
public void propertyChange(PropertyChangeEvent e) {
88+
if (!e.getPropertyName().equals("text")) {
89+
throw new RuntimeException("Failed: firePropertyChange not overridden");
90+
}
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)