undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,104 @@
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.AWTException;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.event.InputEvent;
import java.lang.reflect.InvocationTargetException;
/*
* @test
* @bug 4038580
* @key headful
* @requires os.family != "windows"
* @summary Caret position not accurate in presence of selected areas
* @run main CaretPositionTest
*/
public class CaretPositionTest extends Frame {
private TextField text_field;
private Button caretpos_button;
private Point onScreen;
private Dimension size;
String text = "12 45 789";
private static volatile int position = -1;
public static void main(String[] args) throws InterruptedException,
InvocationTargetException, AWTException {
CaretPositionTest test = new CaretPositionTest();
EventQueue.invokeAndWait(test::setupGUI);
try {
test.test();
if (position != 9) {
throw new RuntimeException("Caret position should be at the end of the string");
}
} finally {
EventQueue.invokeAndWait(test::dispose);
}
}
public void setupGUI() {
setLocationRelativeTo(null);
setTitle("CaretPositionTest");
setLayout(new FlowLayout());
text_field = new TextField(text, 9);
caretpos_button=new Button("CaretPosition");
add(text_field);
add(caretpos_button);
pack();
setVisible(true);
toFront();
}
public void test() throws AWTException, InterruptedException,
InvocationTargetException {
EventQueue.invokeAndWait(() -> {
onScreen = text_field.getLocationOnScreen();
size = text_field.getSize();
});
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.delay(1000);
int y = onScreen.y + (size.height / 2);
robot.mouseMove(onScreen.x + (size.width / 2), y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseMove(onScreen.x + 3, y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
for (int x = onScreen.x + 5; x < onScreen.x + size.width; x += 2) {
robot.mouseMove(x, y);
}
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
EventQueue.invokeAndWait(() -> {
position = text_field.getCaretPosition();
});
}
}

View file

@ -0,0 +1,266 @@
/*
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @key headful
* @bug 4302718 6753165 8177386
* @summary verifies that undo does not work in a disabled TextField on windows
* @requires (os.family == "windows")
* @run main/manual DisabledUndoTest
*/
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DisabledUndoTest {
private static void init() throws Exception {
String[] instructions
= {
"1.Type a few symbols in the textfield.",
"2.Right-click on the textfield to invoke context menu and select \"Undo\". Make sure the typed symbol is undone.",
"3.Then click the button Disable textfield to disable the textfield.",
"4.Right-click on the textfield to invoke context menu.Verify that Undo option is disabled in context menu and you can't undo the text",
"5.If they are not, press Pass, else press Fail."
};
Sysout.createDialog();
Sysout.printInstructions(instructions);
}
static Frame mainFrame;
static Button bt;
static TextField tf;
static Panel p1;
public static void initTestWindow() {
mainFrame = new Frame();
p1 = new Panel();
mainFrame.setTitle("TestWindow");
mainFrame.setBounds(700, 10, 400, 100);
tf = new TextField(20);
tf.select(0, 10);
bt = new Button("Disable textfield");
p1.add(tf);
p1.add(bt);
mainFrame.add(p1);
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
tf.setEditable(false);
}
});
mainFrame.setVisible(true);
}
public static void dispose() {
Sysout.dispose();
mainFrame.dispose();
}
/**
* ***************************************************
* Standard Test Machinery Section DO NOT modify anything in this section --
* it's a standard chunk of code which has all of the synchronisation
* necessary for the test harness. By keeping it the same in all tests, it
* is easier to read and understand someone else's test, as well as insuring
* that all tests behave correctly with the test harness. There is a section
* following this for test-defined classes
* ****************************************************
*/
private static boolean theTestPassed = false;
private static boolean testGeneratedInterrupt = false;
private static String failureMessage = "";
private static Thread mainThread = null;
final private static int sleepTime = 300000;
public static void main(String args[]) throws Exception {
mainThread = Thread.currentThread();
try {
init();
initTestWindow();
} catch (Exception e) {
e.printStackTrace();
}
try {
mainThread.sleep(sleepTime);
} catch (InterruptedException e) {
dispose();
if (testGeneratedInterrupt && !theTestPassed) {
throw new Exception(failureMessage);
}
}
if (!testGeneratedInterrupt) {
dispose();
throw new RuntimeException("Timed out after " + sleepTime / 1000
+ " seconds");
}
}
public static synchronized void pass() {
theTestPassed = true;
testGeneratedInterrupt = true;
mainThread.interrupt();
}
public static synchronized void fail(String whyFailed) {
theTestPassed = false;
testGeneratedInterrupt = true;
failureMessage = whyFailed;
mainThread.interrupt();
}
}
// *********** End Standard Test Machinery Section **********
/**
* **************************************************
* Standard Test Machinery DO NOT modify anything below -- it's a standard chunk
* of code whose purpose is to make user interaction uniform, and thereby make
* it simpler to read and understand someone else's test.
* **************************************************
*/
/**
* This is part of the standard test machinery. It creates a dialog (with the
* instructions), and is the interface for sending text messages to the user. To
* print the instructions, send an array of strings to Sysout.createDialog
* WithInstructions method. Put one line of instructions per array entry. To
* display a message for the tester to see, simply call Sysout.println with the
* string to be displayed. This mimics System.out.println but works within the
* test harness as well as standalone.
*/
class Sysout {
private static TestDialog dialog;
private static Frame frame;
public static void createDialog() {
frame = new Frame();
dialog = new TestDialog(frame, "Instructions");
String[] defInstr = {"Instructions will appear here. ", ""};
dialog.printInstructions(defInstr);
dialog.show();
println("Any messages for the tester will display here.");
}
public static void printInstructions(String[] instructions) {
dialog.printInstructions(instructions);
}
public static void println(String messageIn) {
dialog.displayMessage(messageIn);
}
public static void dispose() {
dialog.dispose();
frame.dispose();
}
}
/**
* This is part of the standard test machinery. It provides a place for the test
* instructions to be displayed, and a place for interactive messages to the
* user to be displayed. To have the test instructions displayed, see Sysout. To
* have a message to the user be displayed, see Sysout. Do not call anything in
* this dialog directly.
*/
class TestDialog extends Dialog implements ActionListener {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
Panel buttonP;
Button failB;
Button passB;
// DO NOT call this directly, go through Sysout
public TestDialog(Frame frame, String name) {
super(frame, name);
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
add("North", instructionsText);
messageText = new TextArea("", 5, maxStringLength, scrollBoth);
add("Center", messageText);
buttonP = new Panel();
failB = new Button("Fail");
failB.setActionCommand("fail");
failB.addActionListener(this);
passB = new Button("Pass");
buttonP.add(passB);
buttonP.add(failB);
passB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
DisabledUndoTest.pass();
}
});
add("South", buttonP);
pack();
setVisible(true);
}
// DO NOT call this directly, go through Sysout
public void printInstructions(String[] instructions) {
instructionsText.setText("");
String printStr, remainingStr;
for (int i = 0; i < instructions.length; i++) {
remainingStr = instructions[i];
while (remainingStr.length() > 0) {
if (remainingStr.length() >= maxStringLength) {
int posOfSpace = remainingStr.
lastIndexOf(' ', maxStringLength - 1);
if (posOfSpace <= 0) {
posOfSpace = maxStringLength - 1;
}
printStr = remainingStr.substring(0, posOfSpace + 1);
remainingStr = remainingStr.substring(posOfSpace + 1);
} else {
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append(printStr + "\n");
}
}
}
public void displayMessage(String messageIn) {
messageText.append(messageIn + "\n");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "fail") {
DisabledUndoTest.fail("User Clicked Fail");
}
}
}

