From 4af793d06a0f4a6577aad13403ca7982c6fce224 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sauli=20T=C3=A4hk=C3=A4p=C3=A4=C3=A4?= Date: Fri, 16 Jan 2015 23:19:06 +0200 Subject: [PATCH] Prevent field from updating when removing text change listener. (#16270) Change-Id: I65c598ae71414550eb648fabf6e94fb1dabbef97 --- .../ui/textfield/TextFieldConnector.java | 5 ++- .../src/com/vaadin/ui/AbstractTextField.java | 33 +++++++++---------- .../textfield/RemoveTextChangeListener.java | 32 ++++++++++++++++++ .../RemoveTextChangeListenerTest.java | 24 ++++++++++++++ 4 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListener.java create mode 100644 uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListenerTest.java diff --git a/client/src/com/vaadin/client/ui/textfield/TextFieldConnector.java b/client/src/com/vaadin/client/ui/textfield/TextFieldConnector.java index 0d85e98ee3..fad094b87f 100644 --- a/client/src/com/vaadin/client/ui/textfield/TextFieldConnector.java +++ b/client/src/com/vaadin/client/ui/textfield/TextFieldConnector.java @@ -89,10 +89,9 @@ public class TextFieldConnector extends AbstractFieldConnector implements * side value). is updated only when it looses focus, so we * force updating if not focused. Lost focus issue appeared in (#15144) */ - if (!(Util.getFocusedElement() == getWidget().getElement()) + if (Util.getFocusedElement() != getWidget().getElement() || !uidl.getBooleanAttribute(TextFieldConstants.ATTR_NO_VALUE_CHANGE_BETWEEN_PAINTS) - || getWidget().valueBeforeEdit == null - || !text.equals(getWidget().valueBeforeEdit)) { + || getWidget().valueBeforeEdit == null) { getWidget().updateFieldContent(text); } diff --git a/server/src/com/vaadin/ui/AbstractTextField.java b/server/src/com/vaadin/ui/AbstractTextField.java index 93025ac0fd..14c135228c 100644 --- a/server/src/com/vaadin/ui/AbstractTextField.java +++ b/server/src/com/vaadin/ui/AbstractTextField.java @@ -126,25 +126,22 @@ public abstract class AbstractTextField extends AbstractField implements selectionPosition = -1; } - if (hasListeners(TextChangeEvent.class)) { - target.addAttribute(TextFieldConstants.ATTR_TEXTCHANGE_EVENTMODE, - getTextChangeEventMode().toString()); - target.addAttribute(TextFieldConstants.ATTR_TEXTCHANGE_TIMEOUT, - getTextChangeTimeout()); - if (lastKnownTextContent != null) { - /* - * The field has be repainted for some reason (e.g. caption, - * size, stylename), but the value has not been changed since - * the last text change event. Let the client side know about - * the value the server side knows. Client side may then ignore - * the actual value, depending on its state. - */ - target.addAttribute( - TextFieldConstants.ATTR_NO_VALUE_CHANGE_BETWEEN_PAINTS, - true); - } + target.addAttribute(TextFieldConstants.ATTR_TEXTCHANGE_EVENTMODE, + getTextChangeEventMode().toString()); + target.addAttribute(TextFieldConstants.ATTR_TEXTCHANGE_TIMEOUT, + getTextChangeTimeout()); + if (lastKnownTextContent != null) { + /* + * The field has be repainted for some reason (e.g. caption, size, + * stylename), but the value has not been changed since the last + * text change event. Let the client side know about the value the + * server side knows. Client side may then ignore the actual value, + * depending on its state. + */ + target.addAttribute( + TextFieldConstants.ATTR_NO_VALUE_CHANGE_BETWEEN_PAINTS, + true); } - } @Override diff --git a/uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListener.java b/uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListener.java new file mode 100644 index 0000000000..339160e6ff --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListener.java @@ -0,0 +1,32 @@ +package com.vaadin.tests.components.textfield; + +import com.vaadin.event.FieldEvents; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.TextField; + +public class RemoveTextChangeListener extends AbstractTestUI { + @Override + protected void setup(VaadinRequest request) { + final TextField textfield = new TextField(); + + textfield.addTextChangeListener(new FieldEvents.TextChangeListener() { + @Override + public void textChange(FieldEvents.TextChangeEvent event) { + textfield.removeTextChangeListener(this); + } + }); + + addComponent(textfield); + } + + @Override + protected Integer getTicketNumber() { + return 16270; + } + + @Override + protected String getTestDescription() { + return "Removing text change listener on text change event should not reset the input."; + } +} diff --git a/uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListenerTest.java b/uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListenerTest.java new file mode 100644 index 0000000000..0b876864bc --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/textfield/RemoveTextChangeListenerTest.java @@ -0,0 +1,24 @@ +package com.vaadin.tests.components.textfield; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; + +import com.vaadin.testbench.elements.TextFieldElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class RemoveTextChangeListenerTest extends MultiBrowserTest { + + @Test + public void serverValueIsUpdated() { + openTestURL(); + + TextFieldElement textfield = $(TextFieldElement.class).first(); + + textfield.sendKeys("f"); + + assertThat(textfield.getValue(), is("f")); + } + +} \ No newline at end of file -- 2.39.5