From 13e3d235e5ce62ad03a2da0046162e10bd5bd94c Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Tue, 13 Sep 2016 12:31:08 +0300 Subject: Add multi selection support to CheckBoxGroup This patch adds multi selection support only for CheckBoxGroup without even trying to generalize anything. Adopting the concepts to work with other components will be done separately. Change-Id: Id4ccd2c743b74cb022dc9dfd8cd8dae3bf8f0c54 --- .../components/checkbox/CheckBoxGroupTestUI.java | 51 +++++++++++++++++++-- .../checkboxgroup/CheckBoxGroupTest.java | 53 ++++++++++++++++++++-- 2 files changed, 96 insertions(+), 8 deletions(-) (limited to 'uitest/src') diff --git a/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java index 343faf3930..fef23f56d3 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java @@ -1,12 +1,12 @@ /* * Copyright 2000-2014 Vaadin Ltd. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -15,6 +15,9 @@ */ package com.vaadin.tests.components.checkbox; +import java.util.stream.IntStream; + +import com.vaadin.shared.data.selection.SelectionModel.Multi; import com.vaadin.tests.components.abstractlisting.AbstractListingTestUI; import com.vaadin.ui.CheckBoxGroup; @@ -25,8 +28,50 @@ import com.vaadin.ui.CheckBoxGroup; */ public class CheckBoxGroupTestUI extends AbstractListingTestUI> { + + private final String selectionCategory = "Selection"; + + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected Class> getTestClass() { return (Class) CheckBoxGroup.class; } + + @Override + protected void createActions() { + super.createActions(); + createListenerMenu(); + createSelectionMenu(); + } + + protected void createSelectionMenu() { + createClickAction( + "Clear selection", selectionCategory, (component, item, + data) -> component.getSelectionModel().deselectAll(), + ""); + + Command, String> toggleSelection = (component, + item, data) -> toggleSelection(item); + + IntStream.of(0, 1, 5, 10, 25).mapToObj(i -> "Item " + i) + .forEach(item -> { + createClickAction("Toggle " + item, selectionCategory, + toggleSelection, item); + }); + } + + private void toggleSelection(String item) { + Multi selectionModel = getComponent().getSelectionModel(); + if (selectionModel.isSelected(item)) { + selectionModel.deselect(item); + } else { + selectionModel.select(item); + } + } + + protected void createListenerMenu() { + createListenerAction("Selection listener", "Listeners", + c -> c.addSelectionListener( + e -> log("Selected: " + e.getNewSelection()))); + } } diff --git a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java index 9a3c2a656c..9a8f631594 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java @@ -1,6 +1,6 @@ /* * Copyright 2000-2014 Vaadin Ltd. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at @@ -15,13 +15,17 @@ */ package com.vaadin.tests.components.checkboxgroup; -import com.vaadin.testbench.customelements.CheckBoxGroupElement; -import com.vaadin.tests.components.checkbox.CheckBoxGroupTestUI; -import com.vaadin.tests.tb3.MultiBrowserTest; +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; + +import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.assertEquals; +import com.vaadin.testbench.customelements.CheckBoxGroupElement; +import com.vaadin.tests.components.checkbox.CheckBoxGroupTestUI; +import com.vaadin.tests.tb3.MultiBrowserTest; /** * Test for CheckBoxGroup @@ -53,6 +57,45 @@ public class CheckBoxGroupTest extends MultiBrowserTest { assertItems(100); } + @Test + public void clickToSelect() { + selectMenuPath("Component", "Listeners", "Selection listener"); + + getSelect().selectByText("Item 4"); + Assert.assertEquals("1. Selected: [Item 4]", getLogRow(0)); + + getSelect().selectByText("Item 2"); + // Selection order (most recently selected is last) + Assert.assertEquals("2. Selected: [Item 4, Item 2]", getLogRow(0)); + + getSelect().selectByText("Item 4"); + Assert.assertEquals("3. Selected: [Item 2]", getLogRow(0)); + } + + @Test + public void selectProgramatically() { + selectMenuPath("Component", "Listeners", "Selection listener"); + + selectMenuPath("Component", "Selection", "Toggle Item 5"); + Assert.assertEquals("2. Selected: [Item 5]", getLogRow(0)); + assertSelected("Item 5"); + + selectMenuPath("Component", "Selection", "Toggle Item 1"); + // Selection order (most recently selected is last) + Assert.assertEquals("4. Selected: [Item 5, Item 1]", getLogRow(0)); + // DOM order + assertSelected("Item 1", "Item 5"); + + selectMenuPath("Component", "Selection", "Toggle Item 5"); + Assert.assertEquals("6. Selected: [Item 1]", getLogRow(0)); + assertSelected("Item 1"); + } + + private void assertSelected(String... expectedSelection) { + Assert.assertEquals(Arrays.asList(expectedSelection), + getSelect().getSelection()); + } + @Override protected Class getUIClass() { return CheckBoxGroupTestUI.class; -- cgit v1.2.3