]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add API to detect if GridMultiSelect select-all checkbox is checked (#12086)
authorTarek Oraby <42799254+tarekoraby@users.noreply.github.com>
Wed, 26 Aug 2020 14:31:58 +0000 (17:31 +0300)
committerGitHub <noreply@github.com>
Wed, 26 Aug 2020 14:31:58 +0000 (17:31 +0300)
server/src/main/java/com/vaadin/ui/components/grid/GridMultiSelect.java
server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModel.java
server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java
uitest/src/main/java/com/vaadin/tests/components/grid/GridSelectAllStatus.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectAllStatusTest.java [new file with mode: 0644]

index 2e98230a6f88588d013e386eec13ff0100b9fd7f..e376f7271142479c02c6632a471cfa1692b9951e 100644 (file)
@@ -187,6 +187,20 @@ public class GridMultiSelect<T> implements MultiSelect<T> {
         model.setUserSelectionAllowed(allowed);
     }
 
+    /**
+     * Returns whether all items are selected or not.
+     * <p>
+     * This is only {@code true} if user has selected all rows with the select
+     * all checkbox on client side, or if {@link #selectAll()} has been used
+     * from server side.
+     *
+     * @return {@code true} if all selected, {@code false} if not
+     * @since 8.12.0
+     */
+    public boolean isAllSelected() {
+        return model.isAllSelected();
+    }
+
     /**
      * Checks if the user is allowed to change the selection.
      * <p>
index 305a4fbaf98b3c6ed3e82a22ae7f45baae20a549..35f948ca46ca601a1f243aa46a6f2857754a1ce5 100644 (file)
@@ -135,4 +135,16 @@ public interface MultiSelectionModel<T>
      * @see #setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility)
      */
     public boolean isSelectAllCheckBoxVisible();
+
+    /**
+     * Returns whether all items are selected or not.
+     * <p>
+     * This is only {@code true} if user has selected all rows with the select
+     * all checkbox on client side, or if {@link #selectAll()} has been used
+     * from server side.
+     *
+     * @return {@code true} if all selected, {@code false} if not
+     * @since 8.12.0
+     */
+    boolean isAllSelected();
 }
index 0ef1d1a5f76baa4142850683abbc83251fbfa04c..7d8c6d7d85249215f73a7f3603840e9d4138bb9a 100644 (file)
@@ -131,15 +131,7 @@ public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T>
         return getState(false).selectAllCheckBoxVisible;
     }
 
-    /**
-     * Returns whether all items are selected or not.
-     * <p>
-     * This is only {@code true} if user has selected all rows with the select
-     * all checkbox on client side, or if {@link #selectAll()} has been used
-     * from server side.
-     *
-     * @return {@code true} if all selected, {@code false} if not
-     */
+    @Override
     public boolean isAllSelected() {
         return getState(false).allSelected;
     }
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridSelectAllStatus.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridSelectAllStatus.java
new file mode 100644 (file)
index 0000000..1f9c555
--- /dev/null
@@ -0,0 +1,42 @@
+package com.vaadin.tests.components.grid;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.SelectionMode;
+import com.vaadin.ui.Label;
+
+public class GridSelectAllStatus extends AbstractTestUI {
+
+    public Grid<String> grid;
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        grid = new Grid<>();
+        grid.setSelectionMode(SelectionMode.MULTI);
+        grid.setItems("Item 1", "Item 2");
+        grid.addColumn(item -> item);
+
+        Label label = new Label("Select-all checkbox is checked?");
+        Label selectAllStatus = new Label(
+                String.valueOf(grid.asMultiSelect().isAllSelected()));
+        selectAllStatus.setId("status");
+
+        grid.asMultiSelect()
+                .addMultiSelectionListener(e -> selectAllStatus.setValue(
+                        String.valueOf(grid.asMultiSelect().isAllSelected())));
+
+        addComponents(grid, label, selectAllStatus);
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 12081;
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "The status of the Grid's select-all checkbox should be "
+                + "accessible through the Java API.";
+    }
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectAllStatusTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectAllStatusTest.java
new file mode 100644 (file)
index 0000000..d640959
--- /dev/null
@@ -0,0 +1,37 @@
+package com.vaadin.tests.components.grid;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.testbench.elements.LabelElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class GridSelectAllStatusTest extends MultiBrowserTest {
+
+    @Test
+    public void checkSelectAllStatus() {
+        openTestURL();
+
+        GridElement grid = $(GridElement.class).first();
+        LabelElement selectAllStatusLabel = $(LabelElement.class).id("status");
+        WebElement selectAllCheckbox = grid
+                .findElement(By.className("v-grid-select-all-checkbox"));
+
+        // select all
+        selectAllCheckbox.click();
+        assertTrue(
+                "The status of the select-all checkbox is expected to be True.",
+                Boolean.parseBoolean(selectAllStatusLabel.getText()));
+
+        // De-select all selections
+        selectAllCheckbox.click();
+        assertFalse(
+                "The status of the select-all checkbox is expected to be False.",
+                Boolean.parseBoolean(selectAllStatusLabel.getText()));
+    }
+}