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.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.core.IsNot.not;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.util.List;
28 import org.junit.Test;
29 import org.openqa.selenium.NoSuchElementException;
30 import org.openqa.selenium.WebElement;
32 import com.vaadin.testbench.By;
33 import com.vaadin.testbench.TestBenchElement;
34 import com.vaadin.testbench.elements.GridElement.GridCellElement;
35 import com.vaadin.testbench.elements.NotificationElement;
36 import com.vaadin.v7.testbench.customelements.GridElement;
37 import com.vaadin.v7.tests.components.grid.basicfeatures.GridBasicFeatures;
38 import com.vaadin.v7.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
40 public class GridStructureTest extends GridBasicFeaturesTest {
43 public void testRemovingAllColumns() {
46 for (int i = 0; i < GridBasicFeatures.COLUMNS; ++i) {
47 selectMenuPath("Component", "Columns", "Column " + i,
49 assertFalse(isElementPresent(NotificationElement.class));
52 assertEquals("Headers still visible.", 0,
53 getGridHeaderRowCells().size());
57 public void testRemoveAndAddColumn() {
61 assertEquals("column 0",
62 getGridElement().getHeaderCell(0, 0).getText().toLowerCase());
63 selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
64 assertEquals("column 1",
65 getGridElement().getHeaderCell(0, 0).getText().toLowerCase());
66 selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
68 // Column 0 is now the last column in Grid.
69 assertEquals("Unexpected column content", "(0, 0)",
70 getGridElement().getCell(0, 11).getText());
74 public void testRemovingColumn() throws Exception {
77 // Column 0 should be visible
78 List<TestBenchElement> cells = getGridHeaderRowCells();
79 assertEquals("column 0", cells.get(0).getText().toLowerCase());
82 selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
84 // Column 1 should now be the first cell
85 cells = getGridHeaderRowCells();
86 assertEquals("column 1", cells.get(0).getText().toLowerCase());
90 public void testDataLoadingAfterRowRemoval() throws Exception {
93 // Remove columns 2,3,4
94 selectMenuPath("Component", "Columns", "Column 2", "Add / Remove");
95 selectMenuPath("Component", "Columns", "Column 3", "Add / Remove");
96 selectMenuPath("Component", "Columns", "Column 4", "Add / Remove");
98 // Scroll so new data is lazy loaded
99 scrollGridVerticallyTo(1000);
101 // Let lazy loading do its job
104 // Check that row is loaded
105 assertThat(getGridElement().getCell(11, 0).getText(), not("..."));
109 public void testFreezingColumn() throws Exception {
113 selectMenuPath("Component", "State", "Frozen column count", "1");
115 WebElement cell = getGridElement().getCell(0, 0);
116 assertTrue(cell.getAttribute("class").contains("frozen"));
118 cell = getGridElement().getCell(0, 1);
119 assertFalse(cell.getAttribute("class").contains("frozen"));
123 public void testInitialColumnWidths() throws Exception {
126 WebElement cell = getGridElement().getCell(0, 0);
127 assertEquals(100, cell.getSize().getWidth());
129 cell = getGridElement().getCell(0, 1);
130 assertEquals(150, cell.getSize().getWidth());
132 cell = getGridElement().getCell(0, 2);
133 assertEquals(200, cell.getSize().getWidth());
137 public void testColumnWidths() throws Exception {
140 // Default column width is 100px
141 WebElement cell = getGridElement().getCell(0, 0);
142 assertEquals(100, cell.getSize().getWidth());
144 // Set first column to be 200px wide
145 selectMenuPath("Component", "Columns", "Column 0", "Column 0 Width",
148 cell = getGridElement().getCell(0, 0);
149 assertEquals(200, cell.getSize().getWidth());
151 // Set second column to be 150px wide
152 selectMenuPath("Component", "Columns", "Column 1", "Column 1 Width",
154 cell = getGridElement().getCell(0, 1);
155 assertEquals(150, cell.getSize().getWidth());
157 selectMenuPath("Component", "Columns", "Column 0", "Column 0 Width",
160 // since the column 0 was previously 200, it should've shrunk when
162 cell = getGridElement().getCell(0, 0);
163 assertLessThan("", cell.getSize().getWidth(), 200);
167 public void testPrimaryStyleNames() throws Exception {
170 // v-grid is default primary style namea
171 assertPrimaryStylename("v-grid");
173 selectMenuPath("Component", "State", "Primary style name",
175 assertPrimaryStylename("v-escalator");
177 selectMenuPath("Component", "State", "Primary style name", "my-grid");
178 assertPrimaryStylename("my-grid");
180 selectMenuPath("Component", "State", "Primary style name", "v-grid");
181 assertPrimaryStylename("v-grid");
185 * Test that the current view is updated when a server-side container change
186 * occurs (without scrolling back and forth)
189 public void testItemSetChangeEvent() throws Exception {
192 final org.openqa.selenium.By newRow = By
193 .xpath("//td[text()='newcell: 0']");
195 assertTrue("Unexpected initial state", !isElementPresent(newRow));
197 selectMenuPath("Component", "Body rows", "Add first row");
198 assertTrue("Add row failed", isElementPresent(newRow));
200 selectMenuPath("Component", "Body rows", "Remove first row");
201 assertTrue("Remove row failed", !isElementPresent(newRow));
205 * Test that the current view is updated when a property's value is reflect
206 * to the client, when the value is modified server-side.
209 public void testPropertyValueChangeEvent() throws Exception {
212 assertEquals("Unexpected cell initial state", "(0, 0)",
213 getGridElement().getCell(0, 0).getText());
215 selectMenuPath("Component", "Body rows",
216 "Modify first row (getItemProperty)");
217 assertEquals("(First) modification with getItemProperty failed",
218 "modified: 0", getGridElement().getCell(0, 0).getText());
220 selectMenuPath("Component", "Body rows",
221 "Modify first row (getContainerProperty)");
222 assertEquals("(Second) modification with getItemProperty failed",
223 "modified: Column 0", getGridElement().getCell(0, 0).getText());
227 public void testRemovingAllItems() throws Exception {
230 selectMenuPath("Component", "Body rows", "Remove all rows");
232 assertEquals(0, getGridElement().findElement(By.tagName("tbody"))
233 .findElements(By.tagName("tr")).size());
237 public void testRemoveFirstRowTwice() {
240 selectMenuPath("Component", "Body rows", "Remove first row");
241 selectMenuPath("Component", "Body rows", "Remove first row");
243 getGridElement().scrollToRow(50);
244 assertFalse("Listener setup problem occurred.",
245 logContainsText("AssertionError: Value change listeners"));
249 public void testVerticalScrollBarVisibilityWhenEnoughRows()
253 assertTrue(verticalScrollbarIsPresent());
255 selectMenuPath("Component", "Body rows", "Remove all rows");
256 assertFalse(verticalScrollbarIsPresent());
258 selectMenuPath("Component", "Size", "HeightMode Row");
259 selectMenuPath("Component", "Size", "Height by Rows", "2.33 rows");
260 selectMenuPath("Component", "Body rows", "Add first row");
261 selectMenuPath("Component", "Body rows", "Add first row");
262 assertFalse(verticalScrollbarIsPresent());
264 selectMenuPath("Component", "Body rows", "Add first row");
265 assertTrue(verticalScrollbarIsPresent());
269 public void testBareItemSetChange() throws Exception {
271 filterSomeAndAssert();
275 public void testBareItemSetChangeRemovingAllRows() throws Exception {
277 selectMenuPath("Component", "Filter", "Impassable filter");
278 assertFalse("A notification shouldn't have been displayed",
279 $(NotificationElement.class).exists());
280 assertTrue("No body cells should've been found", getGridElement()
281 .getBody().findElements(By.tagName("td")).isEmpty());
285 public void testBareItemSetChangeWithMidScroll() throws Exception {
287 getGridElement().scrollToRow(GridBasicFeatures.ROWS / 2);
288 filterSomeAndAssert();
292 public void testBareItemSetChangeWithBottomScroll() throws Exception {
294 getGridElement().scrollToRow(GridBasicFeatures.ROWS);
295 filterSomeAndAssert();
299 public void testBareItemSetChangeWithBottomScrollAndSmallViewport()
302 selectMenuPath("Component", "Size", "HeightMode Row");
303 getGridElement().getRow(GridBasicFeatures.ROWS - 1);
305 selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
307 String text = getGridElement().getCell(10, 0).getText();
309 assertFalse(text.isEmpty());
312 private void filterSomeAndAssert() {
313 selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
314 boolean foundElements = false;
315 for (int row = 0; row < 100; row++) {
317 GridCellElement cell = getGridElement().getCell(row, 1);
318 foundElements = true;
320 "Unexpected cell contents. "
321 + "Did the ItemSetChange work after all?",
322 cell.getText().startsWith("(23"));
323 } catch (NoSuchElementException e) {
324 assertTrue("No rows were found", foundElements);
328 fail("unexpected amount of rows post-filter. Did the ItemSetChange work after all?");
332 public void testRemoveLastColumn() {
336 int col = GridBasicFeatures.COLUMNS;
337 String columnName = "Column " + (GridBasicFeatures.COLUMNS - 1);
338 assertTrue(columnName + " was not present in DOM",
339 isElementPresent(By.xpath("//th[" + col + "]/div[1]")));
340 selectMenuPath("Component", "Columns", columnName, "Add / Remove");
341 assertFalse(isElementPresent(NotificationElement.class));
342 assertFalse(columnName + " was still present in DOM",
343 isElementPresent(By.xpath("//th[" + col + "]/div[1]")));
347 public void testReverseColumns() {
350 String[] gridData = new String[GridBasicFeatures.COLUMNS];
351 GridElement grid = getGridElement();
352 for (int i = 0; i < gridData.length; ++i) {
353 gridData[i] = grid.getCell(0, i).getAttribute("innerHTML");
356 selectMenuPath("Component", "State", "Reverse Grid Columns");
358 // Compare with reversed order
359 for (int i = 0; i < gridData.length; ++i) {
360 final int column = gridData.length - 1 - i;
361 final String newText = grid.getCell(0, column)
362 .getAttribute("innerHTML");
364 "Grid contained unexpected values. (0, " + column + ")",
365 gridData[i], newText);
370 public void testAddingProperty() {
374 assertNotEquals("property value",
375 getGridElement().getCell(0, 0).getText());
376 selectMenuPath("Component", "Properties", "Prepend property");
377 assertEquals("property value",
378 getGridElement().getCell(0, 0).getText());
382 public void testRemovingAddedProperty() {
385 assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
386 assertNotEquals("property value",
387 getGridElement().getCell(0, 0).getText());
389 selectMenuPath("Component", "Properties", "Prepend property");
390 selectMenuPath("Component", "Properties", "Prepend property");
392 assertNotEquals("property value",
393 getGridElement().getCell(0, 0).getText());
394 assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
397 private boolean verticalScrollbarIsPresent() {
399 .equals(getGridVerticalScrollbar().getCssValue("overflow-y"));
403 public void testAddRowAboveViewport() {
407 GridCellElement cell = getGridElement().getCell(500, 1);
408 String cellContent = cell.getText();
409 selectMenuPath("Component", "Body rows", "Add first row");
411 assertFalse("Error notification was present",
412 isElementPresent(NotificationElement.class));
414 assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
418 public void testRemoveAndAddRowAboveViewport() {
422 GridCellElement cell = getGridElement().getCell(500, 1);
423 String cellContent = cell.getText();
424 selectMenuPath("Component", "Body rows", "Remove first row");
426 assertFalse("Error notification was present after removing row",
427 isElementPresent(NotificationElement.class));
429 assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
431 selectMenuPath("Component", "Body rows", "Add first row");
433 assertFalse("Error notification was present after adding row",
434 isElementPresent(NotificationElement.class));
436 assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
440 public void testScrollAndRemoveAll() {
444 getGridElement().scrollToRow(500);
445 selectMenuPath("Component", "Body rows", "Remove all rows");
447 assertFalse("Error notification was present after removing all rows",
448 isElementPresent(NotificationElement.class));
451 getGridElement().isElementPresent(By.vaadin("#cell[0][0]")));
454 private void assertPrimaryStylename(String stylename) {
455 assertTrue(getGridElement().getAttribute("class").contains(stylename));
457 String tableWrapperStyleName = getGridElement().getTableWrapper()
458 .getAttribute("class");
459 assertTrue(tableWrapperStyleName.contains(stylename + "-tablewrapper"));
461 String hscrollStyleName = getGridElement().getHorizontalScroller()
462 .getAttribute("class");
463 assertTrue(hscrollStyleName.contains(stylename + "-scroller"));
465 hscrollStyleName.contains(stylename + "-scroller-horizontal"));
467 String vscrollStyleName = getGridElement().getVerticalScroller()
468 .getAttribute("class");
469 assertTrue(vscrollStyleName.contains(stylename + "-scroller"));
470 assertTrue(vscrollStyleName.contains(stylename + "-scroller-vertical"));
474 public void testScrollPosDoesNotChangeAfterStateChange() {
476 scrollGridVerticallyTo(1000);
477 int scrollPos = getGridVerticalScrollPos();
478 selectMenuPath("Component", "Editor", "Enabled");
479 assertEquals("Scroll position should've not have changed", scrollPos,
480 getGridVerticalScrollPos());
484 public void testReloadPage() throws InterruptedException {
490 // After opening the URL Grid can be stuck in a state where it thinks it
491 // should wait for something that's not going to happen.
492 testBench().disableWaitForVaadin();
494 // Wait until page is loaded completely.
496 while (!$(GridElement.class).exists()) {
498 fail("Reloading page failed");
504 // Wait a bit more for notification to occur.
507 assertFalse("Exception occurred when reloading page",
508 isElementPresent(NotificationElement.class));
512 public void testAddThirdRowToGrid() {
514 selectMenuPath("Component", "Body rows", "Add third row");
515 assertFalse(logContainsText("Exception occured"));