]> source.dussan.org Git - vaadin-framework.git/commitdiff
Always calculate Escalator max row count the same way (#8740)
authorArtur <artur@vaadin.com>
Tue, 7 Mar 2017 10:44:01 +0000 (12:44 +0200)
committerHenri Sara <henri.sara@gmail.com>
Tue, 7 Mar 2017 10:44:01 +0000 (12:44 +0200)
* Rename getMaxEscalatorRowCapacity to describe what it does

* Always calculate Escalator max row count the same way

This changes Escalator to not take a horizontal scrollbar
into account when trying to determine "maximum visible rows". This will
add another row, compared to previous versions, when there is a horizontal
scrollbar. In reality, it would likely make sense to always add 10 more rows
to have some buffer above and below the visible area.

Fixes #8661

client/src/main/java/com/vaadin/client/widgets/Escalator.java
uitest/src/main/java/com/vaadin/tests/components/grid/HideGridColumnWhenHavingUnsuitableHeight.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/grid/HideGridColumnWhenHavingUnsuitableHeightTest.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/escalator/EscalatorSpacerTest.java
uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/server/GridScrollTest.java

index f9c8d96ad40bd67676675527d739256b6af93716..7d5cd8981bbc849e475be3f5922e4b2747c266c5 100644 (file)
@@ -2999,7 +2999,7 @@ public class Escalator extends Widget
         private List<TableRowElement> fillAndPopulateEscalatorRowsIfNeeded(
                 final int index, final int numberOfRows) {
 
-            final int escalatorRowsStillFit = getMaxEscalatorRowCapacity()
+            final int escalatorRowsStillFit = getMaxVisibleRowCount()
                     - getDomRowCount();
             final int escalatorRowsNeeded = Math.min(numberOfRows,
                     escalatorRowsStillFit);
@@ -3032,16 +3032,22 @@ public class Escalator extends Widget
             }
         }
 
-        private int getMaxEscalatorRowCapacity() {
-            final int maxEscalatorRowCapacity = (int) Math
-                    .ceil(getHeightOfSection() / getDefaultRowHeight()) + 1;
+        private int getMaxVisibleRowCount() {
+            double heightOfSection = getHeightOfSection();
+            // By including the possibly shown scrollbar height, we get a
+            // consistent count and do not add/remove rows whenever a scrollbar
+            // is shown
+            heightOfSection += horizontalScrollbarDeco.getOffsetHeight();
+            double defaultRowHeight = getDefaultRowHeight();
+            final int maxVisibleRowCount = (int) Math
+                    .ceil(heightOfSection / defaultRowHeight) + 1;
 
             /*
-             * maxEscalatorRowCapacity can become negative if the headers and
-             * footers start to overlap. This is a crazy situation, but Vaadin
-             * blinks the components a lot, so it's feasible.
+             * maxVisibleRowCount can become negative if the headers and footers
+             * start to overlap. This is a crazy situation, but Vaadin blinks
+             * the components a lot, so it's feasible.
              */
-            return Math.max(0, maxEscalatorRowCapacity);
+            return Math.max(0, maxVisibleRowCount);
         }
 
         @Override
@@ -3524,12 +3530,12 @@ public class Escalator extends Widget
              * TODO [[spacer]]: these assumptions will be totally broken with
              * spacers.
              */
-            final int maxEscalatorRows = getMaxEscalatorRowCapacity();
+            final int maxVisibleRowCount = getMaxVisibleRowCount();
             final int currentTopRowIndex = getLogicalRowIndex(
                     visualRowOrder.getFirst());
 
             final Range[] partitions = logicalRange.partitionWith(
-                    Range.withLength(currentTopRowIndex, maxEscalatorRows));
+                    Range.withLength(currentTopRowIndex, maxVisibleRowCount));
             final Range insideRange = partitions[1];
             return insideRange.offsetBy(-currentTopRowIndex);
         }
@@ -3641,8 +3647,8 @@ public class Escalator extends Widget
                 return;
             }
 
