Skip to content

Commit 1e7d0a5

Browse files
author
duke
committed
Backport f7bc9ba552cf913eef2131b964c48f1b4b55131c
1 parent b31ac46 commit 1e7d0a5

File tree

3 files changed

+321
-0
lines changed

3 files changed

+321
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
/*
25+
* @test
26+
* @key printer
27+
* @bug 6255196
28+
* @summary Verifies the function of methods edit(java.io.File file) and
29+
* print(java.io.File file)
30+
* @library /java/awt/regtesthelpers
31+
* @build PassFailJFrame
32+
* @run main/manual EditAndPrintTest
33+
*/
34+
35+
import java.awt.Desktop;
36+
import java.awt.Desktop.Action;
37+
import java.io.File;
38+
import java.io.FileWriter;
39+
import java.io.IOException;
40+
import java.lang.reflect.InvocationTargetException;
41+
import javax.swing.JPanel;
42+
43+
public class EditAndPrintTest extends JPanel {
44+
45+
static final String INSTRUCTIONS = """
46+
This test tries to edit and print a directory, which will expectedly raise IOException.
47+
Then this test would edit and print a .txt file, which should be successful.
48+
After test execution close the editor if it was launched by test.
49+
If you see any EXCEPTION messages in the output press FAIL.
50+
""";
51+
52+
public EditAndPrintTest() {
53+
if (!Desktop.isDesktopSupported()) {
54+
PassFailJFrame.log("Class java.awt.Desktop is not supported on " +
55+
"current platform. Further testing will not be performed");
56+
PassFailJFrame.forcePass();
57+
}
58+
59+
Desktop desktop = Desktop.getDesktop();
60+
61+
if (!desktop.isSupported(Action.PRINT) && !desktop.isSupported(Action.EDIT)) {
62+
PassFailJFrame.log("Neither EDIT nor PRINT actions are supported. Nothing to test.");
63+
PassFailJFrame.forcePass();
64+
}
65+
66+
/*
67+
* Part 1: print or edit a directory, which should throw an IOException.
68+
*/
69+
File userHome = new File(System.getProperty("user.home"));
70+
try {
71+
if (desktop.isSupported(Action.EDIT)) {
72+
PassFailJFrame.log("Trying to edit " + userHome);
73+
desktop.edit(userHome);
74+
PassFailJFrame.log("No exception has been thrown for editing " +
75+
"directory " + userHome.getPath());
76+
PassFailJFrame.log("Test failed.");
77+
} else {
78+
PassFailJFrame.log("Action EDIT is unsupported.");
79+
}
80+
} catch (IOException e) {
81+
PassFailJFrame.log("Expected IOException is caught.");
82+
}
83+
84+
try {
85+
if (desktop.isSupported(Action.PRINT)) {
86+
PassFailJFrame.log("Trying to print " + userHome);
87+
desktop.print(userHome);
88+
PassFailJFrame.log("No exception has been thrown for printing " +
89+
"directory " + userHome.getPath());
90+
PassFailJFrame.log("Test failed.");
91+
} else {
92+
PassFailJFrame.log("Action PRINT is unsupported.\n");
93+
}
94+
} catch (IOException e) {
95+
PassFailJFrame.log("Expected IOException is caught.");
96+
}
97+
98+
/*
99+
* Part 2: print or edit a normal .txt file, which may succeed if there
100+
* is associated application to print or edit the given file. It fails
101+
* otherwise.
102+
*/
103+
// Create a temp .txt file for test.
104+
String testFilePath = System.getProperty("java.io.tmpdir") + File.separator + "JDIC-test.txt";
105+
File testFile = null;
106+
try {
107+
PassFailJFrame.log("Creating temporary file.");
108+
testFile = File.createTempFile("JDIC-test", ".txt", new File(System.getProperty("java.io.tmpdir")));
109+
testFile.deleteOnExit();
110+
FileWriter writer = new FileWriter(testFile);
111+
writer.write("This is a temp file used to test print() method of Desktop.");
112+
writer.flush();
113+
writer.close();
114+
} catch (java.io.IOException ioe){
115+
PassFailJFrame.log("EXCEPTION: " + ioe.getMessage());
116+
PassFailJFrame.forceFail("Failed to create temp file for testing.");
117+
}
118+
119+
try {
120+
if (desktop.isSupported(Action.EDIT)) {
121+
PassFailJFrame.log("Try to edit " + testFile);
122+
desktop.edit(testFile);
123+
PassFailJFrame.log("Succeed.");
124+
}
125+
} catch (IOException e) {
126+
PassFailJFrame.log("EXCEPTION: " + e.getMessage());
127+
}
128+
129+
try {
130+
if (desktop.isSupported(Action.PRINT)) {
131+
PassFailJFrame.log("Trying to print " + testFile);
132+
desktop.print(testFile);
133+
PassFailJFrame.log("Succeed.");
134+
}
135+
} catch (IOException e) {
136+
PassFailJFrame.log("EXCEPTION: " + e.getMessage());
137+
}
138+
}
139+
140+
public static void main(String args[]) throws InterruptedException,
141+
InvocationTargetException {
142+
PassFailJFrame.builder()
143+
.title("Edit and Print test")
144+
.splitUI(EditAndPrintTest::new)
145+
.instructions(INSTRUCTIONS)
146+
.rows((int) INSTRUCTIONS.lines().count() + 1)
147+
.columns(60)
148+
.logArea()
149+
.build()
150+
.awaitAndCheck();
151+
}
152+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 1998, 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+
/*
25+
* @test
26+
* @bug 4100188
27+
* @key headful
28+
* @summary Make sure that TextFields contain all of,
29+
* and exactly, the text that was entered into them.
30+
* @run main GetTextTest
31+
*/
32+
33+
import java.awt.AWTException;
34+
import java.awt.Dimension;
35+
import java.awt.EventQueue;
36+
import java.awt.FlowLayout;
37+
import java.awt.Frame;
38+
import java.awt.Label;
39+
import java.awt.Point;
40+
import java.awt.Robot;
41+
import java.awt.TextField;
42+
import java.awt.event.ActionEvent;
43+
import java.awt.event.ActionListener;
44+
import java.awt.event.InputEvent;
45+
import java.awt.event.KeyEvent;
46+
import java.lang.reflect.InvocationTargetException;
47+
48+
public class GetTextTest extends Frame implements ActionListener {
49+
private final String s = "test string";
50+
private volatile String ac;
51+
private TextField t;
52+
private Point location;
53+
private Dimension size;
54+
55+
public void setupGUI() {
56+
setLayout(new FlowLayout(FlowLayout.LEFT));
57+
58+
t = new TextField(s, 32);
59+
add(new Label("Hit <CR> after text"));
60+
add(t);
61+
t.addActionListener(this);
62+
setLocationRelativeTo(null);
63+
pack();
64+
setVisible(true);
65+
}
66+
67+
public void actionPerformed(ActionEvent evt) {
68+
ac = evt.getActionCommand();
69+
}
70+
71+
public void performTest() throws AWTException, InterruptedException,
72+
InvocationTargetException {
73+
EventQueue.invokeAndWait(() -> {
74+
location = t.getLocationOnScreen();
75+
size = t.getSize();
76+
});
77+
Robot robot = new Robot();
78+
robot.setAutoDelay(50);
79+
robot.delay(1000);
80+
robot.waitForIdle();
81+
robot.mouseMove(location.x + size.width - 3, location.y + (size.height / 2));
82+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
83+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
84+
robot.delay(1000);
85+
robot.waitForIdle();
86+
robot.keyPress(KeyEvent.VK_ENTER);
87+
robot.keyRelease(KeyEvent.VK_ENTER);
88+
robot.delay(1000);
89+
if (!s.equals(ac)) {
90+
throw new RuntimeException("Action command should be the same as text field content");
91+
}
92+
}
93+
94+
public static void main(String[] args) throws InterruptedException,
95+
InvocationTargetException, AWTException {
96+
GetTextTest test = new GetTextTest();
97+
EventQueue.invokeAndWait(test::setupGUI);
98+
try {
99+
test.performTest();
100+
} finally {
101+
EventQueue.invokeAndWait(test::dispose);
102+
}
103+
}
104+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 1999, 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+
/*
25+
* @test
26+
* @bug 4222122
27+
* @summary TextField.setEchoCharacter() seems to be broken
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual SetEchoCharTest3
31+
*/
32+
33+
import java.awt.FlowLayout;
34+
import java.awt.Frame;
35+
import java.awt.Label;
36+
import java.awt.TextField;
37+
import java.lang.reflect.InvocationTargetException;
38+
39+
public class SetEchoCharTest3 extends Frame {
40+
static String INSTRUCTIONS = """
41+
Type in the text field and "*" characters should echo.
42+
If only one "*" echoes and then the system beeps after
43+
the second character is typed, then press Fail, otherwise press Pass.
44+
""";
45+
public SetEchoCharTest3() {
46+
setLayout(new FlowLayout());
47+
add(new Label("Enter text:"));
48+
TextField tf = new TextField(15);
49+
tf.setEchoChar('*');
50+
add(tf);
51+
pack();
52+
}
53+
54+
public static void main(String[] args) throws InterruptedException,
55+
InvocationTargetException {
56+
PassFailJFrame.builder()
57+
.title("Set Echo Char Test 3")
58+
.testUI(SetEchoCharTest3::new)
59+
.instructions(INSTRUCTIONS)
60+
.rows((int) INSTRUCTIONS.lines().count() + 1)
61+
.columns(40)
62+
.build()
63+
.awaitAndCheck();
64+
}
65+
}

0 commit comments

Comments
 (0)