123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- /*
- * Copyright 2000-2014 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.tests.components.grid.basicfeatures.server;
-
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.core.IsNot.not;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertNotEquals;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.fail;
-
- import java.util.List;
-
- import org.junit.Test;
- import org.openqa.selenium.NoSuchElementException;
- import org.openqa.selenium.WebElement;
-
- import com.vaadin.testbench.By;
- import com.vaadin.testbench.TestBenchElement;
- import com.vaadin.testbench.elements.GridElement;
- import com.vaadin.testbench.elements.GridElement.GridCellElement;
- import com.vaadin.testbench.elements.NotificationElement;
- import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
- import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
-
- public class GridStructureTest extends GridBasicFeaturesTest {
-
- @Test
- public void testRemovingAllColumns() {
- setDebug(true);
- openTestURL();
- for (int i = 0; i < GridBasicFeatures.COLUMNS; ++i) {
- selectMenuPath("Component", "Columns", "Column " + i,
- "Add / Remove");
- assertFalse(isElementPresent(NotificationElement.class));
- }
-
- assertEquals("Headers still visible.", 0, getGridHeaderRowCells()
- .size());
- }
-
- @Test
- public void testRemoveAndAddColumn() {
- setDebug(true);
- openTestURL();
-
- assertEquals("column 0", getGridElement().getHeaderCell(0, 0).getText()
- .toLowerCase());
- selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
- assertEquals("column 1", getGridElement().getHeaderCell(0, 0).getText()
- .toLowerCase());
- selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
-
- // Column 0 is now the last column in Grid.
- assertEquals("Unexpected column content", "(0, 0)", getGridElement()
- .getCell(0, 11).getText());
- }
-
- @Test
- public void testRemovingColumn() throws Exception {
- openTestURL();
-
- // Column 0 should be visible
- List<TestBenchElement> cells = getGridHeaderRowCells();
- assertEquals("column 0", cells.get(0).getText().toLowerCase());
-
- // Hide column 0
- selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
-
- // Column 1 should now be the first cell
- cells = getGridHeaderRowCells();
- assertEquals("column 1", cells.get(0).getText().toLowerCase());
- }
-
- @Test
- public void testDataLoadingAfterRowRemoval() throws Exception {
- openTestURL();
-
- // Remove columns 2,3,4
- selectMenuPath("Component", "Columns", "Column 2", "Add / Remove");
- selectMenuPath("Component", "Columns", "Column 3", "Add / Remove");
- selectMenuPath("Component", "Columns", "Column 4", "Add / Remove");
-
- // Scroll so new data is lazy loaded
- scrollGridVerticallyTo(1000);
-
- // Let lazy loading do its job
- sleep(1000);
-
- // Check that row is loaded
- assertThat(getGridElement().getCell(11, 0).getText(), not("..."));
- }
-
- @Test
- public void testFreezingColumn() throws Exception {
- openTestURL();
-
- // Freeze column 1
- selectMenuPath("Component", "State", "Frozen column count", "1");
-
- WebElement cell = getGridElement().getCell(0, 0);
- assertTrue(cell.getAttribute("class").contains("frozen"));
-
- cell = getGridElement().getCell(0, 1);
- assertFalse(cell.getAttribute("class").contains("frozen"));
- }
-
- @Test
- public void testInitialColumnWidths() throws Exception {
- openTestURL();
-
- WebElement cell = getGridElement().getCell(0, 0);
- assertEquals(100, cell.getSize().getWidth());
-
- cell = getGridElement().getCell(0, 1);
- assertEquals(150, cell.getSize().getWidth());
-
- cell = getGridElement().getCell(0, 2);
- assertEquals(200, cell.getSize().getWidth());
- }
-
- @Test
- public void testColumnWidths() throws Exception {
- openTestURL();
-
- // Default column width is 100px
- WebElement cell = getGridElement().getCell(0, 0);
- assertEquals(100, cell.getSize().getWidth());
-
- // Set first column to be 200px wide
- selectMenuPath("Component", "Columns", "Column 0", "Column 0 Width",
- "200px");
-
- cell = getGridElement().getCell(0, 0);
- assertEquals(200, cell.getSize().getWidth());
-
- // Set second column to be 150px wide
- selectMenuPath("Component", "Columns", "Column 1", "Column 1 Width",
- "150px");
- cell = getGridElement().getCell(0, 1);
- assertEquals(150, cell.getSize().getWidth());
-
- selectMenuPath("Component", "Columns", "Column 0", "Column 0 Width",
- "Auto");
-
- // since the column 0 was previously 200, it should've shrunk when
- // autoresizing.
- cell = getGridElement().getCell(0, 0);
- assertLessThan("", cell.getSize().getWidth(), 200);
- }
-
- @Test
- public void testPrimaryStyleNames() throws Exception {
- openTestURL();
-
- // v-grid is default primary style namea
- assertPrimaryStylename("v-grid");
-
- selectMenuPath("Component", "State", "Primary style name",
- "v-escalator");
- assertPrimaryStylename("v-escalator");
-
- selectMenuPath("Component", "State", "Primary style name", "my-grid");
- assertPrimaryStylename("my-grid");
-
- selectMenuPath("Component", "State", "Primary style name", "v-grid");
- assertPrimaryStylename("v-grid");
- }
-
- /**
- * Test that the current view is updated when a server-side container change
- * occurs (without scrolling back and forth)
- */
- @Test
- public void testItemSetChangeEvent() throws Exception {
- openTestURL();
-
- final org.openqa.selenium.By newRow = By
- .xpath("//td[text()='newcell: 0']");
-
- assertTrue("Unexpected initial state", !isElementPresent(newRow));
-
- selectMenuPath("Component", "Body rows", "Add first row");
- assertTrue("Add row failed", isElementPresent(newRow));
-
- selectMenuPath("Component", "Body rows", "Remove first row");
- assertTrue("Remove row failed", !isElementPresent(newRow));
- }
-
- /**
- * Test that the current view is updated when a property's value is reflect
- * to the client, when the value is modified server-side.
- */
- @Test
- public void testPropertyValueChangeEvent() throws Exception {
- openTestURL();
-
- assertEquals("Unexpected cell initial state", "(0, 0)",
- getGridElement().getCell(0, 0).getText());
-
- selectMenuPath("Component", "Body rows",
- "Modify first row (getItemProperty)");
- assertEquals("(First) modification with getItemProperty failed",
- "modified: 0", getGridElement().getCell(0, 0).getText());
-
- selectMenuPath("Component", "Body rows",
- "Modify first row (getContainerProperty)");
- assertEquals("(Second) modification with getItemProperty failed",
- "modified: Column 0", getGridElement().getCell(0, 0).getText());
- }
-
- @Test
- public void testRemovingAllItems() throws Exception {
- openTestURL();
-
- selectMenuPath("Component", "Body rows", "Remove all rows");
-
- assertEquals(0, getGridElement().findElement(By.tagName("tbody"))
- .findElements(By.tagName("tr")).size());
- }
-
- @Test
- public void testRemoveFirstRowTwice() {
- openTestURL();
-
- selectMenuPath("Component", "Body rows", "Remove first row");
- selectMenuPath("Component", "Body rows", "Remove first row");
-
- getGridElement().scrollToRow(50);
- assertFalse("Listener setup problem occurred.",
- logContainsText("AssertionError: Value change listeners"));
- }
-
- @Test
- public void testVerticalScrollBarVisibilityWhenEnoughRows()
- throws Exception {
- openTestURL();
-
- assertTrue(verticalScrollbarIsPresent());
-
- selectMenuPath("Component", "Body rows", "Remove all rows");
- assertFalse(verticalScrollbarIsPresent());
-
- selectMenuPath("Component", "Size", "HeightMode Row");
- selectMenuPath("Component", "Size", "Height by Rows", "2.33 rows");
- selectMenuPath("Component", "Body rows", "Add first row");
- selectMenuPath("Component", "Body rows", "Add first row");
- assertFalse(verticalScrollbarIsPresent());
-
- selectMenuPath("Component", "Body rows", "Add first row");
- assertTrue(verticalScrollbarIsPresent());
- }
-
- @Test
- public void testBareItemSetChange() throws Exception {
- openTestURL();
- filterSomeAndAssert();
- }
-
- @Test
- public void testBareItemSetChangeRemovingAllRows() throws Exception {
- openTestURL();
- selectMenuPath("Component", "Filter", "Add impassable filter");
- assertFalse("A notification shouldn't have been displayed",
- $(NotificationElement.class).exists());
- assertTrue("No body cells should've been found", getGridElement()
- .getBody().findElements(By.tagName("td")).isEmpty());
- }
-
- @Test
- public void testBareItemSetChangeWithMidScroll() throws Exception {
- openTestURL();
- getGridElement().scrollToRow(GridBasicFeatures.ROWS / 2);
- filterSomeAndAssert();
- }
-
- @Test
- public void testBareItemSetChangeWithBottomScroll() throws Exception {
- openTestURL();
- getGridElement().scrollToRow(GridBasicFeatures.ROWS);
- filterSomeAndAssert();
- }
-
- private void filterSomeAndAssert() {
- selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
- boolean foundElements = false;
- for (int row = 0; row < 100; row++) {
- try {
- GridCellElement cell = getGridElement().getCell(row, 1);
- foundElements = true;
- assertTrue("Unexpected cell contents. "
- + "Did the ItemSetChange work after all?", cell
- .getText().startsWith("(23"));
- } catch (NoSuchElementException e) {
- assertTrue("No rows were found", foundElements);
- return;
- }
- }
- fail("unexpected amount of rows post-filter. Did the ItemSetChange work after all?");
- }
-
- @Test
- public void testRemoveLastColumn() {
- setDebug(true);
- openTestURL();
-
- String columnName = "Column " + (GridBasicFeatures.COLUMNS - 1);
- assertTrue(columnName + " was not present in DOM",
- isElementPresent(By.xpath("//th[text()='" + columnName + "']")));
- selectMenuPath("Component", "Columns", columnName, "Add / Remove");
- assertFalse(isElementPresent(NotificationElement.class));
- assertFalse(columnName + " was still present in DOM",
- isElementPresent(By.xpath("//th[text()='" + columnName + "']")));
- }
-
- @Test
- public void testReverseColumns() {
- openTestURL();
-
- String[] gridData = new String[GridBasicFeatures.COLUMNS];
- GridElement grid = getGridElement();
- for (int i = 0; i < gridData.length; ++i) {
- gridData[i] = grid.getCell(0, i).getAttribute("innerHTML");
- }
-
- selectMenuPath("Component", "State", "Reverse Grid Columns");
-
- // Compare with reversed order
- for (int i = 0; i < gridData.length; ++i) {
- final int column = gridData.length - 1 - i;
- final String newText = grid.getCell(0, column).getAttribute(
- "innerHTML");
- assertEquals("Grid contained unexpected values. (0, " + column
- + ")", gridData[i], newText);
- }
- }
-
- @Test
- public void testAddingProperty() {
- setDebug(true);
- openTestURL();
-
- assertNotEquals("property value", getGridElement().getCell(0, 0)
- .getText());
- selectMenuPath("Component", "Properties", "Prepend property");
- assertEquals("property value", getGridElement().getCell(0, 0).getText());
- }
-
- @Test
- public void testRemovingAddedProperty() {
- openTestURL();
-
- assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
- assertNotEquals("property value", getGridElement().getCell(0, 0)
- .getText());
-
- selectMenuPath("Component", "Properties", "Prepend property");
- selectMenuPath("Component", "Properties", "Prepend property");
-
- assertNotEquals("property value", getGridElement().getCell(0, 0)
- .getText());
- assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
- }
-
- private boolean verticalScrollbarIsPresent() {
- return "scroll".equals(getGridVerticalScrollbar().getCssValue(
- "overflow-y"));
- }
-
- @Test
- public void testAddRowAboveViewport() {
- setDebug(true);
- openTestURL();
-
- GridCellElement cell = getGridElement().getCell(500, 1);
- String cellContent = cell.getText();
- selectMenuPath("Component", "Body rows", "Add first row");
-
- assertFalse("Error notification was present",
- isElementPresent(NotificationElement.class));
-
- assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
- }
-
- @Test
- public void testRemoveAndAddRowAboveViewport() {
- setDebug(true);
- openTestURL();
-
- GridCellElement cell = getGridElement().getCell(500, 1);
- String cellContent = cell.getText();
- selectMenuPath("Component", "Body rows", "Remove first row");
-
- assertFalse("Error notification was present after removing row",
- isElementPresent(NotificationElement.class));
-
- assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
-
- selectMenuPath("Component", "Body rows", "Add first row");
-
- assertFalse("Error notification was present after adding row",
- isElementPresent(NotificationElement.class));
-
- assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
- }
-
- @Test
- public void testScrollAndRemoveAll() {
- setDebug(true);
- openTestURL();
-
- getGridElement().scrollToRow(500);
- selectMenuPath("Component", "Body rows", "Remove all rows");
-
- assertFalse("Error notification was present after removing all rows",
- isElementPresent(NotificationElement.class));
-
- assertFalse(getGridElement().isElementPresent(By.vaadin("#cell[0][0]")));
- }
-
- private void assertPrimaryStylename(String stylename) {
- assertTrue(getGridElement().getAttribute("class").contains(stylename));
-
- String tableWrapperStyleName = getGridElement().getTableWrapper()
- .getAttribute("class");
- assertTrue(tableWrapperStyleName.contains(stylename + "-tablewrapper"));
-
- String hscrollStyleName = getGridElement().getHorizontalScroller()
- .getAttribute("class");
- assertTrue(hscrollStyleName.contains(stylename + "-scroller"));
- assertTrue(hscrollStyleName
- .contains(stylename + "-scroller-horizontal"));
-
- String vscrollStyleName = getGridElement().getVerticalScroller()
- .getAttribute("class");
- assertTrue(vscrollStyleName.contains(stylename + "-scroller"));
- assertTrue(vscrollStyleName.contains(stylename + "-scroller-vertical"));
- }
-
- @Test
- public void testScrollPosDoesNotChangeAfterStateChange() {
- openTestURL();
- scrollGridVerticallyTo(1000);
- int scrollPos = getGridVerticalScrollPos();
- selectMenuPath("Component", "Editor", "Enabled");
- assertEquals("Scroll position should've not have changed", scrollPos,
- getGridVerticalScrollPos());
- }
-
- @Test
- public void testReloadPage() throws InterruptedException {
- setDebug(true);
- openTestURL();
-
- reopenTestURL();
-
- // After opening the URL Grid can be stuck in a state where it thinks it
- // should wait for something that's not going to happen.
- testBench().disableWaitForVaadin();
-
- // Wait until page is loaded completely.
- int count = 0;
- while (!$(GridElement.class).exists()) {
- if (count == 100) {
- fail("Reloading page failed");
- }
- sleep(100);
- ++count;
- }
-
- // Wait a bit more for notification to occur.
- sleep(1000);
-
- assertFalse("Exception occurred when reloading page",
- isElementPresent(NotificationElement.class));
- }
-
- @Test
- public void testAddThirdRowToGrid() {
- openTestURL();
- selectMenuPath("Component", "Body rows", "Add third row");
- assertFalse(logContainsText("Exception occured"));
- }
- }
|