]> source.dussan.org Git - vaadin-framework.git/blob
319848fec3d444cbfc579bd0331fa5fe530401ce
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2016 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.v7.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.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;
27
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;
34
35 public class GridSelectionTest extends GridBasicFeaturesTest {
36
37     @Test
38     public void testSelectOnOff() throws Exception {
39         openTestURL();
40
41         setSelectionModelMulti();
42
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());
49     }
50
51     @Test
52     public void testSelectOnScrollOffScroll() throws Exception {
53         openTestURL();
54
55         setSelectionModelMulti();
56
57         assertFalse("row shouldn't start out as selected",
58                 getRow(0).isSelected());
59         toggleFirstRowSelection();
60         assertTrue("row should become selected", getRow(0).isSelected());
61
62         scrollGridVerticallyTo(10000); // make sure the row is out of cache
63         scrollGridVerticallyTo(0); // scroll it back into view
64
65         assertTrue("row should still be selected when scrolling "
66                 + "back into view", getRow(0).isSelected());
67     }
68
69     @Test
70     public void testSelectScrollOnScrollOff() throws Exception {
71         openTestURL();
72
73         setSelectionModelMulti();
74
75         assertFalse("row shouldn't start out as selected",
76                 getRow(0).isSelected());
77
78         scrollGridVerticallyTo(10000); // make sure the row is out of cache
79         toggleFirstRowSelection();
80
81         scrollGridVerticallyTo(0); // scroll it back into view
82         assertTrue("row should still be selected when scrolling "
83                 + "back into view", getRow(0).isSelected());
84
85         toggleFirstRowSelection();
86         assertFalse("row shouldn't remain selected", getRow(0).isSelected());
87     }
88
89     @Test
90     public void testSelectScrollOnOffScroll() throws Exception {
91         openTestURL();
92
93         setSelectionModelMulti();
94
95         assertFalse("row shouldn't start out as selected",
96                 getRow(0).isSelected());
97
98         scrollGridVerticallyTo(10000); // make sure the row is out of cache
99         toggleFirstRowSelection();
100         toggleFirstRowSelection();
101
102         scrollGridVerticallyTo(0); // make sure the row is out of cache
103         assertFalse(
104                 "row shouldn't be selected when scrolling " + "back into view",
105                 getRow(0).isSelected());
106     }
107
108     @Test
109     public void testSingleSelectionUpdatesFromServer() {
110         openTestURL();
111         setSelectionModelSingle();
112
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());
133
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"));
144     }
145
146     @Test
147     public void testKeyboardSelection() {
148         openTestURL();
149         setSelectionModelMulti();
150
151         GridElement grid = getGridElement();
152         grid.getCell(3, 1).click();
153         new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
154
155         assertTrue("Grid row 3 was not selected with space key.",
156                 grid.getRow(3).isSelected());
157
158         new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
159
160         assertTrue("Grid row 3 was not deselected with space key.",
161                 !grid.getRow(3).isSelected());
162
163         grid.scrollToRow(500);
164
165         new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
166
167         assertTrue("Grid row 3 was not selected with space key.",
168                 grid.getRow(3).isSelected());
169     }
170
171     @Test
172     public void testKeyboardWithSingleSelection() {
173         openTestURL();
174         setSelectionModelSingle();
175
176         GridElement grid = getGridElement();
177         grid.getCell(3, 1).click();
178
179         assertTrue("Grid row 3 was not selected with clicking.",
180                 grid.getRow(3).isSelected());
181
182         new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
183
184         assertTrue("Grid row 3 was not deselected with space key.",
185                 !grid.getRow(3).isSelected());
186
187         new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
188
189         assertTrue("Grid row 3 was not selected with space key.",
190                 grid.getRow(3).isSelected());
191
192         grid.scrollToRow(500);
193
194         new Actions(getDriver()).sendKeys(Keys.SPACE).perform();
195
196         assertTrue("Grid row 3 was not deselected with space key.",
197                 !grid.getRow(3).isSelected());
198     }
199
200     @Test
201     public void testSelectAllCheckbox() {
202         openTestURL();
203
204         setSelectionModelMulti();
205         GridCellElement header = getGridElement().getHeaderCell(0, 0);
206
207         assertTrue("No checkbox", header.isElementPresent(By.tagName("input")));
208         header.findElement(By.tagName("input")).click();
209
210         for (int i = 0; i < GridBasicFeatures.ROWS; i += 100) {
211             assertTrue("Row " + i + " was not selected.",
212                     getGridElement().getRow(i).isSelected());
213         }
214
215         header.findElement(By.tagName("input")).click();
216         assertFalse("Row 100 was still selected",
217                 getGridElement().getRow(100).isSelected());
218     }
219
220     @Test
221     public void testSelectAllAndSort() {
222         openTestURL();
223
224         setSelectionModelMulti();
225         GridCellElement header = getGridElement().getHeaderCell(0, 0);
226
227         header.findElement(By.tagName("input")).click();
228
229         getGridElement().getHeaderCell(0, 1).click();
230
231         WebElement selectionBox = getGridElement().getCell(4, 0)
232                 .findElement(By.tagName("input"));
233         selectionBox.click();
234         selectionBox.click();
235
236         assertFalse("Exception occured on row reselection.", logContainsText(
237                 "Exception occured, java.lang.IllegalStateException: No item id for key 101 found."));
238     }
239
240     @Test
241     public void testSelectAllCheckboxWhenChangingModels() {
242         openTestURL();
243
244         GridCellElement header;
245         header = getGridElement().getHeaderCell(0, 0);
246         assertFalse(
247                 "Check box shouldn't have been in header for None Selection Model",
248                 header.isElementPresent(By.tagName("input")));
249
250         setSelectionModelMulti();
251         header = getGridElement().getHeaderCell(0, 0);
252         assertTrue("Multi Selection Model should have select all checkbox",
253                 header.isElementPresent(By.tagName("input")));
254
255         setSelectionModelSingle();
256         header = getGridElement().getHeaderCell(0, 0);
257         assertFalse(
258                 "Check box shouldn't have been in header for Single Selection Model",
259                 header.isElementPresent(By.tagName("input")));
260
261         // Single selection model shouldn't have selection column to begin with
262         assertFalse(
263                 "Selection columnn shouldn't have been in grid for Single Selection Model",
264                 getGridElement().getCell(0, 1)
265                         .isElementPresent(By.tagName("input")));
266
267         setSelectionModelNone();
268         header = getGridElement().getHeaderCell(0, 0);
269         assertFalse(
270                 "Check box shouldn't have been in header for None Selection Model",
271                 header.isElementPresent(By.tagName("input")));
272
273     }
274
275     @Test
276     public void testSelectAllCheckboxWithHeaderOperations() {
277         openTestURL();
278
279         setSelectionModelMulti();
280         selectMenuPath("Component", "Header", "Prepend row");
281         selectMenuPath("Component", "Header", "Append row");
282
283         GridCellElement header = getGridElement().getHeaderCell(1, 0);
284         assertTrue("Multi Selection Model should have select all checkbox",
285                 header.isElementPresent(By.tagName("input")));
286
287     }
288
289     @Test
290     public void testToggleDeselectAllowed() {
291         openTestURL();
292
293         setSelectionModelSingle();
294         // Deselect allowed already enabled
295
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());
300
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());
306
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());
311
312         // Also verify that state is updated together with the model
313         setSelectionModelNone();
314         selectMenuPath("Component", "State", "Single select allow deselect");
315         setSelectionModelSingle();
316
317         getGridElement().getCell(5, 1).click();
318         getGridElement().getCell(5, 1).click();
319
320         assertTrue("Row should stay selected after two clicks",
321                 getRow(5).isSelected());
322     }
323
324     @Test
325     public void testChangeSelectionModelUpdatesUI() {
326         openTestURL();
327
328         setSelectionModelSingle();
329
330         getGridElement().getCell(5, 1).click();
331         assertTrue("Row should be selected after clicking",
332                 getRow(5).isSelected());
333
334         setSelectionModelNone();
335         assertFalse("Row should not be selected after changing selection model",
336                 getRow(5).isSelected());
337     }
338
339     @Test
340     public void testSelectionCheckBoxesHaveStyleNames() {
341         openTestURL();
342
343         setSelectionModelMulti();
344
345         assertTrue(
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"));
350
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"));
355     }
356
357     @Test
358     public void testServerSideSelectTogglesSelectAllCheckBox() {
359         openTestURL();
360
361         setSelectionModelMulti();
362         GridCellElement header = getGridElement().getHeaderCell(0, 0);
363
364         WebElement selectAll = header.findElement(By.tagName("input"));
365
366         selectMenuPath("Component", "State", "Select all");
367         waitUntilCheckBoxValue(selectAll, true);
368         assertTrue("Select all CheckBox wasn't selected as expected",
369                 selectAll.isSelected());
370
371         selectMenuPath("Component", "State", "Select none");
372         waitUntilCheckBoxValue(selectAll, false);
373         assertFalse("Select all CheckBox was selected unexpectedly",
374                 selectAll.isSelected());
375
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());
382     }
383
384     @Test
385     public void testRemoveSelectedRow() {
386         openTestURL();
387
388         setSelectionModelSingle();
389         getGridElement().getCell(0, 0).click();
390
391         selectMenuPath("Component", "Body rows", "Remove selected rows");
392
393         assertFalse(
394                 "Unexpected NullPointerException when removing selected rows",
395                 logContainsText(
396                         "Exception occured, java.lang.NullPointerException: null"));
397     }
398
399     private void waitUntilCheckBoxValue(final WebElement checkBoxElememnt,
400             final boolean expectedValue) {
401         waitUntil(new ExpectedCondition<Boolean>() {
402             @Override
403             public Boolean apply(WebDriver input) {
404                 return expectedValue ? checkBoxElememnt.isSelected()
405                         : !checkBoxElememnt.isSelected();
406             }
407         }, 5);
408     }
409
410     private void setSelectionModelMulti() {
411         selectMenuPath("Component", "State", "Selection mode", "multi");
412     }
413
414     private void setSelectionModelSingle() {
415         selectMenuPath("Component", "State", "Selection mode", "single");
416     }
417
418     private void setSelectionModelNone() {
419         selectMenuPath("Component", "State", "Selection mode", "none");
420     }
421
422     private void toggleFirstRowSelection() {
423         selectMenuPath("Component", "Body rows", "Select first row");
424     }
425
426     private GridRowElement getRow(int i) {
427         return getGridElement().getRow(i);
428     }
429 }