]> source.dussan.org Git - vaadin-framework.git/commitdiff
Don't attempt to scroll to the beginning or end if Grid has no rows. (#11570)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Mon, 20 May 2019 06:35:49 +0000 (09:35 +0300)
committerZhe Sun <31067185+ZheSun88@users.noreply.github.com>
Wed, 22 May 2019 13:31:31 +0000 (16:31 +0300)
Fixes #11558

client/src/main/java/com/vaadin/client/widgets/Grid.java
uitest/src/main/java/com/vaadin/tests/components/grid/GridScrollWithoutRows.java [new file with mode: 0644]

index 7add1fd654329a416d62484695393618d8b801cb..cb43bdd7d1992f42c898f11de53ce660b7c3e741 100755 (executable)
@@ -7492,15 +7492,19 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
      * Scrolls to the beginning of the very first row.
      */
     public void scrollToStart() {
-        scrollToRow(0, ScrollDestination.START);
+        if (getEscalator().getBody().getRowCount() > 0) {
+            scrollToRow(0, ScrollDestination.START);
+        }
     }
 
     /**
      * Scrolls to the end of the very last row.
      */
     public void scrollToEnd() {
-        scrollToRow(escalator.getBody().getRowCount() - 1,
-                ScrollDestination.END);
+        if (getEscalator().getBody().getRowCount() > 0) {
+            scrollToRow(escalator.getBody().getRowCount() - 1,
+                    ScrollDestination.END);
+        }
     }
 
     /**
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridScrollWithoutRows.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridScrollWithoutRows.java
new file mode 100644 (file)
index 0000000..7de07a5
--- /dev/null
@@ -0,0 +1,61 @@
+package com.vaadin.tests.components.grid;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.SelectionMode;
+
+/**
+ * There is no corresponding TB test as this problem can only be reproduced
+ * using SuperDevMode.
+ */
+public class GridScrollWithoutRows extends AbstractTestUI {
+
+    private int counter = 0;
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        List<Integer> data = new ArrayList<>();
+
+        Grid<Integer> grid = new Grid<>();
+        grid.addColumn(Integer::valueOf).setCaption("ID").setId("id");
+        grid.addColumn(Integer::valueOf).setCaption("FOO").setId("foo");
+        grid.setItems(data);
+
+        grid.setSelectionMode(SelectionMode.NONE);
+        grid.setWidth("250px");
+        grid.setHeightByRows(3);
+        addComponent(grid);
+
+        addComponent(new Button("Add row", e -> {
+            data.add(counter);
+            ++counter;
+            grid.getDataProvider().refreshAll();
+        }));
+        Button beginningButton = new Button("Scroll to beginning", e -> {
+            grid.scrollToStart();
+        });
+        beginningButton.setId("beginning");
+        addComponent(beginningButton);
+        Button endButton = new Button("Scroll to end", e -> {
+            grid.scrollToEnd();
+        });
+        endButton.setId("end");
+        addComponent(endButton);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "It should be possible to scroll to beginning or end without assertion errors "
+                + "even when there are no rows (requires SuperDevMode).";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 11558;
+    }
+}