From 34d93e6468e3f6a56f0eca74a9122ae39bd7e504 Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Tue, 31 Mar 2020 13:47:28 +0300 Subject: Ensure recalculateColumnWidths works with refreshAll. (#11934) Column widths shouldn't be calculated between the clearing of cache and re-populating it, but be delayed until the cache has some content again. The calculations should only be triggered immediately if no rows are expected. Fixes #9996 --- .../grid/GridRecalculateColumnWidthNewItem.java | 64 +++++++++++++++ .../GridRecalculateColumnWidthNewItemTest.java | 91 ++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItem.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItemTest.java (limited to 'uitest') diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItem.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItem.java new file mode 100644 index 0000000000..8b4fc044c5 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItem.java @@ -0,0 +1,64 @@ +package com.vaadin.tests.components.grid; + +import java.util.ArrayList; +import java.util.List; + +import com.vaadin.data.provider.ListDataProvider; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.CheckBox; +import com.vaadin.ui.Grid; + +public class GridRecalculateColumnWidthNewItem extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + List testItems = new ArrayList<>(); + testItems.add("short1"); + testItems.add("short2"); + + Grid grid = new Grid<>(); + grid.addColumn(String::toString).setCaption("Name"); + grid.addColumn(item -> "col2").setCaption("Col 2"); + grid.addColumn(item -> "col3").setCaption("Col 3"); + grid.setDataProvider(new ListDataProvider<>(testItems)); + + final CheckBox recalculateCheckBox = new CheckBox( + "Recalculate column widths", true); + + Button addButton = new Button("add row", e -> { + testItems.add( + "Wiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiide"); + grid.getDataProvider().refreshAll(); + if (recalculateCheckBox.getValue()) { + grid.recalculateColumnWidths(); + } + }); + addButton.setId("add"); + + Button removeButton = new Button("remove row", e -> { + if (testItems.size() > 0) { + testItems.remove(testItems.size() - 1); + } + grid.getDataProvider().refreshAll(); + if (recalculateCheckBox.getValue()) { + grid.recalculateColumnWidths(); + } + }); + removeButton.setId("remove"); + + addComponents(grid, addButton, removeButton, recalculateCheckBox); + } + + @Override + protected String getTestDescription() { + return "Adding or removing a row with wider contents should update " + + "column widths if requested but not otherwise."; + } + + @Override + protected Integer getTicketNumber() { + return 9996; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItemTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItemTest.java new file mode 100644 index 0000000000..6d910208b7 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItemTest.java @@ -0,0 +1,91 @@ +package com.vaadin.tests.components.grid; + +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.number.IsCloseTo.closeTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.CheckBoxElement; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class GridRecalculateColumnWidthNewItemTest extends SingleBrowserTest { + + GridElement grid; + ButtonElement addButton; + ButtonElement removeButton; + + @Before + public void init() { + openTestURL(); + grid = $(GridElement.class).first(); + addButton = $(ButtonElement.class).id("add"); + removeButton = $(ButtonElement.class).id("remove"); + } + + @Test + public void recalculateAfterAddingAndRemovingWorks() throws IOException { + assertEquals("CheckBox should be checked.", "checked", + $(CheckBoxElement.class).first().getValue()); + + int initialWidth = grid.getHeaderCell(0, 0).getSize().width; + + addButton.click(); + int newWidth = grid.getHeaderCell(0, 0).getSize().width; + // ensure the column width has increased significantly + assertThat( + "Unexpected column width after adding a row and calling recalculate.", + (double) newWidth, not(closeTo(initialWidth, 20))); + + removeButton.click(); + newWidth = grid.getHeaderCell(0, 0).getSize().width; + // ensure the column width has decreased significantly (even if it might + // not be exactly the original width) + assertThat( + "Unexpected column width after removing a row and calling recalculate.", + (double) newWidth, closeTo(initialWidth, 2)); + } + + @Test + public void addingWithoutRecalculateWorks() throws IOException { + CheckBoxElement checkBox = $(CheckBoxElement.class).first(); + checkBox.click(); + assertEquals("CheckBox should not be checked.", "unchecked", + checkBox.getValue()); + + int initialWidth = grid.getHeaderCell(0, 0).getSize().width; + + addButton.click(); + int newWidth = grid.getHeaderCell(0, 0).getSize().width; + // ensure the column width did not change significantly + assertThat( + "Unexpected column width after adding a row without calling recalculate.", + (double) newWidth, closeTo(initialWidth, 2)); + } + + @Test + public void removingWithoutRecalculateWorks() throws IOException { + // add a row before unchecking + addButton.click(); + + CheckBoxElement checkBox = $(CheckBoxElement.class).first(); + checkBox.click(); + assertEquals("CheckBox should not be checked.", "unchecked", + checkBox.getValue()); + + int initialWidth = grid.getHeaderCell(0, 0).getSize().width; + + removeButton.click(); + int newWidth = grid.getHeaderCell(0, 0).getSize().width; + // ensure the column width did not change significantly + assertThat( + "Unexpected column width after removing a row without calling recalculate.", + (double) newWidth, closeTo(initialWidth, 2)); + } +} -- cgit v1.2.3