aboutsummaryrefslogtreecommitdiffstats
path: root/uitest
diff options
context:
space:
mode:
authorAnna Koskinen <Ansku@users.noreply.github.com>2020-03-31 13:47:28 +0300
committerGitHub <noreply@github.com>2020-03-31 13:47:28 +0300
commit34d93e6468e3f6a56f0eca74a9122ae39bd7e504 (patch)
treebfc3052df9d55a508318549615a3045755ac0c16 /uitest
parentd75836272b2d7200dd3b84f98d88529978a805e4 (diff)
downloadvaadin-framework-34d93e6468e3f6a56f0eca74a9122ae39bd7e504.tar.gz
vaadin-framework-34d93e6468e3f6a56f0eca74a9122ae39bd7e504.zip
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
Diffstat (limited to 'uitest')
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItem.java64
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/grid/GridRecalculateColumnWidthNewItemTest.java91
2 files changed, 155 insertions, 0 deletions
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<String> testItems = new ArrayList<>();
+ testItems.add("short1");
+ testItems.add("short2");
+
+ Grid<String> 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));
+ }
+}