8060137: Removing Text from TextField / TextArea is not possible after typing
Reviewed-by: ssadetsky, psadhukhan
--- a/jdk/src/java.desktop/share/classes/java/awt/TextComponent.java Tue Dec 08 11:25:47 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/TextComponent.java Wed Dec 09 02:41:51 2015 +0400
@@ -229,15 +229,21 @@
* @see java.awt.TextComponent#getText
*/
public synchronized void setText(String t) {
- boolean skipTextEvent = (text == null || text.isEmpty())
- && (t == null || t.isEmpty());
- text = (t != null) ? t : "";
+ if (t == null) {
+ t = "";
+ }
TextComponentPeer peer = (TextComponentPeer)this.peer;
- // Please note that we do not want to post an event
- // if TextArea.setText() or TextField.setText() replaces an empty text
- // by an empty text, that is, if component's text remains unchanged.
- if (peer != null && !skipTextEvent) {
- peer.setText(text);
+ if (peer != null) {
+ text = peer.getText();
+ // Please note that we do not want to post an event
+ // if TextArea.setText() or TextField.setText() replaces text
+ // by same text, that is, if component's text remains unchanged.
+ if (!t.equals(text)) {
+ text = t;
+ peer.setText(text);
+ }
+ } else {
+ text = t;
}
}
--- a/jdk/test/java/awt/TextArea/TextAreaEditing/TextAreaEditing.java Tue Dec 08 11:25:47 2015 +0300
+++ b/jdk/test/java/awt/TextArea/TextAreaEditing/TextAreaEditing.java Wed Dec 09 02:41:51 2015 +0400
@@ -23,16 +23,23 @@
/*
@test
- @bug 8040322
+ @bug 8040322 8060137
+ @library ../../regtesthelpers
+ @build Util
@summary Test TextArea APIs replaceRange, insert, append & setText
@run main TextAreaEditing
*/
import java.awt.Frame;
+import java.awt.Robot;
import java.awt.TextArea;
+import java.awt.AWTException;
+import java.awt.event.KeyEvent;
+import test.java.awt.regtesthelpers.Util;
public class TextAreaEditing {
+ final static Robot robot = Util.createRobot();
private int testFailCount;
private boolean isTestFail;
private StringBuilder testFailMessage;
@@ -61,6 +68,7 @@
textArea.testReplaceRange();
textArea.testInsert();
textArea.testAppend();
+ textArea.testSetText();
textArea.checkFailures();
textArea.dispose();
}
@@ -119,6 +127,24 @@
checkTest("");
}
+ private void testSetText() {
+ textArea.setText(null);
+ textArea.requestFocus();
+ Util.clickOnComp(textArea, robot);
+ Util.waitForIdle(robot);
+ robot.keyPress(KeyEvent.VK_A);
+ robot.delay(5);
+ robot.keyRelease(KeyEvent.VK_A);
+ Util.waitForIdle(robot);
+ textArea.setText(null);
+ checkTest("");
+ textArea.setText("CaseSensitive");
+ checkTest("CaseSensitive");
+ textArea.setText("caseSensitive");
+ checkTest("caseSensitive");
+
+ }
+
private void checkTest(String str) {
if (str != null && !str.equals(textArea.getText())) {
testFailMessage.append("TestFail line : ");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/TextField/TextFieldEditing/TextFieldEditing.java Wed Dec 09 02:41:51 2015 +0400
@@ -0,0 +1,113 @@
+/*
+ * 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
+ @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());
+ }
+ }
+}