]> source.dussan.org Git - vaadin-framework.git/commitdiff
Check actual Grid selection instead of relying on allSelected flag. (#11787)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Mon, 4 Nov 2019 11:26:32 +0000 (13:26 +0200)
committerGitHub <noreply@github.com>
Mon, 4 Nov 2019 11:26:32 +0000 (13:26 +0200)
The checkbox for selecting all rows only selects all the rows that have
not been filtered out. Changing the filtering does not change the
selection or the checkbox state so assuming that all rows are selected
simply because the checkbox has been checked cannot work.

Fixes #11479

client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java
server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java
uitest/src/main/java/com/vaadin/tests/components/grid/GridSelectAllFiltering.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectAllFilteringTest.java [new file with mode: 0644]

index 7bbc66d271717bef1c6f6d6d0af106d743ea5a13..de003e6daf258ce51b58b4522d4ba71641a1b7a5 100644 (file)
@@ -250,7 +250,7 @@ public class MultiSelectionModelConnector
 
     @Override
     protected boolean isSelected(JsonObject item) {
-        return getState().allSelected || super.isSelected(item);
+        return super.isSelected(item);
     }
 
     /**
index f51744c96e006622571b1e97f3ddb620d21a5db8..0ef1d1a5f76baa4142850683abbc83251fbfa04c 100644 (file)
  */
 package com.vaadin.ui.components.grid;
 
-import com.vaadin.data.provider.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.vaadin.data.provider.DataCommunicator;
+import com.vaadin.data.provider.DataProvider;
+import com.vaadin.data.provider.HierarchicalDataProvider;
+import com.vaadin.data.provider.HierarchicalQuery;
+import com.vaadin.data.provider.Query;
 import com.vaadin.event.selection.MultiSelectionEvent;
 import com.vaadin.event.selection.MultiSelectionListener;
 import com.vaadin.shared.Registration;
@@ -23,11 +40,6 @@ import com.vaadin.shared.data.selection.GridMultiSelectServerRpc;
 import com.vaadin.shared.ui.grid.MultiSelectionModelState;
 import com.vaadin.ui.MultiSelect;
 
-import java.util.*;
-import java.util.function.Consumer;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
 /**
  * Multiselection model for grid.
  * <p>
@@ -134,8 +146,7 @@ public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T>
 
     @Override
     public boolean isSelected(T item) {
-        return isAllSelected()
-                || selectionContainsId(getGrid().getDataProvider().getId(item));
+        return selectionContainsId(getGrid().getDataProvider().getId(item));
     }
 
     /**
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridSelectAllFiltering.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridSelectAllFiltering.java
new file mode 100644 (file)
index 0000000..3c85e9c
--- /dev/null
@@ -0,0 +1,71 @@
+package com.vaadin.tests.components.grid;
+
+import java.util.Iterator;
+import java.util.Set;
+
+import com.vaadin.data.provider.DataProvider;
+import com.vaadin.data.provider.ListDataProvider;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.data.bean.Person;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.SelectionMode;
+import com.vaadin.ui.renderers.NumberRenderer;
+
+public class GridSelectAllFiltering extends SimpleGridUI {
+    private String filterText = "Johannes";
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        Grid<Person> grid = new Grid<>();
+        grid.setSelectionMode(SelectionMode.MULTI);
+        grid.setHeightByRows(3);
+        grid.addColumn(Person::getFirstName);
+        grid.addColumn(Person::getAge, new NumberRenderer());
+
+        ListDataProvider<Person> dataProvider = DataProvider
+                .ofCollection(createPersons());
+        dataProvider.setFilter(person -> {
+            if (person.getFirstName().contains(filterText)) {
+                return false;
+            }
+            return true;
+        });
+        grid.setDataProvider(dataProvider);
+
+        Button toggleButton = new Button("Toggle filter", e -> {
+            if ("Johannes".equals(filterText)) {
+                filterText = "Galileo";
+            } else {
+                filterText = "Johannes";
+            }
+            dataProvider.refreshAll();
+        });
+        toggleButton.setId("toggle");
+
+        Button checkButton = new Button("Check selection", e -> {
+            Set<Person> selected = grid.getSelectedItems();
+            Iterator<Person> i = selected.iterator();
+            log("selected " + selected.size()
+                    + (i.hasNext() ? ": " + i.next().getFirstName() : "")
+                    + (i.hasNext() ? ", " + i.next().getFirstName() : "")
+                    + (i.hasNext() ? ", " + i.next().getFirstName() : "")
+                    + (i.hasNext() ? "... " : ""));
+        });
+        checkButton.setId("check");
+
+        addComponents(grid, toggleButton, checkButton);
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 11479;
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Selecting all does not select items that have been "
+                + "filtered out, they should not be shown selected "
+                + "after the filter changes.";
+    }
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectAllFilteringTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectAllFilteringTest.java
new file mode 100644 (file)
index 0000000..37c7d62
--- /dev/null
@@ -0,0 +1,62 @@
+package com.vaadin.tests.components.grid;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class GridSelectAllFilteringTest extends MultiBrowserTest {
+
+    @Test
+    public void checkSelectAll() {
+        openTestURL();
+
+        GridElement grid = $(GridElement.class).first();
+        ButtonElement toggleButton = $(ButtonElement.class).id("toggle");
+        ButtonElement checkButton = $(ButtonElement.class).id("check");
+
+        // ensure no initial selection
+        checkButton.click();
+        assertEquals("Unexpected log entry,", "1. selected 0", getLogRow(0));
+        assertEquals("Unexpected amount of visually selected rows,", 0,
+                grid.findElements(By.className("v-grid-row-selected")).size());
+
+        // select all
+        WebElement selectAllCheckbox = grid
+                .findElement(By.className("v-grid-select-all-checkbox"));
+        selectAllCheckbox.click();
+
+        // ensure only the two visible rows get selected
+        checkButton.click();
+        assertEquals("Unexpected log entry,",
+                "2. selected 2: Nicolaus Copernicus, Galileo Galilei",
+                getLogRow(0));
+        assertEquals("Unexpected amount of visually selected rows,", 2,
+                grid.findElements(By.className("v-grid-row-selected")).size());
+
+        // toggle filter
+        toggleButton.click();
+
+        // ensure selection did not change but only one selected row is visible
+        checkButton.click();
+        assertEquals("Unexpected log entry,",
+                "3. selected 2: Nicolaus Copernicus, Galileo Galilei",
+                getLogRow(0));
+        assertEquals("Unexpected amount of visually selected rows,", 1,
+                grid.findElements(By.className("v-grid-row-selected")).size());
+
+        // remove all selections
+        selectAllCheckbox.click();
+
+        // ensure all selections got removed whether they were visible or not
+        checkButton.click();
+        assertEquals("Unexpected log entry,", "4. selected 0", getLogRow(0));
+        assertEquals("Unexpected amount of visually selected rows,", 0,
+                grid.findElements(By.className("v-grid-row-selected")).size());
+    }
+}