From 46e0c06fe758b109f62edda7d4665fc21417759c Mon Sep 17 00:00:00 2001 From: Anastasia Smirnova Date: Mon, 5 Mar 2018 22:29:13 -1200 Subject: [PATCH] Enable setting cursor after the last character (#10670) * Enable setting cursor after the last character Resolves #9157 * Reformat the code * Reformat the code using conventions --- .../textfield/AbstractTextFieldConnector.java | 3 +- .../TextFieldTestCursorPosition.java | 46 ++++++++++++++ .../TextFieldTestCursorPositionTest.java | 62 +++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPosition.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPositionTest.java 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 d5a1295f95..4d73996b7f 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 @@ -40,7 +40,8 @@ public abstract class AbstractTextFieldConnector extends AbstractFieldConnector @Override public void selectRange(int start, int length) { int textLength = getAbstractTextField().getValue().length(); - start = restrictTo(start, 0, textLength - 1); + //Enable setting cursor position after the last character + start = restrictTo(start, 0, length == 0 ? textLength : (textLength - 1)); length = restrictTo(length, 0, textLength - start); getAbstractTextField().setSelectionRange(start, length); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPosition.java b/uitest/src/main/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPosition.java new file mode 100644 index 0000000000..612e0f80a8 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPosition.java @@ -0,0 +1,46 @@ +package com.vaadin.tests.components.textfield; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.TextField; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class TextFieldTestCursorPosition extends AbstractTestUI { + + static final String DEFAULT_TEXT = "So we have some text to select"; + static final String BUTTON_SETPOSITION = "buttonPos"; + static final String BUTTON_SETRANGE = "rS"; + static final String RANGE_LENGTH_TF = "rLTF"; + static final String CURSOR_POS_TF = "cpTF"; + static final int valueLength = DEFAULT_TEXT.length(); + final TextField textField = new TextField( + "Set cursor position after the last character"); + final TextField textField1 = new TextField("Set Selection range"); + + @Override + protected void setup(VaadinRequest request) { + textField.setValue(DEFAULT_TEXT); + textField.setId(CURSOR_POS_TF); + textField.setWidth("500px"); + + Button posButton = new Button("Set Position to the last character"); + posButton.setId(BUTTON_SETPOSITION); + posButton.addClickListener( + c -> textField.setCursorPosition(valueLength)); + addComponent(textField); + addComponent(posButton); + + textField1.setId(RANGE_LENGTH_TF); + textField1.setValue(DEFAULT_TEXT); + textField1.setWidth("500px"); + + Button selButton = new Button("Set selection range"); + selButton.setId(BUTTON_SETRANGE); + selButton.addClickListener( + c -> textField1.setSelection(valueLength / 2, valueLength)); + addComponent(textField1); + addComponent(selButton); + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPositionTest.java b/uitest/src/test/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPositionTest.java new file mode 100644 index 0000000000..fbba6eddcc --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPositionTest.java @@ -0,0 +1,62 @@ +package com.vaadin.tests.components.textfield; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.MultiBrowserTest; +import org.junit.Test; +import org.openqa.selenium.WebElement; +import static org.junit.Assert.assertEquals; + +import java.util.List; + +public class TextFieldTestCursorPositionTest extends MultiBrowserTest { + + private WebElement textFieldCheckCursor; + private WebElement textFieldCheckRang; + + @Test + public void testSelection() { + openTestURL(); + textFieldCheckCursor = findElement( + By.id(TextFieldTestCursorPosition.CURSOR_POS_TF)); + textFieldCheckRang = findElement( + By.id(TextFieldTestCursorPosition.RANGE_LENGTH_TF)); + + // Range selected correctly + setSelectionRange(); + assertSelection((TextFieldTestCursorPosition.valueLength) / 2, + TextFieldTestCursorPosition.valueLength, textFieldCheckRang); + + // Cursor position + setCursorPosition(); + assertCursorPosition(TextFieldTestCursorPosition.valueLength); + } + + private void assertCursorPosition(int i) { + assertSelection(i, i, textFieldCheckCursor); + } + + private void setCursorPosition() { + $(ButtonElement.class) + .id(TextFieldTestCursorPosition.BUTTON_SETPOSITION).click(); + } + + private void setSelectionRange() { + $(ButtonElement.class).id(TextFieldTestCursorPosition.BUTTON_SETRANGE) + .click(); + } + + // expected and actual + private void assertSelection(int start, int length, WebElement textField) { + assertEquals(new Selection(start, length), getSelection(textField)); + } + + private Selection getSelection(WebElement textField) { + @SuppressWarnings("unchecked") + List range = (List) executeScript( + "return [arguments[0].selectionStart,arguments[0].selectionEnd]", + textField); + return new Selection(Math.toIntExact(range.get(0)), + Math.toIntExact(range.get(1))); + } +} -- 2.39.5