]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix invalid assumptions about empty Grid (#16734, #16684)
authorLeif Åstrand <leif@vaadin.com>
Sun, 15 Mar 2015 19:08:21 +0000 (21:08 +0200)
committerLeif Åstrand <leif@vaadin.com>
Mon, 16 Mar 2015 06:28:22 +0000 (08:28 +0200)
* Don't reset to default column sizes when adding row to emtpy escalator
* Don't wait for data after size is explicilty reset to 0
* Don't assume there were previous rows when adjusting focus on add

Change-Id: I59e8c0ef0c5633cf3678db63a660e3f1d1ca7d2a

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

index 5f69be9142ee1c061335515d636603d3f763ba67..3d4459a0ccb59413c52334aa1a75ed2a4bbdeb4c 100644 (file)
@@ -1390,10 +1390,10 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker
                      * first time.
                      */
                     Map<Integer, Double> colWidths = new HashMap<Integer, Double>();
-                    Double width = Double
-                            .valueOf(ColumnConfigurationImpl.Column.DEFAULT_COLUMN_WIDTH_PX);
                     for (int i = 0; i < getColumnConfiguration()
                             .getColumnCount(); i++) {
+                        Double width = Double.valueOf(getColumnConfiguration()
+                                .getColumnWidth(i));
                         Integer col = Integer.valueOf(i);
                         colWidths.put(col, width);
                     }
index 75d5ee0f99fad16131bb702513db8beafafe82de..8a8aa75e920b44e8d42ff739838f062a60bd45cf 100644 (file)
@@ -2119,6 +2119,8 @@ public class Grid<T> extends ResizeComposite implements
             boolean insertionIsAboveFocusedCell = (added.getStart() <= rowWithFocus);
             if (bodyHasFocus && insertionIsAboveFocusedCell) {
                 rowWithFocus += added.length();
+                rowWithFocus = Math.min(rowWithFocus, escalator.getBody()
+                        .getRowCount() - 1);
                 refreshRow(rowWithFocus);
             }
         }
@@ -4578,6 +4580,10 @@ public class Grid<T> extends ResizeComposite implements
                     Range visibleRowRange = escalator.getVisibleRowRange();
                     dataSource.ensureAvailability(visibleRowRange.getStart(),
                             visibleRowRange.length());
+                } else {
+                    // We won't expect any data more data updates, so just make
+                    // the bookkeeping happy
+                    dataAvailable(0, 0);
                 }
 
                 assert body.getRowCount() == newSize;
diff --git a/uitest/src/com/vaadin/tests/components/grid/GridColumnWidthsWithoutData.java b/uitest/src/com/vaadin/tests/components/grid/GridColumnWidthsWithoutData.java
new file mode 100644 (file)
index 0000000..1dfc7bc
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * 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;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.VerticalLayout;
+
+public class GridColumnWidthsWithoutData extends AbstractTestUI {
+
+    private Grid grid = createGrid(true);
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        addComponent(grid);
+
+        addComponent(new Button("Recreate without data",
+                new Button.ClickListener() {
+                    @Override
+                    public void buttonClick(ClickEvent event) {
+                        replaceGrid(createGrid(false));
+                    }
+                }));
+
+        addComponent(new Button("Recreate with data",
+                new Button.ClickListener() {
+                    @Override
+                    public void buttonClick(ClickEvent event) {
+                        replaceGrid(createGrid(true));
+                    }
+                }));
+
+        addComponent(new Button("Add data", new Button.ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                addDataToGrid(grid);
+            }
+        }));
+
+        addComponent(new Button("Remove data", new Button.ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                grid.getContainerDataSource().removeAllItems();
+            }
+        }));
+
+    }
+
+    private void replaceGrid(Grid newGrid) {
+        ((VerticalLayout) grid.getParent()).replaceComponent(grid, newGrid);
+        grid = newGrid;
+    }
+
+    private Grid createGrid(boolean withData) {
+        Grid grid = new Grid();
+        grid.addColumn("foo");
+        grid.addColumn("bar");
+        grid.setWidth("300px");
+
+        if (withData) {
+            addDataToGrid(grid);
+        }
+
+        return grid;
+    }
+
+    private void addDataToGrid(Grid grid) {
+        grid.addRow("Some", "Data with more data in one col");
+    }
+
+}
diff --git a/uitest/src/com/vaadin/tests/components/grid/GridColumnWidthsWithoutDataTest.java b/uitest/src/com/vaadin/tests/components/grid/GridColumnWidthsWithoutDataTest.java
new file mode 100644 (file)
index 0000000..dd63d6a
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.testbench.elements.GridElement.GridCellElement;
+import com.vaadin.testbench.elements.NotificationElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class GridColumnWidthsWithoutDataTest extends SingleBrowserTest {
+
+    @Test
+    public void testWidthsWhenAddingDataBack() {
+        openTestURL();
+        GridElement grid = $(GridElement.class).first();
+
+        int[] baseWidths = getColWidths(grid);
+        Assert.assertEquals("Sanity check", 2, baseWidths.length);
+
+        Assert.assertTrue("Columns should not have equal width",
+                Math.abs(baseWidths[0] - baseWidths[1]) > 2);
+
+        removeData();
+
+        assertSameWidths(baseWidths, getColWidths(grid));
+
+        addData();
+
+        assertSameWidths(baseWidths, getColWidths(grid));
+    }
+
+    @Test
+    public void restWidthsWhenInitiallyEmpty() {
+        setDebug(true);
+        openTestURL();
+        $(ButtonElement.class).caption("Recreate without data").first().click();
+
+        GridElement grid = $(GridElement.class).first();
+
+        int[] baseWidths = getColWidths(grid);
+        Assert.assertEquals("Sanity check", 2, baseWidths.length);
+
+        Assert.assertTrue("Columns should have roughly equal width",
+                Math.abs(baseWidths[0] - baseWidths[1]) < 10);
+        Assert.assertTrue("Columns should not have default widths",
+                baseWidths[0] > 140);
+        Assert.assertTrue("Columns should not have default widths",
+                baseWidths[1] > 140);
+
+        addData();
+
+        assertSameWidths(baseWidths, getColWidths(grid));
+
+        Assert.assertFalse("Notification was present",
+                isElementPresent(NotificationElement.class));
+    }
+
+    private static void assertSameWidths(int[] expected, int[] actual) {
+        Assert.assertEquals("Arrays have differing lengths", expected.length,
+                actual.length);
+
+        for (int i = 0; i < expected.length; i++) {
+            if (Math.abs(expected[i] - actual[i]) > 1) {
+                Assert.fail("Differing sizes at index " + i + ". Expected "
+                        + expected[i] + " but got " + actual[i]);
+            }
+        }
+    }
+
+    private void removeData() {
+        $(ButtonElement.class).caption("Remove data").first().click();
+    }
+
+    private void addData() {
+        $(ButtonElement.class).caption("Add data").first().click();
+    }
+
+    private int[] getColWidths(GridElement grid) {
+        List<GridCellElement> headerCells = grid.getHeaderCells(0);
+        int[] widths = new int[headerCells.size()];
+        for (int i = 0; i < widths.length; i++) {
+            widths[i] = headerCells.get(i).getSize().getWidth();
+        }
+        return widths;
+    }
+}