]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix rendering of TreeGrid's frozen columns after hierarchy-column reset (#12028) pr12027/r17
authorTarek Oraby <42799254+tarekoraby@users.noreply.github.com>
Thu, 18 Jun 2020 05:41:19 +0000 (08:41 +0300)
committerGitHub <noreply@github.com>
Thu, 18 Jun 2020 05:41:19 +0000 (08:41 +0300)
* Add tests

* Fix getVisibleFrozenColumnCount() if SelectionMode is multi

client/src/main/java/com/vaadin/client/widgets/Grid.java
uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridChangeHierarchyColumn.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridChangeHierarchyColumnTest.java [new file with mode: 0644]

index 2faddaf68d9a1daaf77c8a2ad0291075ea202f68..9f36864bc8bdde4032825299f30d7c742361245b 100755 (executable)
@@ -7416,7 +7416,15 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
 
         // for the escalator the hidden columns are not in the frozen column
         // count, but for grid they are. thus need to convert the index
-        for (int i = 0; i < frozenColumnCount; i++) {
+        int limit = getFrozenColumnCount();
+        if (getSelectionColumn().isPresent()) {
+            // If the grid is in MultiSelect mode, getColumn(0) in the following
+            // for loop returns the selection column. Accordingly, verifying
+            // which frozen columns are visible if the selection column is
+            // present should take this fact into account.
+            limit++;
+        }
+        for (int i = 0; i < limit; i++) {
             if (i >= getColumnCount() || getColumn(i).isHidden()) {
                 numberOfColumns--;
             }
diff --git a/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridChangeHierarchyColumn.java b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridChangeHierarchyColumn.java
new file mode 100644 (file)
index 0000000..0e6e650
--- /dev/null
@@ -0,0 +1,75 @@
+package com.vaadin.tests.components.treegrid;
+
+import com.vaadin.data.TreeData;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.CssLayout;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.TreeGrid;
+
+public class TreeGridChangeHierarchyColumn extends AbstractTestUI {
+
+    @Override
+    protected String getTestDescription() {
+        return "TreeGrid in MultiSelect mode should take hiden columns into account when"
+                + " rendering frozen columns after hierarchy-column reset.";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 12026;
+    }
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        TreeGrid<String> treeGrid = new TreeGrid<>();
+        treeGrid.setId("TreeGrid");
+
+        for (int i = 0; i < 20; i++) {
+            String columnId = String.valueOf(i);
+            Grid.Column<String, Component> column = addColumn(treeGrid,
+                    columnId);
+            column.setCaption(columnId);
+            column.setId(columnId);
+        }
+
+        TreeData<String> data = treeGrid.getTreeData();
+        data.addItem(null, "child");
+        data.addItem("child", "grandChild");
+
+        treeGrid.setHierarchyColumn(treeGrid.getColumns().get(0));
+
+        Button hideHierCol = new Button("Hide Hierarchy Column");
+        hideHierCol.addClickListener(e -> {
+            treeGrid.getHierarchyColumn().setHidden(true);
+        });
+        hideHierCol.setId("hideHierColButton");
+
+        Button setHierCol = new Button("Set new Hierarchy Column");
+        setHierCol.addClickListener(e -> {
+            treeGrid.getColumns().stream().filter(column -> !column.isHidden())
+                    .findFirst().ifPresent(col -> {
+                        treeGrid.setHierarchyColumn(col.getId());
+                    });
+        });
+        setHierCol.setId("setHierColButton");
+
+        treeGrid.setSelectionMode(Grid.SelectionMode.MULTI);
+        treeGrid.setFrozenColumnCount(1);
+
+        addComponents(treeGrid, hideHierCol, setHierCol);
+    }
+
+    private Grid.Column<String, Component> addColumn(Grid<String> grid,
+            String columnId) {
+        return grid.addComponentColumn(val -> {
+            Label label = new Label(columnId);
+            label.setWidth(50, Unit.PIXELS);
+            return new CssLayout(label);
+        });
+    }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridChangeHierarchyColumnTest.java b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridChangeHierarchyColumnTest.java
new file mode 100644 (file)
index 0000000..77efba9
--- /dev/null
@@ -0,0 +1,40 @@
+package com.vaadin.tests.components.treegrid;
+
+import static org.junit.Assert.assertEquals;
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.TreeGridElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class TreeGridChangeHierarchyColumnTest extends MultiBrowserTest {
+
+    @Test
+    public void renderingFrozenColumnsShouldFactorInHiddenColumns() {
+        openTestURL();
+        waitForElementPresent(By.id("TreeGrid"));
+        waitForElementPresent(By.id("hideHierColButton"));
+        waitForElementPresent(By.id("setHierColButton"));
+
+        TreeGridElement treeGrid = $(TreeGridElement.class).id("TreeGrid");
+        ButtonElement hideHierCol = $(ButtonElement.class)
+                .id("hideHierColButton");
+        ButtonElement setHierCol = $(ButtonElement.class)
+                .id("setHierColButton");
+
+        hideHierCol.click();
+        setHierCol.click();
+
+        // Wait for the new hierarchy column to be rendered
+        waitForElementPresent(By.className("v-treegrid-expander"));
+
+        List<WebElement> frozenCells = treeGrid
+                .findElements(By.className("frozen"));
+
+        assertEquals("Only the MultiSelect column should have frozen cells.", 2,
+                frozenCells.size());
+    }
+}