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.client;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.List;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openqa.selenium.Keys;
28 import org.openqa.selenium.interactions.Actions;
30 import com.vaadin.testbench.By;
31 import com.vaadin.testbench.elements.GridElement.GridCellElement;
32 import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
34 public class GridClientKeyEventsTest extends GridBasicClientFeaturesTest {
36 private List<String> eventOrder = Arrays.asList("Down", "Up", "Press");
39 public void testBodyKeyEvents() throws IOException {
42 getGridElement().getCell(2, 2).click();
44 new Actions(getDriver()).sendKeys("a").perform();
46 for (int i = 0; i < 3; ++i) {
47 assertEquals("Body key event handler was not called.",
48 "(2, 2) event: GridKey" + eventOrder.get(i) + "Event:["
49 + (eventOrder.get(i).equals("Press") ? "a" : 65)
51 findElements(By.className("v-label")).get(i * 3).getText());
53 assertTrue("Header key event handler got called unexpectedly.",
54 findElements(By.className("v-label")).get(i * 3 + 1)
55 .getText().isEmpty());
56 assertTrue("Footer key event handler got called unexpectedly.",
57 findElements(By.className("v-label")).get(i * 3 + 2)
58 .getText().isEmpty());
64 public void testHeaderKeyEvents() throws IOException {
67 getGridElement().getHeaderCell(0, 2).click();
69 new Actions(getDriver()).sendKeys("a").perform();
71 for (int i = 0; i < 3; ++i) {
72 assertEquals("Header key event handler was not called.",
73 "(0, 2) event: GridKey" + eventOrder.get(i) + "Event:["
74 + (eventOrder.get(i).equals("Press") ? "a" : 65)
76 findElements(By.className("v-label")).get(i * 3 + 1)
79 assertTrue("Body key event handler got called unexpectedly.",
80 findElements(By.className("v-label")).get(i * 3).getText()
82 assertTrue("Footer key event handler got called unexpectedly.",
83 findElements(By.className("v-label")).get(i * 3 + 2)
84 .getText().isEmpty());
89 public void selectAllUsingKeyboard() {
92 selectMenuPath("Component", "Header", "Prepend row");
93 selectMenuPath("Component", "Header", "Append row");
94 selectMenuPath("Component", "State", "Selection mode", "multi");
96 // IE8 does not handle 1k rows well with assertions enabled. Less rows!
97 selectMenuPath("Component", "DataSource",
98 "Reset with 100 rows of Data");
100 // Focus cell above select all checkbox
101 getGridElement().getHeaderCell(0, 0).click();
102 Assert.assertFalse(isRowSelected(1));
103 new Actions(getDriver()).sendKeys(" ").perform();
104 Assert.assertFalse(isRowSelected(1));
106 // Move down to select all checkbox cell
107 new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN).perform();
108 Assert.assertFalse(isRowSelected(1));
109 new Actions(getDriver()).sendKeys(" ").perform(); // select all
110 Assert.assertTrue(isRowSelected(1));
111 new Actions(getDriver()).sendKeys(" ").perform(); // deselect all
112 Assert.assertFalse(isRowSelected(1));
114 // Move down to header below select all checkbox cell
115 new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN).perform();
116 Assert.assertFalse(isRowSelected(1));
117 new Actions(getDriver()).sendKeys(" ").perform(); // deselect all
118 Assert.assertFalse(isRowSelected(1));
123 public void testFooterKeyEvents() throws IOException {
126 selectMenuPath("Component", "Footer", "Append row");
127 getGridElement().getFooterCell(0, 2).click();
129 new Actions(getDriver()).sendKeys("a").perform();
131 for (int i = 0; i < 3; ++i) {
132 assertEquals("Footer key event handler was not called.",
133 "(0, 2) event: GridKey" + eventOrder.get(i) + "Event:["
134 + (eventOrder.get(i).equals("Press") ? "a" : 65)
136 findElements(By.className("v-label")).get(i * 3 + 2)
139 assertTrue("Body key event handler got called unexpectedly.",
140 findElements(By.className("v-label")).get(i * 3).getText()
142 assertTrue("Header key event handler got called unexpectedly.",
143 findElements(By.className("v-label")).get(i * 3 + 1)
144 .getText().isEmpty());
150 public void testNoKeyEventsFromWidget() {
153 selectMenuPath("Component", "Columns", "Column 2", "Header Type",
155 GridCellElement header = getGridElement().getHeaderCell(0, 2);
156 header.findElement(By.tagName("button")).click();
157 new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
159 for (int i = 0; i < 3; ++i) {
160 assertTrue("Header key event handler got called unexpectedly.",
161 findElements(By.className("v-label")).get(i * 3 + 1)
162 .getText().isEmpty());