View file

@ -0,0 +1,107 @@
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Portions Copyright (c) 2012 IBM Corporation
*/
/* @test
* @bug 7155298
* @key headful
* @run main/othervm/timeout=60 TestDispose
* @summary Editable TextField blocks GUI application from exit.
* @author Sean Chou
*/
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.Robot;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestDispose {
public static Frame frame = null;
public static TextField textField = null;
public static volatile Process worker = null;
public void testDispose() throws InvocationTargetException,
InterruptedException {
Robot robot;
try {
robot = new Robot();
}catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Unexpected failure");
}
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("Test");
textField = new TextField("editable textArea");
textField.setEditable(true);
// textField.setEditable(false); // this testcase passes if textField is non-editable
frame.setLayout(new FlowLayout());
frame.add(textField);
frame.pack();
frame.setVisible(true);
}
});
robot.waitForIdle();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame.dispose();
}
});
robot.waitForIdle();
}
public static void main(String[] args) throws Exception{
if(args.length == 0) {
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
worker.destroy();
}
});
System.out.println(System.getProperty("java.home")+"/bin/java TestDispose workprocess");
worker = Runtime.getRuntime().exec(System.getProperty("java.home")+"/bin/java TestDispose workprocess");
worker.waitFor();
return;
}
TestDispose app = new TestDispose();
app.testDispose();
}
}

View file

