aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorTabSkipsNonEditableCellsTest.java
blob: fca0439fdfb27f3ff22ad768c541223b94a8cc60 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.vaadin.tests.components.grid;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.testbench.elements.TextFieldElement;
import com.vaadin.testbench.parallel.TestCategory;
import com.vaadin.tests.tb3.MultiBrowserTest;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import static org.junit.Assert.fail;

/**
 * Makes sure that pressing Tab when the Grid is in edit mode will make focus
 * skip cells that are not editable.
 */
@TestCategory("grid")
public class GridEditorTabSkipsNonEditableCellsTest extends MultiBrowserTest {
    /**
     * The grid with 7 columns. First, third and fifth columns are not editable.
     */
    private GridElement grid;

    @Override
    public void setup() throws Exception {
        super.setup();
        openTestURL();
        grid = $(GridElement.class).first();
    }

    @Test
    public void tabSkipsOverNotEditableFieldBuffered() {
        setBuffered(true);
        openEditor(0, 1);
        pressTab();
        Assert.assertEquals("col3_0", getFocusedEditorCellContents());
    }

    @Test
    public void tabDoesNothingIfAlreadyOnLastEditableFieldBuffered() {
        setBuffered(true);
        openEditor(0, 3);
        pressTab();
        Assert.assertEquals("col3_0", getFocusedEditorCellContents());
    }

    @Test
    public void tabSkipsOverNotEditableFieldUnbuffered() {
        setBuffered(false);
        openEditor(0, 1);
        pressTab();
        Assert.assertEquals("col3_0", getFocusedEditorCellContents());
    }

    @Test
    public void tabMovesToNextRowFirstEditableFieldUnbuffered() {
        setBuffered(false);
        openEditor(0, 3);
        pressTab();
        Assert.assertEquals("col1_1", getFocusedEditorCellContents());
    }

    @Test
    public void shiftTabSkipsOverNotEditableFieldBuffered() {
        setBuffered(true);
        openEditor(0, 3);
        pressShiftTab();
        Assert.assertEquals("col1_0", getFocusedEditorCellContents());
    }

    @Test
    public void shiftTabDoesNothingIfAlreadyOnLastEditableFieldBuffered() {
        setBuffered(true);
        openEditor(0, 1);
        pressShiftTab();
        Assert.assertEquals("col1_0", getFocusedEditorCellContents());
    }

    @Test
    public void shiftTabSkipsOverNotEditableFieldUnbuffered() {
        setBuffered(false);
        openEditor(0, 3);
        pressShiftTab();
        Assert.assertEquals("col1_0", getFocusedEditorCellContents());
    }

    @Test
    public void shiftTabMovesToNextRowFirstEditableFieldUnbuffered() {
        setBuffered(false);
        openEditor(1, 1);
        pressShiftTab();
        Assert.assertEquals("col3_0", getFocusedEditorCellContents());
    }

    private void openEditor(int rowIndex, int colIndex) {
        grid.getCell(rowIndex, colIndex).doubleClick();
    }

    private void pressTab() {
        new Actions(getDriver()).sendKeys(Keys.TAB).perform();
    }

    private void pressShiftTab() {
        new Actions(getDriver()).keyDown(Keys.SHIFT).sendKeys(Keys.TAB)
                .keyUp(Keys.SHIFT).perform();
    }

    private void setBuffered(boolean buffered) {
        $(ButtonElement.class).caption(buffered ? "Set Editor Buffered Mode On"
                : "Set Editor Buffered Mode Off").first().click();
    }

    private String getFocusedEditorCellContents() {
        final GridElement.GridEditorElement editor = grid.getEditor();
        final WebElement focusedElement = getFocusedElement();
        for (int i = 0; i < 7; i++) {
            if (editor.isEditable(i)
                    && editor.getField(i).equals(focusedElement)) {
                return (editor.getField(i).wrap(TextFieldElement.class))
                        .getValue();
            }
        }
        fail("Currently focused element is not a cell editor: "
                + focusedElement);
        return null;
    }
}