]> source.dussan.org Git - vaadin-framework.git/commitdiff
Send cursor position to server on blur even if the value has not changed (#9728)
authorArtur <artur@vaadin.com>
Wed, 26 Jul 2017 10:19:35 +0000 (13:19 +0300)
committerHenri Sara <henri.sara@gmail.com>
Wed, 26 Jul 2017 10:19:35 +0000 (13:19 +0300)
Fixes #9653

client/src/main/java/com/vaadin/client/ui/textfield/AbstractTextFieldConnector.java
uitest/src/main/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPosition.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/textarea/TextAreaTextFieldCursorPositionTest.java [new file with mode: 0644]

index 23d76d07172bfaf35c6d185c081180bb5971543b..d5a1295f950eb193f1a9e13dfbf23493c7ef5e0c 100644 (file)
@@ -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 (file)
index 0000000..40b2af3
--- /dev/null
@@ -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 (file)
index 0000000..8906505
--- /dev/null
@@ -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));
+    }
+}