diff options
Diffstat (limited to 'uitest/src')
-rw-r--r-- | uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java | 31 | ||||
-rw-r--r-- | uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java | 157 |
2 files changed, 185 insertions, 3 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java index 70b7eab470..430692477d 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java @@ -36,8 +36,10 @@ import com.vaadin.ui.Label; import com.vaadin.ui.MenuBar; import com.vaadin.ui.MenuBar.Command; import com.vaadin.ui.MenuBar.MenuItem; +import com.vaadin.ui.MultiSelect; import com.vaadin.ui.Notification; import com.vaadin.ui.Panel; +import com.vaadin.ui.SingleSelect; import com.vaadin.ui.StyleGenerator; import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; @@ -64,6 +66,8 @@ public class GridBasics extends AbstractTestUIWithLog { public static final String CELL_STYLE_GENERATOR_EMPTY = "Empty string"; public static final String CELL_STYLE_GENERATOR_NULL = "Null"; + private boolean isUserSelectionAllowed = true; + public static final String[] COLUMN_CAPTIONS = { "Column 0", "Column 1", "Column 2", "Row Number", "Date", "HTML String", "Big Random", "Small Random" }; @@ -403,6 +407,23 @@ public class GridBasics extends AbstractTestUIWithLog { } }).setCheckable(true); + MenuItem selectionAllowedItem = stateMenu + .addItem("Allow user selection", item -> { + isUserSelectionAllowed = !isUserSelectionAllowed; + if (grid.getSelectionModel() instanceof MultiSelectionModelImpl) { + MultiSelect<DataObject> multiSelect = grid + .asMultiSelect(); + multiSelect.setReadOnly(isUserSelectionAllowed); + } + if (grid.getSelectionModel() instanceof SingleSelectionModelImpl) { + SingleSelect<DataObject> singleSelect = grid + .asSingleSelect(); + singleSelect.setReadOnly(isUserSelectionAllowed); + } + }); + selectionAllowedItem.setChecked(true); + selectionAllowedItem.setCheckable(true); + stateMenu.addItem("Column reorder listener", toggleReorderListenerCommand).setCheckable(true); @@ -492,6 +513,7 @@ public class GridBasics extends AbstractTestUIWithLog { selectionListenerRegistration = ((SingleSelectionModelImpl<DataObject>) grid .getSelectionModel()) .addSelectionListener(this::onSingleSelect); + grid.asSingleSelect().setReadOnly(isUserSelectionAllowed); }); selectionModelItem.addItem("multi", menuItem -> { switchToMultiSelect(); @@ -534,9 +556,12 @@ public class GridBasics extends AbstractTestUIWithLog { private void switchToMultiSelect() { if (!(grid.getSelectionModel() instanceof MultiSelectionModel)) { selectionListenerRegistration.remove(); - ((MultiSelectionModelImpl<DataObject>) grid - .setSelectionMode(SelectionMode.MULTI)) - .addSelectionListener(this::onMultiSelect); + MultiSelectionModelImpl<DataObject> model = (MultiSelectionModelImpl<DataObject>) grid + .setSelectionMode(SelectionMode.MULTI); + model.addSelectionListener(this::onMultiSelect); + grid.asMultiSelect().setReadOnly(isUserSelectionAllowed); + selectionListenerRegistration = model + .addSelectionListener(this::onMultiSelect); } } diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java index e42fcf2d69..6682d0bec5 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java @@ -255,6 +255,163 @@ public class GridSelectionTest extends GridBasicsTest { "Exception occured, java.lang.NullPointerException: null")); } + @Test + public void singleSelectUserSelectionDisallowedSpaceSelectionNoOp() { + openTestURL(); + setSelectionModelSingle(); + getGridElement().focus(); + getGridElement().sendKeys(Keys.DOWN, Keys.SPACE); + assertTrue("row was selected when selection was allowed", + getRow(1).isSelected()); + toggleUserSelectionAllowed(); + getGridElement().sendKeys(Keys.SPACE); + assertTrue("deselect disallowed", getRow(1).isSelected()); + getGridElement().sendKeys(Keys.DOWN, Keys.SPACE); + assertFalse("select disallowed", getRow(2).isSelected()); + assertTrue("old selection remains", getRow(1).isSelected()); + toggleUserSelectionAllowed(); + getGridElement().sendKeys(Keys.SPACE); + assertTrue("select allowed again", getRow(2).isSelected()); + assertFalse("old selection removed", getRow(1).isSelected()); + + } + + @Test + public void singleSelectUserSelectionDisallowedClickSelectionNoOp() { + openTestURL(); + setSelectionModelSingle(); + getGridElement().getCell(1, 0).click(); + assertTrue("selection allowed, should have been selected", + getRow(1).isSelected()); + toggleUserSelectionAllowed(); + getGridElement().getCell(1, 0).click(); + assertTrue("deselect disallowed, should remain selected", + getRow(1).isSelected()); + getGridElement().getCell(2, 0).click(); + assertFalse("select disallowed, should not have been selected", + getRow(2).isSelected()); + assertTrue("select disallowed, old selection should have remained", + getRow(1).isSelected()); + toggleUserSelectionAllowed(); + getGridElement().getCell(2, 0).click(); + assertTrue("select allowed again, row should have been selected", + getRow(2).isSelected()); + assertFalse("old selection removed", getRow(1).isSelected()); + + } + + @Test + public void multiSelectUserSelectionDisallowedSpaceSelectionNoOp() { + openTestURL(); + setSelectionModelMulti(); + getGridElement().focus(); + getGridElement().sendKeys(Keys.DOWN, Keys.SPACE); + assertTrue("selection allowed, should have been selected", + getRow(1).isSelected()); + toggleUserSelectionAllowed(); + getGridElement().sendKeys(Keys.SPACE); + assertTrue("deselect disallowed, should remain selected", + getRow(1).isSelected()); + getGridElement().sendKeys(Keys.DOWN, Keys.SPACE); + assertFalse("select disallowed, should not have been selected", + getRow(2).isSelected()); + assertTrue("select disallowed, old selection should have remained", + getRow(1).isSelected()); + + toggleUserSelectionAllowed(); + getGridElement().sendKeys(Keys.SPACE); + assertTrue("select allowed again, row should have been selected", + getRow(2).isSelected()); + assertTrue( + "select allowed again but old selection should have remained", + getRow(1).isSelected()); + } + + @Test + public void multiSelectUserSelectionDisallowedCheckboxSelectionNoOp() { + openTestURL(); + setSelectionModelMulti(); + assertTrue(getSelectionCheckbox(0).isEnabled()); + toggleUserSelectionAllowed(); + assertFalse(getSelectionCheckbox(0).isEnabled()); + + // Select by clicking on checkbox (should always fail as it is disabled) + getSelectionCheckbox(0).click(); + assertFalse(getGridElement().getRow(0).isSelected()); + // Select by clicking on cell (should fail) + getGridElement().getCell(0, 0).click(); + assertFalse(getGridElement().getRow(0).isSelected()); + + toggleUserSelectionAllowed(); + assertTrue(getSelectionCheckbox(0).isEnabled()); + getSelectionCheckbox(0).click(); + assertTrue(getGridElement().getRow(0).isSelected()); + } + + @Test + public void multiSelectUserSelectionDisallowedCheckboxSelectAllNoOp() { + openTestURL(); + setSelectionModelMulti(); + + assertTrue(getSelectAllCheckbox().isEnabled()); + toggleUserSelectionAllowed(); + assertFalse(getSelectAllCheckbox().isEnabled()); + + // Select all by clicking on checkbox (should not select) + getSelectAllCheckbox().click(); + assertFalse(getSelectAllCheckbox().isSelected()); + assertFalse(getGridElement().getRow(0).isSelected()); + assertFalse(getGridElement().getRow(10).isSelected()); + + // Select all by clicking on header cell (should not select) + getGridElement().getHeaderCell(0, 0).click(); + assertFalse(getSelectAllCheckbox().isSelected()); + assertFalse(getGridElement().getRow(0).isSelected()); + assertFalse(getGridElement().getRow(10).isSelected()); + + // Select all by press SPACE on the header cell (should not select) + getGridElement().getHeaderCell(0, 0).sendKeys(Keys.SPACE); + assertFalse(getSelectAllCheckbox().isSelected()); + assertFalse(getGridElement().getRow(0).isSelected()); + assertFalse(getGridElement().getRow(10).isSelected()); + + toggleUserSelectionAllowed(); + + assertTrue(getSelectAllCheckbox().isEnabled()); + getSelectAllCheckbox().click(); + assertTrue(getGridElement().getRow(0).isSelected()); + assertTrue(getGridElement().getRow(10).isSelected()); + } + + @Test + public void singleSelectUserSelectionDisallowedServerSelect() { + openTestURL(); + setSelectionModelSingle(); + toggleUserSelectionAllowed(); + + toggleFirstRowSelection(); + assertTrue(getGridElement().getRow(0).isSelected()); + } + + @Test + public void multiSelectUserSelectionDisallowedServerSelect() { + openTestURL(); + setSelectionModelMulti(); + toggleUserSelectionAllowed(); + + toggleFirstRowSelection(); + assertTrue(getGridElement().getRow(0).isSelected()); + } + + private void toggleUserSelectionAllowed() { + selectMenuPath("Component", "State", "Allow user selection"); + } + + private WebElement getSelectionCheckbox(int row) { + return getGridElement().getCell(row, 0) + .findElement(By.tagName("input")); + } + private void waitUntilCheckBoxValue(final WebElement checkBoxElememnt, final boolean expectedValue) { waitUntil(new ExpectedCondition<Boolean>() { |