aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/textfield/TextFieldTestCursorPositionTest.java
blob: fbba6eddcccc96999a31915a7cfa5a4ec946eb6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)));
    }
}