diff options
author | Anastasia Smirnova <anasmi@utu.fi> | 2018-03-05 22:29:13 -1200 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2018-03-06 13:29:13 +0300 |
commit | 46e0c06fe758b109f62edda7d4665fc21417759c (patch) | |
tree | a363a7dc11785c145b163bb988c14f28f1f9218e /uitest | |
parent | bc515ea11d08850a59162a22874e929eb4562d37 (diff) | |
download | vaadin-framework-46e0c06fe758b109f62edda7d4665fc21417759c.tar.gz vaadin-framework-46e0c06fe758b109f62edda7d4665fc21417759c.zip |
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
Diffstat (limited to 'uitest')
2 files changed, 108 insertions, 0 deletions
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<Long> range = (List<Long>) executeScript( + "return [arguments[0].selectionStart,arguments[0].selectionEnd]", + textField); + return new Selection(Math.toIntExact(range.get(0)), + Math.toIntExact(range.get(1))); + } +} |