From e80358a39130face6eeeccf68a6c81782a710105 Mon Sep 17 00:00:00 2001 From: Zhe Sun <31067185+ZheSun88@users.noreply.github.com> Date: Thu, 6 Jun 2019 08:45:02 +0300 Subject: Recalculate the column widths before the first row is added (#11609) * Recalculate the column widths before the first row is added Fixes #11607 --- .../main/java/com/vaadin/client/widgets/Grid.java | 8 ++++ ...lignments_are_correct_ANY_Chrome__alignment.png | Bin 0 -> 20652 bytes .../grid/CheckboxAlignmentWithNoHeaderGrid.java | 49 +++++++++++++++++++++ .../CheckboxAlignmentWithNoHeaderGridTest.java | 37 ++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 uitest/reference-screenshots/chrome/CheckboxAlignmentWithNoHeaderGridTest-alignments_are_correct_ANY_Chrome__alignment.png create mode 100644 uitest/src/main/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGrid.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGridTest.java 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 ae63749c5f..4c88879ad1 100755 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -7220,6 +7220,14 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, } if (newSize > oldSize) { + if (oldSize == 0 && !isHeaderVisible()) { + // Fixes framework/issues/11607 + // Need to recalculate column widths when the + // first row is added to a non-header grid, + // otherwise the checkbox will be aligned in a + // wrong place. + recalculateColumnWidths(); + } body.insertRows(oldSize, newSize - oldSize); cellFocusHandler.rowsAddedToBody(Range .withLength(oldSize, newSize - oldSize)); diff --git a/uitest/reference-screenshots/chrome/CheckboxAlignmentWithNoHeaderGridTest-alignments_are_correct_ANY_Chrome__alignment.png b/uitest/reference-screenshots/chrome/CheckboxAlignmentWithNoHeaderGridTest-alignments_are_correct_ANY_Chrome__alignment.png new file mode 100644 index 0000000000..81c821bffd Binary files /dev/null and b/uitest/reference-screenshots/chrome/CheckboxAlignmentWithNoHeaderGridTest-alignments_are_correct_ANY_Chrome__alignment.png differ diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGrid.java b/uitest/src/main/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGrid.java new file mode 100644 index 0000000000..303f8651b0 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGrid.java @@ -0,0 +1,49 @@ +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.VerticalLayout; + +public class CheckboxAlignmentWithNoHeaderGrid extends AbstractTestUI { + + List items = new ArrayList<>(); + int count = 1; + + @Override + protected void setup(VaadinRequest request) { + + VerticalLayout lay = new VerticalLayout(); + + Grid grid = new Grid<>(); + grid.setSelectionMode(Grid.SelectionMode.MULTI); + grid.setHeaderVisible(false); + grid.addColumn(Object::toString); + + grid.setItems(items); + + lay.addComponent(grid); + lay.addComponent(new Button("add", e -> { + items.add("ABCDEFG" + count); + grid.getDataProvider().refreshAll(); + count++; + })); + addComponent(lay); + + } + + @Override + protected String getTestDescription() { + return "Rows added to empty grid with multiselect and no header should not break "; + } + + @Override + protected Integer getTicketNumber() { + return 11607; + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGridTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGridTest.java new file mode 100644 index 0000000000..a800d9b0ec --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/CheckboxAlignmentWithNoHeaderGridTest.java @@ -0,0 +1,37 @@ +package com.vaadin.tests.components.grid; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class CheckboxAlignmentWithNoHeaderGridTest extends SingleBrowserTest { + + GridElement grid; + + @Before + public void init() { + openTestURL(); + grid = $(GridElement.class).first(); + } + + @Test + public void alignments_are_correct() throws IOException { + Assert.assertTrue("This should be an empty grid", + grid.getRowCount() == 0); + + for (int i =0; i<5; i++) { + $(ButtonElement.class).first().click(); + } + sleep(100); + + Assert.assertTrue("This grid should have 5 rows", + grid.getRowCount() == 5); + compareScreen("alignment"); + } +} -- cgit v1.2.3