2 * Copyright 2000-2016 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.tests.components.grid.basicfeatures.server;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
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;
27 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
28 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
29 import com.vaadin.v7.testbench.customelements.GridElement;
31 public class GridKeyboardNavigationTest extends GridBasicFeaturesTest {
34 public void testCellFocusOnClick() {
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());
48 public void testCellNotFocusedWhenRendererHandlesEvent() {
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());
62 public void testSimpleKeyboardNavigation() {
65 GridElement grid = getGridElement();
66 grid.getCell(0, 0).click();
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());
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());
77 for (i = 1; i < 40; ++i) {
78 new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN).perform();
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());
90 public void testNavigateFromHeaderToBody() {
93 GridElement grid = getGridElement();
94 grid.scrollToRow(300);
95 new Actions(driver).moveToElement(grid.getHeaderCell(0, 7)).click()
97 grid.scrollToRow(280);
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());
107 public void testNavigationFromFooterToBody() {
110 selectMenuPath("Component", "Footer", "Visible");
112 GridElement grid = getGridElement();
113 grid.scrollToRow(300);
114 grid.getFooterCell(0, 2).click();
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());
124 public void testNavigateBetweenHeaderAndBodyWithTab() {
127 GridElement grid = getGridElement();
128 grid.getCell(10, 2).click();
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());
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)
144 assertTrue("Header cell 0, 2 does not have focus",
145 grid.getHeaderCell(0, 2).isFocused());
149 public void testNavigateBetweenFooterAndBodyWithTab() {
152 selectMenuPath("Component", "Footer", "Visible");
154 GridElement grid = getGridElement();
155 grid.getCell(10, 2).click();
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());
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());
175 public void testHomeEnd() throws Exception {
178 getGridElement().getCell(100, 2).click();
180 new Actions(getDriver()).sendKeys(Keys.HOME).perform();
181 assertTrue("First row is not visible",
182 getGridElement().getCell(0, 2).isDisplayed());
184 new Actions(getDriver()).sendKeys(Keys.END).perform();
185 assertTrue("Last row cell not visible", getGridElement()
186 .getCell(GridBasicFeatures.ROWS - 1, 2).isDisplayed());
190 public void testPageUpPageDown() throws Exception {
193 selectMenuPath("Component", "Size", "HeightMode Row");
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)']")));
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);
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)']")));
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);
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.
227 "The wrong cell is focused. Expected (" + row + "," + column
228 + "), was " + cellContents,
229 column == focusedColumn
230 && Math.abs(row - focusedRow) <= rowTolerance);