]> source.dussan.org Git - vaadin-framework.git/blob
aacb32c98237a6584277555a5c71a376b35fe5e4
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2014 Vaadin Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.vaadin.tests.components.grid.basicfeatures.server;
17
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
20
21 import org.junit.Test;
22 import org.openqa.selenium.By;
23 import org.openqa.selenium.Keys;
24 import org.openqa.selenium.WebElement;
25 import org.openqa.selenium.interactions.Actions;
26
27 import com.vaadin.testbench.elements.GridElement;
28 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
29 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
30
31 public class GridKeyboardNavigationTest extends GridBasicFeaturesTest {
32
33     @Test
34     public void testCellFocusOnClick() {
35         openTestURL();
36
37         GridElement grid = getGridElement();
38         assertTrue("Body cell 0, 0 is not focused on init.",
39                 grid.getCell(0, 0).isFocused());
40         grid.getCell(5, 2).click();
41         assertFalse("Body cell 0, 0 was still focused after clicking",
42                 grid.getCell(0, 0).isFocused());
43         assertTrue("Body cell 5, 2 is not focused after clicking",
44                 grid.getCell(5, 2).isFocused());
45     }
46
47     @Test
48     public void testCellNotFocusedWhenRendererHandlesEvent() {
49         openTestURL();
50
51         GridElement grid = getGridElement();
52         assertTrue("Body cell 0, 0 is not focused on init.",
53                 grid.getCell(0, 0).isFocused());
54         grid.getHeaderCell(0, 3).click();
55         assertFalse("Body cell 0, 0 is focused after click on header.",
56                 grid.getCell(0, 0).isFocused());
57         assertTrue("Header cell 0, 3 is not focused after click on header.",
58                 grid.getHeaderCell(0, 3).isFocused());
59     }
60
61     @Test
62     public void testSimpleKeyboardNavigation() {
63         openTestURL();
64
65         GridElement grid = getGridElement();
66         grid.getCell(0, 0).click();
67
68         new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN).perform();
69         assertTrue("Body cell 1, 0 is not focused after keyboard navigation.",
70                 grid.getCell(1, 0).isFocused());
71
72         new Actions(getDriver()).sendKeys(Keys.ARROW_RIGHT).perform();
73         assertTrue("Body cell 1, 1 is not focused after keyboard navigation.",
74                 grid.getCell(1, 1).isFocused());
75
76         int i;
77         for (i = 1; i < 40; ++i) {
78             new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN).perform();
79         }
80
81         assertFalse("Grid has not scrolled with cell focus",
82                 isElementPresent(By.xpath("//td[text() = '(0, 0)']")));
83         assertTrue("Cell focus is not visible",
84                 isElementPresent(By.xpath("//td[text() = '(" + i + ", 0)']")));
85         assertTrue("Body cell " + i + ", 1 is not focused",
86                 grid.getCell(i, 1).isFocused());
87     }
88
89     @Test
90     public void testNavigateFromHeaderToBody() {
91         openTestURL();
92
93         GridElement grid = getGridElement();
94         grid.scrollToRow(300);
95         new Actions(driver).moveToElement(grid.getHeaderCell(0, 7)).click()
96                 .perform();
97         grid.scrollToRow(280);
98
99         assertTrue("Header cell is not focused.",
100                 grid.getHeaderCell(0, 7).isFocused());
101         new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN).perform();
102         assertTrue("Body cell 280, 7 is not focused",
103                 grid.getCell(280, 7).isFocused());
104     }
105
106     @Test
107     public void testNavigationFromFooterToBody() {
108         openTestURL();
109
110         selectMenuPath("Component", "Footer", "Visible");
111
112         GridElement grid = getGridElement();
113         grid.scrollToRow(300);
114         grid.getFooterCell(0, 2).click();
115
116         assertTrue("Footer cell does not have focus.",
117                 grid.getFooterCell(0, 2).isFocused());
118         new Actions(getDriver()).sendKeys(Keys.ARROW_UP).perform();
119         assertTrue("Body cell 300, 2 does not have focus.",
120                 grid.getCell(300, 2).isFocused());
121     }
122
123     @Test
124     public void testNavigateBetweenHeaderAndBodyWithTab() {
125         openTestURL();
126
127         GridElement grid = getGridElement();
128         grid.getCell(10, 2).click();
129
130         assertTrue("Body cell 10, 2 does not have focus",
131                 grid.getCell(10, 2).isFocused());
132         new Actions(getDriver()).keyDown(Keys.SHIFT).sendKeys(Keys.TAB)
133                 .keyUp(Keys.SHIFT).perform();
134         assertTrue("Header cell 0, 2 does not have focus",
135                 grid.getHeaderCell(0, 2).isFocused());
136         new Actions(getDriver()).sendKeys(Keys.TAB).perform();
137         assertTrue("Body cell 10, 2 does not have focus",
138                 grid.getCell(10, 2).isFocused());
139
140         // Navigate out of the Grid and try to navigate with arrow keys.
141         new Actions(getDriver()).keyDown(Keys.SHIFT).sendKeys(Keys.TAB)
142                 .sendKeys(Keys.TAB).keyUp(Keys.SHIFT).sendKeys(Keys.ARROW_DOWN)
143                 .perform();
144         assertTrue("Header cell 0, 2 does not have focus",
145                 grid.getHeaderCell(0, 2).isFocused());
146     }
147
148     @Test
149     public void testNavigateBetweenFooterAndBodyWithTab() {
150         openTestURL();
151
152         selectMenuPath("Component", "Footer", "Visible");
153
154         GridElement grid = getGridElement();
155         grid.getCell(10, 2).click();
156
157         assertTrue("Body cell 10, 2 does not have focus",
158                 grid.getCell(10, 2).isFocused());
159         new Actions(getDriver()).sendKeys(Keys.TAB).perform();
160         assertTrue("Footer cell 0, 2 does not have focus",
161                 grid.getFooterCell(0, 2).isFocused());
162         new Actions(getDriver()).keyDown(Keys.SHIFT).sendKeys(Keys.TAB)
163                 .keyUp(Keys.SHIFT).perform();
164         assertTrue("Body cell 10, 2 does not have focus",
165                 grid.getCell(10, 2).isFocused());
166
167         // Navigate out of the Grid and try to navigate with arrow keys.
168         new Actions(getDriver()).sendKeys(Keys.TAB).sendKeys(Keys.TAB)
169                 .sendKeys(Keys.ARROW_UP).perform();
170         assertTrue("Footer cell 0, 2 does not have focus",
171                 grid.getFooterCell(0, 2).isFocused());
172     }
173
174     @Test
175     public void testHomeEnd() throws Exception {
176         openTestURL();
177
178         getGridElement().getCell(100, 2).click();
179
180         new Actions(getDriver()).sendKeys(Keys.HOME).perform();
181         assertTrue("First row is not visible",
182                 getGridElement().getCell(0, 2).isDisplayed());
183
184         new Actions(getDriver()).sendKeys(Keys.END).perform();
185         assertTrue("Last row cell not visible", getGridElement()
186                 .getCell(GridBasicFeatures.ROWS - 1, 2).isDisplayed());
187     }
188
189     @Test
190     public void testPageUpPageDown() throws Exception {
191         openTestURL();
192
193         selectMenuPath("Component", "Size", "HeightMode Row");
194
195         getGridElement().getCell(9, 2).click();
196         new Actions(getDriver()).sendKeys(Keys.PAGE_DOWN).perform();
197         assertTrue("Row 17 did not become visible",
198                 isElementPresent(By.xpath("//td[text() = '(17, 2)']")));
199
200         new Actions(getDriver()).sendKeys(Keys.PAGE_DOWN).perform();
201         assertTrue("Row 25 did not become visible",
202                 isElementPresent(By.xpath("//td[text() = '(25, 2)']")));
203         checkFocusedCell(29, 2, 4);
204
205         getGridElement().getCell(41, 2).click();
206         new Actions(getDriver()).sendKeys(Keys.PAGE_UP).perform();
207         assertTrue("Row 33 did not become visible",
208                 isElementPresent(By.xpath("//td[text() = '(33, 2)']")));
209
210         new Actions(getDriver()).sendKeys(Keys.PAGE_UP).perform();
211         assertTrue("Row 25 did not become visible",
212                 isElementPresent(By.xpath("//td[text() = '(25, 2)']")));
213         checkFocusedCell(21, 2, 4);
214     }
215
216     private void checkFocusedCell(int row, int column, int rowTolerance) {
217         WebElement focusedCell = getGridElement()
218                 .findElement(By.className("v-grid-cell-focused"));
219         String cellContents = focusedCell.getText();
220         String[] rowAndCol = cellContents.replaceAll("[()\\s]", "").split(",");
221         int focusedRow = Integer.parseInt(rowAndCol[0].trim());
222         int focusedColumn = Integer.parseInt(rowAndCol[1].trim());
223         // rowTolerance is the maximal allowed difference from the expected
224         // focused row. It is required because scrolling using page up/down
225         // may not move the position by exactly the visible height of the grid.
226         assertTrue(
227                 "The wrong cell is focused. Expected (" + row + "," + column
228                         + "), was " + cellContents,
229                 column == focusedColumn
230                         && Math.abs(row - focusedRow) <= rowTolerance);
231     }
232 }