summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-07-26 13:19:35 +0300
committerHenri Sara <henri.sara@gmail.com>2017-07-26 13:19:35 +0300
commitf106026e41c667a4013031f59b0416532fd877bc (patch)
tree155d32aff572a3c21732366ddc0dca34104bbcd0
parentf38da154c1e6f5706c95e7bbc1468d47a0a65104 (diff)
downloadvaadin-framework-f106026e41c667a4013031f59b0416532fd877bc.tar.gz
vaadin-framework-f106026e41c667a4013031f59b0416532fd877bc.zip
Send cursor position to server on blur even if the value has not changed (#9728)
Fixes #9653
-rw-r--r--client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java10
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPosition.java59
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPositionTest.java44
3 files changed, 112 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java
index 23d76d0717..d5a1295f95 100644
--- a/client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java
@@ -15,6 +15,7 @@
*/
package com.vaadin.client.ui.textfield;
+import com.google.gwt.event.dom.client.HasBlurHandlers;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.DeferredWorker;
import com.vaadin.client.annotations.OnStateChange;
@@ -28,7 +29,7 @@ import com.vaadin.ui.AbstractTextField;
/**
* Connector class for AbstractTextField.
- *
+ *
* @since 8.0
*/
public abstract class AbstractTextFieldConnector extends AbstractFieldConnector
@@ -70,6 +71,13 @@ public abstract class AbstractTextFieldConnector extends AbstractFieldConnector
new AbstractTextFieldClientRpcImpl());
ConnectorFocusAndBlurHandler.addHandlers(this);
valueChangeHandler = new ValueChangeHandler(this);
+
+ // Ensures that the cursor position is sent when leaving the field
+ // (if it has changed)
+ if (getWidget() instanceof HasBlurHandlers) {
+ ((HasBlurHandlers) getWidget())
+ .addBlurHandler(event -> sendValueChange());
+ }
}
protected ValueChangeHandler getValueChangeHandler() {
diff --git a/uitest/src/main/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPosition.java b/uitest/src/main/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPosition.java
new file mode 100644
index 0000000000..40b2af3ed7
--- /dev/null
+++ b/uitest/src/main/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPosition.java
@@ -0,0 +1,59 @@
+package com.vaadin.tests.components.textarea;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
+import com.vaadin.ui.AbstractTextField;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.TextArea;
+import com.vaadin.ui.TextField;
+
+@Widgetset("com.vaadin.DefaultWidgetSet")
+public class TextAreaTextFieldCursorPosition extends AbstractTestUIWithLog {
+
+ public static final String GET_POSITION = "getposition";
+ public static final String INSERT = "insert";
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ TextArea textArea = new TextArea("Simple Text Area");
+ textArea.setValue("I am just a piece of random text");
+ textArea.setWidth("500px");
+ addComponent(textArea);
+ TextField textField = new TextField("Simple Text field");
+ textField.setValue("I am just a piece of random text");
+ textField.setWidth("500px");
+ addComponent(textField);
+
+ Button posButton = new Button("Get Position");
+ posButton.setId(GET_POSITION);
+ posButton.addClickListener(c -> {
+ log("TextArea position: " + textArea.getCursorPosition());
+ log("TextField position: " + textField.getCursorPosition());
+ });
+ addComponent(posButton);
+
+ Button insertButton = new Button("Insert");
+ insertButton.setId(INSERT);
+ insertButton.addClickListener(c -> {
+ insert(textArea);
+ insert(textField);
+ });
+ addComponent(insertButton);
+ }
+
+ private void insert(AbstractTextField field) {
+ String value = field.getValue();
+ if (field.getCursorPosition() != -1) {
+ int pos = field.getCursorPosition();
+ log("Insert position: " + field.getCursorPosition());
+ value = value.substring(0, pos) + "-insertedtext-"
+ + value.substring(pos, value.length());
+ } else {
+ value += "-appendedtext";
+ }
+ field.setValue(value);
+
+ }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPositionTest.java b/uitest/src/test/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPositionTest.java
new file mode 100644
index 0000000000..89065050dc
--- /dev/null
+++ b/uitest/src/test/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPositionTest.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2000-2016 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.components.textarea;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.TextAreaElement;
+import com.vaadin.testbench.elements.TextFieldElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class TextAreaTextFieldCursorPositionTest extends SingleBrowserTest {
+
+ @Test
+ public void positionUpdatedWithoutTextChanges() {
+ openTestURL();
+ $(ButtonElement.class).id(TextAreaTextFieldCursorPosition.GET_POSITION)
+ .click();
+ Assert.assertEquals("2. TextField position: -1", getLogRow(0));
+ Assert.assertEquals("1. TextArea position: -1", getLogRow(1));
+
+ $(TextFieldElement.class).first().focus();
+ $(TextAreaElement.class).first().focus();
+ $(ButtonElement.class).id(TextAreaTextFieldCursorPosition.GET_POSITION)
+ .click();
+ Assert.assertTrue(getLogRow(0).startsWith("4. TextField position:"));
+ Assert.assertNotEquals("4. TextField position: -1", getLogRow(0));
+ Assert.assertNotEquals("3. TextArea position: -1", getLogRow(1));
+ }
+}