diff options
3 files changed, 105 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 3c9116fab3..d463832119 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -697,8 +697,9 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, @Override public void columnVisibilityChanged(String internalId, boolean hidden) { Column<T, ?> column = getColumnByInternalId(internalId); + column.checkColumnIsAttached(); if (column.isHidden() != hidden) { - column.setHidden(hidden); + column.getState().hidden = hidden; fireColumnVisibilityChangeEvent(column, hidden, true); } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridEventSentOnColumnVisibilityChange.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEventSentOnColumnVisibilityChange.java new file mode 100644 index 0000000000..dd4cf5e7e2 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEventSentOnColumnVisibilityChange.java @@ -0,0 +1,64 @@ +package com.vaadin.tests.components.grid; + +import java.util.Arrays; +import java.util.List; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.Grid; + +public class GridEventSentOnColumnVisibilityChange + extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + List<Person> people = Arrays.asList( + new Person("Nicolaus Copernicus", 1543), + new Person("Galileo Galilei", 1564), + new Person("Johannes Kepler", 1571)); + + Grid<Person> grid = new Grid<>(); + + grid.setItems(people); + grid.addColumn(Person::getName).setId("name").setCaption("Name") + .setHidable(true); + grid.addColumn(Person::getBirthYear).setCaption("Year of birth") + .setHidable(true); + + grid.setSizeFull(); + + grid.addColumnVisibilityChangeListener( + event -> log("UserOriginated: " + event.isUserOriginated())); + + addComponent(grid); + } + + private class Person { + private final String name; + private final int birthYear; + + public Person(String name, int birthYear) { + this.name = name; + this.birthYear = birthYear; + } + + public String getName() { + return name; + } + + public int getBirthYear() { + return birthYear; + } + } + + @Override + public String getDescription() { + return "Every time when the user changes the visibility of the column," + + " there should have only one event sent"; + } + + @Override + protected Integer getTicketNumber() { + return 11419; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridEventSentOnColumnVisibilityChangeTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridEventSentOnColumnVisibilityChangeTest.java new file mode 100644 index 0000000000..c315513620 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridEventSentOnColumnVisibilityChangeTest.java @@ -0,0 +1,39 @@ +package com.vaadin.tests.components.grid; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class GridEventSentOnColumnVisibilityChangeTest extends SingleBrowserTest { + + @Test + public void changeVisibilityAssertLog() { + openTestURL(); + + GridElement grid = $(GridElement.class).first(); + getSidebarOpenButton(grid).click(); + // hide the first column + getSidebarPopup().findElements(By.tagName("td")).get(0).click(); + + Assert.assertTrue("Log content should match", + "1. UserOriginated: true".equals(getLogRow(0))); + } + + 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); + } +} |