@ -0,0 +1,203 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@key headful
@bug 8055197 7186036
@summary TextField should replace EOL character with space character
@run main EOLTest
*/
import java.awt.Frame;
import java.awt.TextField;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
public class EOLTest {
private Frame mainFrame;
private TextField textField;
private String testStrEOL;
private boolean isTestFail;
private int testFailCount;
StringBuilder testFailMessage;
private String expectedString = "Row1 Row2 Row3";
public EOLTest() {
mainFrame = new Frame();
mainFrame.setSize(200, 200);
mainFrame.setVisible(true);
testFailMessage = new StringBuilder();
testStrEOL = "Row1" + System.lineSeparator() + "Row2\nRow3";
}
private void testConstructor1() {
textField = new TextField(testStrEOL);
textField.setSize(200, 100);
mainFrame.add(textField);
checkTest();
mainFrame.remove(textField);
}
private void testConstructor2() {
textField = new TextField(30);
textField.setSize(200, 100);
mainFrame.add(textField);
textField.setText(testStrEOL);
checkTest();
mainFrame.remove(textField);
}
private void testConstructor3() {
textField = new TextField(testStrEOL, 30);
textField.setSize(200, 100);
mainFrame.add(textField);
checkTest();
mainFrame.remove(textField);
}
private void testSetText() {
textField = new TextField();
textField.setSize(200, 100);
textField.setText(testStrEOL);
mainFrame.add(textField);
checkTest();
mainFrame.remove(textField);
}
private void testDeserialization() {
TextField textFieldToSerialize = new TextField(testStrEOL);
textFieldToSerialize.setSize(200, 100);
mainFrame.add(textFieldToSerialize);
try {
// Serialize TextField object "textFieldToSerialize".
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutput outStream = new ObjectOutputStream(baos);
outStream.writeObject(textFieldToSerialize);
// Search the text variable data through serialized object stream.
byte[] streamedBytes = baos.toByteArray();
int foundLoc = 0;
for (int i = 0; i < streamedBytes.length; ++i) {
if (streamedBytes[i] == expectedString.charAt(0)) {
foundLoc = i;
int j = 1;
for (; j < expectedString.length(); ++j) {
if (streamedBytes[i+j] != expectedString.charAt(j)) {
break;
}
}
if (j == expectedString.length()) {
break;
}
}
foundLoc = -1;
}
if (foundLoc == -1) {
// Could not find text data in serialized object stream.
throw new Exception("Could not find text data in serialized "
+ "object stream.");
}
// Replace space character from serialized stream with
// EOL character for testing de-serialization.
String EOLChar = System.lineSeparator();
String newExpectedString = "";
for (int i = foundLoc, j = 0; j < expectedString.length(); ++i, ++j) {
newExpectedString += (char)(streamedBytes[i]);
if (streamedBytes[i] == ' ') {
int k = 0;
for (; k < EOLChar.length(); ++k) {
streamedBytes[i + k] = (byte) EOLChar.charAt(k);
}
i += k-1;
j += k-1;
}
}
// New line character varies with platform,
// ex. For windows '\r\n', for linux '\n'.
// While replacing space from serialized object stream, the length
// of EOL character will affect the expected string as well.
expectedString = newExpectedString;
// De-serialize TextField object stream.
ByteArrayInputStream bais = new ByteArrayInputStream(streamedBytes);
ObjectInput inStream = new ObjectInputStream(bais);
textField = (TextField) inStream.readObject();
} catch (Exception ex) {
// Serialization or De-serialization failed.
// Create textField with empty string to show failure.
ex.printStackTrace();
textField = new TextField();
}
checkTest();
mainFrame.remove(textFieldToSerialize);
}
private void checkTest() {
if (!textField.getText().equals(expectedString)) {
testFailMessage.append("TestFail line : ");
testFailMessage.append(Thread.currentThread().getStackTrace()[2].
getLineNumber());
testFailMessage.append(" TextField.getText() : \"");
testFailMessage.append(textField.getText());
testFailMessage.append("\" does not match expected string : \"");
testFailMessage.append(expectedString).append("\"");
testFailMessage.append(System.getProperty("line.separator"));
testFailCount++;
isTestFail = true;
}
}
private void checkFailures() {
if (isTestFail) {
testFailMessage.insert(0, "Test Fail count : " + testFailCount
+ System.getProperty("line.separator"));
dispose();
throw new RuntimeException(testFailMessage.toString());
}
}
private void dispose() {
if (mainFrame != null) {
mainFrame.dispose();
}
}
public static void main(String[] args) {
EOLTest testEOL = new EOLTest();
testEOL.testConstructor1();
testEOL.testConstructor2();
testEOL.testConstructor3();
testEOL.testSetText();
testEOL.testDeserialization();
testEOL.checkFailures();
testEOL.dispose();
}
}

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @key headful
* @bug 8211267
* @summary verifies TextField.setFont()
*/
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
public class FontChangeTest {
static Frame frame;
static Panel p1;
static TextField tf1;
static boolean failed = false;
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
testFont1();
} catch (StackOverflowError soe) {
failed = true;
}
}
});
frame.dispose();
if (failed) {
throw new Exception("Test failed");
}
}
private static void testFont1() {
frame = new Frame();
frame.setLayout(new BorderLayout());
p1 = new Panel();
p1.setLayout(new FlowLayout());
tf1 = new TextField("ABC");
tf1.setFont(new Font("Dialog", Font.PLAIN, 12));
p1.add(tf1);
frame.add(p1, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
p1.setVisible(false);
tf1.setText("xyz");
tf1.setFont(new Font("Dialog", Font.PLAIN, 24));
p1.setVisible(true);
frame.pack();
}
}

View file

