diff options
4 files changed, 112 insertions, 30 deletions
diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index 50616c7930..746ff2be25 100755 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -4884,10 +4884,11 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, ColumnConfiguration conf = null; int index = 0; - if (grid != null && (bodyRenderer instanceof WidgetRenderer - || renderer instanceof WidgetRenderer)) { + if (!isHidden() && grid != null + && (bodyRenderer instanceof WidgetRenderer + || renderer instanceof WidgetRenderer)) { // Column needs to be recreated. - index = grid.getColumns().indexOf(this); + index = grid.getVisibleColumns().indexOf(this); conf = grid.escalator.getColumnConfiguration(); widthInConfiguration = conf.getColumnWidth(index); @@ -4908,7 +4909,7 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, conf.setColumnWidth(index, widthInConfiguration); } - if (grid != null) { + if (!isHidden() && grid != null) { grid.requestRefreshBody(); } } diff --git a/testbench-api/src/main/java/com/vaadin/testbench/elements/GridElement.java b/testbench-api/src/main/java/com/vaadin/testbench/elements/GridElement.java index bdc26ed663..945678ac2d 100644 --- a/testbench-api/src/main/java/com/vaadin/testbench/elements/GridElement.java +++ b/testbench-api/src/main/java/com/vaadin/testbench/elements/GridElement.java @@ -18,6 +18,7 @@ package com.vaadin.testbench.elements; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Optional; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebElement; @@ -81,11 +82,11 @@ public class GridElement extends AbstractComponentElement { /** * Gets the editor field for column in given index. - * + * * @param colIndex * the column index * @return the editor field for given location - * + * * @throws NoSuchElementException * if {@code isEditable(colIndex) == false} */ @@ -96,7 +97,7 @@ public class GridElement extends AbstractComponentElement { /** * Gets whether the column with the given index is editable, that is, * has an associated editor field. - * + * * @param colIndex * the column index * @return {@code true} if the column has an editor field, {@code false} @@ -109,7 +110,7 @@ public class GridElement extends AbstractComponentElement { /** * Checks whether a field is marked with an error. - * + * * @param colIndex * column index * @return <code>true</code> iff the field is marked with an error @@ -157,7 +158,7 @@ public class GridElement extends AbstractComponentElement { /** * Scrolls Grid element so that wanted row is displayed - * + * * @param index * Target row */ @@ -171,7 +172,7 @@ public class GridElement extends AbstractComponentElement { /** * Gets cell element with given row and column index. - * + * * @param rowIndex * Row index * @param colIndex @@ -186,7 +187,7 @@ public class GridElement extends AbstractComponentElement { /** * Gets row element with given row index. - * + * * @param index * Row index * @return Row element with given index. @@ -198,7 +199,7 @@ public class GridElement extends AbstractComponentElement { /** * Gets header cell element with given row and column index. - * + * * @param rowIndex * Row index * @param colIndex @@ -211,8 +212,8 @@ public class GridElement extends AbstractComponentElement { } /** - * Finds the header cell element with the given caption. If - * there are multiple headers with the same name, the first one is returned. + * Finds the header cell element with the given caption. If there are + * multiple headers with the same name, the first one is returned. * * @param caption * The header caption @@ -280,7 +281,7 @@ public class GridElement extends AbstractComponentElement { /** * Gets list of header cell elements on given row. - * + * * @param rowIndex * Row index * @return Header cell elements on given row. @@ -298,7 +299,7 @@ public class GridElement extends AbstractComponentElement { /** * Gets list of header cell elements on given row. - * + * * @param rowIndex * Row index * @return Header cell elements on given row. @@ -316,7 +317,7 @@ public class GridElement extends AbstractComponentElement { /** * Get header row count - * + * * @return Header row count */ public int getHeaderCount() { @@ -325,7 +326,7 @@ public class GridElement extends AbstractComponentElement { /** * Get footer row count - * + * * @return Footer row count */ public int getFooterCount() { @@ -334,7 +335,7 @@ public class GridElement extends AbstractComponentElement { /** * Get a header row by index - * + * * @param rowIndex * Row index * @return The th element of the row @@ -345,7 +346,7 @@ public class GridElement extends AbstractComponentElement { /** * Get a footer row by index - * + * * @param rowIndex * Row index * @return The tr element of the row @@ -356,7 +357,7 @@ public class GridElement extends AbstractComponentElement { /** * Get the vertical scroll element - * + * * @return The element representing the vertical scrollbar */ public TestBenchElement getVerticalScroller() { @@ -366,7 +367,7 @@ public class GridElement extends AbstractComponentElement { /** * Get the horizontal scroll element - * + * * @return The element representing the horizontal scrollbar */ public TestBenchElement getHorizontalScroller() { @@ -376,7 +377,7 @@ public class GridElement extends AbstractComponentElement { /** * Get the header element - * + * * @return The thead element */ public TestBenchElement getHeader() { @@ -385,7 +386,7 @@ public class GridElement extends AbstractComponentElement { /** * Get the body element - * + * * @return the tbody element */ public TestBenchElement getBody() { @@ -394,7 +395,7 @@ public class GridElement extends AbstractComponentElement { /** * Get the footer element - * + * * @return the tfoot element */ public TestBenchElement getFooter() { @@ -403,7 +404,7 @@ public class GridElement extends AbstractComponentElement { /** * Get the element wrapping the table element - * + * * @return The element that wraps the table element */ public TestBenchElement getTableWrapper() { @@ -418,7 +419,7 @@ public class GridElement extends AbstractComponentElement { /** * Helper function to get Grid subparts wrapped correctly - * + * * @param subPartSelector * SubPart to be used in ComponentLocator * @return SubPart element wrapped in TestBenchElement class @@ -429,7 +430,7 @@ public class GridElement extends AbstractComponentElement { /** * Gets the element that contains the details of a row. - * + * * @since * @param rowIndex * the index of the row for the details @@ -443,6 +444,27 @@ public class GridElement extends AbstractComponentElement { return getSubPart("#details[" + rowIndex + "]"); } + public void toggleColumnHidden(String toggleCaption) { + if (!isElementPresent(By.className("v-grid-sidebar-content"))) { + // Open sidebar menu + WebElement sidebarButton = findElement( + By.className("v-grid-sidebar")) + .findElement(By.tagName("button")); + sidebarButton.click(); + } + Optional<WebElement> toggleButton = getDriver() + .findElement(By.className("v-grid-sidebar-content")) + .findElements(By.className("column-hiding-toggle")).stream() + .filter(e -> e.getText().equals(toggleCaption)).findAny(); + if (toggleButton.isPresent()) { + toggleButton.ifPresent(e -> e.click()); + } else { + throw new IllegalArgumentException( + "No column hiding toggle with caption '" + toggleCaption + + "'"); + } + } + /** * Gets the total number of data rows in the grid. * @@ -468,14 +490,17 @@ public class GridElement extends AbstractComponentElement { */ public Iterable<GridRowElement> getRows() { return new Iterable<GridElement.GridRowElement>() { + @Override public Iterator<GridRowElement> iterator() { return new Iterator<GridElement.GridRowElement>() { int nextIndex = 0; + @Override public GridRowElement next() { return getRow(nextIndex++); } + @Override public boolean hasNext() { try { getRow(nextIndex); @@ -485,6 +510,7 @@ public class GridElement extends AbstractComponentElement { } } + @Override public void remove() { throw new UnsupportedOperationException( "remove not supported"); diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridRendererSwitch.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridRendererSwitch.java index b73383d22b..1b69479ffe 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridRendererSwitch.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridRendererSwitch.java @@ -22,9 +22,9 @@ public class GridRendererSwitch extends AbstractTestUI { protected void setup(VaadinRequest request) { Grid<Integer> grid = new Grid<>(); Column<Integer, String> column = grid.addColumn(i -> "Foo " + i) - .setCaption("Foo"); + .setCaption("Foo").setHidable(true); Column<Integer, String> secondColumn = grid.addColumn(i -> "Bar " + i) - .setCaption("Bar"); + .setCaption("Bar").setHidable(true); addComponent(grid); addComponent(new Button("Switch", e -> { diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridRendererSwitchTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridRendererSwitchTest.java index 1fd775b58a..a2ac8e1043 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/GridRendererSwitchTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridRendererSwitchTest.java @@ -97,4 +97,59 @@ public class GridRendererSwitchTest extends SingleBrowserTest { assertNoErrorNotifications(); } + @Test + public void testReorderColumnsHideAndSwitch() { + setDebug(true); + openTestURL(); + + GridElement grid = $(GridElement.class).first(); + Assert.assertEquals("Unexpected content in first grid cell", "Foo 0", + grid.getCell(0, 0).getAttribute("innerHTML")); + ButtonElement reverse = $(ButtonElement.class).caption("Reverse") + .first(); + + reverse.click(); + Assert.assertEquals( + "Unexpected content in first grid cell after reorder", "Bar 0", + grid.getCell(0, 0).getAttribute("innerHTML")); + + grid.toggleColumnHidden("Bar"); + + ButtonElement button = $(ButtonElement.class).caption("Switch").first(); + button.click(); + + assertNoErrorNotifications(); + + Assert.assertFalse( + "No button in cell after reversing order and changing renderer", + grid.getCell(0, 0).findElements(By.tagName("button")) + .isEmpty()); + grid.getCell(0, 0).findElement(By.tagName("button")).click(); + Assert.assertTrue("Notification not shown", + isElementPresent(NotificationElement.class)); + } + + @Test + public void testSwitchRendererOfHiddenColumn() { + setDebug(true); + openTestURL(); + + GridElement grid = $(GridElement.class).first(); + Assert.assertEquals("Unexpected content in first grid cell", "Foo 0", + grid.getCell(0, 0).getAttribute("innerHTML")); + + grid.toggleColumnHidden("Foo"); + + Assert.assertEquals("Unexpected content in first grid cell after hide", + "Bar 0", grid.getCell(0, 0).getAttribute("innerHTML")); + + ButtonElement button = $(ButtonElement.class).caption("Switch").first(); + button.click(); + + assertNoErrorNotifications(); + + Assert.assertEquals( + "Unexpected content in first grid cell after hidden renderer change", + "Bar 0", grid.getCell(0, 0).getAttribute("innerHTML")); + } } |