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.v7.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.Keys;
23 import org.openqa.selenium.WebDriver;
24 import org.openqa.selenium.WebElement;
25 import org.openqa.selenium.interactions.Actions;
26 import org.openqa.selenium.support.ui.ExpectedCondition;
28 import com.vaadin.testbench.By;
29 import com.vaadin.testbench.elements.GridElement.GridCellElement;
30 import com.vaadin.testbench.elements.GridElement.GridRowElement;
31 import com.vaadin.v7.testbench.customelements.GridElement;
32 import com.vaadin.v7.tests.components.grid.basicfeatures.GridBasicFeatures;
33 import com.vaadin.v7.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
35 public class GridSelectionTest extends GridBasicFeaturesTest {
38 public void testSelectOnOff() throws Exception {
41 setSelectionModelMulti();
43 assertFalse("row shouldn't start out as selected",
44 getRow(0).isSelected());
45 toggleFirstRowSelection();
46 assertTrue("row should become selected", getRow(0).isSelected());
47 toggleFirstRowSelection();
48 assertFalse("row shouldn't remain selected", getRow(0).isSelected());
52 public void testSelectOnScrollOffScroll() throws Exception {
55 setSelectionModelMulti();
57 assertFalse("row shouldn't start out as selected",
58 getRow(0).isSelected());
59 toggleFirstRowSelection();
60 assertTrue("row should become selected", getRow(0).isSelected());
62 scrollGridVerticallyTo(10000); // make sure the row is out of cache
63 scrollGridVerticallyTo(0); // scroll it back into view
65 assertTrue("row should still be selected when scrolling "
66 + "back into view", getRow(0).isSelected());
70 public void testSelectScrollOnScrollOff() throws Exception {
73 setSelectionModelMulti();
75 assertFalse("row shouldn't start out as selected",
76 getRow(0).isSelected());
78 scrollGridVerticallyTo(10000); // make sure the row is out of cache
79 toggleFirstRowSelection();
81 scrollGridVerticallyTo(0); // scroll it back into view
82 assertTrue("row should still be selected when scrolling "
83 + "back into view", getRow(0).isSelected());
85 toggleFirstRowSelection();
86 assertFalse("row shouldn't remain selected", getRow(0).isSelected());
90 public void testSelectScrollOnOffScroll() throws Exception {
93 setSelectionModelMulti();
95 assertFalse("row shouldn't start out as selected",
96 getRow(0).isSelected());
98 scrollGridVerticallyTo(10000); // make sure the row is out of cache
99 toggleFirstRowSelection();
100 toggleFirstRowSelection();
102 scrollGridVerticallyTo(0); // make sure the row is out of cache
104 "row shouldn't be selected when scrolling " + "back into view",
105 getRow(0).isSelected());
109 public void testSingleSelectionUpdatesFromServer() {
111 setSelectionModelSingle();
113 GridElement grid = getGridElement();
114 assertFalse("First row was selected from start",
115 grid.getRow(0).isSelected());
116 toggleFirstRowSelection();
117 assertTrue("First row was not selected.", getRow(0).isSelected());
118 assertTrue("Selection event was not correct",
119 logContainsText("Added 0, Removed none"));
120 grid.getCell(5, 0).click();
121 assertTrue("Fifth row was not selected.", getRow(5).isSelected());
122 assertFalse("First row was still selected.", getRow(0).isSelected());
123 assertTrue("Selection event was not correct",
124 logContainsText("Added 5, Removed 0"));
125 grid.getCell(0, 6).click();
126 assertTrue("Selection event was not correct",
127 logContainsText("Added 0, Removed 5"));
128 toggleFirstRowSelection();
129 assertTrue("Selection event was not correct",
130 logContainsText("Added none, Removed 0"));
131 assertFalse("First row was still selected.", getRow(0).isSelected());
132 assertFalse("Fifth row was still selected.", getRow(5).isSelected());
134 grid.scrollToRow(600);
135 grid.getCell(595, 3).click();
136 assertTrue("Row 595 was not selected.", getRow(595).isSelected());
137 assertTrue("Selection event was not correct",
138 logContainsText("Added 595, Removed none"));
139 toggleFirstRowSelection();
140 assertFalse("Row 595 was still selected.", getRow(595).isSelected());
141 assertTrue("First row was not selected.", getRow(0).isSelected());
142 assertTrue("Selection event was not correct",
143 logContainsText("Added 0, Removed 595"));
147 public void testKeyboardSelection() {
149 setSelectionModelMulti();
151 GridElement grid = getGridElement();
152 grid.getCell(3, 1).click();
153 new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
155 assertTrue("Grid row 3 was not selected with space key.",
156 grid.getRow(3).isSelected());
158 new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
160 assertTrue("Grid row 3 was not deselected with space key.",
161 !grid.getRow(3).isSelected());
163 grid.scrollToRow(500);
165 new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
167 assertTrue("Grid row 3 was not selected with space key.",
168 grid.getRow(3).isSelected());
172 public void testKeyboardWithSingleSelection() {
174 setSelectionModelSingle();
176 GridElement grid = getGridElement();
177 grid.getCell(3, 1).click();
179 assertTrue("Grid row 3 was not selected with clicking.",
180 grid.getRow(3).isSelected());
182 new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
184 assertTrue("Grid row 3 was not deselected with space key.",
185 !grid.getRow(3).isSelected());
187 new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
189 assertTrue("Grid row 3 was not selected with space key.",
190 grid.getRow(3).isSelected());
192 grid.scrollToRow(500);
194 new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
196 assertTrue("Grid row 3 was not deselected with space key.",
197 !grid.getRow(3).isSelected());
201 public void testSelectAllCheckbox() {
204 setSelectionModelMulti();
205 GridCellElement header = getGridElement().getHeaderCell(0, 0);
207 assertTrue("No checkbox", header.isElementPresent(By.tagName("input")));
208 header.findElement(By.tagName("input")).click();
210 for (int i = 0; i < GridBasicFeatures.ROWS; i += 100) {
211 assertTrue("Row " + i + " was not selected.",
212 getGridElement().getRow(i).isSelected());
215 header.findElement(By.tagName("input")).click();
216 assertFalse("Row 100 was still selected",
217 getGridElement().getRow(100).isSelected());
221 public void testSelectAllAndSort() {
224 setSelectionModelMulti();
225 GridCellElement header = getGridElement().getHeaderCell(0, 0);
227 header.findElement(By.tagName("input")).click();
229 getGridElement().getHeaderCell(0, 1).click();
231 WebElement selectionBox = getGridElement().getCell(4, 0)
232 .findElement(By.tagName("input"));
233 selectionBox.click();
234 selectionBox.click();
236 assertFalse("Exception occured on row reselection.", logContainsText(
237 "Exception occured, java.lang.IllegalStateException: No item id for key 101 found."));
241 public void testSelectAllCheckboxWhenChangingModels() {
244 GridCellElement header;
245 header = getGridElement().getHeaderCell(0, 0);
247 "Check box shouldn't have been in header for None Selection Model",
248 header.isElementPresent(By.tagName("input")));
250 setSelectionModelMulti();
251 header = getGridElement().getHeaderCell(0, 0);
252 assertTrue("Multi Selection Model should have select all checkbox",
253 header.isElementPresent(By.tagName("input")));
255 setSelectionModelSingle();
256 header = getGridElement().getHeaderCell(0, 0);
258 "Check box shouldn't have been in header for Single Selection Model",
259 header.isElementPresent(By.tagName("input")));
261 // Single selection model shouldn't have selection column to begin with
263 "Selection columnn shouldn't have been in grid for Single Selection Model",
264 getGridElement().getCell(0, 1)
265 .isElementPresent(By.tagName("input")));
267 setSelectionModelNone();
268 header = getGridElement().getHeaderCell(0, 0);
270 "Check box shouldn't have been in header for None Selection Model",
271 header.isElementPresent(By.tagName("input")));
276 public void testSelectAllCheckboxWithHeaderOperations() {
279 setSelectionModelMulti();
280 selectMenuPath("Component", "Header", "Prepend row");
281 selectMenuPath("Component", "Header", "Append row");
283 GridCellElement header = getGridElement().getHeaderCell(1, 0);
284 assertTrue("Multi Selection Model should have select all checkbox",
285 header.isElementPresent(By.tagName("input")));
290 public void testToggleDeselectAllowed() {
293 setSelectionModelSingle();
294 // Deselect allowed already enabled
296 getGridElement().getCell(5, 1).click();
297 getGridElement().getCell(5, 1).click();
298 assertFalse("Row should be not selected after two clicks",
299 getRow(5).isSelected());
301 selectMenuPath("Component", "State", "Single select allow deselect");
302 getGridElement().getCell(5, 1).click();
303 getGridElement().getCell(5, 1).click();
304 assertTrue("Row should be selected after two clicks",
305 getRow(5).isSelected());
307 selectMenuPath("Component", "State", "Single select allow deselect");
308 getGridElement().getCell(5, 1).click();
309 assertFalse("Row should be not selected after another click",
310 getRow(5).isSelected());
312 // Also verify that state is updated together with the model
313 setSelectionModelNone();
314 selectMenuPath("Component", "State", "Single select allow deselect");
315 setSelectionModelSingle();
317 getGridElement().getCell(5, 1).click();
318 getGridElement().getCell(5, 1).click();
320 assertTrue("Row should stay selected after two clicks",
321 getRow(5).isSelected());
325 public void testChangeSelectionModelUpdatesUI() {
328 setSelectionModelSingle();
330 getGridElement().getCell(5, 1).click();
331 assertTrue("Row should be selected after clicking",
332 getRow(5).isSelected());
334 setSelectionModelNone();
335 assertFalse("Row should not be selected after changing selection model",
336 getRow(5).isSelected());
340 public void testSelectionCheckBoxesHaveStyleNames() {
343 setSelectionModelMulti();
346 "Selection column CheckBox should have the proper style name set",
347 getGridElement().getCell(0, 0).findElement(By.tagName("span"))
348 .getAttribute("class")
349 .contains("v-grid-selection-checkbox"));
351 GridCellElement header = getGridElement().getHeaderCell(0, 0);
352 assertTrue("Select all CheckBox should have the proper style name set",
353 header.findElement(By.tagName("span")).getAttribute("class")
354 .contains("v-grid-select-all-checkbox"));
358 public void testServerSideSelectTogglesSelectAllCheckBox() {
361 setSelectionModelMulti();
362 GridCellElement header = getGridElement().getHeaderCell(0, 0);
364 WebElement selectAll = header.findElement(By.tagName("input"));
366 selectMenuPath("Component", "State", "Select all");
367 waitUntilCheckBoxValue(selectAll, true);
368 assertTrue("Select all CheckBox wasn't selected as expected",
369 selectAll.isSelected());
371 selectMenuPath("Component", "State", "Select none");
372 waitUntilCheckBoxValue(selectAll, false);
373 assertFalse("Select all CheckBox was selected unexpectedly",
374 selectAll.isSelected());
376 selectMenuPath("Component", "State", "Select all");
377 waitUntilCheckBoxValue(selectAll, true);
378 getGridElement().getCell(5, 0).click();
379 waitUntilCheckBoxValue(selectAll, false);
380 assertFalse("Select all CheckBox was selected unexpectedly",
381 selectAll.isSelected());
385 public void testRemoveSelectedRow() {
388 setSelectionModelSingle();
389 getGridElement().getCell(0, 0).click();
391 selectMenuPath("Component", "Body rows", "Remove selected rows");
394 "Unexpected NullPointerException when removing selected rows",
396 "Exception occured, java.lang.NullPointerException: null"));
399 private void waitUntilCheckBoxValue(final WebElement checkBoxElememnt,
400 final boolean expectedValue) {
401 waitUntil(new ExpectedCondition<Boolean>() {
403 public Boolean apply(WebDriver input) {
404 return expectedValue ? checkBoxElememnt.isSelected()
405 : !checkBoxElememnt.isSelected();
410 private void setSelectionModelMulti() {
411 selectMenuPath("Component", "State", "Selection mode", "multi");
414 private void setSelectionModelSingle() {
415 selectMenuPath("Component", "State", "Selection mode", "single");
418 private void setSelectionModelNone() {
419 selectMenuPath("Component", "State", "Selection mode", "none");
422 private void toggleFirstRowSelection() {
423 selectMenuPath("Component", "Body rows", "Select first row");
426 private GridRowElement getRow(int i) {
427 return getGridElement().getRow(i);