@ -0,0 +1,104 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4100188
* @key headful
* @summary Make sure that TextFields contain all of,
* and exactly, the text that was entered into them.
* @run main GetTextTest
*/
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
public class GetTextTest extends Frame implements ActionListener {
private final String s = "test string";
private volatile String ac;
private TextField t;
private Point location;
private Dimension size;
public void setupGUI() {
setLayout(new FlowLayout(FlowLayout.LEFT));
t = new TextField(s, 32);
add(new Label("Hit <CR> after text"));
add(t);
t.addActionListener(this);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
ac = evt.getActionCommand();
}
public void performTest() throws AWTException, InterruptedException,
InvocationTargetException {
EventQueue.invokeAndWait(() -> {
location = t.getLocationOnScreen();
size = t.getSize();
});
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.delay(1000);
robot.waitForIdle();
robot.mouseMove(location.x + size.width - 3, location.y + (size.height / 2));
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(1000);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
if (!s.equals(ac)) {
throw new RuntimeException("Action command should be the same as text field content");
}
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException, AWTException {
GetTextTest test = new GetTextTest();
EventQueue.invokeAndWait(test::setupGUI);
try {
test.performTest();
} finally {
EventQueue.invokeAndWait(test::dispose);
}
}
}

View file

@ -0,0 +1,110 @@
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @key headful
* @bug 8149636
* @summary TextField over scrolls to right when selecting text towards right.
* @requires os.family == "windows"
* @run main OverScrollTest
*/
import java.awt.Frame;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.event.InputEvent;
public class OverScrollTest {
Frame mainFrame;
TextField textField;
Robot robot;
OverScrollTest() {
try {
robot = new Robot();
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}
mainFrame = new Frame();
mainFrame.setSize(400, 200);
mainFrame.setLocation(200, 200);
mainFrame.setLayout(new FlowLayout());
textField = new TextField(10);
textField.setSize(300, 100);
textField.setText("123456 789123");
mainFrame.add(textField);
mainFrame.setVisible(true);
textField.requestFocusInWindow();
}
public void dispose() {
if (mainFrame != null) {
mainFrame.dispose();
}
}
public void performTest() {
Point loc = textField.getLocationOnScreen();
Rectangle textFieldBounds = new Rectangle();
textField.getBounds(textFieldBounds);
// Move mouse at center in first row of TextField.
robot.mouseMove(loc.x + textFieldBounds.width / 2, loc.y + 5);
// Perform selection by scrolling to right from end of char sequence.
robot.mousePress(InputEvent.BUTTON1_MASK);
for (int i = 0; i < textFieldBounds.width; i += 15) {
robot.mouseMove(i + loc.x + textFieldBounds.width / 2, loc.y + 5);
robot.delay(10);
}
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
// Perform double click on beginning word of TextField
robot.mouseMove(loc.x + 5, loc.y + 5);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
dispose();
if (!textField.getSelectedText().contains("123456")) {
throw new RuntimeException ("TextField over scrolled towards right. "
+ "Selected text should contain: '123456' "
+ "Actual selected test: '" + textField.getSelectedText() + "'");
}
}
public static void main(String argv[]) throws RuntimeException {
OverScrollTest test = new OverScrollTest();
test.performTest();
}
}

View file

@ -0,0 +1,261 @@
/*
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@bug 4118621 8149636
@summary Test the selection scrolling in TextField.
@run main/manual ScrollSelectionTest
*/
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScrollSelectionTest {
static Frame mainFrame;
static TextField textField;
private static void init() throws Exception {
String[] instructions
= {
"INSTRUCTIONS: There are 2 Tests",
"Test1: Text visibility with Scroll",
"This is a test for a win32 specific problem",
"If you see all the letters from 'a' to 'z' and",
"letters from 'a' to 't' are selected then test passes.",
"You may have to activate the frame to see the selection",
"highlighted (e.g. by clicking on frame's title).",
".",
"Test2: Flicker with selection scroll",
"Mouse press on the TextField text.",
"Move mouse towards left or right with selecting text.",
"Move mouse away outside the bounds of TextField.",
"No flicker should be observed.",
};
Sysout.createDialog();
Sysout.printInstructions(instructions);
}
public static void initTestWindow() {
mainFrame = new Frame("ScrollSelectionTest frame");
mainFrame.setBounds(500, 0, 400, 200);
textField = new TextField(40);
textField.setText("abcdefghijklmnopqrstuvwxyz");
mainFrame.add(textField);
mainFrame.setLayout(new FlowLayout());
textField.select(0, 20);
mainFrame.setVisible(true);
}
public static void dispose() {
Sysout.dispose();
mainFrame.dispose();
}
/**
* ***************************************************
* Standard Test Machinery Section DO NOT modify anything in this section --
* it's a standard chunk of code which has all of the synchronization
* necessary for the test harness. By keeping it the same in all tests, it
* is easier to read and understand someone else's test, as well as insuring
* that all tests behave correctly with the test harness. There is a section
* following this for test-defined classes
* ****************************************************
*/
private static boolean theTestPassed = false;
private static boolean testGeneratedInterrupt = false;
private static String failureMessage = "";
private static Thread mainThread = null;
final private static int sleepTime = 300000;
public static void main(String args[]) throws Exception {
mainThread = Thread.currentThread();
try {
init();
initTestWindow();
} catch (Exception e) {
e.printStackTrace();
}
try {
mainThread.sleep(sleepTime);
} catch (InterruptedException e) {
dispose();
if (testGeneratedInterrupt && !theTestPassed) {
throw new Exception(failureMessage);
}
}
if (!testGeneratedInterrupt) {
dispose();
throw new RuntimeException("Timed out after " + sleepTime / 1000
+ " seconds");
}
}
public static synchronized void pass() {
theTestPassed = true;
testGeneratedInterrupt = true;
mainThread.interrupt();
}
public static synchronized void fail(String whyFailed) {
theTestPassed = false;
testGeneratedInterrupt = true;
failureMessage = whyFailed;
mainThread.interrupt();
}
}
// *********** End Standard Test Machinery Section **********
/**
* **************************************************
* Standard Test Machinery DO NOT modify anything below -- it's a standard chunk
* of code whose purpose is to make user interaction uniform, and thereby make
* it simpler to read and understand someone else's test.
* **************************************************
*/
/**
* This is part of the standard test machinery. It creates a dialog (with the
* instructions), and is the interface for sending text messages to the user. To
* print the instructions, send an array of strings to Sysout.createDialog
* WithInstructions method. Put one line of instructions per array entry. To
* display a message for the tester to see, simply call Sysout.println with the
* string to be displayed. This mimics System.out.println but works within the
* test harness as well as standalone.
*/
class Sysout {
private static TestDialog dialog;
private static Frame frame;
public static void createDialog() {
frame = new Frame();
dialog = new TestDialog(frame, "Instructions");
String[] defInstr = {"Instructions will appear here. ", ""};
dialog.printInstructions(defInstr);
dialog.setVisible(true);
println("Any messages for the tester will display here.");
}
public static void printInstructions(String[] instructions) {
dialog.printInstructions(instructions);
}
public static void println(String messageIn) {
dialog.displayMessage(messageIn);
}
public static void dispose() {
dialog.dispose();
frame.dispose();
}
}
/**
* This is part of the standard test machinery. It provides a place for the test
* instructions to be displayed, and a place for interactive messages to the
* user to be displayed. To have the test instructions displayed, see Sysout. To
* have a message to the user be displayed, see Sysout. Do not call anything in
* this dialog directly.
*/
class TestDialog extends Dialog implements ActionListener {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
Panel buttonP;
Button failB;
Button passB;
// DO NOT call this directly, go through Sysout
public TestDialog(Frame frame, String name) {
super(frame, name);
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
add("North", instructionsText);
messageText = new TextArea("", 5, maxStringLength, scrollBoth);
add("Center", messageText);
buttonP = new Panel();
passB = new Button("pass");
passB.setActionCommand("pass");
passB.addActionListener(this);
buttonP.add("East", passB);
failB = new Button("Fail");
failB.setActionCommand("fail");
failB.addActionListener(this);
buttonP.add("West", failB);
add("South", buttonP);
pack();
setVisible(true);
}
// DO NOT call this directly, go through Sysout
public void printInstructions(String[] instructions) {
instructionsText.setText("");
String printStr, remainingStr;
for (int i = 0; i < instructions.length; i++) {
remainingStr = instructions[i];
while (remainingStr.length() > 0) {
if (remainingStr.length() >= maxStringLength) {
int posOfSpace = remainingStr.
lastIndexOf(' ', maxStringLength - 1);
if (posOfSpace <= 0) {
posOfSpace = maxStringLength - 1;
}
printStr = remainingStr.substring(0, posOfSpace + 1);
remainingStr = remainingStr.substring(posOfSpace + 1);
}
else {
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append(printStr + "\n");
}
}
}
public void displayMessage(String messageIn) {
messageText.append(messageIn + "\n");
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("pass")) {
ScrollSelectionTest.pass();
} else {
ScrollSelectionTest.fail("User Clicked Fail");
}
}
}

View file

@ -0,0 +1,83 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.event.InputEvent;
/**
* @test
* @key headful
* @bug 8036110
* @author Alexander Scherbatiy
* @summary In TextField can only select text visible or to the left
* @run main SelectionInvisibleTest
*/
public class SelectionInvisibleTest {
private static final String TEXT = "One Two Three Four Five Six Seven Eight Nine ";
private static final String LAST_WORD = "Ten";
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField(TEXT + LAST_WORD, 30);
Panel panel = new Panel(new FlowLayout());
panel.add(textField);
frame.add(panel);
frame.setVisible(true);
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.waitForIdle();
Point point = textField.getLocationOnScreen();
int x = point.x + textField.getWidth() / 2;
int y = point.y + textField.getHeight() / 2;
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON1_MASK);
int N = 10;
int dx = textField.getWidth() / N;
for (int i = 0; i < N; i++) {
x += dx;
robot.mouseMove(x, y);
}
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
if (!textField.getSelectedText().endsWith(LAST_WORD)) {
throw new RuntimeException("Last word is not selected!");
}
}
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Frame;
import java.awt.TextField;
import java.lang.reflect.InvocationTargetException;
import static java.awt.EventQueue.invokeAndWait;
/*
@test
@key headful
@bug 4082144 7150100
@summary Ensures that TextField.select() works when called
before setVisible()
@run main SelectionVisible
*/
public class SelectionVisible {
private static Frame frame;
private static TextField tf;
public static void createTestUI() {
frame = new Frame("Test TextField.select()");
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
frame.add(tf);
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void test() throws InterruptedException,
InvocationTargetException {
String selectedText = tf.getSelectedText();
System.out.println("Selected Text : " + selectedText);
invokeAndWait(SelectionVisible::disposeFrame);
if (!selectedText.equals("012345")) {
throw new RuntimeException("Expected 012345 to be selected but " +
"got " + selectedText);
}
System.out.println("Test passed");
}
private static void disposeFrame() {
if (frame != null) {
frame.dispose();
}
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
invokeAndWait(SelectionVisible::createTestUI);
test();
}
}

View file

@ -0,0 +1,123 @@
/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.TextField;
import java.lang.reflect.InvocationTargetException;
/*
* @test
* @bug 6198290 6277332
* @summary TextField painting is broken when placed on a Scrollpane, XToolkit
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual SetBoundsTest
*/
public class SetBoundsTest extends Frame {
private static final String INSTRUCTIONS = """
1) Make active a frame with a scrollpane and a few components.
2) Please, focus attention on the text components
placed on the upper half of the frame
3) Make sure, that the scrollbar of the frame
have the same position during the test.
4) Bring focus to TextField, try deleting the text.
If the text never gets erased, the test failed
5) Bring focus to TextArea, try deleting the text.
If the text never gets erased, the test failed
6) Please, focus attention on the text components
placed on the lower half of the frame
7) Try input something into TextField.
If you can not input anything into TextField, the test failed
8) Try input something into TextArea.
If you can not input anything into TextArea, the test failed
9) The test passed
""";
public SetBoundsTest() {
setTitle("SetBoundsTest Test Frame");
setLayout(new GridLayout(2, 1));
Panel hw = new Panel();
ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
Container lw = new Container();
fill(hw);
fill(lw);
sp.add(hw);
add(sp);
add(lw);
setSize(600, 600);
sp.setScrollPosition(20, 0);
}
private void fill(Container c) {
Button button = new Button("button");
c.add(button);
button.setBackground(new Color(0xd3ceac));
button.setForeground(new Color(0x000000));
button.move(60, 80);
button.resize(400, 60);
button.show(true);
TextField textfield = new TextField("textfield");
c.add(textfield);
textfield.setBackground(new Color(0xd3ceac));
textfield.setForeground(new Color(0x000000));
textfield.move(60, 20);
textfield.resize(400, 40);
textfield.show(true);
TextArea textarea = new TextArea("textarea");
c.add(textarea);
textarea.setBackground(new Color(0xd3ceac));
textarea.setForeground(new Color(0x000000));
textarea.move(60, 80);
textarea.resize(400, 60);
textarea.show(true);
c.setLayout (new FlowLayout());
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Set Bounds Test Instructions")
.instructions(INSTRUCTIONS)
.testUI(SetBoundsTest::new)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(40)
.build()
.awaitAndCheck();
}
}

View file

@ -0,0 +1,178 @@
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import jdk.test.lib.Platform;
/*
* @test
* @bug 4124697
* @key headful
* @summary Make sure that after setting and then changing the echo
* character again, the TextField continues to function as expected.
* @library /test/lib
* @build jdk.test.lib.Platform
* @run main SetEchoCharTest
*/
public class SetEchoCharTest {
private static Frame frame;
private static Robot robot;
private static TextField tfPassword;
private static Button btn1;
private static Button btn2;
private static volatile Point btn1Loc;
private static volatile Point btn2Loc;
private static final String CHANGE = "Change echo char";
private static final String PRINT = "Print text";
private static final String INITIAL_TEXT = "DefaultPwd";
private static final String CHANGED_TEXT = "NewPwd";
private static final char NEW_ECHO_CHAR = '*';
public static void main(String[] args) throws Exception {
try {
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(50);
EventQueue.invokeAndWait(() -> createAndShowUI());
robot.waitForIdle();
robot.delay(1000);
testEchoChar();
robot.waitForIdle();
robot.delay(200);
testNewEchoChar();
robot.waitForIdle();
robot.delay(200);
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
private static void createAndShowUI() {
frame = new Frame("SetEchoCharTest");
frame.setLayout(new FlowLayout());
Label label = new Label("Pwd:");
tfPassword = new TextField(INITIAL_TEXT, 10);
tfPassword.setEchoChar('X');
tfPassword.addActionListener((ActionListener) e -> {
if (e.getActionCommand().equals(CHANGED_TEXT)) {
//check the 2nd condition only if ActionEvent
//is triggered by changed text
if (!(tfPassword.getText().equals(CHANGED_TEXT)
&& tfPassword.getEchoChar() == NEW_ECHO_CHAR)) {
throw new RuntimeException("Test Failed!!! TextField not working"
+ " as expected after echo char change");
}
}
});
frame.add(label);
frame.add(tfPassword);
btn1 = new Button(PRINT);
btn1.addActionListener(new BtnActionListener());
frame.add(btn1);
btn2 = new Button(CHANGE);
btn2.addActionListener(new BtnActionListener());
frame.add(btn2);
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void testEchoChar() throws Exception {
EventQueue.invokeAndWait(() -> {
btn1Loc = btn1.getLocationOnScreen();
btn2Loc = btn2.getLocationOnScreen();
});
robot.mouseMove(btn1Loc.x + btn1.getWidth() / 2,
btn1Loc.y + btn1.getHeight() / 2);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(1000);
robot.mouseMove(btn2Loc.x + btn2.getWidth() / 2,
btn2Loc.y + btn2.getHeight() / 2);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(1000);
}
private static void testNewEchoChar() {
StringSelection stringSelection = new StringSelection(CHANGED_TEXT);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
int ctrlKey = Platform.isOSX() ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
robot.keyPress(ctrlKey);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(ctrlKey);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
private static class BtnActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String ac = evt.getActionCommand();
if (CHANGE.equals(ac)) {
tfPassword.setText("");
tfPassword.setEchoChar(NEW_ECHO_CHAR);
tfPassword.requestFocus();
}
if (PRINT.equals(ac)) {
if (!tfPassword.getText().equals(INITIAL_TEXT)) {
throw new RuntimeException("Test Failed!!!"
+ " Initial text not as expected");
}
}
}
}
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4222122
* @summary TextField.setEchoCharacter() seems to be broken
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual SetEchoCharTest3
*/
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.lang.reflect.InvocationTargetException;
public class SetEchoCharTest3 extends Frame {
static String INSTRUCTIONS = """
Type in the text field and "*" characters should echo.
If only one "*" echoes and then the system beeps after
the second character is typed, then press Fail, otherwise press Pass.
""";
public SetEchoCharTest3() {
setLayout(new FlowLayout());
add(new Label("Enter text:"));
TextField tf = new TextField(15);
tf.setEchoChar('*');
add(tf);
pack();
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Set Echo Char Test 3")
.testUI(SetEchoCharTest3::new)
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(40)
.build()
.awaitAndCheck();
}
}

View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* @test
* @bug 4226580
* @summary TextField with echoChar add+remove+add seems to be broken
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual SetEchoCharTest4
*/
public class SetEchoCharTest4 extends Frame implements ActionListener {
TextField tf1 = new TextField(8);
TextField tf2 = new TextField(8);
TextField tf3 = new TextField(8);
Button b = new Button("Click Several Times");
static final String INSTRUCTIONS = """
Type in the first text field and * characters will echo.
Type in the second text field and $ characters will echo.
Type in the third text field and # characters will echo.
Hit the button several times. All characters should remain
the same and the test should not crash.
Make sure the actual text matches what you typed in for each field.
Press Pass if everything's ok, otherwise Fail
""";
public SetEchoCharTest4() {
super("SetEchoCharTest4");
setLayout(new FlowLayout());
tf1.setEchoChar('*');
tf2.setEchoChar('$');
tf3.setEchoChar('#');
addStuff();
b.addActionListener(this);
setSize (300, 150);
}
private void addStuff() {
add(tf1);
add(tf2);
add(tf3);
add(b);
}
public void actionPerformed(ActionEvent ae) {
PassFailJFrame.log("TextField1 = " + tf1.getText());
PassFailJFrame.log("TextField2 = " + tf2.getText());
PassFailJFrame.log("TextField3 = " + tf3.getText());
PassFailJFrame.log("--------------");
remove(tf1);
remove(tf2);
remove(tf3);
remove(b);
addStuff();
}
public static void main(String[] args) throws Exception {
PassFailJFrame.builder()
.title("Set Echo Character Test")
.testUI(SetEchoCharTest4::new)
.instructions(INSTRUCTIONS)
.columns(40)
.logArea()
.build()
.awaitAndCheck();
}
}

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import javax.swing.JPanel;
import jdk.test.lib.Platform;
/*
* @test
* @bug 6191897 8354646
* @summary Verifies that ctrl+left/right does not move word-by-word in a TextField
* with echo character set
* @library /java/awt/regtesthelpers /test/lib
* @build PassFailJFrame jdk.test.lib.Platform
* @run main/manual SetEchoCharWordOpsTest
*/
public class SetEchoCharWordOpsTest {
public static void main(String[] args) throws Exception {
String selectAllKey;
String moveKeys;
String selectKeys;
if (Platform.isOSX()) {
selectAllKey = "Cmd + A";
moveKeys = "Alt + Right/Left";
selectKeys = "Shift + Alt + Right/Left";
} else {
selectAllKey = "Ctrl + A";
moveKeys = "Ctrl + Right/Left";
selectKeys = "Shift + Ctrl + Right/Left";
}
String instructions =
"The password field (in the bottom panel) in this test contains"
+ " a few words (3 words).\n"
+ "Move the focus to the text field and press " + selectAllKey + ".\n"
+ "Try moving the caret word-by-word with " + moveKeys + " or"
+ " extending selection with " + selectKeys + "."
+ " You should NOT be able to do that.\n\n"
+ "If you are able to move the caret word-by-word press FAIL,"
+ " else press PASS.";
PassFailJFrame.builder()
.title("SetEchoCharClipboard Instructions")
.instructions(instructions)
.rows((int) instructions.lines().count() + 3)
.columns(45)
.splitUIBottom(SetEchoCharWordOpsTest::createAndShowUI)
.build()
.awaitAndCheck();
}
private static JPanel createAndShowUI() {
JPanel jPanel = new JPanel();
TextField tf = new TextField("one two three", 15);
Label tfLabel = new Label("Password Field:");
jPanel.setLayout(new FlowLayout());
tf.setEchoChar('*');
jPanel.add(tfLabel);
jPanel.add(tf);
return jPanel;
}
}

View file

@ -0,0 +1,105 @@
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
/*
* @test
* @bug 4084454
* @summary Make sure that you can set the text in a "password mode"
* text field and that it echoes as the current echo char.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual SetPasswordTest
*/
public class SetPasswordTest extends Frame implements ActionListener {
private String setText = "Set text";
private String getText = "Get text";
private TextField tfPassword;
static final String INSTRUCTIONS = """
The purpose of this test is to ensure that when using a textField for
password entry that text set programmatically is not shown in the clear.
We also test "pasting" text into the text field.
1. Press the "Set Text" button
Text should appear as '*' chars
- if the string "secret" appears then the test is failed.
2. Use the mouse to select (highlight) all the text and press the DELETE key
3. Use the system's copy/paste functionality to copy a text string from the
desktop or this window, and paste it into the text field.
4. Text should appear in the text field as '*' chars
- if the string you pasted appears then the test is failed.
5. press the "Get Text" button and the string you pasted
should be printed in the log area
- if it prints echo symbols instead the test is failed.
""";
public SetPasswordTest() {
setLayout(new FlowLayout());
tfPassword = new TextField("Initial text", 30);
tfPassword.setEchoChar('*');
add(tfPassword);
Button b1 = new Button(setText);
b1.addActionListener(this);
add(b1);
Button b2 = new Button(getText);
b2.addActionListener(this);
add(b2);
pack();
}
public void actionPerformed(ActionEvent evt) {
String ac = evt.getActionCommand();
if (setText.equals(ac)) {
tfPassword.setText("secret");
}
if (getText.equals(ac)) {
PassFailJFrame.log("Text: \"" + tfPassword.getText() + "\"");
}
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Set Password Test")
.testUI(SetPasswordTest::new)
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(40)
.logArea()
.build()
.awaitAndCheck();
}
}

View file

@ -0,0 +1,114 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@key headful
@bug 8060137
@library ../../regtesthelpers
@build Util
@summary Test TextField setText API
@run main TextFieldEditing
*/
import java.awt.Frame;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.AWTException;
import java.awt.event.KeyEvent;
import test.java.awt.regtesthelpers.Util;
public class TextFieldEditing {
final static Robot robot = Util.createRobot();
private int testFailCount;
private boolean isTestFail;
private StringBuilder testFailMessage;
private Frame mainFrame;
private TextField textField;
private TextFieldEditing() {
testFailMessage = new StringBuilder();
mainFrame = new Frame();
mainFrame.setSize(200, 200);
textField = new TextField();
mainFrame.add(textField);
mainFrame.setVisible(true);
}
private void dispose() {
if (mainFrame != null) {
mainFrame.dispose();
}
}
public static void main(String[] s) {
TextFieldEditing textField = new TextFieldEditing();
textField.testSetText();
textField.checkFailures();
textField.dispose();
}
private void testSetText() {
textField.setText(null);
textField.requestFocus();
Util.clickOnComp(textField, robot);
Util.waitForIdle(robot);
robot.keyPress(KeyEvent.VK_A);
robot.delay(5);
robot.keyRelease(KeyEvent.VK_A);
Util.waitForIdle(robot);
textField.setText(null);
checkTest("");
textField.setText("CaseSensitive");
checkTest("CaseSensitive");
textField.setText("caseSensitive");
checkTest("caseSensitive");
}
private void checkTest(String str) {
if (str != null && !str.equals(textField.getText())) {
testFailMessage.append("TestFail line : ");
testFailMessage.append(Thread.currentThread().getStackTrace()[2].
getLineNumber());
testFailMessage.append(" TextField string : \"");
testFailMessage.append(textField.getText());
testFailMessage.append("\" does not match expected string : \"");
testFailMessage.append(str).append("\"");
testFailMessage.append(System.getProperty("line.separator"));
testFailCount++;
isTestFail = true;
}
}
private void checkFailures() {
if (isTestFail) {
testFailMessage.insert(0, "Test Fail count : " + testFailCount
+ System.getProperty("line.separator"));
dispose();
throw new RuntimeException(testFailMessage.toString());
}
}
}

View file

@ -0,0 +1,89 @@
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
/*
* @test
* @bug 4307281
* @summary verify that after setting the echo char to 0 disguises are
* removed and user input is echoed to the screen unchanged.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual ZeroEchoCharTest
*/
public class ZeroEchoCharTest extends Frame implements ActionListener {
private final TextField textfield = new TextField();
private final Button button1 = new Button("Set echo char to *");
private final Button button2 = new Button("Set echo char to 0");
static final String INSTRUCTIONS = """
1.Type in the text field. The user input must be echoed unchanged.
2.Set echo char to '*' by pressing the corresponding button.
If all characters in the text field aren't immediately replaced
with '*', the test fails.
3.Set echo char to 0 by pressing the corresponding button.
If disguises in the text field don't immediately revert to
the original characters, the test fails.
4.Type in the text field. If the input is echoed unchanged,
the test passes. Otherwise, the test fails.
""";
public ZeroEchoCharTest() {
button1.addActionListener(this);
button2.addActionListener(this);
setLayout(new GridLayout(3, 1));
add(textfield);
add(button1);
add(button2);
pack();
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button1) {
textfield.setEchoChar('*');
} else if (event.getSource() == button2) {
textfield.setEchoChar((char)0);
}
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Zero Echo Char Test")
.testUI(ZeroEchoCharTest::new)
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(40)
.build()
.awaitAndCheck();
}
}