-            final int maxEscalatorRows = getMaxEscalatorRowCapacity();
-            final int neededEscalatorRows = Math.min(maxEscalatorRows,
+            final int maxVisibleRowCount = getMaxVisibleRowCount();
+            final int neededEscalatorRows = Math.min(maxVisibleRowCount,
                     body.getRowCount());
             final int neededEscalatorRowsDiff = neededEscalatorRows
                     - visualRowOrder.size();
@@ -6687,7 +6693,7 @@ public class Escalator extends Widget
      * @return the maximum capacity
      */
     public int getMaxVisibleRowCount() {
-        return body.getMaxEscalatorRowCapacity();
+        return body.getMaxVisibleRowCount();
     }
 
     /**
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/HideGridColumnWhenHavingUnsuitableHeight.java b/uitest/src/main/java/com/vaadin/tests/components/grid/HideGridColumnWhenHavingUnsuitableHeight.java
new file mode 100644 (file)
index 0000000..6eb054a
--- /dev/null
@@ -0,0 +1,92 @@
+package com.vaadin.tests.components.grid;
+
+import com.vaadin.annotations.Theme;
+import com.vaadin.data.Item;
+import com.vaadin.data.util.BeanItemContainer;
+import com.vaadin.data.util.GeneratedPropertyContainer;
+import com.vaadin.data.util.PropertyValueGenerator;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.Column;
+import com.vaadin.ui.renderers.ButtonRenderer;
+
+@Theme("valo")
+public class HideGridColumnWhenHavingUnsuitableHeight extends AbstractTestUI {
+
+    private Grid grid;
+
+    public static class SampleBean {
+
+        private String col1;
+        private String col2;
+
+        public SampleBean() {
+        }
+
+        public String getCol1() {
+            return col1;
+        }
+
+        public void setCol1(String col1) {
+            this.col1 = col1;
+        }
+
+        public String getCol2() {
+            return col2;
+        }
+
+        public void setCol2(String col2) {
+            this.col2 = col2;
+        }
+    }
+
+    @SuppressWarnings("serial")
+    @Override
+    protected void setup(VaadinRequest vaadinRequest) {
+        grid = new Grid();
+
+        BeanItemContainer<SampleBean> container = generateData(50);
+        GeneratedPropertyContainer gpc = new GeneratedPropertyContainer(
+                container);
+        grid.setContainerDataSource(gpc);
+
+        gpc.addGeneratedProperty("Button1",
+                new PropertyValueGenerator<String>() {
+                    @Override
+                    public String getValue(Item item, Object itemId,
+                            Object propertyId) {
+                        return "Button 1";
+                    }
+
+                    @Override
+                    public Class<String> getType() {
+                        return String.class;
+                    }
+                });
+        grid.getColumn("Button1").setRenderer(new ButtonRenderer());
+        grid.getColumn("col1").setWidth(1600);
+        for (Column gridCol : grid.getColumns()) {
+            gridCol.setHidable(true);
+        }
+        grid.setWidth("100%");
+        grid.setHeight("425px");
+
+        grid.setColumns("col1", "col2", "Button1");
+
+        addComponent(grid);
+    }
+
+    private BeanItemContainer<SampleBean> generateData(int rows) {
+        BeanItemContainer<SampleBean> container = new BeanItemContainer<SampleBean>(
+                SampleBean.class);
+        for (int y = 0; y < rows; ++y) {
+            SampleBean sampleBean = new SampleBean();
+            sampleBean.setCol1("Row " + y + " Column 1");
+            sampleBean.setCol2("Row " + y + " Column 2");
+            container.addBean(sampleBean);
+        }
+        return container;
+    }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/HideGridColumnWhenHavingUnsuitableHeightTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/HideGridColumnWhenHavingUnsuitableHeightTest.java
new file mode 100644 (file)
index 0000000..e9d465b
--- /dev/null
@@ -0,0 +1,40 @@
+package com.vaadin.tests.components.grid;
+
+import java.util.List;
+import java.util.logging.Level;
+
+import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class HideGridColumnWhenHavingUnsuitableHeightTest
+        extends SingleBrowserTest {
+
+    @Test
+    public void hideAndScroll() {
+        openTestURL("debug");
+        GridElement grid = $(GridElement.class).first();
+
+        getSidebarOpenButton(grid).click();
+        // Hide first column
+        getSidebarPopup().findElements(By.tagName("td")).get(0).click();
+
+        grid.scrollToRow(25);
+        assertNoDebugMessage(Level.SEVERE);
+    }
+
+    protected WebElement getSidebarOpenButton(GridElement grid) {
+        List<WebElement> elements = grid
+                .findElements(By.className("v-grid-sidebar-button"));
+        return elements.isEmpty() ? null : elements.get(0);
+    }
+
+    protected WebElement getSidebarPopup() {
+        List<WebElement> elements = findElements(
+                By.className("v-grid-sidebar-popup"));
+        return elements.isEmpty() ? null : elements.get(0);
+    }
+}
index 34b69027a425d76a6649a0f4c7b517e34a0f466f..e64c57a49dde1581f73f2796406726e2fa88c057 100644 (file)
@@ -290,12 +290,12 @@ public class EscalatorSpacerTest extends EscalatorBasicClientFeaturesTest {
         selectMenuPath(FEATURES, SPACERS, ROW_1, SET_100PX);
 
         /*
-         * we check for row -2 instead of -1, because escalator has the one row
+         * we check for row -3 instead of -1, because escalator has two rows
          * buffered underneath the footer
          */
         selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, SCROLL_TO, ROW_75);
         Thread.sleep(500);
-        assertEquals("Row 75: 0,75", getBodyCell(-2, 0).getText());
+        assertEquals("Row 75: 0,75", getBodyCell(-3, 0).getText());
 
         selectMenuPath(COLUMNS_AND_ROWS, BODY_ROWS, SCROLL_TO, ROW_25);
         Thread.sleep(500);
index 815d6f72a65c243063b8630c63281940345cb258..c2d4926f73e64bbb95f89150c408efe723f0063a 100644 (file)
@@ -41,17 +41,17 @@ public class GridScrollTest extends GridBasicFeaturesTest {
         selectMenuPath("Settings", "Clear log");
         $(GridElement.class).first().scrollToRow(40);
         assertEquals("Log row did not contain expected item request",
-                "0. Requested items 0 - 86", getLogRow(0));
+                "0. Requested items 0 - 91", getLogRow(0));
         assertEquals("There should be only one log row", " ", getLogRow(1));
         selectMenuPath("Settings", "Clear log");
         $(GridElement.class).first().scrollToRow(100);
         assertEquals("Log row did not contain expected item request",
-                "0. Requested items 47 - 146", getLogRow(0));
+                "0. Requested items 43 - 151", getLogRow(0));
         assertEquals("There should be only one log row", " ", getLogRow(1));
         selectMenuPath("Settings", "Clear log");
         $(GridElement.class).first().scrollToRow(300);
         assertEquals("Log row did not contain expected item request",
-                "0. Requested items 247 - 346", getLogRow(0));
+                "0. Requested items 243 - 351", getLogRow(0));
         assertEquals("There should be only one log row", " ", getLogRow(1));
     }